|
1 | 1 | package dev.hdprojects.website; |
2 | 2 |
|
3 | | -import dev.hdprojects.HttpServer.HttpServer; |
| 3 | +import dev.hdprojects.HttpServer.config.Configuration; |
4 | 4 | import org.slf4j.Logger; |
5 | 5 | import org.slf4j.LoggerFactory; |
6 | 6 |
|
7 | 7 | import java.io.*; |
8 | 8 |
|
9 | 9 | public class HtmlParser { |
10 | 10 |
|
11 | | - private final static Logger LOGGER = LoggerFactory.getLogger(HttpServer.class); |
| 11 | + private final static Logger LOGGER = LoggerFactory.getLogger(HtmlParser.class); |
12 | 12 |
|
13 | 13 | private File file; |
14 | 14 | private String filePath; |
| 15 | + private String css; |
| 16 | + private String JS; |
| 17 | + private int error = 200; |
15 | 18 | private String contents = ""; |
16 | 19 | private String html = ""; |
| 20 | + private Configuration configuration; |
17 | 21 |
|
18 | | - public HtmlParser(String filePath, String css, String cssReplace, String JS, String JSReplace) { |
| 22 | + public HtmlParser(String filePath, Configuration conf) { |
19 | 23 | this.file = new File(filePath); |
20 | 24 | this.filePath = filePath; |
| 25 | + this.configuration = conf; |
21 | 26 |
|
22 | 27 | // Read the file |
23 | 28 | try { |
@@ -62,36 +67,118 @@ public HtmlParser(String filePath, String css, String cssReplace, String JS, Str |
62 | 67 | // can throw a IOException |
63 | 68 | while ((charValue = bufferedReader.read()) != -1) contents += (char) charValue; |
64 | 69 | } catch (FileNotFoundException exception1) { |
65 | | - // ERROR: 404 |
| 70 | + contents = error(404); |
66 | 71 | LOGGER.error("File Not Found: " + exception); |
67 | 72 | } catch (IOException exception1) { |
68 | | - // ERROR: 500 |
| 73 | + contents = error(500); |
69 | 74 | LOGGER.error("Error reading file: " + exception); |
70 | 75 | } |
71 | 76 | } else { |
72 | | - // ERROR: 404 |
| 77 | + contents = error(404); |
73 | 78 | LOGGER.error("File Not Found: " + exception); |
74 | 79 | } |
75 | 80 | } catch (IOException exception) { |
76 | | - // ERROR: 500 |
| 81 | + contents = error(500); |
77 | 82 | LOGGER.error("File Not Found: " + exception); |
78 | 83 | } |
79 | | - LOGGER.info(contents); |
| 84 | + |
| 85 | + try { |
| 86 | + // Open the file |
| 87 | + BufferedReader tempBufferedReader = new BufferedReader(new FileReader(new File(configuration.getWebroot() + "/" + configuration.getJs()))); |
| 88 | + |
| 89 | + int charValue; |
| 90 | + |
| 91 | + JS = ""; |
| 92 | + |
| 93 | + // Loop through the characters of |
| 94 | + // the file and store them to the contents String |
| 95 | + // can throw a IOException |
| 96 | + while ((charValue = tempBufferedReader.read()) != -1) JS += (char) charValue; |
| 97 | + |
| 98 | + // Set the html tags |
| 99 | + JS = "<script>" + JS + "</script>"; |
| 100 | + } catch (Exception exception) { |
| 101 | + JS = "/* Unknown Error */"; |
| 102 | + JS = "<script>" + JS + "</script>"; |
| 103 | + LOGGER.info("Error opening JS"); |
| 104 | + } |
| 105 | + |
| 106 | + try { |
| 107 | + // Open the file |
| 108 | + BufferedReader tempBufferedReader = new BufferedReader(new FileReader(new File(configuration.getWebroot() + "/" + configuration.getCss()))); |
| 109 | + |
| 110 | + int charValue; |
| 111 | + |
| 112 | + css = ""; |
| 113 | + |
| 114 | + // Loop through the characters of |
| 115 | + // the file and store them to the contents String |
| 116 | + // can throw a IOException |
| 117 | + while ((charValue = tempBufferedReader.read()) != -1) css += (char) charValue; |
| 118 | + |
| 119 | + // Set the html tags |
| 120 | + css = "<style>" + css + "</style>"; |
| 121 | + } catch (Exception exception) { |
| 122 | + css = "/* Unknown Error */"; |
| 123 | + css = "<style>" + css + "</style>"; |
| 124 | + LOGGER.info("Error opening css"); |
| 125 | + } |
80 | 126 |
|
81 | 127 | // Replace the style |
82 | | - //Replacer styleReplace = new Replacer(cssReplace, css); |
| 128 | + contents = contents.replaceAll(configuration.getStyle_replace(), css); |
83 | 129 |
|
84 | 130 | // And the js |
85 | | - //Replacer jsReplace = new Replacer(styleReplace.toString(), JS); |
| 131 | + contents = contents.replaceAll(configuration.getJs_replace(), JS); |
86 | 132 |
|
87 | 133 | html = contents; |
88 | 134 | } |
89 | 135 |
|
| 136 | + private String error(int error) { |
| 137 | + LOGGER.info("Error: " + 404); |
| 138 | + switch (error) { |
| 139 | + case 404: |
| 140 | + error = 404; |
| 141 | + File file = new File(configuration.getWebroot() + "/" + error + ".html"); |
| 142 | + |
| 143 | + BufferedReader bufferedReader = null; |
| 144 | + try { |
| 145 | + bufferedReader = new BufferedReader(new FileReader(file)); |
| 146 | + } catch (FileNotFoundException exception) { |
| 147 | + LOGGER.error(exception.getMessage()); |
| 148 | + return "Error: 500 INTERNAL SERVER ERROR"; |
| 149 | + } |
| 150 | + |
| 151 | + int charValue; |
| 152 | + |
| 153 | + while (true) { |
| 154 | + try { |
| 155 | + if (!((charValue = bufferedReader.read()) != -1)) break; |
| 156 | + } catch (IOException exception) { |
| 157 | + LOGGER.error(exception.getMessage()); |
| 158 | + return "Error: 500 INTERNAL SERVER ERROR"; |
| 159 | + } |
| 160 | + contents += (char) charValue; |
| 161 | + } |
| 162 | + return contents; |
| 163 | + default: |
| 164 | + error = 500; |
| 165 | + return "Error: 500 INTERNAL SERVER ERROR"; |
| 166 | + } |
| 167 | + } |
| 168 | + |
90 | 169 | @Override |
91 | 170 | public String toString() { |
92 | 171 | return html; |
93 | 172 | } |
94 | 173 |
|
| 174 | + public int getError() { |
| 175 | + return error; |
| 176 | + } |
| 177 | + |
| 178 | + public void setError(int error) { |
| 179 | + this.error = error; |
| 180 | + } |
| 181 | + |
95 | 182 | public String getHtml() { |
96 | 183 | return html; |
97 | 184 | } |
|
0 commit comments