@@ -11,11 +11,13 @@ public class HtmlParser {
1111 private final static Logger LOGGER = LoggerFactory .getLogger (HttpServer .class );
1212
1313 private File file ;
14+ private String filePath ;
1415 private String contents = "" ;
1516 private String html = "" ;
1617
17- public HtmlParser (File file , String css , String cssReplace , String JS , String JSReplace ) {
18- this .file = file ;
18+ public HtmlParser (String filePath , String css , String cssReplace , String JS , String JSReplace ) {
19+ this .file = new File (filePath );
20+ this .filePath = filePath ;
1921
2022 // Read the file
2123 try {
@@ -30,9 +32,49 @@ public HtmlParser(File file, String css, String cssReplace, String JS, String JS
3032 // can throw a IOException
3133 while ((charValue = bufferedReader .read ()) != -1 ) contents += (char ) charValue ;
3234 } catch (FileNotFoundException exception ) {
33- LOGGER .error ("File Not Found: " + exception );
35+ if (filePath .charAt (filePath .length ()-1 ) == '/' ) {
36+ try {
37+ // Open the file
38+ BufferedReader bufferedReader = new BufferedReader (new FileReader (new File (filePath + "index.html" )));
39+
40+ int charValue ;
41+
42+ // Loop through the characters of
43+ // the file and store them to the contents String
44+ // can throw a IOException
45+ while ((charValue = bufferedReader .read ()) != -1 ) contents += (char ) charValue ;
46+ } catch (FileNotFoundException exception1 ) {
47+ // ERROR: 404
48+ LOGGER .error ("File Not Found: " + exception );
49+ } catch (IOException exception1 ) {
50+ // ERROR: 500
51+ LOGGER .error ("Error reading file: " + exception );
52+ }
53+ } else if (!((filePath .substring (filePath .length ()-5 ) == ".html" ) || (filePath .substring (filePath .length ()-4 ) == ".htm" ))) {
54+ try {
55+ // Open the file
56+ BufferedReader bufferedReader = new BufferedReader (new FileReader (new File (filePath + "/index.html" )));
57+
58+ int charValue ;
59+
60+ // Loop through the characters of
61+ // the file and store them to the contents String
62+ // can throw a IOException
63+ while ((charValue = bufferedReader .read ()) != -1 ) contents += (char ) charValue ;
64+ } catch (FileNotFoundException exception1 ) {
65+ // ERROR: 404
66+ LOGGER .error ("File Not Found: " + exception );
67+ } catch (IOException exception1 ) {
68+ // ERROR: 500
69+ LOGGER .error ("Error reading file: " + exception );
70+ }
71+ } else {
72+ // ERROR: 404
73+ LOGGER .error ("File Not Found: " + exception );
74+ }
3475 } catch (IOException exception ) {
35- LOGGER .error ("Can't Read File: " + exception );
76+ // ERROR: 500
77+ LOGGER .error ("File Not Found: " + exception );
3678 }
3779 LOGGER .info (contents );
3880
0 commit comments