KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > xhtml > UAContentParser


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

9
10 package org.eclipse.help.internal.xhtml;
11
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.Hashtable 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.HelpPlugin;
23 import org.w3c.dom.Document JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.xml.sax.EntityResolver JavaDoc;
26 import org.xml.sax.InputSource JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28
29
30 /**
31  *
32  */

33 public class UAContentParser {
34
35     private static String JavaDoc TAG_HTML = "html"; //$NON-NLS-1$
36
protected static String JavaDoc XHTML1_TRANSITIONAL = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"; //$NON-NLS-1$
37
protected static String JavaDoc XHTML1_STRICT = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"; //$NON-NLS-1$
38
protected static String JavaDoc XHTML1_FRAMESET = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"; //$NON-NLS-1$
39

40
41     protected static Hashtable JavaDoc dtdMap = new Hashtable JavaDoc();
42
43     static {
44         String JavaDoc dtdBaseLocation = "dtds/xhtml1-20020801/"; //$NON-NLS-1$
45

46         String JavaDoc dtdLocation = dtdBaseLocation + "xhtml1-transitional.dtd"; //$NON-NLS-1$
47
URL JavaDoc dtdURL_T = BundleUtil.getResourceAsURL(dtdLocation, "org.eclipse.ui.intro"); //$NON-NLS-1$
48
dtdMap.put(XHTML1_TRANSITIONAL, dtdURL_T);
49
50         dtdLocation = dtdBaseLocation + "xhtml1-strict.dtd"; //$NON-NLS-1$
51
URL JavaDoc dtdURL_S = BundleUtil.getResourceAsURL(dtdLocation, "org.eclipse.ui.intro"); //$NON-NLS-1$
52
dtdMap.put(XHTML1_STRICT, dtdURL_S);
53
54         dtdLocation = dtdBaseLocation + "xhtml1-frameset.dtd"; //$NON-NLS-1$
55
URL JavaDoc dtdURL_F = BundleUtil.getResourceAsURL(dtdLocation, "org.eclipse.ui.intro"); //$NON-NLS-1$
56
dtdMap.put(XHTML1_FRAMESET, dtdURL_F);
57     }
58
59
60
61     private Document JavaDoc document;
62     private boolean hasXHTMLContent;
63
64     public UAContentParser(String JavaDoc content) {
65         parseDocument(content);
66     }
67
68     public UAContentParser(InputStream JavaDoc content) {
69         parseDocument(content);
70     }
71
72     /**
73      * Creates a config parser assuming that the passed content represents a URL to the content
74      * file.
75      */

76     public void parseDocument(Object JavaDoc content) {
77         document = doParse(content);
78         if (document != null) {
79             Element JavaDoc rootElement = document.getDocumentElement();
80             // DocumentType docType = document.getDoctype();
81
if (rootElement.getTagName().equals(TAG_HTML)) {
82                 hasXHTMLContent = true;
83             }
84         }
85     }
86
87     private DocumentBuilder JavaDoc createParser() {
88
89         try {
90             DocumentBuilderFactory JavaDoc docFactory = DocumentBuilderFactory.newInstance();
91             docFactory.setValidating(false);
92             docFactory.setNamespaceAware(true);
93             docFactory.setExpandEntityReferences(false);
94             DocumentBuilder JavaDoc parser = docFactory.newDocumentBuilder();
95
96             parser.setEntityResolver(new EntityResolver JavaDoc() {
97
98                 public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc,
99                         IOException JavaDoc {
100
101                     if (systemId.equals(XHTML1_TRANSITIONAL) || systemId.equals(XHTML1_STRICT)
102                             || systemId.equals(XHTML1_FRAMESET)) {
103
104                         URL JavaDoc dtdURL = (URL JavaDoc) dtdMap.get(systemId);
105                         InputSource JavaDoc in = new InputSource JavaDoc(dtdURL.openStream());
106                         in.setSystemId(dtdURL.toExternalForm());
107                         return in;
108                     }
109                     return null;
110                 }
111             });
112             return parser;
113         } catch (ParserConfigurationException JavaDoc pce) {
114             HelpPlugin.logError(pce.getMessage(), pce);
115         }
116         return null;
117     }
118
119
120
121     private Document JavaDoc doParse(Object JavaDoc fileObject) {
122         try {
123             DocumentBuilder JavaDoc parser = createParser();
124             if (fileObject instanceof String JavaDoc)
125                 return parser.parse((String JavaDoc) fileObject);
126             else if (fileObject instanceof InputStream JavaDoc)
127                 return parser.parse((InputStream JavaDoc) fileObject);
128             return null;
129         }
130         catch (Exception JavaDoc e) {
131             // log it
132
HelpPlugin.logError("An error occured while parsing: " + fileObject, e); //$NON-NLS-1$
133
// wrap it in an unchecked wrapper so that it finds its way
134
// to the error message
135
throw new UndeclaredThrowableException JavaDoc(e);
136         }
137     }
138
139     /**
140      * Returned the DOM representing the xml content file. May return null if parsing the file
141      * failed.
142      *
143      * @return Returns the document.
144      */

145     public Document JavaDoc getDocument() {
146         return document;
147     }
148
149     public boolean hasXHTMLContent() {
150         return hasXHTMLContent;
151     }
152
153
154
155
156 }
157
Popular Tags