1 11 package org.eclipse.help.internal.dynamic; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.InputStreamReader ; 16 import java.io.StringReader ; 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.UAElement; 23 import org.eclipse.help.internal.UAElementFactory; 24 import org.w3c.dom.Document ; 25 import org.xml.sax.EntityResolver ; 26 import org.xml.sax.InputSource ; 27 import org.xml.sax.SAXException ; 28 29 public class DocumentReader { 30 31 private DocumentBuilder builder; 32 33 public UAElement read(InputStream in) throws IOException , SAXException , ParserConfigurationException { 34 return read(in, null); 35 } 36 37 public UAElement read(InputStream in, String charset) throws IOException , SAXException , ParserConfigurationException { 38 if (builder == null) { 39 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 40 factory.setNamespaceAware(false); 41 factory.setExpandEntityReferences(false); 42 builder = factory.newDocumentBuilder(); 43 builder.setEntityResolver(new EntityResolver () { 44 public InputSource resolveEntity(String publicId, String systemId) throws SAXException { 45 return new InputSource (new StringReader ("")); } 47 }); 48 } 49 InputSource input = null; 50 if (charset != null) { 51 input = new InputSource (new InputStreamReader (in, charset)); 52 } 53 else { 54 input = new InputSource (in); 55 } 56 Document document = builder.parse(input); 57 return UAElementFactory.newElement(document.getDocumentElement()); 58 } 59 } 60 | Popular Tags |