KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > template > CmsXmlXercesParser


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/template/CmsXmlXercesParser.java,v $
3 * Date : $Date: 2005/05/31 15:51:19 $
4 * Version: $Revision: 1.2 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * For further information about OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */

28
29
30 package com.opencms.template;
31
32 import org.opencms.main.CmsLog;
33 import org.opencms.main.OpenCms;
34
35 import java.io.InputStream JavaDoc;
36 import java.io.OutputStream JavaDoc;
37 import java.io.Reader JavaDoc;
38 import java.io.StringReader JavaDoc;
39 import java.io.Writer JavaDoc;
40
41 import org.apache.xerces.parsers.DOMParser;
42 import org.apache.xml.serialize.DOMSerializer;
43 import org.apache.xml.serialize.OutputFormat;
44 import org.apache.xml.serialize.XMLSerializer;
45
46 import org.w3c.dom.Document JavaDoc;
47 import org.w3c.dom.Node JavaDoc;
48 import org.xml.sax.InputSource JavaDoc;
49 import org.xml.sax.SAXException JavaDoc;
50
51 /**
52  * Implementation of the OpenCms XML parser interface for
53  * the Xerces parser.
54  *
55  * @author Alexander Kandzior
56  * @author Alexander Lucas
57  * @version $Revision: 1.2 $ $Date: 2005/05/31 15:51:19 $
58  *
59  * @deprecated Will not be supported past the OpenCms 6 release.
60  */

