src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/ToXMLStream.java
author chegar
Thu, 17 Oct 2019 20:53:35 +0100
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 47216 71c04702a3d5
child 58679 9c3209ff7550
permissions -rw-r--r--
datagramsocketimpl-branch: update to default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
58678
9cf78a70fa4f datagramsocketimpl-branch: update to default
chegar
parents: 47216
diff changeset
     2
 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
/*
42805
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
     5
 * Licensed to the Apache Software Foundation (ASF) under one or more
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
     6
 * contributor license agreements.  See the NOTICE file distributed with
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
     7
 * this work for additional information regarding copyright ownership.
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
     8
 * The ASF licenses this file to You under the Apache License, Version 2.0
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
     9
 * (the "License"); you may not use this file except in compliance with
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
    10
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
42805
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
42805
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
    20
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 package com.sun.org.apache.xml.internal.serializer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import javax.xml.transform.ErrorListener;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import javax.xml.transform.Result;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import javax.xml.transform.Transformer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import javax.xml.transform.TransformerException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.xml.internal.serializer.utils.MsgKey;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.xml.internal.serializer.utils.Utils;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import org.xml.sax.SAXException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * This class converts SAX or SAX-like calls to a
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * serialized xml document.  The xsl:output method is "xml".
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 * This class is used explicitly in code generated by XSLTC,
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 * so it is "public", but it should
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 * be viewed as internal or package private, this is not an API.
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * @xsl.usage internal
58678
9cf78a70fa4f datagramsocketimpl-branch: update to default
chegar
parents: 47216
diff changeset
    43
 * @LastModified: Aug 2019
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
public final class ToXMLStream extends ToStream
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
     * remembers if we need to write out "]]>" to close the CDATA
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
    boolean m_cdataTagOpen = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
     * Map that tells which XML characters should have special treatment, and it
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
     *  provides character to entity name lookup.
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
     */
12902
0a840d92fa30 7151118: Regressions on 7u4 b11 comp. 7u4 b06 on specjvm2008.xml.transform subbenchmark
joehw
parents: 12458
diff changeset
    58
    private static CharInfo m_xmlcharInfo =
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
//      new CharInfo(CharInfo.XML_ENTITIES_RESOURCE);
23954
1161e065d446 8029282: Enhance CharInfo set up
joehw
parents: 12902
diff changeset
    60
        CharInfo.getCharInfoInternal(CharInfo.XML_ENTITIES_RESOURCE, Method.XML);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
     * Default constructor.
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    public ToXMLStream()
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
    {
58678
9cf78a70fa4f datagramsocketimpl-branch: update to default
chegar
parents: 47216
diff changeset
    67
        this(null);
9cf78a70fa4f datagramsocketimpl-branch: update to default
chegar
parents: 47216
diff changeset
    68
    }
9cf78a70fa4f datagramsocketimpl-branch: update to default
chegar
parents: 47216
diff changeset
    69
9cf78a70fa4f datagramsocketimpl-branch: update to default
chegar
parents: 47216
diff changeset
    70
    /**
9cf78a70fa4f datagramsocketimpl-branch: update to default
chegar
parents: 47216
diff changeset
    71
     * Default constructor.
9cf78a70fa4f datagramsocketimpl-branch: update to default
chegar
parents: 47216
diff changeset
    72
     */
9cf78a70fa4f datagramsocketimpl-branch: update to default
chegar
parents: 47216
diff changeset
    73
    public ToXMLStream(ErrorListener l)
