jaxp/src/com/sun/org/apache/xml/internal/serializer/ToStream.java
author joehw
Thu, 12 Apr 2012 08:38:26 -0700
changeset 12457 c348e06f0e82
parent 6 jaxp/src/share/classes/com/sun/org/apache/xml/internal/serializer/ToStream.java@7f561c08de6b
child 12458 d601e4bba306
permissions -rw-r--r--
7160496: Rename JDK8 JAXP source directory Summary: moving src/share/classes to src Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 2001-2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
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
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * $Id: ToStream.java,v 1.4 2005/11/10 06:43:26 suresh_emailid Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
package com.sun.org.apache.xml.internal.serializer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.io.OutputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.io.UnsupportedEncodingException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.io.Writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import java.util.Properties;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import java.util.StringTokenizer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import java.util.Vector;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import javax.xml.transform.ErrorListener;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import javax.xml.transform.OutputKeys;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import javax.xml.transform.Transformer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import javax.xml.transform.TransformerException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import com.sun.org.apache.xml.internal.serializer.utils.MsgKey;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import com.sun.org.apache.xml.internal.serializer.utils.Utils;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import com.sun.org.apache.xml.internal.serializer.utils.WrappedRuntimeException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import org.w3c.dom.Node;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
import org.xml.sax.Attributes;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
import org.xml.sax.ContentHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
import org.xml.sax.SAXException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
//import com.sun.media.sound.IESecurity;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * This abstract class is a base class for other stream
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * serializers (xml, html, text ...) that write output to a stream.
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 * @xsl.usage internal
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
abstract public class ToStream extends SerializerBase
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
    private static final String COMMENT_BEGIN = "<!--";
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
    private static final String COMMENT_END = "-->";
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
    /** Stack to keep track of disabling output escaping. */
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    protected BoolStack m_disableOutputEscapingStates = new BoolStack();
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
     * The encoding information associated with this serializer.
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
     * Although initially there is no encoding,
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
     * there is a dummy EncodingInfo object that will say
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
     * that every character is in the encoding. This is useful
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
     * for a serializer that is in temporary output state and has
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
     * no associated encoding. A serializer in final output state
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
     * will have an encoding, and will worry about whether
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
     * single chars or surrogate pairs of high/low chars form
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
     * characters in the output encoding.
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
    EncodingInfo m_encodingInfo = new EncodingInfo(null,null);
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
     * Method reference to the sun.io.CharToByteConverter#canConvert method
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
     * for this encoding.  Invalid if m_charToByteConverter is null.
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
    java.lang.reflect.Method m_canConvertMeth;
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
     * Boolean that tells if we already tried to get the converter.
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
    boolean m_triedToGetConverter = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
     * Opaque reference to the sun.io.CharToByteConverter for this
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
     * encoding.
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    Object m_charToByteConverter = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
     * Stack to keep track of whether or not we need to
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
     * preserve whitespace.
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
     * Used to push/pop values used for the field m_ispreserve, but
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
     * m_ispreserve is only relevant if m_doIndent is true.
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
     * If m_doIndent is false this field has no impact.
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
    protected BoolStack m_preserves = new BoolStack();
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
     * State flag to tell if preservation of whitespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
     * is important.
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
     * Used only in shouldIndent() but only if m_doIndent is true.
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
     * If m_doIndent is false this flag has no impact.
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
    protected boolean m_ispreserve = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
     * State flag that tells if the previous node processed
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
     * was text, so we can tell if we should preserve whitespace.
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
     * Used in endDocument() and shouldIndent() but
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
     * only if m_doIndent is true.
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
     * If m_doIndent is false this flag has no impact.
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
    protected boolean m_isprevtext = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
     * The maximum character size before we have to resort
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
     * to escaping.
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
    protected int m_maxCharacter = Encodings.getLastPrintable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
     * The system line separator for writing out line breaks.
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
     * The default value is from the system property,
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
     * but this value can be set through the xsl:output
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
     * extension attribute xalan:line-separator.
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
    protected char[] m_lineSep =
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        System.getProperty("line.separator").toCharArray();
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
     * True if the the system line separator is to be used.
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
    protected boolean m_lineSepUse = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
     * The length of the line seperator, since the write is done
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
     * one character at a time.
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    protected int m_lineSepLen = m_lineSep.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
     * Map that tells which characters should have special treatment, and it
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
     *  provides character to entity name lookup.
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
    protected CharInfo m_charInfo;
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
    /** True if we control the buffer, and we should flush the output on endDocument. */
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
    boolean m_shouldFlush = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
     * Add space before '/>' for XHTML.
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
    protected boolean m_spaceBeforeClose = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
     * Flag to signal that a newline should be added.
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
     * Used only in indent() which is called only if m_doIndent is true.
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
     * If m_doIndent is false this flag has no impact.
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
    boolean m_startNewLine;
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
     * Tells if we're in an internal document type subset.
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
    protected boolean m_inDoctype = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
       * Flag to quickly tell if the encoding is UTF8.
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    boolean m_isUTF8 = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
    /** The xsl:output properties. */
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
    protected Properties m_format;
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
     * remembers if we are in between the startCDATA() and endCDATA() callbacks
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
    protected boolean m_cdataStartCalled = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
     * If this flag is true DTD entity references are not left as-is,
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
     * which is exiting older behavior.
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
    private boolean m_expandDTDEntities = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
     * Default constructor
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
    public ToStream()
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
     * This helper method to writes out "]]>" when closing a CDATA section.
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
    protected void closeCDATA() throws org.xml.sax.SAXException
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
            m_writer.write(CDATA_DELIMITER_CLOSE);
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
            // write out a CDATA section closing "]]>"
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
            m_cdataTagOpen = false; // Remember that we have done so.
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
        catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
            throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
     * Serializes the DOM node. Throws an exception only if an I/O
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
     * exception occured while serializing.
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
     * @param node Node to serialize.
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
     * @throws IOException An I/O exception occured while serializing
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
    public void serialize(Node node) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
            TreeWalker walker =
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
                new TreeWalker(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
            walker.traverse(node);
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
        catch (org.xml.sax.SAXException se)
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
            throw new WrappedRuntimeException(se);
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
     * Return true if the character is the high member of a surrogate pair.
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
     * NEEDSDOC @param c
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
     * NEEDSDOC ($objectName$) @return
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
    static final boolean isUTF16Surrogate(char c)
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
        return (c & 0xFC00) == 0xD800;
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
     * Taken from XSLTC
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
    private boolean m_escaping = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
     * Flush the formatter's result stream.
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
    protected final void flushWriter() throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
        final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
        if (null != writer)
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
            try
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
                if (writer instanceof WriterToUTF8Buffered)
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
                    if (m_shouldFlush)
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
                         ((WriterToUTF8Buffered) writer).flush();
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
                    else
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
                         ((WriterToUTF8Buffered) writer).flushBuffer();
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
                if (writer instanceof WriterToASCI)
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
                    if (m_shouldFlush)
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
                        writer.flush();
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
                    // Flush always.
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
                    // Not a great thing if the writer was created
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
                    // by this class, but don't have a choice.
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
                    writer.flush();
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
            catch (IOException ioe)
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
                throw new org.xml.sax.SAXException(ioe);
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
     * Get the output stream where the events will be serialized to.
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
     * @return reference to the result stream, or null of only a writer was
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
     * set.
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
    public OutputStream getOutputStream()
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
        if (m_writer instanceof WriterToUTF8Buffered)
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
            return ((WriterToUTF8Buffered) m_writer).getOutputStream();
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
        if (m_writer instanceof WriterToASCI)
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
            return ((WriterToASCI) m_writer).getOutputStream();
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
    // Implement DeclHandler
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
     *   Report an element type declaration.
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
     *   <p>The content model will consist of the string "EMPTY", the
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
     *   string "ANY", or a parenthesised group, optionally followed
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
     *   by an occurrence indicator.  The model will be normalized so
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
     *   that all whitespace is removed,and will include the enclosing
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
     *   parentheses.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
     *   @param name The element type name.
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
     *   @param model The content model as a normalized string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
     *   @exception SAXException The application may raise an exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
    public void elementDecl(String name, String model) throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
        // Do not inline external DTD
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
        if (m_inExternalDTD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
            final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
            DTDprolog();
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
            writer.write("<!ELEMENT ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
            writer.write(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
            writer.write(' ');
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
            writer.write(model);
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
            writer.write('>');
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
            writer.write(m_lineSep, 0, m_lineSepLen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
        catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
            throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
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
     * Report an internal entity declaration.
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
     * <p>Only the effective (first) declaration for each entity
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
     * will be reported.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
     * @param name The name of the entity.  If it is a parameter
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
     *        entity, the name will begin with '%'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
     * @param value The replacement text of the entity.
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
     * @exception SAXException The application may raise an exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
     * @see #externalEntityDecl
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
     * @see org.xml.sax.DTDHandler#unparsedEntityDecl
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
    public void internalEntityDecl(String name, String value)
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
        // Do not inline external DTD
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
        if (m_inExternalDTD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
            DTDprolog();
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
            outputEntityDecl(name, value);
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
        catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
            throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
     * Output the doc type declaration.
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
     * @param name non-null reference to document type name.
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
     * NEEDSDOC @param value
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
    void outputEntityDecl(String name, String value) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
        final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
        writer.write("<!ENTITY ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
        writer.write(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
        writer.write(" \"");
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
        writer.write(value);
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
        writer.write("\">");
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
        writer.write(m_lineSep, 0, m_lineSepLen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
     * Output a system-dependent line break.
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
    protected final void outputLineSep() throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
        m_writer.write(m_lineSep, 0, m_lineSepLen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
     * Specifies an output format for this serializer. It the
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
     * serializer has already been associated with an output format,
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
     * it will switch to the new format. This method should not be
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
     * called while the serializer is in the process of serializing
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
     * a document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
     * @param format The output format to use
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
    public void setOutputFormat(Properties format)
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
        boolean shouldFlush = m_shouldFlush;
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
        init(m_writer, format, false, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
        m_shouldFlush = shouldFlush;
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
     * Initialize the serializer with the specified writer and output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
     * Must be called before calling any of the serialize methods.
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
     * This method can be called multiple times and the xsl:output properties
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
     * passed in the 'format' parameter are accumulated across calls.
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
     * @param writer The writer to use
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
     * @param format The output format
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
     * @param shouldFlush True if the writer should be flushed at EndDocument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
    private synchronized void init(
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
        Writer writer,
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
        Properties format,
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
        boolean defaultProperties,
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
        boolean shouldFlush)
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
        m_shouldFlush = shouldFlush;
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
        // if we are tracing events we need to trace what
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
        // characters are written to the output writer.
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
        if (m_tracer != null
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
         && !(writer instanceof SerializerTraceWriter)  )
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
            m_writer = new SerializerTraceWriter(writer, m_tracer);
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
            m_writer = writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
        m_format = format;
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
        //        m_cdataSectionNames =
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
        //            OutputProperties.getQNameProperties(
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
        //                OutputKeys.CDATA_SECTION_ELEMENTS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
        //                format);
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
        setCdataSectionElements(OutputKeys.CDATA_SECTION_ELEMENTS, format);
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
        setIndentAmount(
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
            OutputPropertyUtils.getIntProperty(
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
                OutputPropertiesFactory.S_KEY_INDENT_AMOUNT,
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
                format));
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
        setIndent(
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
            OutputPropertyUtils.getBooleanProperty(OutputKeys.INDENT, format));
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
            String sep =
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
                    format.getProperty(OutputPropertiesFactory.S_KEY_LINE_SEPARATOR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
            if (sep != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
                m_lineSep = sep.toCharArray();
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
                m_lineSepLen = sep.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
        boolean shouldNotWriteXMLHeader =
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
            OutputPropertyUtils.getBooleanProperty(
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
                OutputKeys.OMIT_XML_DECLARATION,
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
                format);
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
        setOmitXMLDeclaration(shouldNotWriteXMLHeader);
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
        setDoctypeSystem(format.getProperty(OutputKeys.DOCTYPE_SYSTEM));
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
        String doctypePublic = format.getProperty(OutputKeys.DOCTYPE_PUBLIC);
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
        setDoctypePublic(doctypePublic);
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
        // if standalone was explicitly specified
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
        if (format.get(OutputKeys.STANDALONE) != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
            String val = format.getProperty(OutputKeys.STANDALONE);
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
            if (defaultProperties)
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
                setStandaloneInternal(val);
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
                setStandalone(val);
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
        setMediaType(format.getProperty(OutputKeys.MEDIA_TYPE));
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
        if (null != doctypePublic)
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
            if (doctypePublic.startsWith("-//W3C//DTD XHTML"))
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
                m_spaceBeforeClose = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
        /*
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
         * This code is added for XML 1.1 Version output.
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
        String version = getVersion();
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
        if (null == version)
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
            version = format.getProperty(OutputKeys.VERSION);
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
            setVersion(version);
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
        // initCharsMap();
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
        String encoding = getEncoding();
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
        if (null == encoding)
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
            encoding =
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
                Encodings.getMimeEncoding(
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
                    format.getProperty(OutputKeys.ENCODING));
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
            setEncoding(encoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
        m_isUTF8 = encoding.equals(Encodings.DEFAULT_MIME_ENCODING);
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
        // Access this only from the Hashtable level... we don't want to
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
        // get default properties.
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
        String entitiesFileName =
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
            (String) format.get(OutputPropertiesFactory.S_KEY_ENTITIES);
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
        if (null != entitiesFileName)
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
            String method =
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
                (String) format.get(OutputKeys.METHOD);
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
            m_charInfo = CharInfo.getCharInfo(entitiesFileName, method);
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
     * Initialize the serializer with the specified writer and output format.
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
     * Must be called before calling any of the serialize methods.
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
     * @param writer The writer to use
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
     * @param format The output format
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
    private synchronized void init(Writer writer, Properties format)
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
        init(writer, format, false, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
     * Initialize the serializer with the specified output stream and output
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
     * format. Must be called before calling any of the serialize methods.
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
     * @param output The output stream to use
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
     * @param format The output format
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
     * @param defaultProperties true if the properties are the default
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
     * properties
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
     * @throws UnsupportedEncodingException The encoding specified   in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
     * output format is not supported
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
    protected synchronized void init(
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
        OutputStream output,
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
        Properties format,
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
        boolean defaultProperties)
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
        throws UnsupportedEncodingException
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
        String encoding = getEncoding();
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
        if (encoding == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
            // if not already set then get it from the properties
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
            encoding =
7f561c08de6b Initial load
duke
parents:
diff changeset
   596
                Encodings.getMimeEncoding(
7f561c08de6b Initial load
duke
parents:
diff changeset
   597
                    format.getProperty(OutputKeys.ENCODING));
7f561c08de6b Initial load
duke
parents:
diff changeset
   598
            setEncoding(encoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
   599
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   600
7f561c08de6b Initial load
duke
parents:
diff changeset
   601
        if (encoding.equalsIgnoreCase("UTF-8"))
7f561c08de6b Initial load
duke
parents:
diff changeset
   602
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   603
            m_isUTF8 = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   604
            //            if (output instanceof java.io.BufferedOutputStream)
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
            //            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
            //                init(new WriterToUTF8(output), format, defaultProperties, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
            //            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
            //            else if (output instanceof java.io.FileOutputStream)
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
            //            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
            //                init(new WriterToUTF8Buffered(output), format, defaultProperties, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
            //            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
            //            else
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
            //            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
            //                // Not sure what to do in this case.  I'm going to be conservative
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
            //                // and not buffer.
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
            //                init(new WriterToUTF8(output), format, defaultProperties, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
            //            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
7f561c08de6b Initial load
duke
parents:
diff changeset
   620
                init(
7f561c08de6b Initial load
duke
parents:
diff changeset
   621
                    new WriterToUTF8Buffered(output),
7f561c08de6b Initial load
duke
parents:
diff changeset
   622
                    format,
7f561c08de6b Initial load
duke
parents:
diff changeset
   623
                    defaultProperties,
7f561c08de6b Initial load
duke
parents:
diff changeset
   624
                    true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   625
7f561c08de6b Initial load
duke
parents:
diff changeset
   626
7f561c08de6b Initial load
duke
parents:
diff changeset
   627
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   628
        else if (
7f561c08de6b Initial load
duke
parents:
diff changeset
   629
            encoding.equals("WINDOWS-1250")
7f561c08de6b Initial load
duke
parents:
diff changeset
   630
                || encoding.equals("US-ASCII")
7f561c08de6b Initial load
duke
parents:
diff changeset
   631
                || encoding.equals("ASCII"))
7f561c08de6b Initial load
duke
parents:
diff changeset
   632
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   633
            init(new WriterToASCI(output), format, defaultProperties, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   634
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   635
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   636
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   637
            Writer osw;
7f561c08de6b Initial load
duke
parents:
diff changeset
   638
7f561c08de6b Initial load
duke
parents:
diff changeset
   639
            try
7f561c08de6b Initial load
duke
parents:
diff changeset
   640
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   641
                osw = Encodings.getWriter(output, encoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
   642
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   643
            catch (UnsupportedEncodingException uee)
7f561c08de6b Initial load
duke
parents:
diff changeset
   644
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   645
                System.out.println(
7f561c08de6b Initial load
duke
parents:
diff changeset
   646
                    "Warning: encoding \""
7f561c08de6b Initial load
duke
parents:
diff changeset
   647
                        + encoding
7f561c08de6b Initial load
duke
parents:
diff changeset
   648
                        + "\" not supported"
7f561c08de6b Initial load
duke
parents:
diff changeset
   649
                        + ", using "
7f561c08de6b Initial load
duke
parents:
diff changeset
   650
                        + Encodings.DEFAULT_MIME_ENCODING);
7f561c08de6b Initial load
duke
parents:
diff changeset
   651
7f561c08de6b Initial load
duke
parents:
diff changeset
   652
                encoding = Encodings.DEFAULT_MIME_ENCODING;
7f561c08de6b Initial load
duke
parents:
diff changeset
   653
                setEncoding(encoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
   654
                osw = Encodings.getWriter(output, encoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
   655
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   656
7f561c08de6b Initial load
duke
parents:
diff changeset
   657
            init(osw, format, defaultProperties, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   658
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   659
7f561c08de6b Initial load
duke
parents:
diff changeset
   660
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   661
7f561c08de6b Initial load
duke
parents:
diff changeset
   662
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   663
     * Returns the output format for this serializer.
7f561c08de6b Initial load
duke
parents:
diff changeset
   664
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   665
     * @return The output format in use
7f561c08de6b Initial load
duke
parents:
diff changeset
   666
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   667
    public Properties getOutputFormat()
7f561c08de6b Initial load
duke
parents:
diff changeset
   668
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   669
        return m_format;
7f561c08de6b Initial load
duke
parents:
diff changeset
   670
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   671
7f561c08de6b Initial load
duke
parents:
diff changeset
   672
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   673
     * Specifies a writer to which the document should be serialized.
7f561c08de6b Initial load
duke
parents:
diff changeset
   674
     * This method should not be called while the serializer is in
7f561c08de6b Initial load
duke
parents:
diff changeset
   675
     * the process of serializing a document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   676
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   677
     * @param writer The output writer stream
7f561c08de6b Initial load
duke
parents:
diff changeset
   678
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   679
    public void setWriter(Writer writer)
7f561c08de6b Initial load
duke
parents:
diff changeset
   680
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   681
        // if we are tracing events we need to trace what
7f561c08de6b Initial load
duke
parents:
diff changeset
   682
        // characters are written to the output writer.
7f561c08de6b Initial load
duke
parents:
diff changeset
   683
        if (m_tracer != null
7f561c08de6b Initial load
duke
parents:
diff changeset
   684
         && !(writer instanceof SerializerTraceWriter)  )
7f561c08de6b Initial load
duke
parents:
diff changeset
   685
            m_writer = new SerializerTraceWriter(writer, m_tracer);
7f561c08de6b Initial load
duke
parents:
diff changeset
   686
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   687
            m_writer = writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   688
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   689
7f561c08de6b Initial load
duke
parents:
diff changeset
   690
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   691
     * Set if the operating systems end-of-line line separator should
7f561c08de6b Initial load
duke
parents:
diff changeset
   692
     * be used when serializing.  If set false NL character
7f561c08de6b Initial load
duke
parents:
diff changeset
   693
     * (decimal 10) is left alone, otherwise the new-line will be replaced on
7f561c08de6b Initial load
duke
parents:
diff changeset
   694
     * output with the systems line separator. For example on UNIX this is
7f561c08de6b Initial load
duke
parents:
diff changeset
   695
     * NL, while on Windows it is two characters, CR NL, where CR is the
7f561c08de6b Initial load
duke
parents:
diff changeset
   696
     * carriage-return (decimal 13).
7f561c08de6b Initial load
duke
parents:
diff changeset
   697
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   698
     * @param use_sytem_line_break True if an input NL is replaced with the
7f561c08de6b Initial load
duke
parents:
diff changeset
   699
     * operating systems end-of-line separator.
7f561c08de6b Initial load
duke
parents:
diff changeset
   700
     * @return The previously set value of the serializer.
7f561c08de6b Initial load
duke
parents:
diff changeset
   701
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   702
    public boolean setLineSepUse(boolean use_sytem_line_break)
7f561c08de6b Initial load
duke
parents:
diff changeset
   703
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   704
        boolean oldValue = m_lineSepUse;
7f561c08de6b Initial load
duke
parents:
diff changeset
   705
        m_lineSepUse = use_sytem_line_break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   706
        return oldValue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   707
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   708
7f561c08de6b Initial load
duke
parents:
diff changeset
   709
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   710
     * Specifies an output stream to which the document should be
7f561c08de6b Initial load
duke
parents:
diff changeset
   711
     * serialized. This method should not be called while the
7f561c08de6b Initial load
duke
parents:
diff changeset
   712
     * serializer is in the process of serializing a document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   713
     * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   714
     * The encoding specified in the output properties is used, or
7f561c08de6b Initial load
duke
parents:
diff changeset
   715
     * if no encoding was specified, the default for the selected
7f561c08de6b Initial load
duke
parents:
diff changeset
   716
     * output method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   717
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   718
     * @param output The output stream
7f561c08de6b Initial load
duke
parents:
diff changeset
   719
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   720
    public void setOutputStream(OutputStream output)
7f561c08de6b Initial load
duke
parents:
diff changeset
   721
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   722
7f561c08de6b Initial load
duke
parents:
diff changeset
   723
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
   724
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   725
            Properties format;
7f561c08de6b Initial load
duke
parents:
diff changeset
   726
            if (null == m_format)
7f561c08de6b Initial load
duke
parents:
diff changeset
   727
                format =
7f561c08de6b Initial load
duke
parents:
diff changeset
   728
                    OutputPropertiesFactory.getDefaultMethodProperties(
7f561c08de6b Initial load
duke
parents:
diff changeset
   729
                        Method.XML);
7f561c08de6b Initial load
duke
parents:
diff changeset
   730
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
   731
                format = m_format;
7f561c08de6b Initial load
duke
parents:
diff changeset
   732
            init(output, format, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   733
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   734
        catch (UnsupportedEncodingException uee)
7f561c08de6b Initial load
duke
parents:
diff changeset
   735
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   736
7f561c08de6b Initial load
duke
parents:
diff changeset
   737
            // Should have been warned in init, I guess...
7f561c08de6b Initial load
duke
parents:
diff changeset
   738
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   739
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   740
7f561c08de6b Initial load
duke
parents:
diff changeset
   741
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   742
     * @see SerializationHandler#setEscaping(boolean)
7f561c08de6b Initial load
duke
parents:
diff changeset
   743
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   744
    public boolean setEscaping(boolean escape)
7f561c08de6b Initial load
duke
parents:
diff changeset
   745
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   746
        final boolean temp = m_escaping;
7f561c08de6b Initial load
duke
parents:
diff changeset
   747
        m_escaping = escape;
7f561c08de6b Initial load
duke
parents:
diff changeset
   748
        return temp;
7f561c08de6b Initial load
duke
parents:
diff changeset
   749
7f561c08de6b Initial load
duke
parents:
diff changeset
   750
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   751
7f561c08de6b Initial load
duke
parents:
diff changeset
   752
7f561c08de6b Initial load
duke
parents:
diff changeset
   753
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   754
     * Might print a newline character and the indentation amount
7f561c08de6b Initial load
duke
parents:
diff changeset
   755
     * of the given depth.
7f561c08de6b Initial load
duke
parents:
diff changeset
   756
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   757
     * @param depth the indentation depth (element nesting depth)
7f561c08de6b Initial load
duke
parents:
diff changeset
   758
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   759
     * @throws org.xml.sax.SAXException if an error occurs during writing.
7f561c08de6b Initial load
duke
parents:
diff changeset
   760
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   761
    protected void indent(int depth) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   762
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   763
7f561c08de6b Initial load
duke
parents:
diff changeset
   764
        if (m_startNewLine)
7f561c08de6b Initial load
duke
parents:
diff changeset
   765
            outputLineSep();
7f561c08de6b Initial load
duke
parents:
diff changeset
   766
        /* For m_indentAmount > 0 this extra test might be slower
7f561c08de6b Initial load
duke
parents:
diff changeset
   767
         * but Xalan's default value is 0, so this extra test
7f561c08de6b Initial load
duke
parents:
diff changeset
   768
         * will run faster in that situation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   769
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
   770
        if (m_indentAmount > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   771
            printSpace(depth * m_indentAmount);
7f561c08de6b Initial load
duke
parents:
diff changeset
   772
7f561c08de6b Initial load
duke
parents:
diff changeset
   773
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   774
7f561c08de6b Initial load
duke
parents:
diff changeset
   775
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   776
     * Indent at the current element nesting depth.
7f561c08de6b Initial load
duke
parents:
diff changeset
   777
     * @throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   778
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   779
    protected void indent() throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   780
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   781
        indent(m_elemContext.m_currentElemDepth);
7f561c08de6b Initial load
duke
parents:
diff changeset
   782
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   783
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   784
     * Prints <var>n</var> spaces.
7f561c08de6b Initial load
duke
parents:
diff changeset
   785
     * @param n         Number of spaces to print.
7f561c08de6b Initial load
duke
parents:
diff changeset
   786
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   787
     * @throws org.xml.sax.SAXException if an error occurs when writing.
7f561c08de6b Initial load
duke
parents:
diff changeset
   788
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   789
    private void printSpace(int n) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   790
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   791
        final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   792
        for (int i = 0; i < n; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   793
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   794
            writer.write(' ');
7f561c08de6b Initial load
duke
parents:
diff changeset
   795
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   796
7f561c08de6b Initial load
duke
parents:
diff changeset
   797
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   798
7f561c08de6b Initial load
duke
parents:
diff changeset
   799
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   800
     * Report an attribute type declaration.
7f561c08de6b Initial load
duke
parents:
diff changeset
   801
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   802
     * <p>Only the effective (first) declaration for an attribute will
7f561c08de6b Initial load
duke
parents:
diff changeset
   803
     * be reported.  The type will be one of the strings "CDATA",
7f561c08de6b Initial load
duke
parents:
diff changeset
   804
     * "ID", "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY",
7f561c08de6b Initial load
duke
parents:
diff changeset
   805
     * "ENTITIES", or "NOTATION", or a parenthesized token group with
7f561c08de6b Initial load
duke
parents:
diff changeset
   806
     * the separator "|" and all whitespace removed.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   807
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   808
     * @param eName The name of the associated element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   809
     * @param aName The name of the attribute.
7f561c08de6b Initial load
duke
parents:
diff changeset
   810
     * @param type A string representing the attribute type.
7f561c08de6b Initial load
duke
parents:
diff changeset
   811
     * @param valueDefault A string representing the attribute default
7f561c08de6b Initial load
duke
parents:
diff changeset
   812
     *        ("#IMPLIED", "#REQUIRED", or "#FIXED") or null if
7f561c08de6b Initial load
duke
parents:
diff changeset
   813
     *        none of these applies.
7f561c08de6b Initial load
duke
parents:
diff changeset
   814
     * @param value A string representing the attribute's default value,
7f561c08de6b Initial load
duke
parents:
diff changeset
   815
     *        or null if there is none.
7f561c08de6b Initial load
duke
parents:
diff changeset
   816
     * @exception SAXException The application may raise an exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
   817
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   818
    public void attributeDecl(
7f561c08de6b Initial load
duke
parents:
diff changeset
   819
        String eName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   820
        String aName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   821
        String type,
7f561c08de6b Initial load
duke
parents:
diff changeset
   822
        String valueDefault,
7f561c08de6b Initial load
duke
parents:
diff changeset
   823
        String value)
7f561c08de6b Initial load
duke
parents:
diff changeset
   824
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   825
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   826
        // Do not inline external DTD
7f561c08de6b Initial load
duke
parents:
diff changeset
   827
        if (m_inExternalDTD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   828
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   829
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
   830
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   831
            final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   832
            DTDprolog();
7f561c08de6b Initial load
duke
parents:
diff changeset
   833
7f561c08de6b Initial load
duke
parents:
diff changeset
   834
            writer.write("<!ATTLIST ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   835
            writer.write(eName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   836
            writer.write(' ');
7f561c08de6b Initial load
duke
parents:
diff changeset
   837
7f561c08de6b Initial load
duke
parents:
diff changeset
   838
            writer.write(aName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   839
            writer.write(' ');
7f561c08de6b Initial load
duke
parents:
diff changeset
   840
            writer.write(type);
7f561c08de6b Initial load
duke
parents:
diff changeset
   841
            if (valueDefault != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   842
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   843
                writer.write(' ');
7f561c08de6b Initial load
duke
parents:
diff changeset
   844
                writer.write(valueDefault);
7f561c08de6b Initial load
duke
parents:
diff changeset
   845
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   846
7f561c08de6b Initial load
duke
parents:
diff changeset
   847
            //writer.write(" ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   848
            //writer.write(value);
7f561c08de6b Initial load
duke
parents:
diff changeset
   849
            writer.write('>');
7f561c08de6b Initial load
duke
parents:
diff changeset
   850
            writer.write(m_lineSep, 0, m_lineSepLen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   851
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   852
        catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
   853
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   854
            throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
   855
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   856
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   857
7f561c08de6b Initial load
duke
parents:
diff changeset
   858
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   859
     * Get the character stream where the events will be serialized to.
7f561c08de6b Initial load
duke
parents:
diff changeset
   860
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   861
     * @return Reference to the result Writer, or null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   862
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   863
    public Writer getWriter()
7f561c08de6b Initial load
duke
parents:
diff changeset
   864
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   865
        return m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   866
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   867
7f561c08de6b Initial load
duke
parents:
diff changeset
   868
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   869
     * Report a parsed external entity declaration.
7f561c08de6b Initial load
duke
parents:
diff changeset
   870
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   871
     * <p>Only the effective (first) declaration for each entity
7f561c08de6b Initial load
duke
parents:
diff changeset
   872
     * will be reported.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   873
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   874
     * @param name The name of the entity.  If it is a parameter
7f561c08de6b Initial load
duke
parents:
diff changeset
   875
     *        entity, the name will begin with '%'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   876
     * @param publicId The declared public identifier of the entity, or
7f561c08de6b Initial load
duke
parents:
diff changeset
   877
     *        null if none was declared.
7f561c08de6b Initial load
duke
parents:
diff changeset
   878
     * @param systemId The declared system identifier of the entity.
7f561c08de6b Initial load
duke
parents:
diff changeset
   879
     * @exception SAXException The application may raise an exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
   880
     * @see #internalEntityDecl
7f561c08de6b Initial load
duke
parents:
diff changeset
   881
     * @see org.xml.sax.DTDHandler#unparsedEntityDecl
7f561c08de6b Initial load
duke
parents:
diff changeset
   882
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   883
    public void externalEntityDecl(
7f561c08de6b Initial load
duke
parents:
diff changeset
   884
        String name,
7f561c08de6b Initial load
duke
parents:
diff changeset
   885
        String publicId,
7f561c08de6b Initial load
duke
parents:
diff changeset
   886
        String systemId)
7f561c08de6b Initial load
duke
parents:
diff changeset
   887
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   888
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   889
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   890
            DTDprolog();
7f561c08de6b Initial load
duke
parents:
diff changeset
   891
7f561c08de6b Initial load
duke
parents:
diff changeset
   892
            m_writer.write("<!ENTITY ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   893
            m_writer.write(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   894
            if (publicId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   895
                m_writer.write(" PUBLIC \"");
7f561c08de6b Initial load
duke
parents:
diff changeset
   896
                m_writer.write(publicId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   897
7f561c08de6b Initial load
duke
parents:
diff changeset
   898
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   899
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   900
                m_writer.write(" SYSTEM \"");
7f561c08de6b Initial load
duke
parents:
diff changeset
   901
                m_writer.write(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   902
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   903
            m_writer.write("\" >");
7f561c08de6b Initial load
duke
parents:
diff changeset
   904
            m_writer.write(m_lineSep, 0, m_lineSepLen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   905
        } catch (IOException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   906
            // TODO Auto-generated catch block
7f561c08de6b Initial load
duke
parents:
diff changeset
   907
            e.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   908
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   909
7f561c08de6b Initial load
duke
parents:
diff changeset
   910
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   911
7f561c08de6b Initial load
duke
parents:
diff changeset
   912
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   913
     * Tell if this character can be written without escaping.
7f561c08de6b Initial load
duke
parents:
diff changeset
   914
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   915
    protected boolean escapingNotNeeded(char ch)
7f561c08de6b Initial load
duke
parents:
diff changeset
   916
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   917
        final boolean ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   918
        if (ch < 127)
7f561c08de6b Initial load
duke
parents:
diff changeset
   919
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   920
            // This is the old/fast code here, but is this
7f561c08de6b Initial load
duke
parents:
diff changeset
   921
            // correct for all encodings?
7f561c08de6b Initial load
duke
parents:
diff changeset
   922
            if (ch >= 0x20 || (0x0A == ch || 0x0D == ch || 0x09 == ch))
7f561c08de6b Initial load
duke
parents:
diff changeset
   923
                ret= true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   924
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
   925
                ret = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   926
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   927
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   928
            ret = m_encodingInfo.isInEncoding(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
   929
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   930
        return ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   931
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   932
7f561c08de6b Initial load
duke
parents:
diff changeset
   933
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   934
     * Once a surrogate has been detected, write out the pair of
7f561c08de6b Initial load
duke
parents:
diff changeset
   935
     * characters if it is in the encoding, or if there is no
7f561c08de6b Initial load
duke
parents:
diff changeset
   936
     * encoding, otherwise write out an entity reference
7f561c08de6b Initial load
duke
parents:
diff changeset
   937
     * of the value of the unicode code point of the character
7f561c08de6b Initial load
duke
parents:
diff changeset
   938
     * represented by the high/low surrogate pair.
7f561c08de6b Initial load
duke
parents:
diff changeset
   939
     * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   940
     * An exception is thrown if there is no low surrogate in the pair,
7f561c08de6b Initial load
duke
parents:
diff changeset
   941
     * because the array ends unexpectely, or if the low char is there
7f561c08de6b Initial load
duke
parents:
diff changeset
   942
     * but its value is such that it is not a low surrogate.
7f561c08de6b Initial load
duke
parents:
diff changeset
   943
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   944
     * @param c the first (high) part of the surrogate, which
7f561c08de6b Initial load
duke
parents:
diff changeset
   945
     * must be confirmed before calling this method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   946
     * @param ch Character array.
7f561c08de6b Initial load
duke
parents:
diff changeset
   947
     * @param i position Where the surrogate was detected.
7f561c08de6b Initial load
duke
parents:
diff changeset
   948
     * @param end The end index of the significant characters.
7f561c08de6b Initial load
duke
parents:
diff changeset
   949
     * @return 0 if the pair of characters was written out as-is,
7f561c08de6b Initial load
duke
parents:
diff changeset
   950
     * the unicode code point of the character represented by
7f561c08de6b Initial load
duke
parents:
diff changeset
   951
     * the surrogate pair if an entity reference with that value
7f561c08de6b Initial load
duke
parents:
diff changeset
   952
     * was written out.
7f561c08de6b Initial load
duke
parents:
diff changeset
   953
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   954
     * @throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   955
     * @throws org.xml.sax.SAXException if invalid UTF-16 surrogate detected.
7f561c08de6b Initial load
duke
parents:
diff changeset
   956
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   957
    protected int writeUTF16Surrogate(char c, char ch[], int i, int end)
7f561c08de6b Initial load
duke
parents:
diff changeset
   958
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   959
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   960
        int codePoint = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   961
        if (i + 1 >= end)
7f561c08de6b Initial load
duke
parents:
diff changeset
   962
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   963
            throw new IOException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   964
                Utils.messages.createMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
   965
                    MsgKey.ER_INVALID_UTF16_SURROGATE,
7f561c08de6b Initial load
duke
parents:
diff changeset
   966
                    new Object[] { Integer.toHexString((int) c)}));
7f561c08de6b Initial load
duke
parents:
diff changeset
   967
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   968
7f561c08de6b Initial load
duke
parents:
diff changeset
   969
        final char high = c;
7f561c08de6b Initial load
duke
parents:
diff changeset
   970
        final char low = ch[i+1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   971
        if (!Encodings.isLowUTF16Surrogate(low)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   972
            throw new IOException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   973
                Utils.messages.createMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
   974
                    MsgKey.ER_INVALID_UTF16_SURROGATE,
7f561c08de6b Initial load
duke
parents:
diff changeset
   975
                    new Object[] {
7f561c08de6b Initial load
duke
parents:
diff changeset
   976
                        Integer.toHexString((int) c)
7f561c08de6b Initial load
duke
parents:
diff changeset
   977
                            + " "
7f561c08de6b Initial load
duke
parents:
diff changeset
   978
                            + Integer.toHexString(low)}));
7f561c08de6b Initial load
duke
parents:
diff changeset
   979
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   980
7f561c08de6b Initial load
duke
parents:
diff changeset
   981
        final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   982
7f561c08de6b Initial load
duke
parents:
diff changeset
   983
        // If we make it to here we have a valid high, low surrogate pair
7f561c08de6b Initial load
duke
parents:
diff changeset
   984
        if (m_encodingInfo.isInEncoding(c,low)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   985
            // If the character formed by the surrogate pair
7f561c08de6b Initial load
duke
parents:
diff changeset
   986
            // is in the encoding, so just write it out
7f561c08de6b Initial load
duke
parents:
diff changeset
   987
            writer.write(ch,i,2);
7f561c08de6b Initial load
duke
parents:
diff changeset
   988
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   989
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   990
            // Don't know what to do with this char, it is
7f561c08de6b Initial load
duke
parents:
diff changeset
   991
            // not in the encoding and not a high char in
7f561c08de6b Initial load
duke
parents:
diff changeset
   992
            // a surrogate pair, so write out as an entity ref
7f561c08de6b Initial load
duke
parents:
diff changeset
   993
            final String encoding = getEncoding();
7f561c08de6b Initial load
duke
parents:
diff changeset
   994
            if (encoding != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   995
                /* The output encoding is known,
7f561c08de6b Initial load
duke
parents:
diff changeset
   996
                 * so somthing is wrong.
7f561c08de6b Initial load
duke
parents:
diff changeset
   997
                  */
7f561c08de6b Initial load
duke
parents:
diff changeset
   998
                codePoint = Encodings.toCodePoint(high, low);
7f561c08de6b Initial load
duke
parents:
diff changeset
   999
                // not in the encoding, so write out a character reference
7f561c08de6b Initial load
duke
parents:
diff changeset
  1000
                writer.write('&');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1001
                writer.write('#');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1002
                writer.write(Integer.toString(codePoint));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1003
                writer.write(';');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1004
            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1005
                /* The output encoding is not known,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1006
                 * so just write it out as-is.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1007
                 */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1008
                writer.write(ch, i, 2);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1009
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1010
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1011
        // non-zero only if character reference was written out.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1012
        return codePoint;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1013
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1014
7f561c08de6b Initial load
duke
parents:
diff changeset
  1015
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1016
     * Handle one of the default entities, return false if it
7f561c08de6b Initial load
duke
parents:
diff changeset
  1017
     * is not a default entity.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1018
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1019
     * @param ch character to be escaped.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1020
     * @param i index into character array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1021
     * @param chars non-null reference to character array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1022
     * @param len length of chars.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1023
     * @param fromTextNode true if the characters being processed
7f561c08de6b Initial load
duke
parents:
diff changeset
  1024
     * are from a text node, false if they are from an attribute value
7f561c08de6b Initial load
duke
parents:
diff changeset
  1025
     * @param escLF true if the linefeed should be escaped.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1026
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1027
     * @return i+1 if the character was written, else i.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1028
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1029
     * @throws java.io.IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1030
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1031
    protected int accumDefaultEntity(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1032
        java.io.Writer writer,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1033
        char ch,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1034
        int i,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1035
        char[] chars,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1036
        int len,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1037
        boolean fromTextNode,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1038
        boolean escLF)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1039
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1040
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1041
7f561c08de6b Initial load
duke
parents:
diff changeset
  1042
        if (!escLF && CharInfo.S_LINEFEED == ch)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1043
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1044
            writer.write(m_lineSep, 0, m_lineSepLen);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1045
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1046
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1047
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1048
            // if this is text node character and a special one of those,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1049
            // or if this is a character from attribute value and a special one of those
7f561c08de6b Initial load
duke
parents:
diff changeset
  1050
            if ((fromTextNode && m_charInfo.isSpecialTextChar(ch)) || (!fromTextNode && m_charInfo.isSpecialAttrChar(ch)))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1051
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1052
                String outputStringForChar = m_charInfo.getOutputStringForChar(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1053
7f561c08de6b Initial load
duke
parents:
diff changeset
  1054
                if (null != outputStringForChar)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1055
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1056
                    writer.write(outputStringForChar);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1057
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1058
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1059
                    return i;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1060
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1061
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1062
                return i;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1063
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1064
7f561c08de6b Initial load
duke
parents:
diff changeset
  1065
        return i + 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1066
7f561c08de6b Initial load
duke
parents:
diff changeset
  1067
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1068
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1069
     * Normalize the characters, but don't escape.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1070
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1071
     * @param ch The characters from the XML document.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1072
     * @param start The start position in the array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1073
     * @param length The number of characters to read from the array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1074
     * @param isCData true if a CDATA block should be built around the characters.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1075
     * @param useSystemLineSeparator true if the operating systems
7f561c08de6b Initial load
duke
parents:
diff changeset
  1076
     * end-of-line separator should be output rather than a new-line character.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1077
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1078
     * @throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1079
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1080
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1081
    void writeNormalizedChars(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1082
        char ch[],
7f561c08de6b Initial load
duke
parents:
diff changeset
  1083
        int start,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1084
        int length,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1085
        boolean isCData,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1086
        boolean useSystemLineSeparator)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1087
        throws IOException, org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1088
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1089
        final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1090
        int end = start + length;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1091
7f561c08de6b Initial load
duke
parents:
diff changeset
  1092
        for (int i = start; i < end; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1093
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1094
            char c = ch[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1095
7f561c08de6b Initial load
duke
parents:
diff changeset
  1096
            if (CharInfo.S_LINEFEED == c && useSystemLineSeparator)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1097
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1098
                writer.write(m_lineSep, 0, m_lineSepLen);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1099
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1100
            else if (isCData && (!escapingNotNeeded(c)))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1101
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1102
                //                if (i != 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1103
                if (m_cdataTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1104
                    closeCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1105
7f561c08de6b Initial load
duke
parents:
diff changeset
  1106
                // This needs to go into a function...
7f561c08de6b Initial load
duke
parents:
diff changeset
  1107
                if (Encodings.isHighUTF16Surrogate(c))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1108
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1109
                    writeUTF16Surrogate(c, ch, i, end);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1110
                    i++ ; // process two input characters
7f561c08de6b Initial load
duke
parents:
diff changeset
  1111
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1112
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1113
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1114
                    writer.write("&#");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1115
7f561c08de6b Initial load
duke
parents:
diff changeset
  1116
                    String intStr = Integer.toString((int) c);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1117
7f561c08de6b Initial load
duke
parents:
diff changeset
  1118
                    writer.write(intStr);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1119
                    writer.write(';');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1120
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1121
7f561c08de6b Initial load
duke
parents:
diff changeset
  1122
                //                if ((i != 0) && (i < (end - 1)))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1123
                //                if (!m_cdataTagOpen && (i < (end - 1)))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1124
                //                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1125
                //                    writer.write(CDATA_DELIMITER_OPEN);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1126
                //                    m_cdataTagOpen = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1127
                //                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1128
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1129
            else if (
7f561c08de6b Initial load
duke
parents:
diff changeset
  1130
                isCData
7f561c08de6b Initial load
duke
parents:
diff changeset
  1131
                    && ((i < (end - 2))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1132
                        && (']' == c)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1133
                        && (']' == ch[i + 1])
7f561c08de6b Initial load
duke
parents:
diff changeset
  1134
                        && ('>' == ch[i + 2])))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1135
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1136
                writer.write(CDATA_CONTINUE);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1137
7f561c08de6b Initial load
duke
parents:
diff changeset
  1138
                i += 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1139
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1140
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1141
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1142
                if (escapingNotNeeded(c))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1143
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1144
                    if (isCData && !m_cdataTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1145
                    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1146
                        writer.write(CDATA_DELIMITER_OPEN);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1147
                        m_cdataTagOpen = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1148
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1149
                    writer.write(c);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1150
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1151
7f561c08de6b Initial load
duke
parents:
diff changeset
  1152
                // This needs to go into a function...
7f561c08de6b Initial load
duke
parents:
diff changeset
  1153
                else if (Encodings.isHighUTF16Surrogate(c))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1154
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1155
                    if (m_cdataTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1156
                        closeCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1157
                    writeUTF16Surrogate(c, ch, i, end);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1158
                    i++; // process two input characters
7f561c08de6b Initial load
duke
parents:
diff changeset
  1159
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1160
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1161
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1162
                    if (m_cdataTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1163
                        closeCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1164
                    writer.write("&#");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1165
7f561c08de6b Initial load
duke
parents:
diff changeset
  1166
                    String intStr = Integer.toString((int) c);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1167
7f561c08de6b Initial load
duke
parents:
diff changeset
  1168
                    writer.write(intStr);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1169
                    writer.write(';');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1170
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1171
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1172
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1173
7f561c08de6b Initial load
duke
parents:
diff changeset
  1174
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1175
7f561c08de6b Initial load
duke
parents:
diff changeset
  1176
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1177
     * Ends an un-escaping section.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1178
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1179
     * @see #startNonEscaping
7f561c08de6b Initial load
duke
parents:
diff changeset
  1180
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1181
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1182
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1183
    public void endNonEscaping() throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1184
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1185
        m_disableOutputEscapingStates.pop();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1186
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1187
7f561c08de6b Initial load
duke
parents:
diff changeset
  1188
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1189
     * Starts an un-escaping section. All characters printed within an un-
7f561c08de6b Initial load
duke
parents:
diff changeset
  1190
     * escaping section are printed as is, without escaping special characters
7f561c08de6b Initial load
duke
parents:
diff changeset
  1191
     * into entity references. Only XML and HTML serializers need to support
7f561c08de6b Initial load
duke
parents:
diff changeset
  1192
     * this method.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1193
     * <p> The contents of the un-escaping section will be delivered through the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1194
     * regular <tt>characters</tt> event.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1195
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1196
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1197
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1198
    public void startNonEscaping() throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1199
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1200
        m_disableOutputEscapingStates.push(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1201
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1202
7f561c08de6b Initial load
duke
parents:
diff changeset
  1203
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1204
     * Receive notification of cdata.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1205
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1206
     * <p>The Parser will call this method to report each chunk of
7f561c08de6b Initial load
duke
parents:
diff changeset
  1207
     * character data.  SAX parsers may return all contiguous character
7f561c08de6b Initial load
duke
parents:
diff changeset
  1208
     * data in a single chunk, or they may split it into several
7f561c08de6b Initial load
duke
parents:
diff changeset
  1209
     * chunks; however, all of the characters in any single event
7f561c08de6b Initial load
duke
parents:
diff changeset
  1210
     * must come from the same external entity, so that the Locator
7f561c08de6b Initial load
duke
parents:
diff changeset
  1211
     * provides useful information.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1212
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1213
     * <p>The application must not attempt to read from the array
7f561c08de6b Initial load
duke
parents:
diff changeset
  1214
     * outside of the specified range.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1215
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1216
     * <p>Note that some parsers will report whitespace using the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1217
     * ignorableWhitespace() method rather than this one (validating
7f561c08de6b Initial load
duke
parents:
diff changeset
  1218
     * parsers must do so).</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1219
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1220
     * @param ch The characters from the XML document.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1221
     * @param start The start position in the array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1222
     * @param length The number of characters to read from the array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1223
     * @throws org.xml.sax.SAXException Any SAX exception, possibly
7f561c08de6b Initial load
duke
parents:
diff changeset
  1224
     *            wrapping another exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1225
     * @see #ignorableWhitespace
7f561c08de6b Initial load
duke
parents:
diff changeset
  1226
     * @see org.xml.sax.Locator
7f561c08de6b Initial load
duke
parents:
diff changeset
  1227
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1228
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1229
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1230
    protected void cdata(char ch[], int start, final int length)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1231
        throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1232
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1233
7f561c08de6b Initial load
duke
parents:
diff changeset
  1234
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
  1235
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1236
            final int old_start = start;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1237
            if (m_elemContext.m_startTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1238
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1239
                closeStartTag();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1240
                m_elemContext.m_startTagOpen = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1241
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1242
            m_ispreserve = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1243
7f561c08de6b Initial load
duke
parents:
diff changeset
  1244
            if (shouldIndent())
7f561c08de6b Initial load
duke
parents:
diff changeset
  1245
                indent();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1246
7f561c08de6b Initial load
duke
parents:
diff changeset
  1247
            boolean writeCDataBrackets =
7f561c08de6b Initial load
duke
parents:
diff changeset
  1248
                (((length >= 1) && escapingNotNeeded(ch[start])));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1249
7f561c08de6b Initial load
duke
parents:
diff changeset
  1250
            /* Write out the CDATA opening delimiter only if
7f561c08de6b Initial load
duke
parents:
diff changeset
  1251
             * we are supposed to, and if we are not already in
7f561c08de6b Initial load
duke
parents:
diff changeset
  1252
             * the middle of a CDATA section
7f561c08de6b Initial load
duke
parents:
diff changeset
  1253
             */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1254
            if (writeCDataBrackets && !m_cdataTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1255
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1256
                m_writer.write(CDATA_DELIMITER_OPEN);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1257
                m_cdataTagOpen = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1258
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1259
7f561c08de6b Initial load
duke
parents:
diff changeset
  1260
            // writer.write(ch, start, length);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1261
            if (isEscapingDisabled())
7f561c08de6b Initial load
duke
parents:
diff changeset
  1262
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1263
                charactersRaw(ch, start, length);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1264
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1265
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1266
                writeNormalizedChars(ch, start, length, true, m_lineSepUse);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1267
7f561c08de6b Initial load
duke
parents:
diff changeset
  1268
            /* used to always write out CDATA closing delimiter here,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1269
             * but now we delay, so that we can merge CDATA sections on output.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1270
             * need to write closing delimiter later
7f561c08de6b Initial load
duke
parents:
diff changeset
  1271
             */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1272
            if (writeCDataBrackets)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1273
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1274
                /* if the CDATA section ends with ] don't leave it open
7f561c08de6b Initial load
duke
parents:
diff changeset
  1275
                 * as there is a chance that an adjacent CDATA sections
7f561c08de6b Initial load
duke
parents:
diff changeset
  1276
                 * starts with ]>.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1277
                 * We don't want to merge ]] with > , or ] with ]>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1278
                 */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1279
                if (ch[start + length - 1] == ']')
7f561c08de6b Initial load
duke
parents:
diff changeset
  1280
                    closeCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1281
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1282
7f561c08de6b Initial load
duke
parents:
diff changeset
  1283
            // time to fire off CDATA event
7f561c08de6b Initial load
duke
parents:
diff changeset
  1284
            if (m_tracer != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1285
                super.fireCDATAEvent(ch, old_start, length);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1286
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1287
        catch (IOException ioe)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1288
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1289
            throw new org.xml.sax.SAXException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1290
                Utils.messages.createMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1291
                    MsgKey.ER_OIERROR,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1292
                    null),
7f561c08de6b Initial load
duke
parents:
diff changeset
  1293
                ioe);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1294
            //"IO error", ioe);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1295
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1296
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1297
7f561c08de6b Initial load
duke
parents:
diff changeset
  1298
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1299
     * Tell if the character escaping should be disabled for the current state.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1300
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1301
     * @return true if the character escaping should be disabled.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1302
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1303
    private boolean isEscapingDisabled()
7f561c08de6b Initial load
duke
parents:
diff changeset
  1304
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1305
        return m_disableOutputEscapingStates.peekOrFalse();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1306
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1307
7f561c08de6b Initial load
duke
parents:
diff changeset
  1308
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1309
     * If available, when the disable-output-escaping attribute is used,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1310
     * output raw text without escaping.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1311
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1312
     * @param ch The characters from the XML document.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1313
     * @param start The start position in the array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1314
     * @param length The number of characters to read from the array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1315
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1316
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1317
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1318
    protected void charactersRaw(char ch[], int start, int length)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1319
        throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1320
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1321
7f561c08de6b Initial load
duke
parents:
diff changeset
  1322
        if (m_inEntityRef)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1323
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1324
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
  1325
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1326
            if (m_elemContext.m_startTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1327
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1328
                closeStartTag();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1329
                m_elemContext.m_startTagOpen = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1330
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1331
7f561c08de6b Initial load
duke
parents:
diff changeset
  1332
            m_ispreserve = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1333
7f561c08de6b Initial load
duke
parents:
diff changeset
  1334
            m_writer.write(ch, start, length);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1335
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1336
        catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1337
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1338
            throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1339
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1340
7f561c08de6b Initial load
duke
parents:
diff changeset
  1341
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1342
7f561c08de6b Initial load
duke
parents:
diff changeset
  1343
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1344
     * Receive notification of character data.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1345
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1346
     * <p>The Parser will call this method to report each chunk of
7f561c08de6b Initial load
duke
parents:
diff changeset
  1347
     * character data.  SAX parsers may return all contiguous character
7f561c08de6b Initial load
duke
parents:
diff changeset
  1348
     * data in a single chunk, or they may split it into several
7f561c08de6b Initial load
duke
parents:
diff changeset
  1349
     * chunks; however, all of the characters in any single event
7f561c08de6b Initial load
duke
parents:
diff changeset
  1350
     * must come from the same external entity, so that the Locator
7f561c08de6b Initial load
duke
parents:
diff changeset
  1351
     * provides useful information.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1352
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1353
     * <p>The application must not attempt to read from the array
7f561c08de6b Initial load
duke
parents:
diff changeset
  1354
     * outside of the specified range.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1355
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1356
     * <p>Note that some parsers will report whitespace using the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1357
     * ignorableWhitespace() method rather than this one (validating
7f561c08de6b Initial load
duke
parents:
diff changeset
  1358
     * parsers must do so).</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1359
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1360
     * @param chars The characters from the XML document.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1361
     * @param start The start position in the array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1362
     * @param length The number of characters to read from the array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1363
     * @throws org.xml.sax.SAXException Any SAX exception, possibly
7f561c08de6b Initial load
duke
parents:
diff changeset
  1364
     *            wrapping another exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1365
     * @see #ignorableWhitespace
7f561c08de6b Initial load
duke
parents:
diff changeset
  1366
     * @see org.xml.sax.Locator
7f561c08de6b Initial load
duke
parents:
diff changeset
  1367
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1368
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1369
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1370
    public void characters(final char chars[], final int start, final int length)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1371
        throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1372
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1373
        // It does not make sense to continue with rest of the method if the number of
7f561c08de6b Initial load
duke
parents:
diff changeset
  1374
        // characters to read from array is 0.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1375
        // Section 7.6.1 of XSLT 1.0 (http://www.w3.org/TR/xslt#value-of) suggest no text node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1376
        // is created if string is empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1377
        if (length == 0 || (m_inEntityRef && !m_expandDTDEntities))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1378
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1379
        if (m_elemContext.m_startTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1380
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1381
            closeStartTag();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1382
            m_elemContext.m_startTagOpen = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1383
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1384
        else if (m_needToCallStartDocument)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1385
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1386
            startDocumentInternal();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1387
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1388
7f561c08de6b Initial load
duke
parents:
diff changeset
  1389
        if (m_cdataStartCalled || m_elemContext.m_isCdataSection)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1390
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1391
            /* either due to startCDATA() being called or due to
7f561c08de6b Initial load
duke
parents:
diff changeset
  1392
             * cdata-section-elements atribute, we need this as cdata
7f561c08de6b Initial load
duke
parents:
diff changeset
  1393
             */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1394
            cdata(chars, start, length);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1395
7f561c08de6b Initial load
duke
parents:
diff changeset
  1396
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1397
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1398
7f561c08de6b Initial load
duke
parents:
diff changeset
  1399
        if (m_cdataTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1400
            closeCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1401
        // the check with _escaping is a bit of a hack for XLSTC
7f561c08de6b Initial load
duke
parents:
diff changeset
  1402
7f561c08de6b Initial load
duke
parents:
diff changeset
  1403
        if (m_disableOutputEscapingStates.peekOrFalse() || (!m_escaping))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1404
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1405
            charactersRaw(chars, start, length);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1406
7f561c08de6b Initial load
duke
parents:
diff changeset
  1407
            // time to fire off characters generation event
7f561c08de6b Initial load
duke
parents:
diff changeset
  1408
            if (m_tracer != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1409
                super.fireCharEvent(chars, start, length);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1410
7f561c08de6b Initial load
duke
parents:
diff changeset
  1411
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1412
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1413
7f561c08de6b Initial load
duke
parents:
diff changeset
  1414
        if (m_elemContext.m_startTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1415
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1416
            closeStartTag();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1417
            m_elemContext.m_startTagOpen = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1418
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1419
7f561c08de6b Initial load
duke
parents:
diff changeset
  1420
7f561c08de6b Initial load
duke
parents:
diff changeset
  1421
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
  1422
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1423
            int i;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1424
            char ch1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1425
            int startClean;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1426
7f561c08de6b Initial load
duke
parents:
diff changeset
  1427
            // skip any leading whitspace
7f561c08de6b Initial load
duke
parents:
diff changeset
  1428
            // don't go off the end and use a hand inlined version
7f561c08de6b Initial load
duke
parents:
diff changeset
  1429
            // of isWhitespace(ch)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1430
            final int end = start + length;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1431
            int lastDirty = start - 1; // last character that needed processing
7f561c08de6b Initial load
duke
parents:
diff changeset
  1432
            for (i = start;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1433
                ((i < end)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1434
                    && ((ch1 = chars[i]) == 0x20
7f561c08de6b Initial load
duke
parents:
diff changeset
  1435
                        || (ch1 == 0xA && m_lineSepUse)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1436
                        || ch1 == 0xD
7f561c08de6b Initial load
duke
parents:
diff changeset
  1437
                        || ch1 == 0x09));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1438
                i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1439
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1440
                /*
7f561c08de6b Initial load
duke
parents:
diff changeset
  1441
                 * We are processing leading whitespace, but are doing the same
7f561c08de6b Initial load
duke
parents:
diff changeset
  1442
                 * processing for dirty characters here as for non-whitespace.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1443
                 *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1444
                 */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1445
                if (!m_charInfo.isTextASCIIClean(ch1))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1446
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1447
                    lastDirty = processDirty(chars,end, i,ch1, lastDirty, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1448
                    i = lastDirty;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1449
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1450
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1451
            /* If there is some non-whitespace, mark that we may need
7f561c08de6b Initial load
duke
parents:
diff changeset
  1452
             * to preserve this. This is only important if we have indentation on.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1453
             */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1454
            if (i < end)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1455
                m_ispreserve = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1456
7f561c08de6b Initial load
duke
parents:
diff changeset
  1457
7f561c08de6b Initial load
duke
parents:
diff changeset
  1458
//            int lengthClean;    // number of clean characters in a row
7f561c08de6b Initial load
duke
parents:
diff changeset
  1459
//            final boolean[] isAsciiClean = m_charInfo.getASCIIClean();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1460
7f561c08de6b Initial load
duke
parents:
diff changeset
  1461
            final boolean isXML10 = XMLVERSION10.equals(getVersion());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1462
            // we've skipped the leading whitespace, now deal with the rest
7f561c08de6b Initial load
duke
parents:
diff changeset
  1463
            for (; i < end; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1464
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1465
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1466
                    // A tight loop to skip over common clean chars
7f561c08de6b Initial load
duke
parents:
diff changeset
  1467
                    // This tight loop makes it easier for the JIT
7f561c08de6b Initial load
duke
parents:
diff changeset
  1468
                    // to optimize.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1469
                    char ch2;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1470
                    while (i<end
7f561c08de6b Initial load
duke
parents:
diff changeset
  1471
                            && ((ch2 = chars[i])<127)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1472
                            && m_charInfo.isTextASCIIClean(ch2))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1473
                            i++;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1474
                    if (i == end)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1475
                        break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1476
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1477
7f561c08de6b Initial load
duke
parents:
diff changeset
  1478
                final char ch = chars[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1479
                /*  The check for isCharacterInC0orC1Ranger and
7f561c08de6b Initial load
duke
parents:
diff changeset
  1480
                 *  isNELorLSEPCharacter has been added
7f561c08de6b Initial load
duke
parents:
diff changeset
  1481
                 *  to support Control Characters in XML 1.1
7f561c08de6b Initial load
duke
parents:
diff changeset
  1482
                 */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1483
                if (!isCharacterInC0orC1Range(ch) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1484
                    (isXML10 || !isNELorLSEPCharacter(ch)) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1485
                    (escapingNotNeeded(ch) && (!m_charInfo.isSpecialTextChar(ch)))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1486
                        || ('"' == ch))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1487
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1488
                    ; // a character needing no special processing
7f561c08de6b Initial load
duke
parents:
diff changeset
  1489
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1490
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1491
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1492
                    lastDirty = processDirty(chars,end, i, ch, lastDirty, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1493
                    i = lastDirty;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1494
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1495
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1496
7f561c08de6b Initial load
duke
parents:
diff changeset
  1497
            // we've reached the end. Any clean characters at the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1498
            // end of the array than need to be written out?
7f561c08de6b Initial load
duke
parents:
diff changeset
  1499
            startClean = lastDirty + 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1500
            if (i > startClean)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1501
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1502
                int lengthClean = i - startClean;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1503
                m_writer.write(chars, startClean, lengthClean);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1504
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1505
7f561c08de6b Initial load
duke
parents:
diff changeset
  1506
            // For indentation purposes, mark that we've just writen text out
7f561c08de6b Initial load
duke
parents:
diff changeset
  1507
            m_isprevtext = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1508
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1509
        catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1510
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1511
            throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1512
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1513
7f561c08de6b Initial load
duke
parents:
diff changeset
  1514
        // time to fire off characters generation event
7f561c08de6b Initial load
duke
parents:
diff changeset
  1515
        if (m_tracer != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1516
            super.fireCharEvent(chars, start, length);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1517
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1518
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1519
     * This method checks if a given character is between C0 or C1 range
7f561c08de6b Initial load
duke
parents:
diff changeset
  1520
     * of Control characters.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1521
     * This method is added to support Control Characters for XML 1.1
7f561c08de6b Initial load
duke
parents:
diff changeset
  1522
     * If a given character is TAB (0x09), LF (0x0A) or CR (0x0D), this method
7f561c08de6b Initial load
duke
parents:
diff changeset
  1523
     * return false. Since they are whitespace characters, no special processing is needed.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1524
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1525
     * @param ch
7f561c08de6b Initial load
duke
parents:
diff changeset
  1526
     * @return boolean
7f561c08de6b Initial load
duke
parents:
diff changeset
  1527
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1528
    private static boolean isCharacterInC0orC1Range(char ch)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1529
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1530
        if(ch == 0x09 || ch == 0x0A || ch == 0x0D)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1531
                return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1532
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1533
                return (ch >= 0x7F && ch <= 0x9F)|| (ch >= 0x01 && ch <= 0x1F);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1534
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1535
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1536
     * This method checks if a given character either NEL (0x85) or LSEP (0x2028)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1537
     * These are new end of line charcters added in XML 1.1.  These characters must be
7f561c08de6b Initial load
duke
parents:
diff changeset
  1538
     * written as Numeric Character References (NCR) in XML 1.1 output document.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1539
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1540
     * @param ch
7f561c08de6b Initial load
duke
parents:
diff changeset
  1541
     * @return boolean
7f561c08de6b Initial load
duke
parents:
diff changeset
  1542
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1543
    private static boolean isNELorLSEPCharacter(char ch)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1544
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1545
        return (ch == 0x85 || ch == 0x2028);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1546
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1547
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1548
     * Process a dirty character and any preeceding clean characters
7f561c08de6b Initial load
duke
parents:
diff changeset
  1549
     * that were not yet processed.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1550
     * @param chars array of characters being processed
7f561c08de6b Initial load
duke
parents:
diff changeset
  1551
     * @param end one (1) beyond the last character
7f561c08de6b Initial load
duke
parents:
diff changeset
  1552
     * in chars to be processed
7f561c08de6b Initial load
duke
parents:
diff changeset
  1553
     * @param i the index of the dirty character
7f561c08de6b Initial load
duke
parents:
diff changeset
  1554
     * @param ch the character in chars[i]
7f561c08de6b Initial load
duke
parents:
diff changeset
  1555
     * @param lastDirty the last dirty character previous to i
7f561c08de6b Initial load
duke
parents:
diff changeset
  1556
     * @param fromTextNode true if the characters being processed are
7f561c08de6b Initial load
duke
parents:
diff changeset
  1557
     * from a text node, false if they are from an attribute value.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1558
     * @return the index of the last character processed
7f561c08de6b Initial load
duke
parents:
diff changeset
  1559
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1560
    private int processDirty(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1561
        char[] chars,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1562
        int end,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1563
        int i,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1564
        char ch,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1565
        int lastDirty,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1566
        boolean fromTextNode) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1567
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1568
        int startClean = lastDirty + 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1569
        // if we have some clean characters accumulated
7f561c08de6b Initial load
duke
parents:
diff changeset
  1570
        // process them before the dirty one.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1571
        if (i > startClean)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1572
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1573
            int lengthClean = i - startClean;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1574
            m_writer.write(chars, startClean, lengthClean);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1575
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1576
7f561c08de6b Initial load
duke
parents:
diff changeset
  1577
        // process the "dirty" character
7f561c08de6b Initial load
duke
parents:
diff changeset
  1578
        if (CharInfo.S_LINEFEED == ch && fromTextNode)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1579
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1580
            m_writer.write(m_lineSep, 0, m_lineSepLen);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1581
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1582
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1583
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1584
            startClean =
7f561c08de6b Initial load
duke
parents:
diff changeset
  1585
                accumDefaultEscape(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1586
                    m_writer,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1587
                    (char)ch,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1588
                    i,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1589
                    chars,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1590
                    end,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1591
                    fromTextNode,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1592
                    false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1593
            i = startClean - 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1594
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1595
        // Return the index of the last character that we just processed
7f561c08de6b Initial load
duke
parents:
diff changeset
  1596
        // which is a dirty character.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1597
        return i;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1598
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1599
7f561c08de6b Initial load
duke
parents:
diff changeset
  1600
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1601
     * Receive notification of character data.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1602
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1603
     * @param s The string of characters to process.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1604
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1605
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1606
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1607
    public void characters(String s) throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1608
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1609
        if (m_inEntityRef && !m_expandDTDEntities)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1610
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1611
        final int length = s.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1612
        if (length > m_charsBuff.length)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1613
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1614
            m_charsBuff = new char[length * 2 + 1];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1615
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1616
        s.getChars(0, length, m_charsBuff, 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1617
        characters(m_charsBuff, 0, length);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1618
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1619
7f561c08de6b Initial load
duke
parents:
diff changeset
  1620
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1621
     * Escape and writer.write a character.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1622
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1623
     * @param ch character to be escaped.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1624
     * @param i index into character array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1625
     * @param chars non-null reference to character array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1626
     * @param len length of chars.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1627
     * @param fromTextNode true if the characters being processed are
7f561c08de6b Initial load
duke
parents:
diff changeset
  1628
     * from a text node, false if the characters being processed are from
7f561c08de6b Initial load
duke
parents:
diff changeset
  1629
     * an attribute value.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1630
     * @param escLF true if the linefeed should be escaped.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1631
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1632
     * @return i+1 if a character was written, i+2 if two characters
7f561c08de6b Initial load
duke
parents:
diff changeset
  1633
     * were written out, else return i.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1634
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1635
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1636
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1637
    protected int accumDefaultEscape(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1638
        Writer writer,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1639
        char ch,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1640
        int i,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1641
        char[] chars,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1642
        int len,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1643
        boolean fromTextNode,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1644
        boolean escLF)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1645
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1646
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1647
7f561c08de6b Initial load
duke
parents:
diff changeset
  1648
        int pos = accumDefaultEntity(writer, ch, i, chars, len, fromTextNode, escLF);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1649
7f561c08de6b Initial load
duke
parents:
diff changeset
  1650
        if (i == pos)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1651
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1652
            if (Encodings.isHighUTF16Surrogate(ch))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1653
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1654
7f561c08de6b Initial load
duke
parents:
diff changeset
  1655
                // Should be the UTF-16 low surrogate of the hig/low pair.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1656
                char next;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1657
                // Unicode code point formed from the high/low pair.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1658
                int codePoint = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1659
7f561c08de6b Initial load
duke
parents:
diff changeset
  1660
                if (i + 1 >= len)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1661
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1662
                    throw new IOException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1663
                        Utils.messages.createMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1664
                            MsgKey.ER_INVALID_UTF16_SURROGATE,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1665
                            new Object[] { Integer.toHexString(ch)}));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1666
                    //"Invalid UTF-16 surrogate detected: "
7f561c08de6b Initial load
duke
parents:
diff changeset
  1667
7f561c08de6b Initial load
duke
parents:
diff changeset
  1668
                    //+Integer.toHexString(ch)+ " ?");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1669
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1670
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1671
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1672
                    next = chars[++i];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1673
7f561c08de6b Initial load
duke
parents:
diff changeset
  1674
                    if (!(Encodings.isLowUTF16Surrogate(next)))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1675
                        throw new IOException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1676
                            Utils.messages.createMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1677
                                MsgKey
7f561c08de6b Initial load
duke
parents:
diff changeset
  1678
                                    .ER_INVALID_UTF16_SURROGATE,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1679
                                new Object[] {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1680
                                    Integer.toHexString(ch)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1681
                                        + " "
7f561c08de6b Initial load
duke
parents:
diff changeset
  1682
                                        + Integer.toHexString(next)}));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1683
                    //"Invalid UTF-16 surrogate detected: "
7f561c08de6b Initial load
duke
parents:
diff changeset
  1684
7f561c08de6b Initial load
duke
parents:
diff changeset
  1685
                    //+Integer.toHexString(ch)+" "+Integer.toHexString(next));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1686
                    codePoint = Encodings.toCodePoint(ch,next);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1687
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1688
7f561c08de6b Initial load
duke
parents:
diff changeset
  1689
                writer.write("&#");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1690
                writer.write(Integer.toString(codePoint));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1691
                writer.write(';');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1692
                pos += 2; // count the two characters that went into writing out this entity
7f561c08de6b Initial load
duke
parents:
diff changeset
  1693
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1694
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1695
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1696
                /*  This if check is added to support control characters in XML 1.1.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1697
                 *  If a character is a Control Character within C0 and C1 range, it is desirable
7f561c08de6b Initial load
duke
parents:
diff changeset
  1698
                 *  to write it out as Numeric Character Reference(NCR) regardless of XML Version
7f561c08de6b Initial load
duke
parents:
diff changeset
  1699
                 *  being used for output document.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1700
                 */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1701
                if (isCharacterInC0orC1Range(ch) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1702
                        (XMLVERSION11.equals(getVersion()) && isNELorLSEPCharacter(ch)))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1703
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1704
                    writer.write("&#");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1705
                    writer.write(Integer.toString(ch));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1706
                    writer.write(';');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1707
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1708
                else if ((!escapingNotNeeded(ch) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1709
                    (  (fromTextNode && m_charInfo.isSpecialTextChar(ch))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1710
                     || (!fromTextNode && m_charInfo.isSpecialAttrChar(ch))))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1711
                && m_elemContext.m_currentElemDepth > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1712
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1713
                    writer.write("&#");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1714
                    writer.write(Integer.toString(ch));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1715
                    writer.write(';');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1716
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1717
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1718
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1719
                    writer.write(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1720
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1721
                pos++;  // count the single character that was processed
7f561c08de6b Initial load
duke
parents:
diff changeset
  1722
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1723
7f561c08de6b Initial load
duke
parents:
diff changeset
  1724
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1725
        return pos;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1726
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1727
7f561c08de6b Initial load
duke
parents:
diff changeset
  1728
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1729
     * Receive notification of the beginning of an element, although this is a
7f561c08de6b Initial load
duke
parents:
diff changeset
  1730
     * SAX method additional namespace or attribute information can occur before
7f561c08de6b Initial load
duke
parents:
diff changeset
  1731
     * or after this call, that is associated with this element.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1732
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1733
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1734
     * @param namespaceURI The Namespace URI, or the empty string if the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1735
     *        element has no Namespace URI or if Namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
  1736
     *        processing is not being performed.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1737
     * @param localName The local name (without prefix), or the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1738
     *        empty string if Namespace processing is not being
7f561c08de6b Initial load
duke
parents:
diff changeset
  1739
     *        performed.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1740
     * @param name The element type name.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1741
     * @param atts The attributes attached to the element, if any.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1742
     * @throws org.xml.sax.SAXException Any SAX exception, possibly
7f561c08de6b Initial load
duke
parents:
diff changeset
  1743
     *            wrapping another exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1744
     * @see org.xml.sax.ContentHandler#startElement
7f561c08de6b Initial load
duke
parents:
diff changeset
  1745
     * @see org.xml.sax.ContentHandler#endElement
7f561c08de6b Initial load
duke
parents:
diff changeset
  1746
     * @see org.xml.sax.AttributeList
7f561c08de6b Initial load
duke
parents:
diff changeset
  1747
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1748
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1749
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1750
    public void startElement(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1751
        String namespaceURI,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1752
        String localName,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1753
        String name,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1754
        Attributes atts)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1755
        throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1756
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1757
        if (m_inEntityRef)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1758
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1759
7f561c08de6b Initial load
duke
parents:
diff changeset
  1760
        if (m_needToCallStartDocument)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1761
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1762
            startDocumentInternal();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1763
            m_needToCallStartDocument = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1764
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1765
        else if (m_cdataTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1766
            closeCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1767
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
  1768
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1769
            if ((true == m_needToOutputDocTypeDecl)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1770
                && (null != getDoctypeSystem()))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1771
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1772
                outputDocTypeDecl(name, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1773
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1774
7f561c08de6b Initial load
duke
parents:
diff changeset
  1775
            m_needToOutputDocTypeDecl = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1776
7f561c08de6b Initial load
duke
parents:
diff changeset
  1777
            /* before we over-write the current elementLocalName etc.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1778
             * lets close out the old one (if we still need to)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1779
             */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1780
            if (m_elemContext.m_startTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1781
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1782
                closeStartTag();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1783
                m_elemContext.m_startTagOpen = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1784
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1785
7f561c08de6b Initial load
duke
parents:
diff changeset
  1786
            if (namespaceURI != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1787
                ensurePrefixIsDeclared(namespaceURI, name);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1788
7f561c08de6b Initial load
duke
parents:
diff changeset
  1789
            m_ispreserve = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1790
7f561c08de6b Initial load
duke
parents:
diff changeset
  1791
            if (shouldIndent() && m_startNewLine)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1792
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1793
                indent();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1794
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1795
7f561c08de6b Initial load
duke
parents:
diff changeset
  1796
            m_startNewLine = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1797
7f561c08de6b Initial load
duke
parents:
diff changeset
  1798
            final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1799
            writer.write('<');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1800
            writer.write(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1801
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1802
        catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1803
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1804
            throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1805
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1806
7f561c08de6b Initial load
duke
parents:
diff changeset
  1807
        // process the attributes now, because after this SAX call they might be gone
7f561c08de6b Initial load
duke
parents:
diff changeset
  1808
        if (atts != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1809
            addAttributes(atts);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1810
7f561c08de6b Initial load
duke
parents:
diff changeset
  1811
        m_elemContext = m_elemContext.push(namespaceURI,localName,name);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1812
        m_isprevtext = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1813
7f561c08de6b Initial load
duke
parents:
diff changeset
  1814
        if (m_tracer != null){
7f561c08de6b Initial load
duke
parents:
diff changeset
  1815
            firePseudoAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1816
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1817
7f561c08de6b Initial load
duke
parents:
diff changeset
  1818
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1819
7f561c08de6b Initial load
duke
parents:
diff changeset
  1820
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1821
      * Receive notification of the beginning of an element, additional
7f561c08de6b Initial load
duke
parents:
diff changeset
  1822
      * namespace or attribute information can occur before or after this call,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1823
      * that is associated with this element.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1824
      *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1825
      *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1826
      * @param elementNamespaceURI The Namespace URI, or the empty string if the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1827
      *        element has no Namespace URI or if Namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
  1828
      *        processing is not being performed.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1829
      * @param elementLocalName The local name (without prefix), or the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1830
      *        empty string if Namespace processing is not being
7f561c08de6b Initial load
duke
parents:
diff changeset
  1831
      *        performed.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1832
      * @param elementName The element type name.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1833
      * @throws org.xml.sax.SAXException Any SAX exception, possibly
7f561c08de6b Initial load
duke
parents:
diff changeset
  1834
      *            wrapping another exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1835
      * @see org.xml.sax.ContentHandler#startElement
7f561c08de6b Initial load
duke
parents:
diff changeset
  1836
      * @see org.xml.sax.ContentHandler#endElement
7f561c08de6b Initial load
duke
parents:
diff changeset
  1837
      * @see org.xml.sax.AttributeList
7f561c08de6b Initial load
duke
parents:
diff changeset
  1838
      *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1839
      * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1840
      */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1841
    public void startElement(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1842
        String elementNamespaceURI,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1843
        String elementLocalName,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1844
        String elementName)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1845
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1846
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1847
        startElement(elementNamespaceURI, elementLocalName, elementName, null);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1848
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1849
7f561c08de6b Initial load
duke
parents:
diff changeset
  1850
    public void startElement(String elementName) throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1851
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1852
        startElement(null, null, elementName, null);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1853
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1854
7f561c08de6b Initial load
duke
parents:
diff changeset
  1855
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1856
     * Output the doc type declaration.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1857
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1858
     * @param name non-null reference to document type name.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1859
     * NEEDSDOC @param closeDecl
7f561c08de6b Initial load
duke
parents:
diff changeset
  1860
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1861
     * @throws java.io.IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1862
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1863
    void outputDocTypeDecl(String name, boolean closeDecl) throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1864
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1865
        if (m_cdataTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1866
            closeCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1867
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
  1868
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1869
            final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1870
            writer.write("<!DOCTYPE ");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1871
            writer.write(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1872
7f561c08de6b Initial load
duke
parents:
diff changeset
  1873
            String doctypePublic = getDoctypePublic();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1874
            if (null != doctypePublic)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1875
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1876
                writer.write(" PUBLIC \"");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1877
                writer.write(doctypePublic);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1878
                writer.write('\"');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1879
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1880
7f561c08de6b Initial load
duke
parents:
diff changeset
  1881
            String doctypeSystem = getDoctypeSystem();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1882
            if (null != doctypeSystem)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1883
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1884
                if (null == doctypePublic)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1885
                    writer.write(" SYSTEM \"");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1886
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1887
                    writer.write(" \"");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1888
7f561c08de6b Initial load
duke
parents:
diff changeset
  1889
                writer.write(doctypeSystem);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1890
7f561c08de6b Initial load
duke
parents:
diff changeset
  1891
                if (closeDecl)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1892
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1893
                    writer.write("\">");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1894
                    writer.write(m_lineSep, 0, m_lineSepLen);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1895
                    closeDecl = false; // done closing
7f561c08de6b Initial load
duke
parents:
diff changeset
  1896
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1897
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1898
                    writer.write('\"');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1899
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1900
            boolean dothis = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1901
            if (dothis)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1902
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1903
                // at one point this code seemed right,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1904
                // but not anymore - Brian M.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1905
                if (closeDecl)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1906
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1907
                    writer.write('>');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1908
                    writer.write(m_lineSep, 0, m_lineSepLen);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1909
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1910
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1911
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1912
        catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1913
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1914
            throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1915
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1916
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1917
7f561c08de6b Initial load
duke
parents:
diff changeset
  1918
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1919
     * Process the attributes, which means to write out the currently
7f561c08de6b Initial load
duke
parents:
diff changeset
  1920
     * collected attributes to the writer. The attributes are not
7f561c08de6b Initial load
duke
parents:
diff changeset
  1921
     * cleared by this method
7f561c08de6b Initial load
duke
parents:
diff changeset
  1922
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1923
     * @param writer the writer to write processed attributes to.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1924
     * @param nAttrs the number of attributes in m_attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
  1925
     * to be processed
7f561c08de6b Initial load
duke
parents:
diff changeset
  1926
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1927
     * @throws java.io.IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1928
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1929
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1930
    public void processAttributes(java.io.Writer writer, int nAttrs) throws IOException, SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1931
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1932
            /* real SAX attributes are not passed in, so process the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1933
             * attributes that were collected after the startElement call.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1934
             * _attribVector is a "cheap" list for Stream serializer output
7f561c08de6b Initial load
duke
parents:
diff changeset
  1935
             * accumulated over a series of calls to attribute(name,value)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1936
             */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1937
            String encoding = getEncoding();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1938
            for (int i = 0; i < nAttrs; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1939
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1940
                // elementAt is JDK 1.1.8
7f561c08de6b Initial load
duke
parents:
diff changeset
  1941
                final String name = m_attributes.getQName(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1942
                final String value = m_attributes.getValue(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1943
                writer.write(' ');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1944
                writer.write(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1945
                writer.write("=\"");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1946
                writeAttrString(writer, value, encoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1947
                writer.write('\"');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1948
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1949
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1950
7f561c08de6b Initial load
duke
parents:
diff changeset
  1951
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1952
     * Returns the specified <var>string</var> after substituting <VAR>specials</VAR>,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1953
     * and UTF-16 surrogates for chracter references <CODE>&amp;#xnn</CODE>.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1954
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1955
     * @param   string      String to convert to XML format.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1956
     * @param   encoding    CURRENTLY NOT IMPLEMENTED.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1957
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1958
     * @throws java.io.IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1959
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1960
    public void writeAttrString(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1961
        Writer writer,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1962
        String string,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1963
        String encoding)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1964
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1965
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1966
        final int len = string.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1967
        if (len > m_attrBuff.length)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1968
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1969
           m_attrBuff = new char[len*2 + 1];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1970
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1971
        string.getChars(0,len, m_attrBuff, 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1972
        final char[] stringChars = m_attrBuff;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1973
7f561c08de6b Initial load
duke
parents:
diff changeset
  1974
        for (int i = 0; i < len; )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1975
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1976
            char ch = stringChars[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1977
            if (escapingNotNeeded(ch) && (!m_charInfo.isSpecialAttrChar(ch)))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1978
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1979
                writer.write(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1980
                i++;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1981
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1982
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1983
            { // I guess the parser doesn't normalize cr/lf in attributes. -sb
7f561c08de6b Initial load
duke
parents:
diff changeset
  1984
//                if ((CharInfo.S_CARRIAGERETURN == ch)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1985
//                    && ((i + 1) < len)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1986
//                    && (CharInfo.S_LINEFEED == stringChars[i + 1]))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1987
//                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1988
//                    i++;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1989
//                    ch = CharInfo.S_LINEFEED;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1990
//                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1991
7f561c08de6b Initial load
duke
parents:
diff changeset
  1992
                i = accumDefaultEscape(writer, ch, i, stringChars, len, false, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1993
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1994
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1995
7f561c08de6b Initial load
duke
parents:
diff changeset
  1996
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1997
7f561c08de6b Initial load
duke
parents:
diff changeset
  1998
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1999
     * Receive notification of the end of an element.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2000
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2001
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2002
     * @param namespaceURI The Namespace URI, or the empty string if the
7f561c08de6b Initial load
duke
parents:
diff changeset
  2003
     *        element has no Namespace URI or if Namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
  2004
     *        processing is not being performed.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2005
     * @param localName The local name (without prefix), or the
7f561c08de6b Initial load
duke
parents:
diff changeset
  2006
     *        empty string if Namespace processing is not being
7f561c08de6b Initial load
duke
parents:
diff changeset
  2007
     *        performed.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2008
     * @param name The element type name
7f561c08de6b Initial load
duke
parents:
diff changeset
  2009
     * @throws org.xml.sax.SAXException Any SAX exception, possibly
7f561c08de6b Initial load
duke
parents:
diff changeset
  2010
     *            wrapping another exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2011
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2012
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2013
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2014
    public void endElement(String namespaceURI, String localName, String name)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2015
        throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2016
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2017
7f561c08de6b Initial load
duke
parents:
diff changeset
  2018
        if (m_inEntityRef)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2019
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2020
7f561c08de6b Initial load
duke
parents:
diff changeset
  2021
        // namespaces declared at the current depth are no longer valid
7f561c08de6b Initial load
duke
parents:
diff changeset
  2022
        // so get rid of them
7f561c08de6b Initial load
duke
parents:
diff changeset
  2023
        m_prefixMap.popNamespaces(m_elemContext.m_currentElemDepth, null);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2024
7f561c08de6b Initial load
duke
parents:
diff changeset
  2025
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
  2026
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2027
            final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2028
            if (m_elemContext.m_startTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2029
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2030
                if (m_tracer != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2031
                    super.fireStartElem(m_elemContext.m_elementName);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2032
                int nAttrs = m_attributes.getLength();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2033
                if (nAttrs > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2034
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2035
                    processAttributes(m_writer, nAttrs);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2036
                    // clear attributes object for re-use with next element
7f561c08de6b Initial load
duke
parents:
diff changeset
  2037
                    m_attributes.clear();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2038
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2039
                if (m_spaceBeforeClose)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2040
                    writer.write(" />");
7f561c08de6b Initial load
duke
parents:
diff changeset
  2041
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  2042
                    writer.write("/>");
7f561c08de6b Initial load
duke
parents:
diff changeset
  2043
                /* don't need to pop cdataSectionState because
7f561c08de6b Initial load
duke
parents:
diff changeset
  2044
                 * this element ended so quickly that we didn't get
7f561c08de6b Initial load
duke
parents:
diff changeset
  2045
                 * to push the state.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2046
                 */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2047
7f561c08de6b Initial load
duke
parents:
diff changeset
  2048
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2049
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
  2050
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2051
                if (m_cdataTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2052
                    closeCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2053
7f561c08de6b Initial load
duke
parents:
diff changeset
  2054
                if (shouldIndent())
7f561c08de6b Initial load
duke
parents:
diff changeset
  2055
                    indent(m_elemContext.m_currentElemDepth - 1);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2056
                writer.write('<');
7f561c08de6b Initial load
duke
parents:
diff changeset
  2057
                writer.write('/');
7f561c08de6b Initial load
duke
parents:
diff changeset
  2058
                writer.write(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2059
                writer.write('>');
7f561c08de6b Initial load
duke
parents:
diff changeset
  2060
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2061
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2062
        catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2063
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2064
            throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2065
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2066
7f561c08de6b Initial load
duke
parents:
diff changeset
  2067
        if (!m_elemContext.m_startTagOpen && m_doIndent)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2068
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2069
            m_ispreserve = m_preserves.isEmpty() ? false : m_preserves.pop();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2070
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2071
7f561c08de6b Initial load
duke
parents:
diff changeset
  2072
        m_isprevtext = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2073
7f561c08de6b Initial load
duke
parents:
diff changeset
  2074
        // fire off the end element event
7f561c08de6b Initial load
duke
parents:
diff changeset
  2075
        if (m_tracer != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2076
            super.fireEndElem(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2077
        m_elemContext = m_elemContext.m_prev;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2078
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2079
7f561c08de6b Initial load
duke
parents:
diff changeset
  2080
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2081
     * Receive notification of the end of an element.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2082
     * @param name The element type name
7f561c08de6b Initial load
duke
parents:
diff changeset
  2083
     * @throws org.xml.sax.SAXException Any SAX exception, possibly
7f561c08de6b Initial load
duke
parents:
diff changeset
  2084
     *     wrapping another exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2085
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2086
    public void endElement(String name) throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2087
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2088
        endElement(null, null, name);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2089
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2090
7f561c08de6b Initial load
duke
parents:
diff changeset
  2091
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2092
     * Begin the scope of a prefix-URI Namespace mapping
7f561c08de6b Initial load
duke
parents:
diff changeset
  2093
     * just before another element is about to start.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2094
     * This call will close any open tags so that the prefix mapping
7f561c08de6b Initial load
duke
parents:
diff changeset
  2095
     * will not apply to the current element, but the up comming child.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2096
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2097
     * @see org.xml.sax.ContentHandler#startPrefixMapping
7f561c08de6b Initial load
duke
parents:
diff changeset
  2098
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2099
     * @param prefix The Namespace prefix being declared.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2100
     * @param uri The Namespace URI the prefix is mapped to.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2101
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2102
     * @throws org.xml.sax.SAXException The client may throw
7f561c08de6b Initial load
duke
parents:
diff changeset
  2103
     *            an exception during processing.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2104
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2105
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2106
    public void startPrefixMapping(String prefix, String uri)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2107
        throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2108
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2109
        // the "true" causes the flush of any open tags
7f561c08de6b Initial load
duke
parents:
diff changeset
  2110
        startPrefixMapping(prefix, uri, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2111
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2112
7f561c08de6b Initial load
duke
parents:
diff changeset
  2113
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2114
     * Handle a prefix/uri mapping, which is associated with a startElement()
7f561c08de6b Initial load
duke
parents:
diff changeset
  2115
     * that is soon to follow. Need to close any open start tag to make
7f561c08de6b Initial load
duke
parents:
diff changeset
  2116
     * sure than any name space attributes due to this event are associated wih
7f561c08de6b Initial load
duke
parents:
diff changeset
  2117
     * the up comming element, not the current one.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2118
     * @see ExtendedContentHandler#startPrefixMapping
7f561c08de6b Initial load
duke
parents:
diff changeset
  2119
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2120
     * @param prefix The Namespace prefix being declared.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2121
     * @param uri The Namespace URI the prefix is mapped to.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2122
     * @param shouldFlush true if any open tags need to be closed first, this
7f561c08de6b Initial load
duke
parents:
diff changeset
  2123
     * will impact which element the mapping applies to (open parent, or its up
7f561c08de6b Initial load
duke
parents:
diff changeset
  2124
     * comming child)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2125
     * @return returns true if the call made a change to the current
7f561c08de6b Initial load
duke
parents:
diff changeset
  2126
     * namespace information, false if it did not change anything, e.g. if the
7f561c08de6b Initial load
duke
parents:
diff changeset
  2127
     * prefix/namespace mapping was already in scope from before.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2128
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2129
     * @throws org.xml.sax.SAXException The client may throw
7f561c08de6b Initial load
duke
parents:
diff changeset
  2130
     *            an exception during processing.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2131
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2132
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2133
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2134
    public boolean startPrefixMapping(
7f561c08de6b Initial load
duke
parents:
diff changeset
  2135
        String prefix,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2136
        String uri,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2137
        boolean shouldFlush)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2138
        throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2139
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2140
7f561c08de6b Initial load
duke
parents:
diff changeset
  2141
        /* Remember the mapping, and at what depth it was declared
7f561c08de6b Initial load
duke
parents:
diff changeset
  2142
         * This is one greater than the current depth because these
7f561c08de6b Initial load
duke
parents:
diff changeset
  2143
         * mappings will apply to the next depth. This is in
7f561c08de6b Initial load
duke
parents:
diff changeset
  2144
         * consideration that startElement() will soon be called
7f561c08de6b Initial load
duke
parents:
diff changeset
  2145
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2146
7f561c08de6b Initial load
duke
parents:
diff changeset
  2147
        boolean pushed;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2148
        int pushDepth;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2149
        if (shouldFlush)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2150
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2151
            flushPending();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2152
            // the prefix mapping applies to the child element (one deeper)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2153
            pushDepth = m_elemContext.m_currentElemDepth + 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2154
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2155
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
  2156
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2157
            // the prefix mapping applies to the current element
7f561c08de6b Initial load
duke
parents:
diff changeset
  2158
            pushDepth = m_elemContext.m_currentElemDepth;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2159
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2160
        pushed = m_prefixMap.pushNamespace(prefix, uri, pushDepth);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2161
7f561c08de6b Initial load
duke
parents:
diff changeset
  2162
        if (pushed)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2163
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2164
            /* Brian M.: don't know if we really needto do this. The
7f561c08de6b Initial load
duke
parents:
diff changeset
  2165
             * callers of this object should have injected both
7f561c08de6b Initial load
duke
parents:
diff changeset
  2166
             * startPrefixMapping and the attributes.  We are
7f561c08de6b Initial load
duke
parents:
diff changeset
  2167
             * just covering our butt here.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2168
             */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2169
            String name;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2170
            if (EMPTYSTRING.equals(prefix))
7f561c08de6b Initial load
duke
parents:
diff changeset
  2171
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2172
                name = "xmlns";
7f561c08de6b Initial load
duke
parents:
diff changeset
  2173
                addAttributeAlways(XMLNS_URI, name, name, "CDATA", uri, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2174
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2175
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
  2176
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2177
                if (!EMPTYSTRING.equals(uri))
7f561c08de6b Initial load
duke
parents:
diff changeset
  2178
                    // hack for XSLTC attribset16 test
7f561c08de6b Initial load
duke
parents:
diff changeset
  2179
                { // that maps ns1 prefix to "" URI
7f561c08de6b Initial load
duke
parents:
diff changeset
  2180
                    name = "xmlns:" + prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2181
7f561c08de6b Initial load
duke
parents:
diff changeset
  2182
                    /* for something like xmlns:abc="w3.pretend.org"
7f561c08de6b Initial load
duke
parents:
diff changeset
  2183
                     *  the      uri is the value, that is why we pass it in the
7f561c08de6b Initial load
duke
parents:
diff changeset
  2184
                     * value, or 5th slot of addAttributeAlways()
7f561c08de6b Initial load
duke
parents:
diff changeset
  2185
                     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2186
                    addAttributeAlways(XMLNS_URI, prefix, name, "CDATA", uri, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2187
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2188
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2189
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2190
        return pushed;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2191
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2192
7f561c08de6b Initial load
duke
parents:
diff changeset
  2193
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2194
     * Receive notification of an XML comment anywhere in the document. This
7f561c08de6b Initial load
duke
parents:
diff changeset
  2195
     * callback will be used for comments inside or outside the document
7f561c08de6b Initial load
duke
parents:
diff changeset
  2196
     * element, including comments in the external DTD subset (if read).
7f561c08de6b Initial load
duke
parents:
diff changeset
  2197
     * @param ch An array holding the characters in the comment.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2198
     * @param start The starting position in the array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2199
     * @param length The number of characters to use from the array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2200
     * @throws org.xml.sax.SAXException The application may raise an exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2201
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2202
    public void comment(char ch[], int start, int length)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2203
        throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2204
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2205
7f561c08de6b Initial load
duke
parents:
diff changeset
  2206
        int start_old = start;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2207
        if (m_inEntityRef)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2208
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2209
        if (m_elemContext.m_startTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2210
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2211
            closeStartTag();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2212
            m_elemContext.m_startTagOpen = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2213
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2214
        else if (m_needToCallStartDocument)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2215
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2216
            startDocumentInternal();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2217
            m_needToCallStartDocument = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2218
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2219
7f561c08de6b Initial load
duke
parents:
diff changeset
  2220
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
  2221
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2222
            if (shouldIndent())
7f561c08de6b Initial load
duke
parents:
diff changeset
  2223
                indent();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2224
7f561c08de6b Initial load
duke
parents:
diff changeset
  2225
            final int limit = start + length;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2226
            boolean wasDash = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2227
            if (m_cdataTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2228
                closeCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2229
            final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2230
            writer.write(COMMENT_BEGIN);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2231
            // Detect occurrences of two consecutive dashes, handle as necessary.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2232
            for (int i = start; i < limit; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2233
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2234
                if (wasDash && ch[i] == '-')
7f561c08de6b Initial load
duke
parents:
diff changeset
  2235
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2236
                    writer.write(ch, start, i - start);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2237
                    writer.write(" -");
7f561c08de6b Initial load
duke
parents:
diff changeset
  2238
                    start = i + 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2239
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2240
                wasDash = (ch[i] == '-');
7f561c08de6b Initial load
duke
parents:
diff changeset
  2241
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2242
7f561c08de6b Initial load
duke
parents:
diff changeset
  2243
            // if we have some chars in the comment
7f561c08de6b Initial load
duke
parents:
diff changeset
  2244
            if (length > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2245
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2246
                // Output the remaining characters (if any)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2247
                final int remainingChars = (limit - start);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2248
                if (remainingChars > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2249
                    writer.write(ch, start, remainingChars);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2250
                // Protect comment end from a single trailing dash
7f561c08de6b Initial load
duke
parents:
diff changeset
  2251
                if (ch[limit - 1] == '-')
7f561c08de6b Initial load
duke
parents:
diff changeset
  2252
                    writer.write(' ');
7f561c08de6b Initial load
duke
parents:
diff changeset
  2253
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2254
            writer.write(COMMENT_END);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2255
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2256
        catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2257
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2258
            throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2259
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2260
7f561c08de6b Initial load
duke
parents:
diff changeset
  2261
        m_startNewLine = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2262
        // time to generate comment event
7f561c08de6b Initial load
duke
parents:
diff changeset
  2263
        if (m_tracer != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2264
            super.fireCommentEvent(ch, start_old,length);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2265
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2266
7f561c08de6b Initial load
duke
parents:
diff changeset
  2267
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2268
     * Report the end of a CDATA section.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2269
     * @throws org.xml.sax.SAXException The application may raise an exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2270
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2271
     *  @see  #startCDATA
7f561c08de6b Initial load
duke
parents:
diff changeset
  2272
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2273
    public void endCDATA() throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2274
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2275
        if (m_cdataTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2276
            closeCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2277
        m_cdataStartCalled = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2278
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2279
7f561c08de6b Initial load
duke
parents:
diff changeset
  2280
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2281
     * Report the end of DTD declarations.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2282
     * @throws org.xml.sax.SAXException The application may raise an exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2283
     * @see #startDTD
7f561c08de6b Initial load
duke
parents:
diff changeset
  2284
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2285
    public void endDTD() throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2286
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2287
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
  2288
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2289
            // Don't output doctype declaration until startDocumentInternal
7f561c08de6b Initial load
duke
parents:
diff changeset
  2290
            // has been called. Otherwise, it can appear before XML decl.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2291
            if (m_needToCallStartDocument) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2292
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2293
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2294
7f561c08de6b Initial load
duke
parents:
diff changeset
  2295
            if (m_needToOutputDocTypeDecl)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2296
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2297
                outputDocTypeDecl(m_elemContext.m_elementName, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2298
                m_needToOutputDocTypeDecl = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2299
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2300
            final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2301
            if (!m_inDoctype)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2302
                writer.write("]>");
7f561c08de6b Initial load
duke
parents:
diff changeset
  2303
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
  2304
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2305
                writer.write('>');
7f561c08de6b Initial load
duke
parents:
diff changeset
  2306
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2307
7f561c08de6b Initial load
duke
parents:
diff changeset
  2308
            writer.write(m_lineSep, 0, m_lineSepLen);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2309
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2310
        catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2311
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2312
            throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2313
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2314
7f561c08de6b Initial load
duke
parents:
diff changeset
  2315
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2316
7f561c08de6b Initial load
duke
parents:
diff changeset
  2317
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2318
     * End the scope of a prefix-URI Namespace mapping.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2319
     * @see org.xml.sax.ContentHandler#endPrefixMapping
7f561c08de6b Initial load
duke
parents:
diff changeset
  2320
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2321
     * @param prefix The prefix that was being mapping.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2322
     * @throws org.xml.sax.SAXException The client may throw
7f561c08de6b Initial load
duke
parents:
diff changeset
  2323
     *            an exception during processing.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2324
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2325
    public void endPrefixMapping(String prefix) throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2326
    { // do nothing
7f561c08de6b Initial load
duke
parents:
diff changeset
  2327
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2328
7f561c08de6b Initial load
duke
parents:
diff changeset
  2329
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2330
     * Receive notification of ignorable whitespace in element content.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2331
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2332
     * Not sure how to get this invoked quite yet.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2333
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2334
     * @param ch The characters from the XML document.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2335
     * @param start The start position in the array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2336
     * @param length The number of characters to read from the array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2337
     * @throws org.xml.sax.SAXException Any SAX exception, possibly
7f561c08de6b Initial load
duke
parents:
diff changeset
  2338
     *            wrapping another exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2339
     * @see #characters
7f561c08de6b Initial load
duke
parents:
diff changeset
  2340
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2341
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2342
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2343
    public void ignorableWhitespace(char ch[], int start, int length)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2344
        throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2345
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2346
7f561c08de6b Initial load
duke
parents:
diff changeset
  2347
        if (0 == length)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2348
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2349
        characters(ch, start, length);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2350
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2351
7f561c08de6b Initial load
duke
parents:
diff changeset
  2352
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2353
     * Receive notification of a skipped entity.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2354
     * @see org.xml.sax.ContentHandler#skippedEntity
7f561c08de6b Initial load
duke
parents:
diff changeset
  2355
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2356
     * @param name The name of the skipped entity.  If it is a
7f561c08de6b Initial load
duke
parents:
diff changeset
  2357
     *       parameter                   entity, the name will begin with '%',
7f561c08de6b Initial load
duke
parents:
diff changeset
  2358
     * and if it is the external DTD subset, it will be the string
7f561c08de6b Initial load
duke
parents:
diff changeset
  2359
     * "[dtd]".
7f561c08de6b Initial load
duke
parents:
diff changeset
  2360
     * @throws org.xml.sax.SAXException Any SAX exception, possibly wrapping
7f561c08de6b Initial load
duke
parents:
diff changeset
  2361
     * another exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2362
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2363
    public void skippedEntity(String name) throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2364
    { // TODO: Should handle
7f561c08de6b Initial load
duke
parents:
diff changeset
  2365
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2366
7f561c08de6b Initial load
duke
parents:
diff changeset
  2367
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2368
     * Report the start of a CDATA section.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2369
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2370
     * @throws org.xml.sax.SAXException The application may raise an exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2371
     * @see #endCDATA
7f561c08de6b Initial load
duke
parents:
diff changeset
  2372
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2373
    public void startCDATA() throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2374
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2375
        m_cdataStartCalled = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2376
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2377
7f561c08de6b Initial load
duke
parents:
diff changeset
  2378
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2379
     * Report the beginning of an entity.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2380
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2381
     * The start and end of the document entity are not reported.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2382
     * The start and end of the external DTD subset are reported
7f561c08de6b Initial load
duke
parents:
diff changeset
  2383
     * using the pseudo-name "[dtd]".  All other events must be
7f561c08de6b Initial load
duke
parents:
diff changeset
  2384
     * properly nested within start/end entity events.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2385
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2386
     * @param name The name of the entity.  If it is a parameter
7f561c08de6b Initial load
duke
parents:
diff changeset
  2387
     *        entity, the name will begin with '%'.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2388
     * @throws org.xml.sax.SAXException The application may raise an exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2389
     * @see #endEntity
7f561c08de6b Initial load
duke
parents:
diff changeset
  2390
     * @see org.xml.sax.ext.DeclHandler#internalEntityDecl
7f561c08de6b Initial load
duke
parents:
diff changeset
  2391
     * @see org.xml.sax.ext.DeclHandler#externalEntityDecl
7f561c08de6b Initial load
duke
parents:
diff changeset
  2392
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2393
    public void startEntity(String name) throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2394
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2395
        if (name.equals("[dtd]"))
7f561c08de6b Initial load
duke
parents:
diff changeset
  2396
            m_inExternalDTD = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2397
7f561c08de6b Initial load
duke
parents:
diff changeset
  2398
        if (!m_expandDTDEntities && !m_inExternalDTD) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2399
            /* Only leave the entity as-is if
7f561c08de6b Initial load
duke
parents:
diff changeset
  2400
             * we've been told not to expand them
7f561c08de6b Initial load
duke
parents:
diff changeset
  2401
             * and this is not the magic [dtd] name.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2402
             */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2403
            startNonEscaping();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2404
            characters("&" + name + ';');
7f561c08de6b Initial load
duke
parents:
diff changeset
  2405
            endNonEscaping();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2406
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2407
7f561c08de6b Initial load
duke
parents:
diff changeset
  2408
        m_inEntityRef = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2409
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2410
7f561c08de6b Initial load
duke
parents:
diff changeset
  2411
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2412
     * For the enclosing elements starting tag write out
7f561c08de6b Initial load
duke
parents:
diff changeset
  2413
     * out any attributes followed by ">"
7f561c08de6b Initial load
duke
parents:
diff changeset
  2414
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2415
     * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2416
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2417
    protected void closeStartTag() throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2418
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2419
        if (m_elemContext.m_startTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2420
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2421
7f561c08de6b Initial load
duke
parents:
diff changeset
  2422
            try
7f561c08de6b Initial load
duke
parents:
diff changeset
  2423
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2424
                if (m_tracer != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2425
                    super.fireStartElem(m_elemContext.m_elementName);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2426
                int nAttrs = m_attributes.getLength();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2427
                if (nAttrs > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2428
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2429
                     processAttributes(m_writer, nAttrs);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2430
                    // clear attributes object for re-use with next element
7f561c08de6b Initial load
duke
parents:
diff changeset
  2431
                    m_attributes.clear();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2432
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2433
                m_writer.write('>');
7f561c08de6b Initial load
duke
parents:
diff changeset
  2434
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2435
            catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2436
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2437
                throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2438
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2439
7f561c08de6b Initial load
duke
parents:
diff changeset
  2440
            /* whether Xalan or XSLTC, we have the prefix mappings now, so
7f561c08de6b Initial load
duke
parents:
diff changeset
  2441
             * lets determine if the current element is specified in the cdata-
7f561c08de6b Initial load
duke
parents:
diff changeset
  2442
             * section-elements list.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2443
             */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2444
            if (m_cdataSectionElements != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2445
                m_elemContext.m_isCdataSection = isCdataSection();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2446
7f561c08de6b Initial load
duke
parents:
diff changeset
  2447
            if (m_doIndent)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2448
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2449
                m_isprevtext = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2450
                m_preserves.push(m_ispreserve);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2451
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2452
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2453
7f561c08de6b Initial load
duke
parents:
diff changeset
  2454
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2455
7f561c08de6b Initial load
duke
parents:
diff changeset
  2456
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2457
     * Report the start of DTD declarations, if any.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2458
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2459
     * Any declarations are assumed to be in the internal subset unless
7f561c08de6b Initial load
duke
parents:
diff changeset
  2460
     * otherwise indicated.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2461
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2462
     * @param name The document type name.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2463
     * @param publicId The declared public identifier for the
7f561c08de6b Initial load
duke
parents:
diff changeset
  2464
     *        external DTD subset, or null if none was declared.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2465
     * @param systemId The declared system identifier for the
7f561c08de6b Initial load
duke
parents:
diff changeset
  2466
     *        external DTD subset, or null if none was declared.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2467
     * @throws org.xml.sax.SAXException The application may raise an
7f561c08de6b Initial load
duke
parents:
diff changeset
  2468
     *            exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2469
     * @see #endDTD
7f561c08de6b Initial load
duke
parents:
diff changeset
  2470
     * @see #startEntity
7f561c08de6b Initial load
duke
parents:
diff changeset
  2471
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2472
    public void startDTD(String name, String publicId, String systemId)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2473
        throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2474
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2475
        setDoctypeSystem(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2476
        setDoctypePublic(publicId);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2477
7f561c08de6b Initial load
duke
parents:
diff changeset
  2478
        m_elemContext.m_elementName = name;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2479
        m_inDoctype = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2480
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2481
7f561c08de6b Initial load
duke
parents:
diff changeset
  2482
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2483
     * Returns the m_indentAmount.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2484
     * @return int
7f561c08de6b Initial load
duke
parents:
diff changeset
  2485
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2486
    public int getIndentAmount()
7f561c08de6b Initial load
duke
parents:
diff changeset
  2487
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2488
        return m_indentAmount;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2489
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2490
7f561c08de6b Initial load
duke
parents:
diff changeset
  2491
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2492
     * Sets the m_indentAmount.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2493
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2494
     * @param m_indentAmount The m_indentAmount to set
7f561c08de6b Initial load
duke
parents:
diff changeset
  2495
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2496
    public void setIndentAmount(int m_indentAmount)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2497
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2498
        this.m_indentAmount = m_indentAmount;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2499
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2500
7f561c08de6b Initial load
duke
parents:
diff changeset
  2501
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2502
     * Tell if, based on space preservation constraints and the doIndent property,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2503
     * if an indent should occur.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2504
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2505
     * @return True if an indent should occur.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2506
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2507
    protected boolean shouldIndent()
7f561c08de6b Initial load
duke
parents:
diff changeset
  2508
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2509
        return m_doIndent && (!m_ispreserve && !m_isprevtext);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2510
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2511
7f561c08de6b Initial load
duke
parents:
diff changeset
  2512
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2513
     * Searches for the list of qname properties with the specified key in the
7f561c08de6b Initial load
duke
parents:
diff changeset
  2514
     * property list. If the key is not found in this property list, the default
7f561c08de6b Initial load
duke
parents:
diff changeset
  2515
     * property list, and its defaults, recursively, are then checked. The
7f561c08de6b Initial load
duke
parents:
diff changeset
  2516
     * method returns <code>null</code> if the property is not found.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2517
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2518
     * @param   key   the property key.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2519
     * @param props the list of properties to search in.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2520
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2521
     * Sets the vector of local-name/URI pairs of the cdata section elements
7f561c08de6b Initial load
duke
parents:
diff changeset
  2522
     * specified in the cdata-section-elements property.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2523
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2524
     * This method is essentially a copy of getQNameProperties() from
7f561c08de6b Initial load
duke
parents:
diff changeset
  2525
     * OutputProperties. Eventually this method should go away and a call
7f561c08de6b Initial load
duke
parents:
diff changeset
  2526
     * to setCdataSectionElements(Vector v) should be made directly.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2527
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2528
    private void setCdataSectionElements(String key, Properties props)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2529
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2530
7f561c08de6b Initial load
duke
parents:
diff changeset
  2531
        String s = props.getProperty(key);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2532
7f561c08de6b Initial load
duke
parents:
diff changeset
  2533
        if (null != s)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2534
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2535
            // Vector of URI/LocalName pairs
7f561c08de6b Initial load
duke
parents:
diff changeset
  2536
            Vector v = new Vector();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2537
            int l = s.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2538
            boolean inCurly = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2539
            StringBuffer buf = new StringBuffer();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2540
7f561c08de6b Initial load
duke
parents:
diff changeset
  2541
            // parse through string, breaking on whitespaces.  I do this instead
7f561c08de6b Initial load
duke
parents:
diff changeset
  2542
            // of a tokenizer so I can track whitespace inside of curly brackets,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2543
            // which theoretically shouldn't happen if they contain legal URLs.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2544
            for (int i = 0; i < l; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2545
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2546
                char c = s.charAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2547
7f561c08de6b Initial load
duke
parents:
diff changeset
  2548
                if (Character.isWhitespace(c))
7f561c08de6b Initial load
duke
parents:
diff changeset
  2549
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2550
                    if (!inCurly)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2551
                    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2552
                        if (buf.length() > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2553
                        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2554
                            addCdataSectionElement(buf.toString(), v);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2555
                            buf.setLength(0);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2556
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2557
                        continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2558
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2559
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2560
                else if ('{' == c)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2561
                    inCurly = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2562
                else if ('}' == c)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2563
                    inCurly = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2564
7f561c08de6b Initial load
duke
parents:
diff changeset
  2565
                buf.append(c);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2566
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2567
7f561c08de6b Initial load
duke
parents:
diff changeset
  2568
            if (buf.length() > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2569
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2570
                addCdataSectionElement(buf.toString(), v);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2571
                buf.setLength(0);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2572
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2573
            // call the official, public method to set the collected names
7f561c08de6b Initial load
duke
parents:
diff changeset
  2574
            setCdataSectionElements(v);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2575
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2576
7f561c08de6b Initial load
duke
parents:
diff changeset
  2577
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2578
7f561c08de6b Initial load
duke
parents:
diff changeset
  2579
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2580
     * Adds a URI/LocalName pair of strings to the list.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2581
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2582
     * @param URI_and_localName String of the form "{uri}local" or "local"
7f561c08de6b Initial load
duke
parents:
diff changeset
  2583
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2584
     * @return a QName object
7f561c08de6b Initial load
duke
parents:
diff changeset
  2585
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2586
    private void addCdataSectionElement(String URI_and_localName, Vector v)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2587
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2588
7f561c08de6b Initial load
duke
parents:
diff changeset
  2589
        StringTokenizer tokenizer =
7f561c08de6b Initial load
duke
parents:
diff changeset
  2590
            new StringTokenizer(URI_and_localName, "{}", false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2591
        String s1 = tokenizer.nextToken();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2592
        String s2 = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2593
7f561c08de6b Initial load
duke
parents:
diff changeset
  2594
        if (null == s2)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2595
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2596
            // add null URI and the local name
7f561c08de6b Initial load
duke
parents:
diff changeset
  2597
            v.addElement(null);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2598
            v.addElement(s1);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2599
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2600
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
  2601
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2602
            // add URI, then local name
7f561c08de6b Initial load
duke
parents:
diff changeset
  2603
            v.addElement(s1);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2604
            v.addElement(s2);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2605
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2606
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2607
7f561c08de6b Initial load
duke
parents:
diff changeset
  2608
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2609
     * Remembers the cdata sections specified in the cdata-section-elements.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2610
     * The "official way to set URI and localName pairs.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2611
     * This method should be used by both Xalan and XSLTC.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2612
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2613
     * @param URI_and_localNames a vector of pairs of Strings (URI/local)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2614
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2615
    public void setCdataSectionElements(Vector URI_and_localNames)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2616
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2617
        m_cdataSectionElements = URI_and_localNames;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2618
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2619
7f561c08de6b Initial load
duke
parents:
diff changeset
  2620
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2621
     * Makes sure that the namespace URI for the given qualified attribute name
7f561c08de6b Initial load
duke
parents:
diff changeset
  2622
     * is declared.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2623
     * @param ns the namespace URI
7f561c08de6b Initial load
duke
parents:
diff changeset
  2624
     * @param rawName the qualified name
7f561c08de6b Initial load
duke
parents:
diff changeset
  2625
     * @return returns null if no action is taken, otherwise it returns the
7f561c08de6b Initial load
duke
parents:
diff changeset
  2626
     * prefix used in declaring the namespace.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2627
     * @throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2628
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2629
    protected String ensureAttributesNamespaceIsDeclared(
7f561c08de6b Initial load
duke
parents:
diff changeset
  2630
        String ns,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2631
        String localName,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2632
        String rawName)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2633
        throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2634
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2635
7f561c08de6b Initial load
duke
parents:
diff changeset
  2636
        if (ns != null && ns.length() > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2637
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2638
7f561c08de6b Initial load
duke
parents:
diff changeset
  2639
            // extract the prefix in front of the raw name
7f561c08de6b Initial load
duke
parents:
diff changeset
  2640
            int index = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2641
            String prefixFromRawName =
7f561c08de6b Initial load
duke
parents:
diff changeset
  2642
                (index = rawName.indexOf(":")) < 0
7f561c08de6b Initial load
duke
parents:
diff changeset
  2643
                    ? ""
7f561c08de6b Initial load
duke
parents:
diff changeset
  2644
                    : rawName.substring(0, index);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2645
7f561c08de6b Initial load
duke
parents:
diff changeset
  2646
            if (index > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2647
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2648
                // we have a prefix, lets see if it maps to a namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
  2649
                String uri = m_prefixMap.lookupNamespace(prefixFromRawName);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2650
                if (uri != null && uri.equals(ns))
7f561c08de6b Initial load
duke
parents:
diff changeset
  2651
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2652
                    // the prefix in the raw name is already maps to the given namespace uri
7f561c08de6b Initial load
duke
parents:
diff changeset
  2653
                    // so we don't need to do anything
7f561c08de6b Initial load
duke
parents:
diff changeset
  2654
                    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2655
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2656
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  2657
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2658
                    // The uri does not map to the prefix in the raw name,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2659
                    // so lets make the mapping.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2660
                    this.startPrefixMapping(prefixFromRawName, ns, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2661
                    this.addAttribute(
7f561c08de6b Initial load
duke
parents:
diff changeset
  2662
                        "http://www.w3.org/2000/xmlns/",
7f561c08de6b Initial load
duke
parents:
diff changeset
  2663
                        prefixFromRawName,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2664
                        "xmlns:" + prefixFromRawName,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2665
                        "CDATA",
7f561c08de6b Initial load
duke
parents:
diff changeset
  2666
                        ns, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2667
                    return prefixFromRawName;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2668
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2669
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2670
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
  2671
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2672
                // we don't have a prefix in the raw name.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2673
                // Does the URI map to a prefix already?
7f561c08de6b Initial load
duke
parents:
diff changeset
  2674
                String prefix = m_prefixMap.lookupPrefix(ns);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2675
                if (prefix == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2676
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2677
                    // uri is not associated with a prefix,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2678
                    // so lets generate a new prefix to use
7f561c08de6b Initial load
duke
parents:
diff changeset
  2679
                    prefix = m_prefixMap.generateNextPrefix();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2680
                    this.startPrefixMapping(prefix, ns, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2681
                    this.addAttribute(
7f561c08de6b Initial load
duke
parents:
diff changeset
  2682
                        "http://www.w3.org/2000/xmlns/",
7f561c08de6b Initial load
duke
parents:
diff changeset
  2683
                        prefix,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2684
                        "xmlns:" + prefix,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2685
                        "CDATA",
7f561c08de6b Initial load
duke
parents:
diff changeset
  2686
                        ns, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2687
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2688
7f561c08de6b Initial load
duke
parents:
diff changeset
  2689
                return prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2690
7f561c08de6b Initial load
duke
parents:
diff changeset
  2691
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2692
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2693
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2694
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2695
7f561c08de6b Initial load
duke
parents:
diff changeset
  2696
    void ensurePrefixIsDeclared(String ns, String rawName)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2697
        throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2698
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2699
7f561c08de6b Initial load
duke
parents:
diff changeset
  2700
        if (ns != null && ns.length() > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2701
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2702
            int index;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2703
            final boolean no_prefix = ((index = rawName.indexOf(":")) < 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2704
            String prefix = (no_prefix) ? "" : rawName.substring(0, index);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2705
7f561c08de6b Initial load
duke
parents:
diff changeset
  2706
            if (null != prefix)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2707
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2708
                String foundURI = m_prefixMap.lookupNamespace(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2709
7f561c08de6b Initial load
duke
parents:
diff changeset
  2710
                if ((null == foundURI) || !foundURI.equals(ns))
7f561c08de6b Initial load
duke
parents:
diff changeset
  2711
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2712
                    this.startPrefixMapping(prefix, ns);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2713
7f561c08de6b Initial load
duke
parents:
diff changeset
  2714
                    // Bugzilla1133: Generate attribute as well as namespace event.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2715
                    // SAX does expect both.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2716
7f561c08de6b Initial load
duke
parents:
diff changeset
  2717
                    this.addAttributeAlways(
7f561c08de6b Initial load
duke
parents:
diff changeset
  2718
                        "http://www.w3.org/2000/xmlns/",
7f561c08de6b Initial load
duke
parents:
diff changeset
  2719
                        no_prefix ? "xmlns" : prefix,  // local name
7f561c08de6b Initial load
duke
parents:
diff changeset
  2720
                        no_prefix ? "xmlns" : ("xmlns:"+ prefix), // qname
7f561c08de6b Initial load
duke
parents:
diff changeset
  2721
                        "CDATA",
7f561c08de6b Initial load
duke
parents:
diff changeset
  2722
                        ns,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2723
                        false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2724
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2725
7f561c08de6b Initial load
duke
parents:
diff changeset
  2726
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2727
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2728
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2729
7f561c08de6b Initial load
duke
parents:
diff changeset
  2730
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2731
     * This method flushes any pending events, which can be startDocument()
7f561c08de6b Initial load
duke
parents:
diff changeset
  2732
     * closing the opening tag of an element, or closing an open CDATA section.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2733
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2734
    public void flushPending() throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2735
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2736
            if (m_needToCallStartDocument)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2737
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2738
                startDocumentInternal();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2739
                m_needToCallStartDocument = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2740
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2741
            if (m_elemContext.m_startTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2742
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2743
                closeStartTag();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2744
                m_elemContext.m_startTagOpen = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2745
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2746
7f561c08de6b Initial load
duke
parents:
diff changeset
  2747
            if (m_cdataTagOpen)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2748
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2749
                closeCDATA();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2750
                m_cdataTagOpen = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2751
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2752
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2753
7f561c08de6b Initial load
duke
parents:
diff changeset
  2754
    public void setContentHandler(ContentHandler ch)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2755
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2756
        // this method is really only useful in the ToSAXHandler classes but it is
7f561c08de6b Initial load
duke
parents:
diff changeset
  2757
        // in the interface.  If the method defined here is ever called
7f561c08de6b Initial load
duke
parents:
diff changeset
  2758
        // we are probably in trouble.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2759
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2760
7f561c08de6b Initial load
duke
parents:
diff changeset
  2761
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2762
     * Adds the given attribute to the set of attributes, even if there is
7f561c08de6b Initial load
duke
parents:
diff changeset
  2763
     * no currently open element. This is useful if a SAX startPrefixMapping()
7f561c08de6b Initial load
duke
parents:
diff changeset
  2764
     * should need to add an attribute before the element name is seen.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2765
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2766
     * This method is a copy of its super classes method, except that some
7f561c08de6b Initial load
duke
parents:
diff changeset
  2767
     * tracing of events is done.  This is so the tracing is only done for
7f561c08de6b Initial load
duke
parents:
diff changeset
  2768
     * stream serializers, not for SAX ones.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2769
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2770
     * @param uri the URI of the attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
  2771
     * @param localName the local name of the attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
  2772
     * @param rawName   the qualified name of the attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
  2773
     * @param type the type of the attribute (probably CDATA)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2774
     * @param value the value of the attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
  2775
     * @param xslAttribute true if this attribute is coming from an xsl:attribute element.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2776
     * @return true if the attribute value was added,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2777
     * false if the attribute already existed and the value was
7f561c08de6b Initial load
duke
parents:
diff changeset
  2778
     * replaced with the new value.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2779
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2780
    public boolean addAttributeAlways(
7f561c08de6b Initial load
duke
parents:
diff changeset
  2781
        String uri,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2782
        String localName,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2783
        String rawName,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2784
        String type,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2785
        String value,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2786
        boolean xslAttribute)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2787
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2788
        boolean was_added;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2789
        int index;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2790
        //if (uri == null || localName == null || uri.length() == 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2791
            index = m_attributes.getIndex(rawName);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2792
        // Don't use 'localName' as it gives incorrect value, rely only on 'rawName'
7f561c08de6b Initial load
duke
parents:
diff changeset
  2793
        /*else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2794
            index = m_attributes.getIndex(uri, localName);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2795
        }*/
7f561c08de6b Initial load
duke
parents:
diff changeset
  2796
        if (index >= 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2797
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2798
            String old_value = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2799
            if (m_tracer != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2800
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2801
                old_value = m_attributes.getValue(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2802
                if (value.equals(old_value))
7f561c08de6b Initial load
duke
parents:
diff changeset
  2803
                    old_value = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2804
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2805
7f561c08de6b Initial load
duke
parents:
diff changeset
  2806
            /* We've seen the attribute before.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2807
             * We may have a null uri or localName, but all we really
7f561c08de6b Initial load
duke
parents:
diff changeset
  2808
             * want to re-set is the value anyway.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2809
             */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2810
            m_attributes.setValue(index, value);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2811
            was_added = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2812
            if (old_value != null){
7f561c08de6b Initial load
duke
parents:
diff changeset
  2813
                firePseudoAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2814
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2815
7f561c08de6b Initial load
duke
parents:
diff changeset
  2816
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2817
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
  2818
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2819
            // the attribute doesn't exist yet, create it
7f561c08de6b Initial load
duke
parents:
diff changeset
  2820
            if (xslAttribute)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2821
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2822
                /*
7f561c08de6b Initial load
duke
parents:
diff changeset
  2823
                 * This attribute is from an xsl:attribute element so we take some care in
7f561c08de6b Initial load
duke
parents:
diff changeset
  2824
                 * adding it, e.g.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2825
                 *   <elem1  foo:attr1="1" xmlns:foo="uri1">
7f561c08de6b Initial load
duke
parents:
diff changeset
  2826
                 *       <xsl:attribute name="foo:attr2">2</xsl:attribute>
7f561c08de6b Initial load
duke
parents:
diff changeset
  2827
                 *   </elem1>
7f561c08de6b Initial load
duke
parents:
diff changeset
  2828
                 *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2829
                 * We are adding attr1 and attr2 both as attributes of elem1,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2830
                 * and this code is adding attr2 (the xsl:attribute ).
7f561c08de6b Initial load
duke
parents:
diff changeset
  2831
                 * We could have a collision with the prefix like in the example above.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2832
                 */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2833
7f561c08de6b Initial load
duke
parents:
diff changeset
  2834
                // In the example above, is there a prefix like foo ?
7f561c08de6b Initial load
duke
parents:
diff changeset
  2835
                final int colonIndex = rawName.indexOf(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
  2836
                if (colonIndex > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2837
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2838
                    String prefix = rawName.substring(0,colonIndex);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2839
                    NamespaceMappings.MappingRecord existing_mapping = m_prefixMap.getMappingFromPrefix(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2840
7f561c08de6b Initial load
duke
parents:
diff changeset
  2841
                    /* Before adding this attribute (foo:attr2),
7f561c08de6b Initial load
duke
parents:
diff changeset
  2842
                     * is the prefix for it (foo) already mapped at the current depth?
7f561c08de6b Initial load
duke
parents:
diff changeset
  2843
                     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2844
                    if (existing_mapping != null
7f561c08de6b Initial load
duke
parents:
diff changeset
  2845
                    && existing_mapping.m_declarationDepth == m_elemContext.m_currentElemDepth
7f561c08de6b Initial load
duke
parents:
diff changeset
  2846
                    && !existing_mapping.m_uri.equals(uri))
7f561c08de6b Initial load
duke
parents:
diff changeset
  2847
                    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2848
                        /*
7f561c08de6b Initial load
duke
parents:
diff changeset
  2849
                         * There is an existing mapping of this prefix,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2850
                         * it differs from the one we need,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2851
                         * and unfortunately it is at the current depth so we
7f561c08de6b Initial load
duke
parents:
diff changeset
  2852
                         * can not over-ride it.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2853
                         */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2854
7f561c08de6b Initial load
duke
parents:
diff changeset
  2855
                        /*
7f561c08de6b Initial load
duke
parents:
diff changeset
  2856
                         * Are we lucky enough that an existing other prefix maps to this URI ?
7f561c08de6b Initial load
duke
parents:
diff changeset
  2857
                         */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2858
                        prefix = m_prefixMap.lookupPrefix(uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2859
                        if (prefix == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2860
                        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2861
                            /* Unfortunately there is no existing prefix that happens to map to ours,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2862
                             * so to avoid a prefix collision we must generated a new prefix to use.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2863
                             * This is OK because the prefix URI mapping
7f561c08de6b Initial load
duke
parents:
diff changeset
  2864
                             * defined in the xsl:attribute is short in scope,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2865
                             * just the xsl:attribute element itself,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2866
                             * and at this point in serialization the body of the
7f561c08de6b Initial load
duke
parents:
diff changeset
  2867
                             * xsl:attribute, if any, is just a String. Right?
7f561c08de6b Initial load
duke
parents:
diff changeset
  2868
                             *   . . . I sure hope so - Brian M.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2869
                             */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2870
                            prefix = m_prefixMap.generateNextPrefix();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2871
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2872
7f561c08de6b Initial load
duke
parents:
diff changeset
  2873
                        rawName = prefix + ':' + localName;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2874
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2875
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2876
7f561c08de6b Initial load
duke
parents:
diff changeset
  2877
                try
7f561c08de6b Initial load
duke
parents:
diff changeset
  2878
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2879
                    /* This is our last chance to make sure the namespace for this
7f561c08de6b Initial load
duke
parents:
diff changeset
  2880
                     * attribute is declared, especially if we just generated an alternate
7f561c08de6b Initial load
duke
parents:
diff changeset
  2881
                     * prefix to avoid a collision (the new prefix/rawName will go out of scope
7f561c08de6b Initial load
duke
parents:
diff changeset
  2882
                     * soon and be lost ...  last chance here.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2883
                     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2884
                    String prefixUsed =
7f561c08de6b Initial load
duke
parents:
diff changeset
  2885
                        ensureAttributesNamespaceIsDeclared(
7f561c08de6b Initial load
duke
parents:
diff changeset
  2886
                            uri,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2887
                            localName,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2888
                            rawName);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2889
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2890
                catch (SAXException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2891
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2892
                    // TODO Auto-generated catch block
7f561c08de6b Initial load
duke
parents:
diff changeset
  2893
                    e.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2894
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2895
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2896
            m_attributes.addAttribute(uri, localName, rawName, type, value);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2897
            was_added = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2898
            if (m_tracer != null){
7f561c08de6b Initial load
duke
parents:
diff changeset
  2899
                firePseudoAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2900
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2901
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2902
        return was_added;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2903
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2904
7f561c08de6b Initial load
duke
parents:
diff changeset
  2905
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2906
     * To fire off the pseudo characters of attributes, as they currently
7f561c08de6b Initial load
duke
parents:
diff changeset
  2907
     * exist. This method should be called everytime an attribute is added,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2908
     * or when an attribute value is changed, or an element is created.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2909
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2910
7f561c08de6b Initial load
duke
parents:
diff changeset
  2911
    protected void firePseudoAttributes()
7f561c08de6b Initial load
duke
parents:
diff changeset
  2912
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2913
        if (m_tracer != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2914
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2915
            try
7f561c08de6b Initial load
duke
parents:
diff changeset
  2916
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2917
                // flush out the "<elemName" if not already flushed
7f561c08de6b Initial load
duke
parents:
diff changeset
  2918
                m_writer.flush();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2919
7f561c08de6b Initial load
duke
parents:
diff changeset
  2920
                // make a StringBuffer to write the name="value" pairs to.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2921
                StringBuffer sb = new StringBuffer();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2922
                int nAttrs = m_attributes.getLength();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2923
                if (nAttrs > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2924
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2925
                    // make a writer that internally appends to the same
7f561c08de6b Initial load
duke
parents:
diff changeset
  2926
                    // StringBuffer
7f561c08de6b Initial load
duke
parents:
diff changeset
  2927
                    java.io.Writer writer =
7f561c08de6b Initial load
duke
parents:
diff changeset
  2928
                        new ToStream.WritertoStringBuffer(sb);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2929
7f561c08de6b Initial load
duke
parents:
diff changeset
  2930
                    processAttributes(writer, nAttrs);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2931
                    // Don't clear the attributes!
7f561c08de6b Initial load
duke
parents:
diff changeset
  2932
                    // We only want to see what would be written out
7f561c08de6b Initial load
duke
parents:
diff changeset
  2933
                    // at this point, we don't want to loose them.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2934
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2935
                sb.append('>');  // the potential > after the attributes.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2936
                // convert the StringBuffer to a char array and
7f561c08de6b Initial load
duke
parents:
diff changeset
  2937
                // emit the trace event that these characters "might"
7f561c08de6b Initial load
duke
parents:
diff changeset
  2938
                // be written
7f561c08de6b Initial load
duke
parents:
diff changeset
  2939
                char ch[] = sb.toString().toCharArray();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2940
                m_tracer.fireGenerateEvent(
7f561c08de6b Initial load
duke
parents:
diff changeset
  2941
                    SerializerTrace.EVENTTYPE_OUTPUT_PSEUDO_CHARACTERS,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2942
                    ch,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2943
                    0,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2944
                    ch.length);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2945
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2946
            catch (IOException ioe)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2947
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2948
                // ignore ?
7f561c08de6b Initial load
duke
parents:
diff changeset
  2949
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2950
            catch (SAXException se)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2951
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2952
                // ignore ?
7f561c08de6b Initial load
duke
parents:
diff changeset
  2953
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2954
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2955
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2956
7f561c08de6b Initial load
duke
parents:
diff changeset
  2957
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2958
     * This inner class is used only to collect attribute values
7f561c08de6b Initial load
duke
parents:
diff changeset
  2959
     * written by the method writeAttrString() into a string buffer.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2960
     * In this manner trace events, and the real writing of attributes will use
7f561c08de6b Initial load
duke
parents:
diff changeset
  2961
     * the same code.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2962
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2963
    private class WritertoStringBuffer extends java.io.Writer
7f561c08de6b Initial load
duke
parents:
diff changeset
  2964
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2965
        final private StringBuffer m_stringbuf;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2966
        /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2967
         * @see java.io.Writer#write(char[], int, int)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2968
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2969
        WritertoStringBuffer(StringBuffer sb)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2970
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2971
            m_stringbuf = sb;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2972
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2973
7f561c08de6b Initial load
duke
parents:
diff changeset
  2974
        public void write(char[] arg0, int arg1, int arg2) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2975
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2976
            m_stringbuf.append(arg0, arg1, arg2);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2977
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2978
        /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2979
         * @see java.io.Writer#flush()
7f561c08de6b Initial load
duke
parents:
diff changeset
  2980
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2981
        public void flush() throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2982
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2983
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2984
        /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2985
         * @see java.io.Writer#close()
7f561c08de6b Initial load
duke
parents:
diff changeset
  2986
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2987
        public void close() throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  2988
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2989
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2990
7f561c08de6b Initial load
duke
parents:
diff changeset
  2991
        public void write(int i)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2992
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2993
            m_stringbuf.append((char) i);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2994
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2995
7f561c08de6b Initial load
duke
parents:
diff changeset
  2996
        public void write(String s)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2997
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2998
            m_stringbuf.append(s);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2999
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3000
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3001
7f561c08de6b Initial load
duke
parents:
diff changeset
  3002
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3003
     * @see SerializationHandler#setTransformer(Transformer)
7f561c08de6b Initial load
duke
parents:
diff changeset
  3004
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3005
    public void setTransformer(Transformer transformer) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3006
        super.setTransformer(transformer);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3007
        if (m_tracer != null
7f561c08de6b Initial load
duke
parents:
diff changeset
  3008
         && !(m_writer instanceof SerializerTraceWriter)  )
7f561c08de6b Initial load
duke
parents:
diff changeset
  3009
            m_writer = new SerializerTraceWriter(m_writer, m_tracer);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3010
7f561c08de6b Initial load
duke
parents:
diff changeset
  3011
7f561c08de6b Initial load
duke
parents:
diff changeset
  3012
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3013
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3014
     * Try's to reset the super class and reset this class for
7f561c08de6b Initial load
duke
parents:
diff changeset
  3015
     * re-use, so that you don't need to create a new serializer
7f561c08de6b Initial load
duke
parents:
diff changeset
  3016
     * (mostly for performance reasons).
7f561c08de6b Initial load
duke
parents:
diff changeset
  3017
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3018
     * @return true if the class was successfuly reset.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3019
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3020
    public boolean reset()
7f561c08de6b Initial load
duke
parents:
diff changeset
  3021
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3022
        boolean wasReset = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3023
        if (super.reset())
7f561c08de6b Initial load
duke
parents:
diff changeset
  3024
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3025
            resetToStream();
7f561c08de6b Initial load
duke
parents:
diff changeset
  3026
            wasReset = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3027
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3028
        return wasReset;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3029
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3030
7f561c08de6b Initial load
duke
parents:
diff changeset
  3031
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3032
     * Reset all of the fields owned by ToStream class
7f561c08de6b Initial load
duke
parents:
diff changeset
  3033
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3034
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3035
    private void resetToStream()
7f561c08de6b Initial load
duke
parents:
diff changeset
  3036
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3037
         this.m_cdataStartCalled = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3038
         /* The stream is being reset. It is one of
7f561c08de6b Initial load
duke
parents:
diff changeset
  3039
          * ToXMLStream, ToHTMLStream ... and this type can't be changed
7f561c08de6b Initial load
duke
parents:
diff changeset
  3040
          * so neither should m_charInfo which is associated with the
7f561c08de6b Initial load
duke
parents:
diff changeset
  3041
          * type of Stream. Just leave m_charInfo as-is for the next re-use.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3042
          *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3043
          */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3044
         // this.m_charInfo = null; // don't set to null
7f561c08de6b Initial load
duke
parents:
diff changeset
  3045
7f561c08de6b Initial load
duke
parents:
diff changeset
  3046
         this.m_disableOutputEscapingStates.clear();
7f561c08de6b Initial load
duke
parents:
diff changeset
  3047
7f561c08de6b Initial load
duke
parents:
diff changeset
  3048
         this.m_escaping = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3049
         // Leave m_format alone for now - Brian M.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3050
         // this.m_format = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3051
         this.m_inDoctype = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3052
         this.m_ispreserve = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3053
         this.m_ispreserve = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3054
         this.m_isprevtext = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3055
         this.m_isUTF8 = false; //  ?? used anywhere ??
7f561c08de6b Initial load
duke
parents:
diff changeset
  3056
         this.m_preserves.clear();
7f561c08de6b Initial load
duke
parents:
diff changeset
  3057
         this.m_shouldFlush = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3058
         this.m_spaceBeforeClose = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3059
         this.m_startNewLine = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3060
         this.m_lineSepUse = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3061
         // DON'T SET THE WRITER TO NULL, IT MAY BE REUSED !!
7f561c08de6b Initial load
duke
parents:
diff changeset
  3062
         // this.m_writer = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3063
         this.m_expandDTDEntities = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3064
7f561c08de6b Initial load
duke
parents:
diff changeset
  3065
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3066
7f561c08de6b Initial load
duke
parents:
diff changeset
  3067
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3068
      * Sets the character encoding coming from the xsl:output encoding stylesheet attribute.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3069
      * @param encoding the character encoding
7f561c08de6b Initial load
duke
parents:
diff changeset
  3070
      */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3071
     public void setEncoding(String encoding)
7f561c08de6b Initial load
duke
parents:
diff changeset
  3072
     {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3073
         String old = getEncoding();
7f561c08de6b Initial load
duke
parents:
diff changeset
  3074
         super.setEncoding(encoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3075
         if (old == null || !old.equals(encoding)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3076
            // If we have changed the setting of the
7f561c08de6b Initial load
duke
parents:
diff changeset
  3077
            m_encodingInfo = Encodings.getEncodingInfo(encoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3078
7f561c08de6b Initial load
duke
parents:
diff changeset
  3079
            if (encoding != null && m_encodingInfo.name == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3080
                // We tried to get an EncodingInfo for Object for the given
7f561c08de6b Initial load
duke
parents:
diff changeset
  3081
                // encoding, but it came back with an internall null name
7f561c08de6b Initial load
duke
parents:
diff changeset
  3082
                // so the encoding is not supported by the JDK, issue a message.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3083
                String msg = Utils.messages.createMessage(
7f561c08de6b Initial load
duke
parents:
diff changeset
  3084
                                MsgKey.ER_ENCODING_NOT_SUPPORTED,new Object[]{ encoding });
7f561c08de6b Initial load
duke
parents:
diff changeset
  3085
                try
7f561c08de6b Initial load
duke
parents:
diff changeset
  3086
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3087
                        // Prepare to issue the warning message
7f561c08de6b Initial load
duke
parents:
diff changeset
  3088
                        Transformer tran = super.getTransformer();
7f561c08de6b Initial load
duke
parents:
diff changeset
  3089
                        if (tran != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3090
                                ErrorListener errHandler = tran.getErrorListener();
7f561c08de6b Initial load
duke
parents:
diff changeset
  3091
                                // Issue the warning message
7f561c08de6b Initial load
duke
parents:
diff changeset
  3092
                                if (null != errHandler && m_sourceLocator != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  3093
                                        errHandler.warning(new TransformerException(msg, m_sourceLocator));
7f561c08de6b Initial load
duke
parents:
diff changeset
  3094
                                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  3095
                                        System.out.println(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3096
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3097
                        else
7f561c08de6b Initial load
duke
parents:
diff changeset
  3098
                                System.out.println(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3099
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3100
                catch (Exception e){}
7f561c08de6b Initial load
duke
parents:
diff changeset
  3101
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3102
         }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3103
         return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3104
     }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3105
7f561c08de6b Initial load
duke
parents:
diff changeset
  3106
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3107
     * Simple stack for boolean values.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3108
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3109
     * This class is a copy of the one in com.sun.org.apache.xml.internal.utils.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3110
     * It exists to cut the serializers dependancy on that package.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3111
     * A minor changes from that package are:
7f561c08de6b Initial load
duke
parents:
diff changeset
  3112
     * doesn't implement Clonable
7f561c08de6b Initial load
duke
parents:
diff changeset
  3113
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3114
     * @xsl.usage internal
7f561c08de6b Initial load
duke
parents:
diff changeset
  3115
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3116
    static final class BoolStack
7f561c08de6b Initial load
duke
parents:
diff changeset
  3117
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3118
7f561c08de6b Initial load
duke
parents:
diff changeset
  3119
      /** Array of boolean values          */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3120
      private boolean m_values[];
7f561c08de6b Initial load
duke
parents:
diff changeset
  3121
7f561c08de6b Initial load
duke
parents:
diff changeset
  3122
      /** Array size allocated           */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3123
      private int m_allocatedSize;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3124
7f561c08de6b Initial load
duke
parents:
diff changeset
  3125
      /** Index into the array of booleans          */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3126
      private int m_index;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3127
7f561c08de6b Initial load
duke
parents:
diff changeset
  3128
      /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3129
       * Default constructor.  Note that the default
7f561c08de6b Initial load
duke
parents:
diff changeset
  3130
       * block size is very small, for small lists.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3131
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3132
      public BoolStack()
7f561c08de6b Initial load
duke
parents:
diff changeset
  3133
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3134
        this(32);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3135
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3136
7f561c08de6b Initial load
duke
parents:
diff changeset
  3137
      /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3138
       * Construct a IntVector, using the given block size.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3139
       *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3140
       * @param size array size to allocate
7f561c08de6b Initial load
duke
parents:
diff changeset
  3141
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3142
      public BoolStack(int size)
7f561c08de6b Initial load
duke
parents:
diff changeset
  3143
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3144
7f561c08de6b Initial load
duke
parents:
diff changeset
  3145
        m_allocatedSize = size;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3146
        m_values = new boolean[size];
7f561c08de6b Initial load
duke
parents:
diff changeset
  3147
        m_index = -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3148
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3149
7f561c08de6b Initial load
duke
parents:
diff changeset
  3150
      /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3151
       * Get the length of the list.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3152
       *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3153
       * @return Current length of the list
7f561c08de6b Initial load
duke
parents:
diff changeset
  3154
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3155
      public final int size()
7f561c08de6b Initial load
duke
parents:
diff changeset
  3156
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3157
        return m_index + 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3158
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3159
7f561c08de6b Initial load
duke
parents:
diff changeset
  3160
      /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3161
       * Clears the stack.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3162
       *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3163
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3164
      public final void clear()
7f561c08de6b Initial load
duke
parents:
diff changeset
  3165
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3166
        m_index = -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3167
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3168
7f561c08de6b Initial load
duke
parents:
diff changeset
  3169
      /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3170
       * Pushes an item onto the top of this stack.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3171
       *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3172
       *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3173
       * @param val the boolean to be pushed onto this stack.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3174
       * @return  the <code>item</code> argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3175
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3176
      public final boolean push(boolean val)
7f561c08de6b Initial load
duke
parents:
diff changeset
  3177
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3178
7f561c08de6b Initial load
duke
parents:
diff changeset
  3179
        if (m_index == m_allocatedSize - 1)
7f561c08de6b Initial load
duke
parents:
diff changeset
  3180
          grow();
7f561c08de6b Initial load
duke
parents:
diff changeset
  3181
7f561c08de6b Initial load
duke
parents:
diff changeset
  3182
        return (m_values[++m_index] = val);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3183
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3184
7f561c08de6b Initial load
duke
parents:
diff changeset
  3185
      /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3186
       * Removes the object at the top of this stack and returns that
7f561c08de6b Initial load
duke
parents:
diff changeset
  3187
       * object as the value of this function.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3188
       *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3189
       * @return     The object at the top of this stack.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3190
       * @throws  EmptyStackException  if this stack is empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3191
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3192
      public final boolean pop()
7f561c08de6b Initial load
duke
parents:
diff changeset
  3193
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3194
        return m_values[m_index--];
7f561c08de6b Initial load
duke
parents:
diff changeset
  3195
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3196
7f561c08de6b Initial load
duke
parents:
diff changeset
  3197
      /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3198
       * Removes the object at the top of this stack and returns the
7f561c08de6b Initial load
duke
parents:
diff changeset
  3199
       * next object at the top as the value of this function.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3200
       *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3201
       *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3202
       * @return Next object to the top or false if none there
7f561c08de6b Initial load
duke
parents:
diff changeset
  3203
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3204
      public final boolean popAndTop()
7f561c08de6b Initial load
duke
parents:
diff changeset
  3205
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3206
7f561c08de6b Initial load
duke
parents:
diff changeset
  3207
        m_index--;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3208
7f561c08de6b Initial load
duke
parents:
diff changeset
  3209
        return (m_index >= 0) ? m_values[m_index] : false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3210
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3211
7f561c08de6b Initial load
duke
parents:
diff changeset
  3212
      /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3213
       * Set the item at the top of this stack
7f561c08de6b Initial load
duke
parents:
diff changeset
  3214
       *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3215
       *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3216
       * @param b Object to set at the top of this stack
7f561c08de6b Initial load
duke
parents:
diff changeset
  3217
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3218
      public final void setTop(boolean b)
7f561c08de6b Initial load
duke
parents:
diff changeset
  3219
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3220
        m_values[m_index] = b;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3221
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3222
7f561c08de6b Initial load
duke
parents:
diff changeset
  3223
      /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3224
       * Looks at the object at the top of this stack without removing it
7f561c08de6b Initial load
duke
parents:
diff changeset
  3225
       * from the stack.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3226
       *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3227
       * @return     the object at the top of this stack.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3228
       * @throws  EmptyStackException  if this stack is empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3229
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3230
      public final boolean peek()
7f561c08de6b Initial load
duke
parents:
diff changeset
  3231
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3232
        return m_values[m_index];
7f561c08de6b Initial load
duke
parents:
diff changeset
  3233
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3234
7f561c08de6b Initial load
duke
parents:
diff changeset
  3235
      /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3236
       * Looks at the object at the top of this stack without removing it
7f561c08de6b Initial load
duke
parents:
diff changeset
  3237
       * from the stack.  If the stack is empty, it returns false.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3238
       *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3239
       * @return     the object at the top of this stack.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3240
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3241
      public final boolean peekOrFalse()
7f561c08de6b Initial load
duke
parents:
diff changeset
  3242
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3243
        return (m_index > -1) ? m_values[m_index] : false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3244
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3245
7f561c08de6b Initial load
duke
parents:
diff changeset
  3246
      /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3247
       * Looks at the object at the top of this stack without removing it
7f561c08de6b Initial load
duke
parents:
diff changeset
  3248
       * from the stack.  If the stack is empty, it returns true.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3249
       *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3250
       * @return     the object at the top of this stack.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3251
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3252
      public final boolean peekOrTrue()
7f561c08de6b Initial load
duke
parents:
diff changeset
  3253
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3254
        return (m_index > -1) ? m_values[m_index] : true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3255
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3256
7f561c08de6b Initial load
duke
parents:
diff changeset
  3257
      /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3258
       * Tests if this stack is empty.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3259
       *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3260
       * @return  <code>true</code> if this stack is empty;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3261
       *          <code>false</code> otherwise.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3262
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3263
      public boolean isEmpty()
7f561c08de6b Initial load
duke
parents:
diff changeset
  3264
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3265
        return (m_index == -1);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3266
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3267
7f561c08de6b Initial load
duke
parents:
diff changeset
  3268
      /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3269
       * Grows the size of the stack
7f561c08de6b Initial load
duke
parents:
diff changeset
  3270
       *
7f561c08de6b Initial load
duke
parents:
diff changeset
  3271
       */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3272
      private void grow()
7f561c08de6b Initial load
duke
parents:
diff changeset
  3273
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3274
7f561c08de6b Initial load
duke
parents:
diff changeset
  3275
        m_allocatedSize *= 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3276
7f561c08de6b Initial load
duke
parents:
diff changeset
  3277
        boolean newVector[] = new boolean[m_allocatedSize];
7f561c08de6b Initial load
duke
parents:
diff changeset
  3278
7f561c08de6b Initial load
duke
parents:
diff changeset
  3279
        System.arraycopy(m_values, 0, newVector, 0, m_index + 1);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3280
7f561c08de6b Initial load
duke
parents:
diff changeset
  3281
        m_values = newVector;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3282
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3283
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3284
7f561c08de6b Initial load
duke
parents:
diff changeset
  3285
    // Implement DTDHandler
7f561c08de6b Initial load
duke
parents:
diff changeset
  3286
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3287
     * If this method is called, the serializer is used as a
7f561c08de6b Initial load
duke
parents:
diff changeset
  3288
     * DTDHandler, which changes behavior how the serializer
7f561c08de6b Initial load
duke
parents:
diff changeset
  3289
     * handles document entities.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3290
     * @see org.xml.sax.DTDHandler#notationDecl(java.lang.String, java.lang.String, java.lang.String)
7f561c08de6b Initial load
duke
parents:
diff changeset
  3291
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3292
    public void notationDecl(String name, String pubID, String sysID) throws SAXException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3293
        // TODO Auto-generated method stub
7f561c08de6b Initial load
duke
parents:
diff changeset
  3294
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3295
            DTDprolog();
7f561c08de6b Initial load
duke
parents:
diff changeset
  3296
7f561c08de6b Initial load
duke
parents:
diff changeset
  3297
            m_writer.write("<!NOTATION ");
7f561c08de6b Initial load
duke
parents:
diff changeset
  3298
            m_writer.write(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3299
            if (pubID != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3300
                m_writer.write(" PUBLIC \"");
7f561c08de6b Initial load
duke
parents:
diff changeset
  3301
                m_writer.write(pubID);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3302
7f561c08de6b Initial load
duke
parents:
diff changeset
  3303
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3304
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3305
                m_writer.write(" SYSTEM \"");
7f561c08de6b Initial load
duke
parents:
diff changeset
  3306
                m_writer.write(sysID);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3307
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3308
            m_writer.write("\" >");
7f561c08de6b Initial load
duke
parents:
diff changeset
  3309
            m_writer.write(m_lineSep, 0, m_lineSepLen);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3310
        } catch (IOException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3311
            // TODO Auto-generated catch block
7f561c08de6b Initial load
duke
parents:
diff changeset
  3312
            e.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
  3313
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3314
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3315
7f561c08de6b Initial load
duke
parents:
diff changeset
  3316
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3317
     * If this method is called, the serializer is used as a
7f561c08de6b Initial load
duke
parents:
diff changeset
  3318
     * DTDHandler, which changes behavior how the serializer
7f561c08de6b Initial load
duke
parents:
diff changeset
  3319
     * handles document entities.
7f561c08de6b Initial load
duke
parents:
diff changeset
  3320
     * @see org.xml.sax.DTDHandler#unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
7f561c08de6b Initial load
duke
parents:
diff changeset
  3321
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3322
    public void unparsedEntityDecl(String name, String pubID, String sysID, String notationName) throws SAXException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3323
        // TODO Auto-generated method stub
7f561c08de6b Initial load
duke
parents:
diff changeset
  3324
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3325
            DTDprolog();
7f561c08de6b Initial load
duke
parents:
diff changeset
  3326
7f561c08de6b Initial load
duke
parents:
diff changeset
  3327
            m_writer.write("<!ENTITY ");
7f561c08de6b Initial load
duke
parents:
diff changeset
  3328
            m_writer.write(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3329
            if (pubID != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3330
                m_writer.write(" PUBLIC \"");
7f561c08de6b Initial load
duke
parents:
diff changeset
  3331
                m_writer.write(pubID);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3332
7f561c08de6b Initial load
duke
parents:
diff changeset
  3333
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3334
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3335
                m_writer.write(" SYSTEM \"");
7f561c08de6b Initial load
duke
parents:
diff changeset
  3336
                m_writer.write(sysID);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3337
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3338
            m_writer.write("\" NDATA ");
7f561c08de6b Initial load
duke
parents:
diff changeset
  3339
            m_writer.write(notationName);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3340
            m_writer.write(" >");
7f561c08de6b Initial load
duke
parents:
diff changeset
  3341
            m_writer.write(m_lineSep, 0, m_lineSepLen);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3342
        } catch (IOException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3343
            // TODO Auto-generated catch block
7f561c08de6b Initial load
duke
parents:
diff changeset
  3344
            e.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
  3345
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3346
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3347
7f561c08de6b Initial load
duke
parents:
diff changeset
  3348
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3349
     * A private helper method to output the
7f561c08de6b Initial load
duke
parents:
diff changeset
  3350
     * @throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
  3351
     * @throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  3352
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3353
    private void DTDprolog() throws SAXException, IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3354
        final java.io.Writer writer = m_writer;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3355
        if (m_needToOutputDocTypeDecl)
7f561c08de6b Initial load
duke
parents:
diff changeset
  3356
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3357
            outputDocTypeDecl(m_elemContext.m_elementName, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3358
            m_needToOutputDocTypeDecl = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3359
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3360
        if (m_inDoctype)
7f561c08de6b Initial load
duke
parents:
diff changeset
  3361
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3362
            writer.write(" [");
7f561c08de6b Initial load
duke
parents:
diff changeset
  3363
            writer.write(m_lineSep, 0, m_lineSepLen);
7f561c08de6b Initial load
duke
parents:
diff changeset
  3364
            m_inDoctype = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3365
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3366
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3367
7f561c08de6b Initial load
duke
parents:
diff changeset
  3368
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  3369
     * If set to false the serializer does not expand DTD entities,
7f561c08de6b Initial load
duke
parents:
diff changeset
  3370
     * but leaves them as is, the default value is true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3371
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  3372
    public void setDTDEntityExpansion(boolean expand) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  3373
        m_expandDTDEntities = expand;
7f561c08de6b Initial load
duke
parents:
diff changeset
  3374
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  3375
}