61 public class CmsXmlXercesParser implements I_CmsXmlParser {
62     
63     /** Prevents the parser from printing multiple error messages.*/
64     private static boolean c_xercesWarning = false;
65     
66     /**
67      * Creates an empty DOM XML document.
68      * Workaround because the original method is not working as expected.
69      *
70      * @param docNod first Node in empty XML document
71      * @return Empty document.
72      */

73     public Document JavaDoc createEmptyDocument(String JavaDoc docNod) throws Exception JavaDoc {
74         String JavaDoc docXml = new String JavaDoc("<?xml version=\"1.0\" encoding=\"" + OpenCms.getSystemInfo().getDefaultEncoding() + "\"?>");
75         docXml = docXml + "<" + docNod + ">" + "</" + docNod + ">";
76         StringReader JavaDoc reader = new StringReader JavaDoc(docXml);
77         return parse(reader);
78     }
79     
80     /**
81      * Calls a XML printer for converting a XML DOM document
82      * to a String.
83      * @param doc Document to be printed.
84      * @param out OutputStream to print to.
85      */

86     public void getXmlText(Document JavaDoc doc, OutputStream JavaDoc out, String JavaDoc encoding) {
87         OutputFormat outf =
88             new OutputFormat(doc, (encoding == null) ? getOriginalEncoding(doc) : encoding, true);
89         outf.setLineWidth(C_XML_LINE_WIDTH);
90         outf.setPreserveSpace(false);
91         XMLSerializer serializer = new XMLSerializer(out, outf);
92         try {
93             DOMSerializer domSerializer = serializer.asDOMSerializer();
94             domSerializer.serialize(doc);
95         } catch (Exception JavaDoc e) {
96             if (CmsLog.getLog(this).isErrorEnabled()) {
97                 CmsLog.getLog(this).error("Xml parsing error", e);
98             }
99         }
100     }
101
102     /**
103      * Calls a XML printer for converting a XML DOM document
104      * to a String.
105      * @param doc Document to be printed.
106      * @param out Writer to print to.
107      * @param encoding the character encoding to be used while serializing
108      */

109     public void getXmlText(Document JavaDoc doc, Writer JavaDoc out, String JavaDoc encoding) {
110         OutputFormat outf =
111             new OutputFormat(doc, (encoding == null) ? getOriginalEncoding(doc) : encoding, true);
112         outf.setLineWidth(C_XML_LINE_WIDTH);
113         outf.setPreserveSpace(false);
114         XMLSerializer serializer = new XMLSerializer(out, outf);
115         try {
116             DOMSerializer domSerializer = serializer.asDOMSerializer();
117             domSerializer.serialize(doc);
118         } catch (Exception JavaDoc e) {
119             if (CmsLog.getLog(this).isErrorEnabled()) {
120                 CmsLog.getLog(this).error("Xml parsing error", e);
121             }
122         }
123     }
124
125     /**
126      * Calls a XML printer for converting a XML DOM document
127      * to a String.
128      * @param doc Document to be printed.
129      * @param out Writer to print to.
130      */

131     public void getXmlText(Document JavaDoc doc, Writer JavaDoc out) {
132         getXmlText(doc, out, getOriginalEncoding(doc));
133     }
134     
135     /**
136      * Used to import a node from a foreign document.
137      * @param doc Destination document that should import the node.
138      * @param node Node to be imported.
139      * @return New node that belongs to the document <code>doc</code>
140      */

141     public Node JavaDoc importNode(Document JavaDoc doc, Node JavaDoc node) {
142         return ((org.apache.xerces.dom.DocumentImpl)doc).importNode(node, true);
143     }
144     
145     /**
146      * Parses the given text with the Xerces parser.
147      * @param in Reader with the input text.
148      * @return Parsed text as DOM document.
149      * @throws Exception
150      */

151     public Document JavaDoc parse(Reader JavaDoc in) throws Exception JavaDoc {
152         return parse(new InputSource JavaDoc(in));
153     }
154     
155     public Document JavaDoc parse(InputStream JavaDoc in) throws Exception JavaDoc {
156         return parse(new InputSource JavaDoc(in));
157     }
158     
159     /**
160      * Common internal method to actually parse input.
161      */

162     protected Document JavaDoc parse(InputSource JavaDoc input) throws Exception JavaDoc {
163         DOMParser parser = new DOMParser();
164         try {
165             parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
166         }
167         catch(SAXException JavaDoc e) {
168             if(CmsLog.getLog(this).isWarnEnabled() && !c_xercesWarning) {
169                 CmsLog.getLog(this).warn("Cannot set parser feature for apache xerces XML parser, you should use Xerces 1.1.1 or newer");
170                 c_xercesWarning = true;
171             }
172         }
173         parser.parse(input);
174         return parser.getDocument();
175     }
176     
177     public void serialize(Document JavaDoc doc, OutputStream JavaDoc in) throws Exception JavaDoc {
178     }
179
180     /**
181      * Gets a description of the parser.
182      * @return Parser description.
183      */

184     public String JavaDoc toString() {
185         return "Apache Xerces XML Parser";
186     }
187
188     private static int m_xercesVersion = 0;
189     
190     public String JavaDoc getOriginalEncoding(Document JavaDoc doc) {
191         // this functionality has experimental status in Apache Xerces parser 1.4.x and 2.x
192
// as it implements DOM level 3 functionality not completly and W3C' DOM3 API
193
// is in working draft stage
194
if (doc instanceof org.apache.xerces.dom.CoreDocumentImpl) {
195             org.apache.xerces.dom.CoreDocumentImpl d = (org.apache.xerces.dom.CoreDocumentImpl)doc;
196             String JavaDoc result = null;
197             
198             // Xerces 1 and 2 APIs are different, we need to accomondate...
199
if ((m_xercesVersion == 2) || (m_xercesVersion == 0)) {
200                 try {
201                     result = (String JavaDoc)d.getClass().getMethod("getXmlEncoding", new Class JavaDoc[]{}).invoke(d, new Object JavaDoc[]{});
202                     m_xercesVersion = 2;
203                 } catch (Throwable JavaDoc t) {
204                     CmsLog.getLog(this).debug("Xerces 2 not found - getXmlEncoding() did not work", t);
205                 }
206             }
207             if ((m_xercesVersion == 1) || (m_xercesVersion == 0)) {
208                 try {
209                     result = (String JavaDoc)d.getClass().getMethod("getEncoding", new Class JavaDoc[]{}).invoke(d, new Object JavaDoc[]{});
210                     m_xercesVersion = 1;
211                 } catch (Throwable JavaDoc t) {
212                     CmsLog.getLog(this).debug("Xerces 1 not found - getEncoding() did not work", t);
213                 }
214             }
215             // String result = ((org.apache.xerces.dom.CoreDocumentImpl)doc).getEncoding();
216
if ((result != null) && !"".equals(result.trim())) {
217                 return result;
218             }
219         }
220         // in other cases we just return default encoding
221
return OpenCms.getSystemInfo().getDefaultEncoding();
222     }
223 }
224
Popular Tags