1 11 12 package org.eclipse.ui.internal.intro.impl.model.loader; 13 14 import java.io.IOException ; 15 import java.io.StringReader ; 16 import java.io.StringWriter ; 17 18 import javax.xml.parsers.DocumentBuilder ; 19 import javax.xml.parsers.DocumentBuilderFactory ; 20 import javax.xml.parsers.ParserConfigurationException ; 21 import javax.xml.transform.OutputKeys ; 22 import javax.xml.transform.Transformer ; 23 import javax.xml.transform.TransformerConfigurationException ; 24 import javax.xml.transform.TransformerException ; 25 import javax.xml.transform.TransformerFactory ; 26 import javax.xml.transform.dom.DOMSource ; 27 import javax.xml.transform.stream.StreamResult ; 28 29 import org.eclipse.ui.internal.intro.impl.util.Log; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.DocumentType ; 32 import org.w3c.dom.Element ; 33 import org.xml.sax.EntityResolver ; 34 import org.xml.sax.InputSource ; 35 import org.xml.sax.SAXException ; 36 import org.xml.sax.SAXParseException ; 37 38 39 42 public class IntroContentParser { 43 44 private static String TAG_INTRO_CONTENT = "introContent"; private static String TAG_HTML = "html"; 47 private Document document; 48 private boolean hasXHTMLContent; 49 50 54 public IntroContentParser(String content) { 55 try { 56 document = parse(content); 57 if (document != null) { 58 Element rootElement = document.getDocumentElement(); 60 if (rootElement.getTagName().equals(TAG_INTRO_CONTENT)) { 62 hasXHTMLContent = false; 64 } else if (rootElement.getTagName().equals(TAG_HTML)) { 65 hasXHTMLContent = true; 69 } else 70 document = null; 72 } 73 } catch (Exception e) { 74 Log.error("Could not load Intro content file: " + content, e); } 76 } 77 78 79 private Document parse(String fileURI) { 80 Document document = null; 81 try { 82 DocumentBuilderFactory docFactory = DocumentBuilderFactory 83 .newInstance(); 84 docFactory.setValidating(false); 85 docFactory.setNamespaceAware(true); 87 docFactory.setExpandEntityReferences(false); 88 DocumentBuilder parser = docFactory.newDocumentBuilder(); 89 parser.setEntityResolver(new EntityResolver () { 90 public InputSource resolveEntity(String publicId, String systemId) throws SAXException { 91 return new InputSource (new StringReader ("")); } 93 }); 94 document = parser.parse(fileURI); 95 return document; 96 97 } catch (SAXParseException spe) { 98 StringBuffer buffer = new StringBuffer ("IntroParser error in line "); buffer.append(spe.getLineNumber()); 100 buffer.append(", uri "); buffer.append(spe.getSystemId()); 102 buffer.append("\n"); buffer.append(spe.getMessage()); 104 105 Exception x = spe; 107 if (spe.getException() != null) 108 x = spe.getException(); 109 Log.error(buffer.toString(), x); 110 111 } catch (SAXException sxe) { 112 Exception x = sxe; 113 if (sxe.getException() != null) 114 x = sxe.getException(); 115 Log.error(x.getMessage(), x); 116 117 } catch (ParserConfigurationException pce) { 118 Log.error(pce.getMessage(), pce); 120 121 } catch (IOException ioe) { 122 Log.error(ioe.getMessage(), ioe); 123 } 124 return null; 125 } 126 127 128 134 public Document getDocument() { 135 return document; 136 } 137 138 public boolean hasXHTMLContent() { 139 return hasXHTMLContent; 140 } 141 142 143 public static String convertToString(Document document) { 144 try { 145 TransformerFactory tFactory = TransformerFactory.newInstance(); 147 Transformer transformer = tFactory.newTransformer(); 148 149 DOMSource source = new DOMSource (document); 150 151 StringWriter stringBuffer = new StringWriter (); 152 StreamResult result = new StreamResult (stringBuffer); 153 154 DocumentType docType = document.getDoctype(); 156 if (docType != null) { 157 String value = docType.getSystemId(); 158 transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, value); 160 value = document.getDoctype().getPublicId(); 161 transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, value); 162 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, 164 "yes"); } else 170 Log 171 .warning("XHTML file used to display this Intro page does not have a Document type defined. " + "XHTML requires document types to be defined."); 174 transformer.transform(source, result); 175 return stringBuffer.toString(); 176 177 } catch (TransformerConfigurationException tce) { 178 Log.error("Transformer Config error: " + tce.getMessage(), null); Throwable x = tce; 182 if (tce.getException() != null) 183 x = tce.getException(); 184 Log.error("Transformer Stack trace: ", x); 186 } catch (TransformerException te) { 187 Log.error("Transformer error: " + te.getMessage(), te); Throwable x = te; 191 if (te.getException() != null) 192 x = te.getException(); 193 Log.error("Transformer Stack trace: ", x); 195 } 196 return null; 197 198 } 199 200 201 202 } 203 | Popular Tags |