- Timestamp:
- 2008-07-17T23:28:27+12:00 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
greenstone3/trunk/src/java/org/greenstone/gsdl3/core/Skin.java
r16374 r16446 19 19 public class Skin { 20 20 21 public File rootDirectory ; 22 public Document config ; 23 private Receptionist receptionist ; 24 25 protected DOMParser parser = null; 26 TransformerFactory transformerFactory=null; 27 28 // delete this later?? 29 //protected XMLConverter converter = null; 30 31 private HashMap<String, String> xsltPagesByAction ; 32 33 public Skin(Page page, Receptionist receptionist) throws Exception{ 34 35 this.receptionist = receptionist ; 36 this.transformerFactory = org.apache.xalan.processor.TransformerFactoryImpl.newInstance(); 37 //this.converter = new XMLConverter(); 38 transformerFactory.setURIResolver(new MyUriResolver()) ; 39 40 this.parser = new DOMParser(); 41 this.parser.setFeature("http://xml.org/sax/features/validation", true); 42 // don't try and load external DTD - no need if we are not validating, and may cause connection errors if a proxy is not set up. 43 this.parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 44 // a performance test showed that having this on lead to increased 45 // memory use for small-medium docs, and not much gain for large 46 // docs. 47 // http://www.sosnoski.com/opensrc/xmlbench/conclusions.html 48 this.parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 49 50 51 52 String siteHome = GSFile.siteHome(GlobalProperties.getGSDL3Home(), page.getSite()) ; 53 Document collectUi = page.getCollectUi() ; 54 55 if (collectUi != null) { 56 //System.out.println("looking for skin defined at collect level") ; 57 58 String collectHome = GSFile.collectionBaseDir(siteHome, page.getCollection()) ; 59 Element xmlSkin = (Element) GSXML.getChildByTagName(collectUi.getFirstChild(), "skin") ; 60 if (xmlSkin != null) { 61 //System.out.println("skin defined at collect level") ; 21 public File rootDirectory ; 22 public Document config ; 23 private Receptionist receptionist ; 24 25 protected DOMParser parser = null; 26 TransformerFactory transformerFactory=null; 27 28 // delete this later?? 29 //protected XMLConverter converter = null; 30 31 //private HashMap<String, String> xsltPagesByAction ; // ***** 32 private HashMap xsltPagesByAction ; 33 34 public Skin(Page page, Receptionist receptionist) throws Exception{ 35 36 this.receptionist = receptionist ; 37 this.transformerFactory = org.apache.xalan.processor.TransformerFactoryImpl.newInstance(); 38 //this.converter = new XMLConverter(); 39 transformerFactory.setURIResolver(new MyUriResolver()) ; 40 41 this.parser = new DOMParser(); 42 this.parser.setFeature("http://xml.org/sax/features/validation", false); 43 // don't try and load external DTD - no need if we are not validating, and may cause connection errors if a proxy is not set up. 44 this.parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 45 // a performance test showed that having this on lead to increased 46 // memory use for small-medium docs, and not much gain for large 47 // docs. 48 // http://www.sosnoski.com/opensrc/xmlbench/conclusions.html 49 this.parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 50 51 52 53 String siteHome = GSFile.siteHome(GlobalProperties.getGSDL3Home(), page.getSite()) ; 54 Document collectUi = page.getCollectUi() ; 55 56 if (collectUi != null) { 57 //System.out.println("looking for skin defined at collect level") ; 58 59 String collectHome = GSFile.collectionBaseDir(siteHome, page.getCollection()) ; 60 Element xmlSkin = (Element) GSXML.getChildByTagName(collectUi.getFirstChild(), "skin") ; 61 if (xmlSkin != null) { 62 //System.out.println("skin defined at collect level") ; 62 63 63 boolean inherit = Boolean.parseBoolean(xmlSkin.getAttribute("inherit")) ; 64 String inherit_att = xmlSkin.getAttribute("inherit") ; 65 Boolean inherit_ref = Boolean.valueOf(inherit_att); 66 boolean inherit = inherit_ref.booleanValue(); 64 67 65 if (inherit != true) { 66 boolean local = Boolean.parseBoolean(xmlSkin.getAttribute("local")) ; 67 if (local == true) { 68 rootDirectory = new File(collectHome + File.separatorChar + "ui") ; 69 } else { 70 String name = xmlSkin.getAttribute("name") ; 71 rootDirectory = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "skins" + File.separatorChar + File.separatorChar + name) ; 72 } 73 } 74 } 68 if (inherit != true) { 69 70 String local_att = xmlSkin.getAttribute("local") ; 71 Boolean local_ref = Boolean.valueOf(local_att); 72 boolean local = local_ref.booleanValue(); 73 74 if (local == true) { 75 rootDirectory = new File(collectHome + File.separatorChar + "ui") ; 76 } else { 77 String name = xmlSkin.getAttribute("name") ; 78 rootDirectory = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "skins" + File.separatorChar + File.separatorChar + name) ; 79 } 80 } 81 } 82 } 83 84 Document siteUi = page.getSiteUi() ; 85 86 if (rootDirectory == null && siteUi != null) { 87 //System.out.println("lookding for skin defined at site level") ; 88 89 Element xmlSkin = (Element) GSXML.getChildByTagName(siteUi.getFirstChild(), "skin") ; 90 if (xmlSkin != null) { 91 //System.out.println("skin defined at site level") ; 92 String local_att = xmlSkin.getAttribute("local") ; 93 Boolean local_ref = Boolean.valueOf(local_att); 94 boolean local = local_ref.booleanValue(); 95 96 if (local == true) { 97 rootDirectory = new File(siteHome + File.separatorChar + "ui") ; 98 } else { 99 String name = xmlSkin.getAttribute("name") ; 100 rootDirectory = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "skins" + File.separatorChar + name) ; 75 101 } 76 77 Document siteUi = page.getSiteUi() ; 78 79 if (rootDirectory == null && siteUi != null) { 80 //System.out.println("lookding for skin defined at site level") ; 81 82 Element xmlSkin = (Element) GSXML.getChildByTagName(siteUi.getFirstChild(), "skin") ; 83 if (xmlSkin != null) { 84 //System.out.println("skin defined at site level") ; 85 boolean local = Boolean.parseBoolean(xmlSkin.getAttribute("local")) ; 86 if (local == true) { 87 rootDirectory = new File(siteHome + File.separatorChar + "ui") ; 88 } else { 89 String name = xmlSkin.getAttribute("name") ; 90 rootDirectory = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "skins" + File.separatorChar + name) ; 91 } 92 } 93 } 94 95 if(rootDirectory == null) { 96 rootDirectory = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "skins" + File.separatorChar + "default") ; 97 } 98 99 100 File configFile = new File(rootDirectory.getAbsolutePath() + File.separatorChar + "skin.xml") ; 101 config = receptionist.converter.getDOM(configFile, "utf-8"); 102 103 104 // store which xslt pages are responsible for which actions 105 106 xsltPagesByAction = new HashMap() ; 107 108 NodeList actions = config.getElementsByTagName("action") ; 109 110 for (int ai=0 , an=actions.getLength() ; ai<an ; ai++){ 111 Element action = (Element) actions.item(ai) ; 112 String a = action.getAttribute("name") ; 113 114 String xsl = action.getAttribute("xslt") ; 115 if (!xsl.equals("")) 116 xsltPagesByAction.put(a, xsl) ; 117 118 NodeList subactions = action.getElementsByTagName("subaction") ; 119 120 for (int si=0 , sn=subactions.getLength() ; si<sn ; si++){ 121 Element subaction = (Element) subactions.item(si) ; 102 } 103 } 104 105 if(rootDirectory == null) { 106 rootDirectory = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "skins" + File.separatorChar + "default") ; 107 } 108 109 110 File configFile = new File(rootDirectory.getAbsolutePath() + File.separatorChar + "skin.xml") ; 111 config = receptionist.converter.getDOM(configFile, "utf-8"); 112 113 114 // store which xslt pages are responsible for which actions 115 116 xsltPagesByAction = new HashMap() ; 117 118 NodeList actions = config.getElementsByTagName("action") ; 119 120 for (int ai=0 , an=actions.getLength() ; ai<an ; ai++){ 121 Element action = (Element) actions.item(ai) ; 122 String a = action.getAttribute("name") ; 123 124 String xsl = action.getAttribute("xslt") ; 125 if (!xsl.equals("")) { 126 xsltPagesByAction.put(a, xsl) ; 127 } 128 129 NodeList subactions = action.getElementsByTagName("subaction") ; 130 131 for (int si=0 , sn=subactions.getLength() ; si<sn ; si++){ 132 Element subaction = (Element) subactions.item(si) ; 122 133 123 String sa = subaction.getAttribute("name") ; 124 xsl = subaction.getAttribute("xslt") ; 125 xsltPagesByAction.put(a + "_" + sa, xsl) ; 126 } 127 } 128 } 129 130 private Document getXSLTDoc(String action, String subaction) throws Exception { 131 //System.out.println("getting xslt for " + action + ", " + subaction) ; 132 133 String name = xsltPagesByAction.get(action + "_" + subaction) ; 134 135 if (name == null) 136 name = xsltPagesByAction.get(action) ; 137 138 File xslt_file = new File(rootDirectory.getAbsolutePath() + File.separatorChar + "xsl" + File.separatorChar + name) ; 139 //System.out.println("Skinning page using: " + xslt_file) ; 140 141 //if (!xslt_file.canRead()) 142 // xslt_file = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "xslt" + File.separatorChar + "error.xsl") ; 143 144 FileReader reader = new FileReader(xslt_file); 145 InputSource xml_source = new InputSource(reader); 146 this.parser.parse(xml_source); 147 Document doc = this.parser.getDocument(); 148 149 return doc ; 150 } 151 152 private Document getPreprocessDoc() throws Exception { 134 String sa = subaction.getAttribute("name") ; 135 xsl = subaction.getAttribute("xslt") ; 136 xsltPagesByAction.put(a + "_" + sa, xsl) ; 137 } 138 } 139 } 140 141 private Document getXSLTDoc(String action, String subaction) throws Exception { 142 //System.out.println("getting xslt for " + action + ", " + subaction) ; 143 144 String full_action = action + "_" + subaction; 145 String name = (String)xsltPagesByAction.get(full_action) ; 146 147 if (name == null) { 148 name = (String)xsltPagesByAction.get(action); 149 } 150 151 File xslt_file = new File(rootDirectory.getAbsolutePath() + File.separatorChar + "xsl" + File.separatorChar + name) ; 152 //System.out.println("Skinning page using: " + xslt_file) ; 153 154 //if (!xslt_file.canRead()) 155 // xslt_file = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "xslt" + File.separatorChar + "error.xsl") ; 156 157 FileReader reader = new FileReader(xslt_file); 158 InputSource xml_source = new InputSource(reader); 159 this.parser.parse(xml_source); 160 Document doc = this.parser.getDocument(); 161 162 return doc ; 163 } 164 165 private Document getPreprocessDoc() throws Exception { 153 166 154 155 156 157 158 159 167 File xslt_file = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "xslt" + File.separatorChar + "preProcess.xsl") ; 168 169 FileReader reader = new FileReader(xslt_file); 170 InputSource xml_source = new InputSource(reader); 171 this.parser.parse(xml_source); 172 Document doc = this.parser.getDocument(); 160 173 161 162 163 164 174 return doc ; 175 } 176 177 private Document getLibraryDoc() throws Exception { 165 178 166 167 168 169 170 171 179 File xslt_file = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "xslt" + File.separatorChar + "library.xsl") ; 180 181 FileReader reader = new FileReader(xslt_file); 182 InputSource xml_source = new InputSource(reader); 183 this.parser.parse(xml_source); 184 Document doc = this.parser.getDocument(); 172 185 173 return doc ; 174 } 175 176 177 public Node transformPage(Page page) throws Exception{ 178 179 Element p = page.getPage() ; 180 Element pr = page.getPageResponse() ; 181 Element transformedPage = null ; 182 183 Element blah = receptionist.doc.createElement("Skin") ; 184 blah.setAttribute("skinLocation", rootDirectory.getAbsolutePath()) ; 185 pr.appendChild(blah) ; 186 187 Document sourceXml ; 188 try { 189 sourceXml = receptionist.converter.newDOM(); 190 sourceXml.appendChild(sourceXml.importNode(p, true)); 191 } catch (Exception e) { 192 System.out.println("error loading source data") ; 193 e.printStackTrace() ; 194 return constructErrorPage(e) ; 195 } 196 197 Document skinXsl ; 198 try { 199 skinXsl = getXSLTDoc(page.getAction(), page.getSubaction()) ; 200 } catch (Exception e) { 201 System.out.println("error loading skin xslt") ; 202 e.printStackTrace() ; 203 return constructErrorPage(e) ; 204 } 205 206 Document preprocessingXsl ; 207 try { 208 preprocessingXsl = getPreprocessDoc() ; 209 } catch (Exception e) { 210 System.out.println("error loading preprocessing xslt") ; 211 e.printStackTrace() ; 212 return constructErrorPage(e) ; 213 } 214 215 Document libraryXsl ; 216 try { 217 libraryXsl = getLibraryDoc() ; 218 } catch (Exception e) { 219 System.out.println("error loading preprocessing xslt") ; 220 e.printStackTrace() ; 221 return constructErrorPage(e) ; 222 } 223 224 // combine skin file and library variables/templates into one document. 225 // we dont just use xsl:import because the preprocessing stage needs 226 // to know what's available in the library. 227 228 Document skinAndLibraryXsl ; 229 Document processedXsl = receptionist.converter.newDOM(); 230 try { 231 232 skinAndLibraryXsl = receptionist.converter.newDOM(); 233 Element root = skinAndLibraryXsl.createElement("skinAndLibraryXsl") ; 234 skinAndLibraryXsl.appendChild(root) ; 186 return doc ; 187 } 188 189 190 public Node transformPage(Page page) throws Exception { 191 192 193 Element p = page.getPage() ; 194 Element pr = page.getPageResponse() ; 195 Element transformedPage = null ; 196 197 Element blah = receptionist.doc.createElement("Skin") ; 198 blah.setAttribute("skinLocation", rootDirectory.getAbsolutePath()) ; 199 pr.appendChild(blah) ; 200 201 Document sourceXml ; 202 try { 203 sourceXml = receptionist.converter.newDOM(); 204 sourceXml.appendChild(sourceXml.importNode(p, true)); 205 } catch (Exception e) { 206 System.out.println("error loading source data") ; 207 e.printStackTrace() ; 208 return constructErrorPage(e) ; 209 } 210 211 Document skinXsl ; 212 try { 213 skinXsl = getXSLTDoc(page.getAction(), page.getSubaction()) ; 214 } catch (Exception e) { 215 System.out.println("error loading skin xslt") ; 216 e.printStackTrace() ; 217 return constructErrorPage(e) ; 218 } 219 220 Document preprocessingXsl ; 221 try { 222 preprocessingXsl = getPreprocessDoc() ; 223 } catch (Exception e) { 224 System.out.println("error loading preprocessing xslt") ; 225 e.printStackTrace() ; 226 return constructErrorPage(e) ; 227 } 228 229 Document libraryXsl ; 230 try { 231 libraryXsl = getLibraryDoc() ; 232 } catch (Exception e) { 233 System.out.println("error loading preprocessing xslt") ; 234 e.printStackTrace() ; 235 return constructErrorPage(e) ; 236 } 237 238 // combine skin file and library variables/templates into one document. 239 // we dont just use xsl:import because the preprocessing stage needs 240 // to know what's available in the library. 241 242 Document skinAndLibraryXsl ; 243 Document processedXsl = receptionist.converter.newDOM(); 244 try { 245 246 skinAndLibraryXsl = receptionist.converter.newDOM(); 247 Element root = skinAndLibraryXsl.createElement("skinAndLibraryXsl") ; 248 skinAndLibraryXsl.appendChild(root) ; 249 250 Element s = skinAndLibraryXsl.createElement("skinXsl") ; 251 s.appendChild(skinAndLibraryXsl.importNode(skinXsl.getDocumentElement(), true)) ; 252 root.appendChild(s) ; 253 254 Element l = skinAndLibraryXsl.createElement("libraryXsl") ; 255 l.appendChild(skinAndLibraryXsl.importNode(libraryXsl.getDocumentElement(), true)) ; 256 root.appendChild(l) ; 257 258 259 // System.out.println("Pre - processing") ; 260 //pre-process the skin style sheet 261 Transformer preProcessor = transformerFactory.newTransformer(new DOMSource(preprocessingXsl)); 262 DOMResult result = new DOMResult(); 263 result.setNode(processedXsl); 264 preProcessor.transform(new DOMSource(skinAndLibraryXsl), result); 265 } catch (TransformerException e) { 266 e.printStackTrace() ; 267 return constructErrorPage(e) ; 268 } catch (Exception e) { 269 System.out.println("error preprocessing the skin xslt") ; 270 e.printStackTrace() ; 271 return constructErrorPage(e) ; 272 } 273 274 //return sourceXml.getDocumentElement() ; 275 276 // Document finalPage = receptionist.converter.newDOM(); // **** 277 Document finalPageParsed = null; 278 279 //transform source doc 280 try { 281 282 /* 283 //Tranform XSL DOM to String 284 TransformerFactory tf = TransformerFactory.newInstance(); 285 Transformer trans = tf.newTransformer(); 286 StringWriter sw = new StringWriter(); 287 trans.transform(new DOMSource(processedXsl), new StreamResult(sw)); 288 // String theXSLAnswer = sw.toString(); // **** 289 290 291 //Tranform XML DOM to String 292 TransformerFactory tf2 = TransformerFactory.newInstance(); 293 Transformer trans2 = tf2.newTransformer(); 294 StringWriter sw2 = new StringWriter(); 295 trans2.transform(new DOMSource(sourceXml), new StreamResult(sw2)); 296 //String theXMLAnswer = sw2.toString(); 297 */ 298 299 Transformer transformer = transformerFactory.newTransformer(new DOMSource(processedXsl)); 300 // transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"); 301 //DOMResult result = new DOMResult(); 302 //result.setNode(finalPage); 303 304 //transformer.transform(new DOMSource(sourceXml), result2); 305 StringWriter result2 = new StringWriter(); 306 transformer.transform(new DOMSource(sourceXml), new StreamResult(result2)); 307 308 XMLConverter converter = new XMLConverter(); 309 310 finalPageParsed = converter.getDOM(result2.toString()); 311 312 } catch (TransformerException e) { 313 e.printStackTrace() ; 314 return constructErrorPage(e) ; 315 } catch (Exception e) { 316 317 System.out.println("error transforming page") ; 318 e.printStackTrace() ; 319 320 //return processedXsl.getDocumentElement() ; 321 return constructErrorPage(e) ; 322 } 323 324 return finalPageParsed; 325 } 326 327 private Element constructErrorPage(TransformerException exception) { 328 Document doc = receptionist.doc; 329 Element page = doc.createElement("page") ; 330 331 Element header = doc.createElement("h1") ; 332 333 334 // header.setTextContent("Error"); // Java 1.5 335 header.appendChild(doc.createTextNode("Error")); // Java 1.4 336 337 page.appendChild(header) ; 338 339 Element prompt = doc.createElement("p") ; 340 // prompt.setTextContent("The following exception occured: ") ; 341 prompt.appendChild(doc.createTextNode("The following exception occured: ")); 342 page.appendChild((prompt)) ; 343 344 Element errorXml = doc.createElement("code") ; 345 //errorXml.setTextContent(exception.getMessageAndLocation()) ; 346 errorXml.appendChild(doc.createTextNode(exception.getMessageAndLocation())) ; 347 page.appendChild(errorXml) ; 348 349 return page ; 350 351 } 352 353 private Element constructErrorPage(Exception exception) { 354 Document doc = receptionist.doc; 355 Element page = doc.createElement("page") ; 356 357 Element header = doc.createElement("h1") ; 358 //header.setTextContent("Error"); // Java 1.5 359 header.appendChild(doc.createTextNode("Error")); // Java 1.4 360 361 page.appendChild(header) ; 362 363 Element prompt = doc.createElement("p") ; 364 // prompt.setTextContent("The following exception occured: ") ; 365 prompt.appendChild(doc.createTextNode("The following exception occured: ")); 366 367 page.appendChild((prompt)) ; 368 369 Element errorXml = doc.createElement("code") ; 370 //errorXml.setTextContent(exception.toString()) ; 371 errorXml.appendChild(doc.createTextNode(exception.toString())) ; 372 page.appendChild(errorXml) ; 373 374 return page ; 375 } 376 377 378 private Element constructErrorPage(Element source, Document style, Exception exception) 379 { 380 Document doc = receptionist.doc; 381 Element page = doc.createElement("page") ; 382 383 Element header = doc.createElement("h1") ; 384 //header.setTextContent("Error") ; // Java 1.5 385 header.appendChild(doc.createTextNode("Error")); // Java 1.4 386 387 page.appendChild(header) ; 388 389 Element prompt = doc.createElement("p") ; 390 //prompt.setTextContent("The following exception occured: ") ; 391 prompt.appendChild(doc.createTextNode("The following exception occured: ")); 392 page.appendChild((prompt)) ; 393 394 Element errorXml = doc.createElement("code") ; 395 //errorXml.setTextContent(exception.getMessage()) ; 396 errorXml.appendChild(doc.createTextNode(exception.getMessage())) ; 397 page.appendChild(errorXml) ; 398 399 /* 400 Element stackTrace = doc.createElement("ul") ; 401 402 StackTraceElement[] st = exception.getStackTrace() ; 403 for (int i=0 ; i< st.length ; i++) { 404 Element ste = doc.createElement("li") ; 405 ste.setTextContent(st[i].toString()) ; 406 stackTrace.appendChild(ste) ; 407 } 408 errorMessage.appendChild(stackTrace) ; 409 */ 410 411 Element sourceHeader = doc.createElement("h2") ; 412 //sourceHeader.setTextContent("Source page:") ; 413 sourceHeader.appendChild(doc.createTextNode("Source page:")) ; 414 page.appendChild(sourceHeader) ; 415 416 Element sourceXml = doc.createElement("pre") ; 417 String source_pp = receptionist.converter.getPrettyString(source); 418 //sourceXml.setTextContent(source_pp) ; 419 sourceXml.appendChild(doc.createTextNode(source_pp)) ; 420 page.appendChild(sourceXml) ; 421 422 Element styleHeader = doc.createElement("h2") ; 423 //styleHeader.setTextContent("Style page:") ; 424 styleHeader.appendChild(doc.createTextNode("Style page:")) ; 425 page.appendChild(styleHeader) ; 426 427 Element styleXml = doc.createElement("pre") ; 428 String style_pp = receptionist.converter.getPrettyString(style); 429 430 //styleXml.setTextContent(style_pp); 431 styleXml.appendChild(doc.createTextNode(style_pp)); 432 page.appendChild(styleXml) ; 433 434 435 return (Element)page ; 436 } 437 438 private class MyUriResolver implements URIResolver { 439 440 public Source resolve(String href, String base) { 441 442 System.out.println("resolving href='" + href + "', base='" + base + "'") ; 235 443 236 Element s = skinAndLibraryXsl.createElement("skinXsl") ; 237 s.appendChild(skinAndLibraryXsl.importNode(skinXsl.getDocumentElement(), true)) ; 238 root.appendChild(s) ; 239 240 Element l = skinAndLibraryXsl.createElement("libraryXsl") ; 241 l.appendChild(skinAndLibraryXsl.importNode(libraryXsl.getDocumentElement(), true)) ; 242 root.appendChild(l) ; 243 244 245 System.out.println("Pre - processing") ; 246 //pre-process the skin style sheet 247 Transformer preProcessor = transformerFactory.newTransformer(new DOMSource(preprocessingXsl)); 248 DOMResult result = new DOMResult(); 249 result.setNode(processedXsl) ; 250 preProcessor.transform(new DOMSource(skinAndLibraryXsl), result); 251 } catch (TransformerException e) { 252 return constructErrorPage(e) ; 253 } catch (Exception e) { 254 System.out.println("error preprocessing the skin xslt") ; 255 e.printStackTrace() ; 256 return constructErrorPage(e) ; 257 } 258 259 //return sourceXml.getDocumentElement() ; 260 261 // Document finalPage = receptionist.converter.newDOM(); // **** 262 Document finalPageParsed = null; 263 264 //transform source doc 265 try { 266 267 /* 268 //Tranform XSL DOM to String 269 TransformerFactory tf = TransformerFactory.newInstance(); 270 Transformer trans = tf.newTransformer(); 271 StringWriter sw = new StringWriter(); 272 trans.transform(new DOMSource(processedXsl), new StreamResult(sw)); 273 // String theXSLAnswer = sw.toString(); // **** 274 275 276 //Tranform XML DOM to String 277 TransformerFactory tf2 = TransformerFactory.newInstance(); 278 Transformer trans2 = tf2.newTransformer(); 279 StringWriter sw2 = new StringWriter(); 280 trans2.transform(new DOMSource(sourceXml), new StreamResult(sw2)); 281 //String theXMLAnswer = sw2.toString(); 282 */ 283 284 Transformer transformer = transformerFactory.newTransformer(new DOMSource(processedXsl)); 285 // transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"); 286 //DOMResult result = new DOMResult(); 287 //result.setNode(finalPage); 288 289 //transformer.transform(new DOMSource(sourceXml), result2); 290 StringWriter result2 = new StringWriter(); 291 transformer.transform(new DOMSource(sourceXml), new StreamResult(result2)); 292 293 XMLConverter converter = new XMLConverter(); 294 finalPageParsed = converter.getDOM(result2.toString()); 295 296 } catch (TransformerException e) { 297 return constructErrorPage(e) ; 298 } catch (Exception e) { 299 300 System.out.println("error transforming page") ; 301 e.printStackTrace() ; 302 303 //return processedXsl.getDocumentElement() ; 304 return constructErrorPage(e) ; 305 } 306 307 return finalPageParsed; 308 } 309 310 private Element constructErrorPage(TransformerException exception) { 311 Element page = receptionist.doc.createElement("page") ; 312 313 Element header = receptionist.doc.createElement("h1") ; 314 header.setTextContent("Error") ; 315 page.appendChild(header) ; 316 317 Element prompt = receptionist.doc.createElement("p") ; 318 prompt.setTextContent("The following exception occured: ") ; 319 page.appendChild((prompt)) ; 320 321 Element errorXml = receptionist.doc.createElement("code") ; 322 errorXml.setTextContent(exception.getMessageAndLocation()) ; 323 page.appendChild(errorXml) ; 324 325 return page ; 326 327 } 328 329 private Element constructErrorPage(Exception exception) { 330 Element page = receptionist.doc.createElement("page") ; 331 332 Element header = receptionist.doc.createElement("h1") ; 333 header.setTextContent("Error") ; 334 page.appendChild(header) ; 335 336 Element prompt = receptionist.doc.createElement("p") ; 337 prompt.setTextContent("The following exception occured: ") ; 338 page.appendChild((prompt)) ; 339 340 Element errorXml = receptionist.doc.createElement("code") ; 341 errorXml.setTextContent(exception.toString()) ; 342 page.appendChild(errorXml) ; 343 344 345 return page ; 346 } 347 348 349 private Element constructErrorPage(Element source, Document style, Exception exception) { 350 Element page = receptionist.doc.createElement("page") ; 351 352 Element header = receptionist.doc.createElement("h1") ; 353 header.setTextContent("Error") ; 354 page.appendChild(header) ; 355 356 Element prompt = receptionist.doc.createElement("p") ; 357 prompt.setTextContent("The following exception occured: ") ; 358 page.appendChild((prompt)) ; 359 360 Element errorXml = receptionist.doc.createElement("code") ; 361 errorXml.setTextContent(exception.getMessage()) ; 362 page.appendChild(errorXml) ; 363 364 /* 365 Element stackTrace = receptionist.doc.createElement("ul") ; 366 367 StackTraceElement[] st = exception.getStackTrace() ; 368 for (int i=0 ; i< st.length ; i++) { 369 Element ste = receptionist.doc.createElement("li") ; 370 ste.setTextContent(st[i].toString()) ; 371 stackTrace.appendChild(ste) ; 372 } 373 errorMessage.appendChild(stackTrace) ; 374 */ 375 376 Element sourceHeader = receptionist.doc.createElement("h2") ; 377 sourceHeader.setTextContent("Source page:") ; 378 page.appendChild(sourceHeader) ; 379 380 Element sourceXml = receptionist.doc.createElement("pre") ; 381 sourceXml.setTextContent(receptionist.converter.getPrettyString(source)) ; 382 page.appendChild(sourceXml) ; 383 384 Element styleHeader = receptionist.doc.createElement("h2") ; 385 styleHeader.setTextContent("Style page:") ; 386 page.appendChild(styleHeader) ; 387 388 Element styleXml = receptionist.doc.createElement("pre") ; 389 styleXml.setTextContent(receptionist.converter.getPrettyString(style)) ; 390 page.appendChild(styleXml) ; 391 392 393 return (Element)page ; 394 } 395 396 private class MyUriResolver implements URIResolver { 397 398 public Source resolve(String href, String base) { 399 400 System.out.println("resolving href='" + href + "', base='" + base + "'") ; 401 402 // check in the skin directory first 403 404 File file = new File(rootDirectory.getAbsolutePath() + File.separatorChar + "xslt" + File.separatorChar + href) ; 405 406 // then check in the xslt library directory 407 if (!file.canRead()) 408 file = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "xslt" + File.separatorChar + href) ; 409 410 if (file.canRead()) { 411 Source source = new StreamSource(file) ; 412 return source ; 413 } else 414 return null ; 415 } 416 417 } 444 // check in the skin directory first 445 446 File file = new File(rootDirectory.getAbsolutePath() + File.separatorChar + "xslt" + File.separatorChar + href) ; 447 448 // then check in the xslt library directory 449 if (!file.canRead()) 450 file = new File(GlobalProperties.getGSDL3Home() + File.separatorChar + "ui" + File.separatorChar + "xslt" + File.separatorChar + href) ; 451 452 if (file.canRead()) { 453 Source source = new StreamSource(file) ; 454 return source ; 455 } else 456 return null ; 457 } 458 459 } 418 460 419 461
Note:
See TracChangeset
for help on using the changeset viewer.