9cf78a70fa4f datagramsocketimpl-branch: update to default
chegar
parents: 47216
diff changeset
    74
    {
9cf78a70fa4f datagramsocketimpl-branch: update to default
chegar
parents: 47216
diff changeset
    75
        super(l);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
        m_charInfo = m_xmlcharInfo;
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
        initCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
        // initialize namespaces
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
        m_prefixMap = new NamespaceMappings();
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
     * Copy properties from another SerializerToXML.
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
     * @param xmlListener non-null reference to a SerializerToXML object.
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
    public void CopyFrom(ToXMLStream xmlListener)
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
        m_writer = xmlListener.m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
        // m_outputStream = xmlListener.m_outputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
        String encoding = xmlListener.getEncoding();
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
        setEncoding(encoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
        setOmitXMLDeclaration(xmlListener.getOmitXMLDeclaration());
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
42805
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
   100
        m_ispreserveSpace = xmlListener.m_ispreserveSpace;
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
   101
        m_preserveSpaces = xmlListener.m_preserveSpaces;
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
   102
        m_childNodeNum = xmlListener.m_childNodeNum;
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
   103
        m_childNodeNumStack = xmlListener.m_childNodeNumStack;
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
   104
        m_charactersBuffer = xmlListener.m_charactersBuffer;
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
   105
        m_inEntityRef = xmlListener.m_inEntityRef;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
        m_isprevtext = xmlListener.m_isprevtext;
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
        m_doIndent = xmlListener.m_doIndent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
        setIndentAmount(xmlListener.getIndentAmount());
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
        m_startNewLine = xmlListener.m_startNewLine;
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
        m_needToOutputDocTypeDecl = xmlListener.m_needToOutputDocTypeDecl;
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
        setDoctypeSystem(xmlListener.getDoctypeSystem());
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
        setDoctypePublic(xmlListener.getDoctypePublic());
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
        setStandalone(xmlListener.getStandalone());
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
        setMediaType(xmlListener.getMediaType());
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
        m_maxCharacter = xmlListener.m_maxCharacter;
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
        m_encodingInfo = xmlListener.m_encodingInfo;
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        m_spaceBeforeClose = xmlListener.m_spaceBeforeClose;
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
        m_cdataStartCalled = xmlListener.m_cdataStartCalled;
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
     * Receive notification of the beginning of a document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
     * @throws org.xml.sax.SAXException Any SAX exception, possibly
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
     *            wrapping another exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
    public void startDocumentInternal() throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
        if (m_needToCallStartDocument)
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
            super.startDocumentInternal();
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
            m_needToCallStartDocument = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
42805
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
   138
            if (isInEntityRef())
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
            m_needToOutputDocTypeDecl = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
            m_startNewLine = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
            /* The call to getXMLVersion() might emit an error message
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
             * and we should emit this message regardless of if we are
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
             * writing out an XML header or not.
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
             */
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
            if (getOmitXMLDeclaration() == false)
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
                String encoding = Encodings.getMimeEncoding(getEncoding());
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
                String version = getVersion();
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
                if (version == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
                    version = "1.0";
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
                String standalone;
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
                if (m_standaloneWasSpecified)
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
                    standalone = " standalone=\"" + getStandalone() + "\"";
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
                    standalone = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
                try
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
                    final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
                    writer.write("<?xml version=\"");
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
                    writer.write(version);
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
                    writer.write("\" encoding=\"");
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
                    writer.write(encoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
                    writer.write('\"');
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
                    writer.write(standalone);
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
                    writer.write("?>");
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   174
                    if (m_doIndent) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   175
                        if (m_standaloneWasSpecified
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   176
                                || getDoctypePublic() != null
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   177
                                || getDoctypeSystem() != null
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   178
                                || m_isStandalone) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   179
                            // We almost never put a newline after the XML
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   180
                            // header because this XML could be used as
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   181
                            // an extenal general parsed entity
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   182
                            // and we don't know the context into which it
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   183
                            // will be used in the future.  Only when
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   184
                            // standalone, or a doctype system or public is
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   185
                            // specified are we free to insert a new line
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   186
                            // after the header.  Is it even worth bothering
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   187
                            // in these rare cases?
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   188
                            writer.write(m_lineSep, 0, m_lineSepLen);
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   189
                        }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   190
                    }
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
                catch(IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
                    throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
     * Receive notification of the end of a document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
     * @throws org.xml.sax.SAXException Any SAX exception, possibly
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
     *            wrapping another exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
    public void endDocument() throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
    {
43744
5436902a27d7 8173290: 3% regression in SPECjvm2008-XML with b150
fyuan
parents: 42805
diff changeset
   211
        if (m_doIndent) {
58678
9cf78a70fa4f datagramsocketimpl-branch: update to default
chegar
parents: 47216
diff changeset
   212
            flushCharactersBuffer(false);
43744
5436902a27d7 8173290: 3% regression in SPECjvm2008-XML with b150
fyuan
parents: 42805
diff changeset
   213
        }
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
        flushPending();
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
        if (m_doIndent && !m_isprevtext)
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
            try
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
            outputLineSep();
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
            catch(IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
                throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
        flushWriter();
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
        if (m_tracer != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
            super.fireEndDoc();
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
     * Starts a whitespace preserving section. All characters printed
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
     * within a preserving section are printed without indentation and
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
     * without consolidating multiple spaces. This is equivalent to
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
     * the <tt>xml:space=&quot;preserve&quot;</tt> attribute. Only XML
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
     * and HTML serializers need to support this method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
     * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
     * The contents of the whitespace preserving section will be delivered
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
     * through the regular <tt>characters</tt> event.
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
    public void startPreserving() throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
     * Ends a whitespace preserving section.
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
     * @see #startPreserving
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
    public void endPreserving() throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
     * Receive notification of a processing instruction.
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
     * @param target The processing instruction target.
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
     * @param data The processing instruction data, or null if
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
     *        none was supplied.
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
     * @throws org.xml.sax.SAXException Any SAX exception, possibly
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
     *            wrapping another exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
    public void processingInstruction(String target, String data)
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
        throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
    {
42805
857b5e6eef37 8087303: LSSerializer pretty print does not work anymore
fyuan
parents: 25868
diff changeset
   274
        if (isInEntityRef())
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
43744
5436902a27d7 8173290: 3% regression in SPECjvm2008-XML with b150
fyuan
parents: 42805
diff changeset
   277
        if (m_doIndent) {
5436902a27d7 8173290: 3% regression in SPECjvm2008-XML with b150
fyuan
parents: 42805
diff changeset
   278
            m_childNodeNum++;
58678
9cf78a70fa4f datagramsocketimpl-branch: update to default
chegar
parents: 47216
diff changeset
   279
            flushCharactersBuffer(false);
43744
5436902a27d7 8173290: 3% regression in SPECjvm2008-XML with b150
fyuan
parents: 42805
diff changeset
   280
        }
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
        flushPending();
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
        if (target.equals(Result.PI_DISABLE_OUTPUT_ESCAPING))
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
            startNonEscaping();
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
        else if (target.equals(Result.PI_ENABLE_OUTPUT_ESCAPING))
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
            endNonEscaping();
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
            try
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
                if (m_elemContext.m_startTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
                    closeStartTag();
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
                    m_elemContext.m_startTagOpen = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
                else if (m_needToCallStartDocument)
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
                    startDocumentInternal();
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
                if (shouldIndent())
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
                    indent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
                final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
                writer.write("<?");
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
                writer.write(target);
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
                if (data.length() > 0
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
                    && !Character.isSpaceChar(data.charAt(0)))
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
                    writer.write(' ');
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
                int indexOfQLT = data.indexOf("?>");
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
                if (indexOfQLT >= 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
                    // See XSLT spec on error recovery of "?>" in PIs.
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
                    if (indexOfQLT > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
                    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
                        writer.write(data.substring(0, indexOfQLT));
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
                    writer.write("? >"); // add space between.
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
                    if ((indexOfQLT + 2) < data.length())
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
                    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
                        writer.write(data.substring(indexOfQLT + 2));
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
                    writer.write(data);
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
                writer.write('?');
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
                writer.write('>');
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   340
                /**
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   341
                 * Before Xalan 1497, a newline char was printed out if not inside of an
12902
0a840d92fa30 7151118: Regressions on 7u4 b11 comp. 7u4 b06 on specjvm2008.xml.transform subbenchmark
joehw
parents: 12458
diff changeset
   342
                 * element. The whitespace is not significant is the output is standalone
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   343
                */
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   344
                if (m_elemContext.m_currentElemDepth <= 0 && m_isStandalone)
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
                    writer.write(m_lineSep, 0, m_lineSepLen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
12902
0a840d92fa30 7151118: Regressions on 7u4 b11 comp. 7u4 b06 on specjvm2008.xml.transform subbenchmark
joehw
parents: 12458
diff changeset
   347
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   348
                /*
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   349
                 * Don't write out any indentation whitespace now,
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   350
                 * because there may be non-whitespace text after this.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   351
                 *
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   352
                 * Simply mark that at this point if we do decide
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   353
                 * to indent that we should
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   354
                 * add a newline on the end of the current line before
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   355
                 * the indentation at the start of the next line.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   356
                 */
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
                m_startNewLine = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
            catch(IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
                throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
        if (m_tracer != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
            super.fireEscapingEvent(target, data);
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
     * Receive notivication of a entityReference.
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
     * @param name The name of the entity.
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
    public void entityReference(String name) throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
        if (m_elemContext.m_startTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
            closeStartTag();
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
            m_elemContext.m_startTagOpen = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
            if (shouldIndent())
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
                indent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
            final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
            writer.write('&');
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
            writer.write(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
            writer.write(';');
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
        catch(IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
            throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
        if (m_tracer != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
            super.fireEntityReference(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
     * This method is used to add an attribute to the currently open element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
     * The caller has guaranted that this attribute is unique, which means that it
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
     * not been seen before and will not be seen again.
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
     * @param name the qualified name of the attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
     * @param value the value of the attribute which can contain only
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
     * ASCII printable characters characters in the range 32 to 127 inclusive.
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
     * @param flags the bit values of this integer give optimization information.
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
    public void addUniqueAttribute(String name, String value, int flags)
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
        if (m_elemContext.m_startTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
            try
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
                final String patchedName = patchName(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
                final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
                if ((flags & NO_BAD_CHARS) > 0 && m_xmlcharInfo.onlyQuotAmpLtGt)
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
                    // "flags" has indicated that the characters
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
                    // '>'  '<'   '&'  and '"' are not in the value and
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
                    // m_htmlcharInfo has recorded that there are no other
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
                    // entities in the range 32 to 127 so we write out the
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
                    // value directly
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
                    writer.write(' ');
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
                    writer.write(patchedName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
                    writer.write("=\"");
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
                    writer.write(value);
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
                    writer.write('"');
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
                    writer.write(' ');
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
                    writer.write(patchedName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
                    writer.write("=\"");
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
                    writeAttrString(writer, value, this.getEncoding());
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
                    writer.write('"');
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
            } catch (IOException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
                throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
     * Add an attribute to the current element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
     * @param uri the URI associated with the element name
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
     * @param localName local part of the attribute name
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
     * @param rawName   prefix:localName
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
     * @param type
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
     * @param value the value of the attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
     * @param xslAttribute true if this attribute is from an xsl:attribute,
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
     * false if declared within the elements opening tag.
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
     * @throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
    public void addAttribute(
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
        String uri,
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
        String localName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
        String rawName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
        String type,
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
        String value,
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
        boolean xslAttribute)
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
        if (m_elemContext.m_startTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
            boolean was_added = addAttributeAlways(uri, localName, rawName, type, value, xslAttribute);
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
            /*
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
             * We don't run this block of code if:
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
             * 1. The attribute value was only replaced (was_added is false).
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
             * 2. The attribute is from an xsl:attribute element (that is handled
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
             *    in the addAttributeAlways() call just above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
             * 3. The name starts with "xmlns", i.e. it is a namespace declaration.
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
             */
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
            if (was_added && !xslAttribute && !rawName.startsWith("xmlns"))
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
                String prefixUsed =
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
                    ensureAttributesNamespaceIsDeclared(
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
                        uri,
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
                        localName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
                        rawName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
                if (prefixUsed != null
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
                    && rawName != null
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
                    && !rawName.startsWith(prefixUsed))
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
                    // use a different raw name, with the prefix used in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
                    // generated namespace declaration
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
                    rawName = prefixUsed + ":" + localName;
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
            addAttributeAlways(uri, localName, rawName, type, value, xslAttribute);
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
            /*
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
             * The startTag is closed, yet we are adding an attribute?
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
             *
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
             * Section: 7.1.3 Creating Attributes Adding an attribute to an
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
             * element after a PI (for example) has been added to it is an
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
             * error. The attributes can be ignored. The spec doesn't explicitly
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
             * say this is disallowed, as it does for child elements, but it
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
             * makes sense to have the same treatment.
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
             *
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
             * We choose to ignore the attribute which is added too late.
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
             */
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
            // Generate a warning of the ignored attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
            // Create the warning message
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
            String msg = Utils.messages.createMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
                    MsgKey.ER_ILLEGAL_ATTRIBUTE_POSITION,new Object[]{ localName });
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
                // Prepare to issue the warning message
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
                Transformer tran = super.getTransformer();
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
                ErrorListener errHandler = tran.getErrorListener();
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
                // Issue the warning message
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
                if (null != errHandler && m_sourceLocator != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
                  errHandler.warning(new TransformerException(msg, m_sourceLocator));
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
                  System.out.println(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
            catch (Exception e){}
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
     * @see ExtendedContentHandler#endElement(String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
    public void endElement(String elemName) throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
        endElement(null, null, elemName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
     * This method is used to notify the serializer of a namespace mapping (or node)
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
     * that applies to the current element whose startElement() call has already been seen.
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
     * The official SAX startPrefixMapping(prefix,uri) is to define a mapping for a child
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
     * element that is soon to be seen with a startElement() call. The official SAX call
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
     * does not apply to the current element, hence the reason for this method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
    public void namespaceAfterStartElement(
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
        final String prefix,
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
        final String uri)
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
        // hack for XSLTC with finding URI for default namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
        if (m_elemContext.m_elementURI == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
            String prefix1 = getPrefixPart(m_elemContext.m_elementName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
            if (prefix1 == null && EMPTYSTRING.equals(prefix))
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
                // the elements URI is not known yet, and it
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
                // doesn't have a prefix, and we are currently
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
                // setting the uri for prefix "", so we have
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
                // the uri for the element... lets remember it
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
                m_elemContext.m_elementURI = uri;
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
        startPrefixMapping(prefix,uri,false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
        return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
     * From XSLTC
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
     * Declare a prefix to point to a namespace URI. Inform SAX handler
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
     * if this is a new prefix mapping.
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
    protected boolean pushNamespace(String prefix, String uri)
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
            if (m_prefixMap.pushNamespace(
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
                prefix, uri, m_elemContext.m_currentElemDepth))
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
                startPrefixMapping(prefix, uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
                return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
        catch (SAXException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
            // falls through
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   596
        return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   597
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   598
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   599
     * Try's to reset the super class and reset this class for
7f561c08de6b Initial load
duke
parents:
diff changeset
   600
     * re-use, so that you don't need to create a new serializer
7f561c08de6b Initial load
duke
parents:
diff changeset
   601
     * (mostly for performance reasons).
7f561c08de6b Initial load
duke
parents:
diff changeset
   602
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   603
     * @return true if the class was successfuly reset.
7f561c08de6b Initial load
duke
parents:
diff changeset
   604
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
    public boolean reset()
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
        boolean wasReset = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
        if (super.reset())
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
            resetToXMLStream();
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
            wasReset = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
        return wasReset;
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
     * Reset all of the fields owned by ToStream class
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   620
    private void resetToXMLStream()
7f561c08de6b Initial load
duke
parents:
diff changeset
   621
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   622
        this.m_cdataTagOpen = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   623
7f561c08de6b Initial load
duke
parents:
diff changeset
   624
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   625
7f561c08de6b Initial load
duke
parents:
diff changeset
   626
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   627
     * This method checks for the XML version of output document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   628
     * If XML version of output document is not specified, then output
7f561c08de6b Initial load
duke
parents:
diff changeset
   629
     * document is of version XML 1.0.
7f561c08de6b Initial load
duke
parents:
diff changeset
   630
     * If XML version of output doucment is specified, but it is not either
7f561c08de6b Initial load
duke
parents:
diff changeset
   631
     * XML 1.0 or XML 1.1, a warning message is generated, the XML Version of
7f561c08de6b Initial load
duke
parents:
diff changeset
   632
     * output document is set to XML 1.0 and processing continues.
7f561c08de6b Initial load
duke
parents:
diff changeset
   633
     * @return string (XML version)
7f561c08de6b Initial load
duke
parents:
diff changeset
   634
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   635
    private String getXMLVersion()
7f561c08de6b Initial load
duke
parents:
diff changeset
   636
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   637
        String xmlVersion = getVersion();
7f561c08de6b Initial load
duke
parents:
diff changeset
   638
        if(xmlVersion == null || xmlVersion.equals(XMLVERSION10))
7f561c08de6b Initial load
duke
parents:
diff changeset
   639
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   640
            xmlVersion = XMLVERSION10;
7f561c08de6b Initial load
duke
parents:
diff changeset
   641
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   642
        else if(xmlVersion.equals(XMLVERSION11))
7f561c08de6b Initial load
duke
parents:
diff changeset
   643
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   644
            xmlVersion = XMLVERSION11;
7f561c08de6b Initial load
duke
parents:
diff changeset
   645
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   646
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   647
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   648
            String msg = Utils.messages.createMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
   649
                               MsgKey.ER_XML_VERSION_NOT_SUPPORTED,new Object[]{ xmlVersion });
7f561c08de6b Initial load
duke
parents:
diff changeset
   650
            try
7f561c08de6b Initial load
duke
parents:
diff changeset
   651
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   652
                // Prepare to issue the warning message
7f561c08de6b Initial load
duke
parents:
diff changeset
   653
                Transformer tran = super.getTransformer();
7f561c08de6b Initial load
duke
parents:
diff changeset
   654
                ErrorListener errHandler = tran.getErrorListener();
7f561c08de6b Initial load
duke
parents:
diff changeset
   655
                // Issue the warning message
7f561c08de6b Initial load
duke
parents:
diff changeset
   656
                if (null != errHandler && m_sourceLocator != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   657
                    errHandler.warning(new TransformerException(msg, m_sourceLocator));
7f561c08de6b Initial load
duke
parents:
diff changeset
   658
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   659
                    System.out.println(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   660
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   661
            catch (Exception e){}
7f561c08de6b Initial load
duke
parents:
diff changeset
   662
            xmlVersion = XMLVERSION10;
7f561c08de6b Initial load
duke
parents:
diff changeset
   663
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   664
        return xmlVersion;
7f561c08de6b Initial load
duke
parents:
diff changeset
   665
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   666
}