⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmsxmlxercesparser.java

📁 cms是开源的框架
💻 JAVA
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/template/CmsXmlXercesParser.java,v $
* Date   : $Date: 2005/05/31 15:51:19 $
* Version: $Revision: 1.2 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001  The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org 
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


package com.opencms.template;

import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;

import org.apache.xerces.parsers.DOMParser;
import org.apache.xml.serialize.DOMSerializer;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Implementation of the OpenCms XML parser interface for
 * the Xerces parser.
 * 
 * @author Alexander Kandzior
 * @author Alexander Lucas
 * @version $Revision: 1.2 $ $Date: 2005/05/31 15:51:19 $
 * 
 * @deprecated Will not be supported past the OpenCms 6 release.
 */
public class CmsXmlXercesParser implements I_CmsXmlParser {
    
    /** Prevents the parser from printing multiple error messages.*/
    private static boolean c_xercesWarning = false;
    
    /**
     * Creates an empty DOM XML document.
     * Workaround because the original method is not working as expected.
     * 
     * @param docNod first Node in empty  XML document
     * @return Empty document.
     */
    public Document createEmptyDocument(String docNod) throws Exception {
        String docXml = new String("<?xml version=\"1.0\" encoding=\"" + OpenCms.getSystemInfo().getDefaultEncoding() + "\"?>");
        docXml = docXml + "<" + docNod + ">" + "</" + docNod + ">";
        StringReader reader = new StringReader(docXml);
        return parse(reader);
    }
    
    /**
     * Calls a XML printer for converting a XML DOM document
     * to a String.
     * @param doc Document to be printed.
     * @param out OutputStream to print to.
     */
    public void getXmlText(Document doc, OutputStream out, String encoding) {
        OutputFormat outf =
            new OutputFormat(doc, (encoding == null) ? getOriginalEncoding(doc) : encoding, true);
        outf.setLineWidth(C_XML_LINE_WIDTH);
        outf.setPreserveSpace(false);
        XMLSerializer serializer = new XMLSerializer(out, outf);
        try {
            DOMSerializer domSerializer = serializer.asDOMSerializer();
            domSerializer.serialize(doc);
        } catch (Exception e) {
            if (CmsLog.getLog(this).isErrorEnabled()) {
                CmsLog.getLog(this).error("Xml parsing error", e);
            }
        }
    }

    /**
     * Calls a XML printer for converting a XML DOM document
     * to a String.
     * @param doc Document to be printed.
     * @param out Writer to print to.
     * @param encoding the character encoding to be used while serializing
     */
    public void getXmlText(Document doc, Writer out, String encoding) {
        OutputFormat outf =
            new OutputFormat(doc, (encoding == null) ? getOriginalEncoding(doc) : encoding, true);
        outf.setLineWidth(C_XML_LINE_WIDTH);
        outf.setPreserveSpace(false);
        XMLSerializer serializer = new XMLSerializer(out, outf);
        try {
            DOMSerializer domSerializer = serializer.asDOMSerializer();
            domSerializer.serialize(doc);
        } catch (Exception e) {
            if (CmsLog.getLog(this).isErrorEnabled()) {
                CmsLog.getLog(this).error("Xml parsing error", e);
            }
        }
    }

    /**
     * Calls a XML printer for converting a XML DOM document
     * to a String.
     * @param doc Document to be printed.
     * @param out Writer to print to.
     */
    public void getXmlText(Document doc, Writer out) {
        getXmlText(doc, out, getOriginalEncoding(doc));
    }
    
    /**
     * Used to import a node from a foreign document.
     * @param doc Destination document that should import the node.
     * @param node Node to be imported.
     * @return New node that belongs to the document <code>doc</code>
     */
    public Node importNode(Document doc, Node node) {
        return ((org.apache.xerces.dom.DocumentImpl)doc).importNode(node, true);
    }
    
    /**
     * Parses the given text with the Xerces parser.
     * @param in Reader with the input text.
     * @return Parsed text as DOM document.
     * @throws Exception
     */
    public Document parse(Reader in) throws Exception {
        return parse(new InputSource(in));
    }
    
    public Document parse(InputStream in) throws Exception {
        return parse(new InputSource(in));
    }
    
    /**
     * Common internal method to actually parse input.
     */ 
    protected Document parse(InputSource input) throws Exception {
        DOMParser parser = new DOMParser();
        try {
            parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
        }
        catch(SAXException e) {
            if(CmsLog.getLog(this).isWarnEnabled()  && !c_xercesWarning) {
                CmsLog.getLog(this).warn("Cannot set parser feature for apache xerces XML parser, you should use Xerces 1.1.1 or newer");
                c_xercesWarning = true;
            }
        }
        parser.parse(input);
        return parser.getDocument();
    }
    
    public void serialize(Document doc, OutputStream in) throws Exception {
    }

    /**
     * Gets a description of the parser.
     * @return Parser description.
     */
    public String toString() {
        return "Apache Xerces XML Parser";
    }

    private static int m_xercesVersion = 0;
    
    public String getOriginalEncoding(Document doc) {
        // this functionality has experimental status in Apache Xerces parser 1.4.x and 2.x
        // as it implements DOM level 3 functionality not completly and W3C' DOM3 API
        // is in working draft stage
        if (doc instanceof org.apache.xerces.dom.CoreDocumentImpl) {
            org.apache.xerces.dom.CoreDocumentImpl d = (org.apache.xerces.dom.CoreDocumentImpl)doc;
            String result = null;
            
            // Xerces 1 and 2 APIs are different, we need to accomondate...
            if ((m_xercesVersion == 2) || (m_xercesVersion == 0)) {
                try {
                    result = (String)d.getClass().getMethod("getXmlEncoding", new Class[]{}).invoke(d, new Object[]{});
                    m_xercesVersion = 2;
                } catch (Throwable t) {
                    CmsLog.getLog(this).debug("Xerces 2 not found - getXmlEncoding() did not work", t);
                }
            }
            if ((m_xercesVersion == 1) || (m_xercesVersion == 0)) {
                try {
                    result = (String)d.getClass().getMethod("getEncoding", new Class[]{}).invoke(d, new Object[]{});
                    m_xercesVersion = 1;
                } catch (Throwable t) {
                    CmsLog.getLog(this).debug("Xerces 1 not found - getEncoding() did not work", t);
                }
            }           
            // String result = ((org.apache.xerces.dom.CoreDocumentImpl)doc).getEncoding();
            if ((result != null) && !"".equals(result.trim())) {
                return result;
            }            
        }
        // in other cases we just return default encoding
        return OpenCms.getSystemInfo().getDefaultEncoding();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -