toxmlstream.java

来自「JAVA 所有包」· Java 代码 · 共 627 行 · 第 1/2 页

JAVA
627
字号
/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Id: ToXMLStream.java,v 1.2.4.2 2005/09/15 12:01:25 suresh_emailid Exp $ */ package com.sun.org.apache.xml.internal.serializer;import java.io.IOException;import javax.xml.transform.ErrorListener;import javax.xml.transform.Result;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerException;import com.sun.org.apache.xml.internal.serializer.utils.MsgKey;import com.sun.org.apache.xml.internal.serializer.utils.Utils;import org.xml.sax.SAXException;/** * This class converts SAX or SAX-like calls to a  * serialized xml document.  The xsl:output method is "xml". *  * This class is used explicitly in code generated by XSLTC,  * so it is "public", but it should  * be viewed as internal or package private, this is not an API. *  * @xsl.usage internal */public final class ToXMLStream extends ToStream{    /**     * remembers if we need to write out "]]>" to close the CDATA     */    boolean m_cdataTagOpen = false;    /**     * Map that tells which XML characters should have special treatment, and it     *  provides character to entity name lookup.     */    private static CharInfo m_xmlcharInfo =//      new CharInfo(CharInfo.XML_ENTITIES_RESOURCE);        CharInfo.getCharInfo(CharInfo.XML_ENTITIES_RESOURCE, Method.XML);    /**     * Default constructor.     */    public ToXMLStream()    {        m_charInfo = m_xmlcharInfo;        initCDATA();        // initialize namespaces        m_prefixMap = new NamespaceMappings();    }    /**     * Copy properties from another SerializerToXML.     *     * @param xmlListener non-null reference to a SerializerToXML object.     */    public void CopyFrom(ToXMLStream xmlListener)    {        m_writer = xmlListener.m_writer;        // m_outputStream = xmlListener.m_outputStream;        String encoding = xmlListener.getEncoding();        setEncoding(encoding);        setOmitXMLDeclaration(xmlListener.getOmitXMLDeclaration());        m_ispreserve = xmlListener.m_ispreserve;        m_preserves = xmlListener.m_preserves;        m_isprevtext = xmlListener.m_isprevtext;        m_doIndent = xmlListener.m_doIndent;        setIndentAmount(xmlListener.getIndentAmount());        m_startNewLine = xmlListener.m_startNewLine;        m_needToOutputDocTypeDecl = xmlListener.m_needToOutputDocTypeDecl;        setDoctypeSystem(xmlListener.getDoctypeSystem());        setDoctypePublic(xmlListener.getDoctypePublic());                setStandalone(xmlListener.getStandalone());        setMediaType(xmlListener.getMediaType());        m_maxCharacter = xmlListener.m_maxCharacter;        m_encodingInfo = xmlListener.m_encodingInfo;        m_spaceBeforeClose = xmlListener.m_spaceBeforeClose;        m_cdataStartCalled = xmlListener.m_cdataStartCalled;    }    /**     * Receive notification of the beginning of a document.     *     * @throws org.xml.sax.SAXException Any SAX exception, possibly     *            wrapping another exception.     *     * @throws org.xml.sax.SAXException     */    public void startDocumentInternal() throws org.xml.sax.SAXException    {        if (m_needToCallStartDocument)        {             super.startDocumentInternal();            m_needToCallStartDocument = false;            if (m_inEntityRef)                return;            m_needToOutputDocTypeDecl = true;            m_startNewLine = false;            /* The call to getXMLVersion() might emit an error message             * and we should emit this message regardless of if we are              * writing out an XML header or not.             */             if (getOmitXMLDeclaration() == false)            {                String encoding = Encodings.getMimeEncoding(getEncoding());                String version = getVersion();                if (version == null)                    version = "1.0";                String standalone;                if (m_standaloneWasSpecified)                {                    standalone = " standalone=\"" + getStandalone() + "\"";                }                else                {                    standalone = "";                }                try                {                    final java.io.Writer writer = m_writer;                    writer.write("<?xml version=\"");                    writer.write(version);                    writer.write("\" encoding=\"");                    writer.write(encoding);                    writer.write('\"');                    writer.write(standalone);                    writer.write("?>");                    if (m_doIndent)                        writer.write(m_lineSep, 0, m_lineSepLen);                }                 catch(IOException e)                {                    throw new SAXException(e);                }            }        }    }    /**     * Receive notification of the end of a document.     *     * @throws org.xml.sax.SAXException Any SAX exception, possibly     *            wrapping another exception.     *     * @throws org.xml.sax.SAXException     */    public void endDocument() throws org.xml.sax.SAXException    {        flushPending();        if (m_doIndent && !m_isprevtext)        {            try            {            outputLineSep();            }            catch(IOException e)            {                throw new SAXException(e);            }        }        flushWriter();                if (m_tracer != null)            super.fireEndDoc();    }    /**     * Starts a whitespace preserving section. All characters printed     * within a preserving section are printed without indentation and     * without consolidating multiple spaces. This is equivalent to     * the <tt>xml:space=&quot;preserve&quot;</tt> attribute. Only XML     * and HTML serializers need to support this method.     * <p>     * The contents of the whitespace preserving section will be delivered     * through the regular <tt>characters</tt> event.     *     * @throws org.xml.sax.SAXException     */    public void startPreserving() throws org.xml.sax.SAXException    {        // Not sure this is really what we want.  -sb        m_preserves.push(true);        m_ispreserve = true;    }    /**     * Ends a whitespace preserving section.     *     * @see #startPreserving     *     * @throws org.xml.sax.SAXException     */    public void endPreserving() throws org.xml.sax.SAXException    {        // Not sure this is really what we want.  -sb        m_ispreserve = m_preserves.isEmpty() ? false : m_preserves.pop();    }    /**     * Receive notification of a processing instruction.     *     * @param target The processing instruction target.     * @param data The processing instruction data, or null if     *        none was supplied.     * @throws org.xml.sax.SAXException Any SAX exception, possibly     *            wrapping another exception.     *     * @throws org.xml.sax.SAXException     */    public void processingInstruction(String target, String data)        throws org.xml.sax.SAXException    {        if (m_inEntityRef)            return;                flushPending();           if (target.equals(Result.PI_DISABLE_OUTPUT_ESCAPING))        {            startNonEscaping();        }        else if (target.equals(Result.PI_ENABLE_OUTPUT_ESCAPING))        {            endNonEscaping();        }        else        {            try            {                if (m_elemContext.m_startTagOpen)                {                    closeStartTag();                    m_elemContext.m_startTagOpen = false;                }                else if (m_needToCallStartDocument)                    startDocumentInternal();                                if (shouldIndent())                    indent();                final java.io.Writer writer = m_writer;                writer.write("<?");                writer.write(target);                if (data.length() > 0                    && !Character.isSpaceChar(data.charAt(0)))                    writer.write(' ');                int indexOfQLT = data.indexOf("?>");                if (indexOfQLT >= 0)                {                    // See XSLT spec on error recovery of "?>" in PIs.                    if (indexOfQLT > 0)                    {                        writer.write(data.substring(0, indexOfQLT));                    }                    writer.write("? >"); // add space between.                    if ((indexOfQLT + 2) < data.length())                    {                        writer.write(data.substring(indexOfQLT + 2));                    }                }                else                {                    writer.write(data);                }                writer.write('?');                writer.write('>');                // Always output a newline char if not inside of an                // element. The whitespace is not significant in that                // case.                if (m_elemContext.m_currentElemDepth <= 0)

⌨️ 快捷键说明

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