KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > dynamic > DocumentReader


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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