1 9 10 package org.eclipse.help.internal.xhtml; 11 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.lang.reflect.UndeclaredThrowableException ; 15 import java.net.URL ; 16 import java.util.Hashtable ; 17 18 import javax.xml.parsers.DocumentBuilder ; 19 import javax.xml.parsers.DocumentBuilderFactory ; 20 import javax.xml.parsers.ParserConfigurationException ; 21 22 import org.eclipse.help.internal.HelpPlugin; 23 import org.w3c.dom.Document ; 24 import org.w3c.dom.Element ; 25 import org.xml.sax.EntityResolver ; 26 import org.xml.sax.InputSource ; 27 import org.xml.sax.SAXException ; 28 29 30 33 public class UAContentParser { 34 35 private static String TAG_HTML = "html"; protected static String XHTML1_TRANSITIONAL = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"; protected static String XHTML1_STRICT = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"; protected static String XHTML1_FRAMESET = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"; 40 41 protected static Hashtable dtdMap = new Hashtable (); 42 43 static { 44 String dtdBaseLocation = "dtds/xhtml1-20020801/"; 46 String dtdLocation = dtdBaseLocation + "xhtml1-transitional.dtd"; URL dtdURL_T = BundleUtil.getResourceAsURL(dtdLocation, "org.eclipse.ui.intro"); dtdMap.put(XHTML1_TRANSITIONAL, dtdURL_T); 49 50 dtdLocation = dtdBaseLocation + "xhtml1-strict.dtd"; URL dtdURL_S = BundleUtil.getResourceAsURL(dtdLocation, "org.eclipse.ui.intro"); dtdMap.put(XHTML1_STRICT, dtdURL_S); 53 54 dtdLocation = dtdBaseLocation + "xhtml1-frameset.dtd"; URL dtdURL_F = BundleUtil.getResourceAsURL(dtdLocation, "org.eclipse.ui.intro"); dtdMap.put(XHTML1_FRAMESET, dtdURL_F); 57 } 58 59 60 61 private Document document; 62 private boolean hasXHTMLContent; 63 64 public UAContentParser(String content) { 65 parseDocument(content); 66 } 67 68 public UAContentParser(InputStream content) { 69 parseDocument(content); 70 } 71 72 76 public void parseDocument(Object content) { 77 document = doParse(content); 78 if (document != null) { 79 Element rootElement = document.getDocumentElement(); 80 if (rootElement.getTagName().equals(TAG_HTML)) { 82 hasXHTMLContent = true; 83 } 84 } 85 } 86 87 private DocumentBuilder createParser() { 88 89 try { 90 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 91 docFactory.setValidating(false); 92 docFactory.setNamespaceAware(true); 93 docFactory.setExpandEntityReferences(false); 94 DocumentBuilder parser = docFactory.newDocumentBuilder(); 95 96 parser.setEntityResolver(new EntityResolver () { 97 98 public InputSource resolveEntity(String publicId, String systemId) throws SAXException , 99 IOException { 100 101 if (systemId.equals(XHTML1_TRANSITIONAL) || systemId.equals(XHTML1_STRICT) 102 || systemId.equals(XHTML1_FRAMESET)) { 103 104 URL dtdURL = (URL ) dtdMap.get(systemId); 105 InputSource in = new InputSource (dtdURL.openStream()); 106 in.setSystemId(dtdURL.toExternalForm()); 107 return in; 108 } 109 return null; 110 } 111 }); 112 return parser; 113 } catch (ParserConfigurationException pce) { 114 HelpPlugin.logError(pce.getMessage(), pce); 115 } 116 return null; 117 } 118 119 120 121 private Document doParse(Object fileObject) { 122 try { 123 DocumentBuilder parser = createParser(); 124 if (fileObject instanceof String ) 125 return parser.parse((String ) fileObject); 126 else if (fileObject instanceof InputStream ) 127 return parser.parse((InputStream ) fileObject); 128 return null; 129 } 130 catch (Exception e) { 131 HelpPlugin.logError("An error occured while parsing: " + fileObject, e); throw new UndeclaredThrowableException (e); 136 } 137 } 138 139 145 public Document getDocument() { 146 return document; 147 } 148 149 public boolean hasXHTMLContent() { 150 return hasXHTMLContent; 151 } 152 153 154 155 156 } 157 | Popular Tags |