package j2html.tags;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import j2html.Config;
import j2html.utils.CSSMin;
import j2html.utils.JSMin;

import static j2html.TagCreator.*;

public class InlineStaticResource {

    public enum TargetFormat {CSS_MIN, CSS, JS_MIN, JS}

    public static ContainerTag get(String path, TargetFormat format) {
        String fileString = getFileAsString(path);
        switch (format) {
            case CSS_MIN : return style().with(rawHtml(Config.cssMinifier.minify(fileString)));
            case JS_MIN  : return script().with(rawHtml(Config.jsMinifier.minify((fileString))));
            case CSS     : return style().with(rawHtml(fileString));
            case JS      : return script().with(rawHtml(fileString));
            default      : throw new RuntimeException("Invalid target format");
        }
    }

    public static String getFileAsString(String path) {
        try {
            return readFileAsString(InlineStaticResource.class.getResource(path).getPath());
        } catch (Exception e1) {
            try {
                return readFileAsString(path);
            } catch (Exception e2) {
                throw new RuntimeException("Couldn't find file with path='" + path + "'");
            }
        }
    }

    /**
     * @author kjheimark <3
     */
    private static String readFileAsString(String path) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
        StringBuilder sb = new StringBuilder();
        int c;
        while ((c = bufferedReader.read()) >= 0 && c >= 0) {
            sb.append((char) c);
        }
        return sb.toString();
    }

}


@@ -30,30 +30,31 @@
 java.io.
-BufferedReader
+FileInputStream
 ;%0Aimport
@@ -62,26 +62,27 @@
 java.io.
-FileReader
+InputStream
 ;%0Aimport
@@ -91,22 +91,20 @@
 ava.
-io.IOException
+util.Scanner
 ;%0A%0Ai
@@ -127,63 +127,8 @@
 fig;
-%0Aimport j2html.utils.CSSMin;%0Aimport j2html.utils.JSMin;
 %0A%0Aim
@@ -918,34 +918,32 @@
      return 
+st
 rea
-dFileAs
+mTo
 String(Inlin
@@ -979,23 +979,21 @@
 urce
-(path).getPath(
+AsStream(path
 ));%0A
@@ -1024,12 +1024,76 @@
 on e
-1) %7B
+xpected) %7B // we don't ask users to specify classpath or file-system
 %0A   
@@ -1134,30 +1134,49 @@
 urn 
+st
 rea
-dFileAsString
+mToString(new FileInputStream
 (path)
+)
 ;%0A  
@@ -1205,17 +1205,24 @@
 eption e
-2
+xception
 ) %7B%0A    
@@ -1343,52 +1343,8 @@
  %7D%0A%0A
-    /**%0A     * @author kjheimark %3C3%0A     */%0A
     
@@ -1369,336 +1369,158 @@
 ing 
+st
 rea
-dFileAsString(String path) throws IOException %7B%0A        BufferedReader bufferedReader = new BufferedReader(new FileReader(path));%0A        StringBuilder sb = new StringBuilder();%0A        int c;%0A        while ((c = bufferedReader.read()) %3E= 0 && c %3E= 0) %7B%0A            sb.append((char) c);%0A        %7D%0A        return sb.toString()
+mToString(InputStream inputStream) %7B%0A        Scanner s = new Scanner(inputStream).useDelimiter(%22%5C%5CA%22);%0A        return s.hasNext() ? s.next() : %22%22
 ;%0A  

Allow reading static resources from jars