KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > model > loader > IntroContentParser


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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
12 package org.eclipse.ui.internal.intro.impl.model.loader;
13
14 import java.io.IOException JavaDoc;
15 import java.io.StringReader JavaDoc;
16 import java.io.StringWriter JavaDoc;
17
18 import javax.xml.parsers.DocumentBuilder JavaDoc;
19 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
20 import javax.xml.parsers.ParserConfigurationException JavaDoc;
21 import javax.xml.transform.OutputKeys JavaDoc;
22 import javax.xml.transform.Transformer JavaDoc;
23 import javax.xml.transform.TransformerConfigurationException JavaDoc;
24 import javax.xml.transform.TransformerException JavaDoc;
25 import javax.xml.transform.TransformerFactory JavaDoc;
26 import javax.xml.transform.dom.DOMSource JavaDoc;
27 import javax.xml.transform.stream.StreamResult JavaDoc;
28
29 import org.eclipse.ui.internal.intro.impl.util.Log;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.DocumentType JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33 import org.xml.sax.EntityResolver JavaDoc;
34 import org.xml.sax.InputSource JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36 import org.xml.sax.SAXParseException JavaDoc;
37
38
39 /**
40  *
41  */

42 public class IntroContentParser {
43
44     private static String JavaDoc TAG_INTRO_CONTENT = "introContent"; //$NON-NLS-1$
45
private static String JavaDoc TAG_HTML = "html"; //$NON-NLS-1$
46

47     private Document JavaDoc document;
48     private boolean hasXHTMLContent;
49
50     /**
51      * Creates a config parser assuming that the passed content represents a URL
52      * to the content file.
53      */

54     public IntroContentParser(String JavaDoc content) {
55         try {
56             document = parse(content);
57             if (document != null) {
58                 // xml file is loaded. It can be either XHTML or intro XML.
59
Element JavaDoc rootElement = document.getDocumentElement();
60                 // DocumentType docType = document.getDoctype();
61
if (rootElement.getTagName().equals(TAG_INTRO_CONTENT)) {
62                     // intro xml file.
63
hasXHTMLContent = false;
64                 } else if (rootElement.getTagName().equals(TAG_HTML)) {
65                     // rely on root element to detect if we have an XHTML file
66
// and not on doctype. We need to support xhtml files with
67
// no doctype.
68
hasXHTMLContent = true;
69                 } else
70                     // not intro XML nor XHTML.
71
document = null;
72             }
73         } catch (Exception JavaDoc e) {
74             Log.error("Could not load Intro content file: " + content, e); //$NON-NLS-1$
75
}
76     }
77
78
79     private Document JavaDoc parse(String JavaDoc fileURI) {
80         Document JavaDoc document = null;
81         try {
82             DocumentBuilderFactory JavaDoc docFactory = DocumentBuilderFactory
83                 .newInstance();
84             docFactory.setValidating(false);
85             // if this is not set, Document.getElementsByTagNameNS() will fail.
86
docFactory.setNamespaceAware(true);
87             docFactory.setExpandEntityReferences(false);
88             DocumentBuilder JavaDoc parser = docFactory.newDocumentBuilder();
89             parser.setEntityResolver(new EntityResolver JavaDoc() {
90                 public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
91                     return new InputSource JavaDoc(new StringReader JavaDoc("")); //$NON-NLS-1$
92
}
93             });
94             document = parser.parse(fileURI);
95             return document;
96
97         } catch (SAXParseException JavaDoc spe) {
98             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("IntroParser error in line "); //$NON-NLS-1$
99
buffer.append(spe.getLineNumber());
100             buffer.append(", uri "); //$NON-NLS-1$
101
buffer.append(spe.getSystemId());
102             buffer.append("\n"); //$NON-NLS-1$
103
buffer.append(spe.getMessage());
104
105             // Use the contained exception.
106
Exception JavaDoc x = spe;
107             if (spe.getException() != null)
108                 x = spe.getException();
109             Log.error(buffer.toString(), x);
110
111         } catch (SAXException JavaDoc sxe) {
112             Exception JavaDoc x = sxe;
113             if (sxe.getException() != null)
114                 x = sxe.getException();
115             Log.error(x.getMessage(), x);
116
117         } catch (ParserConfigurationException JavaDoc pce) {
118             // Parser with specified options can't be built
119
Log.error(pce.getMessage(), pce);
120
121         } catch (IOException JavaDoc ioe) {
122             Log.error(ioe.getMessage(), ioe);
123         }
124         return null;
125     }
126
127
128     /**
129      * Returned the DOM representing the intro xml content file. May return null
130      * if parsing the file failed.
131      *
132      * @return Returns the document.
133      */

134     public Document JavaDoc getDocument() {
135         return document;
136     }
137
138     public boolean hasXHTMLContent() {
139         return hasXHTMLContent;
140     }
141
142
143     public static String JavaDoc convertToString(Document JavaDoc document) {
144         try {
145             // identity xslt.
146
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
147             Transformer JavaDoc transformer = tFactory.newTransformer();
148
149             DOMSource JavaDoc source = new DOMSource JavaDoc(document);
150
151             StringWriter JavaDoc stringBuffer = new StringWriter JavaDoc();
152             StreamResult JavaDoc result = new StreamResult JavaDoc(stringBuffer);
153
154             // setup properties, for doctype.
155
DocumentType JavaDoc docType = document.getDoctype();
156             if (docType != null) {
157                 String JavaDoc value = docType.getSystemId();
158                 // transformer.clearParameters();
159
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, value);
160                 value = document.getDoctype().getPublicId();
161                 transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, value);
162                 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
163
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
164                     "yes"); //$NON-NLS-1$
165
// transformer.setOutputProperty(OutputKeys.MEDIA_TYPE,
166
// "text/html");
167
// transformer
168
// .setOutputProperty(OutputKeys.ENCODING, "iso-8859-1");
169
} else
170                 Log
171                     .warning("XHTML file used to display this Intro page does not have a Document type defined. " //$NON-NLS-1$
172
+ "XHTML requires document types to be defined."); //$NON-NLS-1$
173

174             transformer.transform(source, result);
175             return stringBuffer.toString();
176
177         } catch (TransformerConfigurationException JavaDoc tce) {
178             // Error generated by the parser
179
Log.error("Transformer Config error: " + tce.getMessage(), null); //$NON-NLS-1$
180
// Use the contained exception, if any
181
Throwable JavaDoc x = tce;
182             if (tce.getException() != null)
183                 x = tce.getException();
184             Log.error("Transformer Stack trace: ", x); //$NON-NLS-1$
185

186         } catch (TransformerException JavaDoc te) {
187             // Error generated by the parser
188
Log.error("Transformer error: " + te.getMessage(), te); //$NON-NLS-1$
189
// Use the contained exception, if any
190
Throwable JavaDoc x = te;
191             if (te.getException() != null)
192                 x = te.getException();
193             Log.error("Transformer Stack trace: ", x); //$NON-NLS-1$
194

195         }
196         return null;
197
198     }
199
200
201
202 }
203
Popular Tags