1 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 ; 36 import java.io.OutputStream ; 37 import java.io.Reader ; 38 import java.io.StringReader ; 39 import java.io.Writer ; 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 ; 47 import org.w3c.dom.Node ; 48 import org.xml.sax.InputSource ; 49 import org.xml.sax.SAXException ; 50 51 61 public class CmsXmlXercesParser implements I_CmsXmlParser { 62 63 64 private static boolean c_xercesWarning = false; 65 66 73 public Document createEmptyDocument(String docNod) throws Exception { 74 String docXml = new String ("<?xml version=\"1.0\" encoding=\"" + OpenCms.getSystemInfo().getDefaultEncoding() + "\"?>"); 75 docXml = docXml + "<" + docNod + ">" + "</" + docNod + ">"; 76 StringReader reader = new StringReader (docXml); 77 return parse(reader); 78 } 79 80 86 public void getXmlText(Document doc, OutputStream out, String 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 e) { 96 if (CmsLog.getLog(this).isErrorEnabled()) { 97 CmsLog.getLog(this).error("Xml parsing error", e); 98 } 99 } 100 } 101 102 109 public void getXmlText(Document doc, Writer out, String 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 e) { 119 if (CmsLog.getLog(this).isErrorEnabled()) { 120 CmsLog.getLog(this).error("Xml parsing error", e); 121 } 122 } 123 } 124 125 131 public void getXmlText(Document doc, Writer out) { 132 getXmlText(doc, out, getOriginalEncoding(doc)); 133 } 134 135 141 public Node importNode(Document doc, Node node) { 142 return ((org.apache.xerces.dom.DocumentImpl)doc).importNode(node, true); 143 } 144 145 151 public Document parse(Reader in) throws Exception { 152 return parse(new InputSource (in)); 153 } 154 155 public Document parse(InputStream in) throws Exception { 156 return parse(new InputSource (in)); 157 } 158 159 162 protected Document parse(InputSource input) throws Exception { 163 DOMParser parser = new DOMParser(); 164 try { 165 parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false); 166 } 167 catch(SAXException 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 doc, OutputStream in) throws Exception { 178 } 179 180 184 public String toString() { 185 return "Apache Xerces XML Parser"; 186 } 187 188 private static int m_xercesVersion = 0; 189 190 public String getOriginalEncoding(Document doc) { 191 if (doc instanceof org.apache.xerces.dom.CoreDocumentImpl) { 195 org.apache.xerces.dom.CoreDocumentImpl d = (org.apache.xerces.dom.CoreDocumentImpl)doc; 196 String result = null; 197 198 if ((m_xercesVersion == 2) || (m_xercesVersion == 0)) { 200 try { 201 result = (String )d.getClass().getMethod("getXmlEncoding", new Class []{}).invoke(d, new Object []{}); 202 m_xercesVersion = 2; 203 } catch (Throwable 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 )d.getClass().getMethod("getEncoding", new Class []{}).invoke(d, new Object []{}); 210 m_xercesVersion = 1; 211 } catch (Throwable t) { 212 CmsLog.getLog(this).debug("Xerces 1 not found - getEncoding() did not work", t); 213 } 214 } 215 if ((result != null) && !"".equals(result.trim())) { 217 return result; 218 } 219 } 220 return OpenCms.getSystemInfo().getDefaultEncoding(); 222 } 223 } 224
| Popular Tags
|