|
1 | 1 | package javaxt.express; |
2 | 2 | import javaxt.http.servlet.HttpServletRequest; |
3 | 3 | import javaxt.http.servlet.HttpServletResponse; |
| 4 | +import javaxt.utils.ThreadPool; |
4 | 5 | import java.io.IOException; |
5 | 6 | import java.util.Calendar; |
6 | 7 | import java.util.TimeZone; |
@@ -172,57 +173,98 @@ else if (ext.equals("htm") || ext.equals("html")){ |
172 | 173 | //Note that the parser will be extremely slow if there is |
173 | 174 | //a !DOCTYPE declaration. |
174 | 175 | javaxt.io.File htmlFile = new javaxt.io.File(file); |
175 | | - org.w3c.dom.Document xml = htmlFile.getXML(); |
| 176 | + String xhtml = htmlFile.getText().trim(); |
| 177 | + idx = xhtml.toUpperCase().indexOf("<!DOCTYPE"); |
| 178 | + if (idx>-1){ |
| 179 | + xhtml = xhtml.substring(idx+"<!DOCTYPE".length()).trim(); |
| 180 | + xhtml = xhtml.substring(xhtml.indexOf(">")+1).trim(); |
| 181 | + } |
| 182 | + org.w3c.dom.Document xml = javaxt.xml.DOM.createDocument(xhtml); |
176 | 183 | java.util.TreeSet<Long> dates = new java.util.TreeSet<Long>(); |
177 | 184 | dates.add(file.lastModified()); |
178 | 185 |
|
179 | 186 |
|
180 | | - //Update links to scripts |
181 | | - for (org.w3c.dom.Node node : javaxt.xml.DOM.getElementsByTagName("script", xml)){ |
182 | | - String src = javaxt.xml.DOM.getAttributeValue(node, "src"); |
183 | | - if (src.length()>0) |
184 | | - try{ |
185 | | - javaxt.io.File jsFile = new javaxt.io.File(htmlFile.MapPath(src)); |
186 | | - if (jsFile.exists()){ |
| 187 | + //Update links to scripts and css files by appending a version |
| 188 | + //number to the urls (?v=12345678). This operation is multi- |
| 189 | + //threaded to improve performance. |
| 190 | + try{ |
187 | 191 |
|
188 | | - //Append version number to the path |
189 | | - long lastModified = jsFile.getLastModifiedTime().getTime(); |
190 | | - long currVersion = new javaxt.utils.Date(lastModified).toLong(); |
191 | | - javaxt.xml.DOM.setAttributeValue(node, "src" , src + "?v=" + currVersion); |
192 | | - dates.add(lastModified); |
| 192 | + //Instantiate the ThreadPool |
| 193 | + ThreadPool pool = new ThreadPool(4){ |
| 194 | + public void process(Object obj){ |
| 195 | + org.w3c.dom.Node node = (org.w3c.dom.Node) obj; |
| 196 | + String nodeName = node.getNodeName().toLowerCase(); |
| 197 | + if (nodeName.equals("script")){ |
| 198 | + |
| 199 | + //Update links to scripts |
| 200 | + String src = javaxt.xml.DOM.getAttributeValue(node, "src"); |
| 201 | + if (src.length()>0) |
| 202 | + try{ |
| 203 | + javaxt.io.File jsFile = new javaxt.io.File(htmlFile.MapPath(src)); |
| 204 | + if (jsFile.exists()){ |
| 205 | + |
| 206 | + //Append version number to the path |
| 207 | + long lastModified = jsFile.getLastModifiedTime().getTime(); |
| 208 | + long currVersion = new javaxt.utils.Date(lastModified).toLong(); |
| 209 | + javaxt.xml.DOM.setAttributeValue(node, "src" , src + "?v=" + currVersion); |
| 210 | + dates.add(lastModified); |
| 211 | + } |
| 212 | + } |
| 213 | + catch(Exception e){ |
| 214 | + //System.out.println("Invalid path? " + src); |
| 215 | + } |
| 216 | + } |
| 217 | + else if (nodeName.equals("link")){ |
| 218 | + |
| 219 | + //Update links to css files |
| 220 | + String href = javaxt.xml.DOM.getAttributeValue(node, "href"); |
| 221 | + String type = javaxt.xml.DOM.getAttributeValue(node, "type"); |
| 222 | + String rel = javaxt.xml.DOM.getAttributeValue(node, "rel"); |
| 223 | + boolean isStyleSheet = type.equalsIgnoreCase("text/css"); |
| 224 | + if (!isStyleSheet) isStyleSheet = rel.equalsIgnoreCase("stylesheet"); |
| 225 | + if (href.length()>0 && isStyleSheet){ |
| 226 | + |
| 227 | + try{ |
| 228 | + javaxt.io.File cssFile = new javaxt.io.File(htmlFile.MapPath(href)); |
| 229 | + if (cssFile.exists()){ |
| 230 | + |
| 231 | + //Append version number to the path |
| 232 | + long lastModified = cssFile.getLastModifiedTime().getTime(); |
| 233 | + long currVersion = new javaxt.utils.Date(lastModified).toLong(); |
| 234 | + javaxt.xml.DOM.setAttributeValue(node, "href" , href + "?v=" + currVersion); |
| 235 | + dates.add(lastModified); |
| 236 | + } |
| 237 | + } |
| 238 | + catch(Exception e){ |
| 239 | + //System.out.println("Invalid path? " + href); |
| 240 | + } |
| 241 | + } |
| 242 | + } |
193 | 243 | } |
194 | | - } |
195 | | - catch(Exception e){ |
196 | | - //System.out.println("Invalid path? " + src); |
197 | | - } |
198 | | - } |
199 | 244 |
|
200 | 245 |
|
201 | | - //Update links to css files |
202 | | - for (org.w3c.dom.Node node : javaxt.xml.DOM.getElementsByTagName("link", xml)){ |
203 | | - String href = javaxt.xml.DOM.getAttributeValue(node, "href"); |
204 | | - String type = javaxt.xml.DOM.getAttributeValue(node, "type"); |
205 | | - String rel = javaxt.xml.DOM.getAttributeValue(node, "rel"); |
206 | | - boolean isStyleSheet = type.equalsIgnoreCase("text/css"); |
207 | | - if (!isStyleSheet) isStyleSheet = rel.equalsIgnoreCase("stylesheet"); |
208 | | - if (href.length()>0 && isStyleSheet){ |
| 246 | + }.start(); |
209 | 247 |
|
210 | | - try{ |
211 | | - javaxt.io.File cssFile = new javaxt.io.File(htmlFile.MapPath(href)); |
212 | | - if (cssFile.exists()){ |
213 | 248 |
|
214 | | - //Append version number to the path |
215 | | - long lastModified = cssFile.getLastModifiedTime().getTime(); |
216 | | - long currVersion = new javaxt.utils.Date(lastModified).toLong(); |
217 | | - javaxt.xml.DOM.setAttributeValue(node, "href" , href + "?v=" + currVersion); |
218 | | - dates.add(lastModified); |
219 | | - } |
220 | | - } |
221 | | - catch(Exception e){ |
222 | | - //System.out.println("Invalid path? " + href); |
223 | | - } |
| 249 | + //Insert records |
| 250 | + for (org.w3c.dom.Node node : javaxt.xml.DOM.getElementsByTagName("script", xml)){ |
| 251 | + pool.add(node); |
| 252 | + } |
| 253 | + for (org.w3c.dom.Node node : javaxt.xml.DOM.getElementsByTagName("link", xml)){ |
| 254 | + pool.add(node); |
224 | 255 | } |
| 256 | + |
| 257 | + |
| 258 | + //Notify the pool that we have finished added records and Wait for threads to finish |
| 259 | + pool.done(); |
| 260 | + pool.join(); |
| 261 | + |
225 | 262 | } |
| 263 | + catch(Exception e){ |
| 264 | + throw new RuntimeException(e); |
| 265 | + } |
| 266 | + |
| 267 | + |
226 | 268 |
|
227 | 269 |
|
228 | 270 | //Replace all self enclosing tags |
|
0 commit comments