/**
 * Copyright 2013-present febit.org (support@febit.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.febit.util;

import java.io.IOException;
import java.io.Reader;
import java.util.ServiceLoader;
import jodd.io.StreamUtil;

/**
 *
 * @author zqq90
 */
public class Resources {

    public static final String DEFAULT_ENCODING = "UTF-8";

    private static class ResourceLoaderHolder {

        static final ResourceLoader[] LOADERS;

        static {
            LOADERS = CollectionUtil.read(ServiceLoader.load(ResourceLoader.class))
                    .stream()
                    .sorted(Priority.DESC)
                    .toArray(ResourceLoader[]::new);
        }
    }

    public static Reader open(String path) throws IOException {
        return open(path, DEFAULT_ENCODING);
    }

    public static Reader open(String path, String encoding) throws IOException {
        for (ResourceLoader loader : ResourceLoaderHolder.LOADERS) {
            Reader reader = loader.openReader(path, encoding);
            if (reader != null) {
                return reader;
            }
        }
        throw new IOException("Resource not found: " + path);
    }

    public static char[] readChars(String path) throws IOException {
        Reader reader = open(path);
        return readChars(reader);
    }

    public static char[] readChars(String path, String encoding) throws IOException {
        Reader reader = open(path, encoding);
        return readChars(reader);
    }

    public static char[] readChars(Reader reader) throws IOException {
        try {
            return StreamUtil.readChars(reader);
        } finally {
            reader.close();
        }
    }

    public static String readString(String path) throws IOException {
        Reader reader = open(path);
        return readString(reader);
    }

    public static String readString(String path, String encoding) throws IOException {
        Reader reader = open(path, encoding);
        return readString(reader);
    }

    public static String readString(Reader reader) throws IOException {
        return new String(readChars(reader));
    }

    public static String normalize(String name) {
        if (name == null) {
            return null;
        }
        String result;
        for (ResourceLoader loader : ResourceLoaderHolder.LOADERS) {
            result = loader.normalize(name);
            if (result != null) {
                return result;
            }
        }
        return name;
    }
}

@@ -1228,97 +1228,348 @@
 tic 
-Reader open(String path) throws IOException %7B%0A        return open(path, DEFAULT_ENCODING)
+String normalize(String name) %7B%0A        if (name == null) %7B%0A            return null;%0A        %7D%0A        String result;%0A        for (ResourceLoader loader : ResourceLoaderHolder.LOADERS) %7B%0A            result = loader.normalize(name);%0A            if (result != null) %7B%0A                return result;%0A            %7D%0A        %7D%0A        return name
 ;%0A  
@@ -1962,32 +1962,27 @@
  static 
-char%5B%5D readChars
+Reader open
 (String 
@@ -2020,67 +2020,42 @@
     
-Reader reader = open(path);%0A        return readChars(reader
+return open(path, DEFAULT_ENCODING
 );%0A 
@@ -2146,32 +2146,37 @@
 ption %7B%0A        
+try (
 Reader reader = 
@@ -2191,34 +2191,38 @@
 h, encoding)
-;%0A
+) %7B%0A  
         
 return readC
@@ -2205,31 +2205,44 @@
 %7B%0A          
+  
 return 
+StreamUtil.
 readChars(re
@@ -2240,32 +2240,42 @@
 dChars(reader);%0A
+        %7D%0A
     %7D%0A%0A    publi
@@ -2300,29 +2300,27 @@
 adChars(
-Reader reader
+String path
 ) throws
@@ -2346,120 +2346,49 @@
     
-try %7B%0A            return StreamUtil.readChars(reader);%0A        %7D finally %7B%0A            reader.close();%0A        %7D
+return readChars(path, DEFAULT_ENCODING);
 %0A   
@@ -2465,44 +2465,8 @@
 n %7B%0A
-        Reader reader = open(path);%0A
     
@@ -2468,36 +2468,36 @@
 %0A        return 
-read
+new 
 String(reader);%0A
@@ -2483,34 +2483,43 @@
  new String(read
-er
+Chars(path)
 );%0A    %7D%0A%0A    pu
@@ -2611,571 +2611,52 @@
     
-Reader reader = open(path, encoding);%0A        return readString(reader);%0A    %7D%0A%0A    public static String readString(Reader reader) throws IOException %7B%0A        return new String(readChars(reader));%0A    %7D%0A%0A    public static String normalize(String name) %7B%0A        if (name == null) %7B%0A            return null;%0A        %7D%0A        String result;%0A        for (ResourceLoader loader : ResourceLoaderHolder.LOADERS) %7B%0A            result = loader.normalize(name);%0A            if (result != null) %7B%0A                return result;%0A            %7D%0A        %7D%0A        return name
+return new String(readChars(path, encoding))
 ;%0A  

update Resources