Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.stream.buffer; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.xml.internal.stream.buffer.sax.Properties; |
aoqi@0 | 29 | import com.sun.xml.internal.stream.buffer.sax.SAXBufferCreator; |
aoqi@0 | 30 | import com.sun.xml.internal.stream.buffer.stax.StreamReaderBufferCreator; |
aoqi@0 | 31 | import com.sun.xml.internal.stream.buffer.stax.StreamWriterBufferCreator; |
aoqi@0 | 32 | import org.xml.sax.ContentHandler; |
aoqi@0 | 33 | import org.xml.sax.SAXException; |
aoqi@0 | 34 | import org.xml.sax.XMLReader; |
aoqi@0 | 35 | |
aoqi@0 | 36 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 37 | import javax.xml.stream.XMLStreamReader; |
aoqi@0 | 38 | import javax.xml.stream.XMLStreamWriter; |
aoqi@0 | 39 | import java.io.IOException; |
aoqi@0 | 40 | import java.io.InputStream; |
aoqi@0 | 41 | |
aoqi@0 | 42 | /** |
aoqi@0 | 43 | * |
aoqi@0 | 44 | * A mutable stream-based buffer of an XML infoset. |
aoqi@0 | 45 | * |
aoqi@0 | 46 | * <p> |
aoqi@0 | 47 | * A MutableXMLStreamBuffer is created using specific SAX and StAX-based |
aoqi@0 | 48 | * creators. Utility methods on MutableXMLStreamBuffer are provided for |
aoqi@0 | 49 | * such functionality that utilize SAX and StAX-based creators. |
aoqi@0 | 50 | * |
aoqi@0 | 51 | * <p> |
aoqi@0 | 52 | * Once instantiated the same instance of a MutableXMLStreamBuffer may be reused for |
aoqi@0 | 53 | * creation to reduce the amount of Objects instantiated and garbage |
aoqi@0 | 54 | * collected that are required for internally representing an XML infoset. |
aoqi@0 | 55 | * |
aoqi@0 | 56 | * <p> |
aoqi@0 | 57 | * A MutableXMLStreamBuffer is not designed to be created and processed |
aoqi@0 | 58 | * concurrently. If done so unspecified behaviour may occur. |
aoqi@0 | 59 | */ |
aoqi@0 | 60 | public class MutableXMLStreamBuffer extends XMLStreamBuffer { |
aoqi@0 | 61 | /** |
aoqi@0 | 62 | * The default array size for the arrays used in internal representation |
aoqi@0 | 63 | * of the XML infoset. |
aoqi@0 | 64 | */ |
aoqi@0 | 65 | public static final int DEFAULT_ARRAY_SIZE = 512; |
aoqi@0 | 66 | |
aoqi@0 | 67 | /** |
aoqi@0 | 68 | * Create a new MutableXMLStreamBuffer using the |
aoqi@0 | 69 | * {@link MutableXMLStreamBuffer#DEFAULT_ARRAY_SIZE}. |
aoqi@0 | 70 | */ |
aoqi@0 | 71 | public MutableXMLStreamBuffer() { |
aoqi@0 | 72 | this(DEFAULT_ARRAY_SIZE); |
aoqi@0 | 73 | } |
aoqi@0 | 74 | |
aoqi@0 | 75 | /** |
aoqi@0 | 76 | * Set the system identifier for this buffer. |
aoqi@0 | 77 | * @param systemId The system identifier. |
aoqi@0 | 78 | */ |
aoqi@0 | 79 | public void setSystemId(String systemId) { |
aoqi@0 | 80 | this.systemId = systemId; |
aoqi@0 | 81 | } |
aoqi@0 | 82 | |
aoqi@0 | 83 | /** |
aoqi@0 | 84 | * Create a new MutableXMLStreamBuffer. |
aoqi@0 | 85 | * |
aoqi@0 | 86 | * @param size |
aoqi@0 | 87 | * The size of the arrays used in the internal representation |
aoqi@0 | 88 | * of the XML infoset. |
aoqi@0 | 89 | * @throws NegativeArraySizeException |
aoqi@0 | 90 | * If the <code>size</code> argument is less than <code>0</code>. |
aoqi@0 | 91 | */ |
aoqi@0 | 92 | public MutableXMLStreamBuffer(int size) { |
aoqi@0 | 93 | _structure = new FragmentedArray<byte[]>(new byte[size]); |
aoqi@0 | 94 | _structureStrings = new FragmentedArray<String[]>(new String[size]); |
aoqi@0 | 95 | _contentCharactersBuffer = new FragmentedArray<char[]>(new char[4096]); |
aoqi@0 | 96 | _contentObjects = new FragmentedArray<Object[]>(new Object[size]); |
aoqi@0 | 97 | |
aoqi@0 | 98 | // Set the first element of structure array to indicate an empty buffer |
aoqi@0 | 99 | // that has not been created |
aoqi@0 | 100 | _structure.getArray()[0] = (byte) AbstractCreatorProcessor.T_END; |
aoqi@0 | 101 | } |
aoqi@0 | 102 | |
aoqi@0 | 103 | /** |
aoqi@0 | 104 | * Create contents of a buffer from a XMLStreamReader. |
aoqi@0 | 105 | * |
aoqi@0 | 106 | * <p> |
aoqi@0 | 107 | * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation. |
aoqi@0 | 108 | * |
aoqi@0 | 109 | * <p> |
aoqi@0 | 110 | * The MutableXMLStreamBuffer is created by consuming the events on the XMLStreamReader using |
aoqi@0 | 111 | * an instance of {@link StreamReaderBufferCreator}. |
aoqi@0 | 112 | * |
aoqi@0 | 113 | * @param reader |
aoqi@0 | 114 | * A XMLStreamReader to read from to create. |
aoqi@0 | 115 | */ |
aoqi@0 | 116 | public void createFromXMLStreamReader(XMLStreamReader reader) throws XMLStreamException { |
aoqi@0 | 117 | reset(); |
aoqi@0 | 118 | StreamReaderBufferCreator c = new StreamReaderBufferCreator(this); |
aoqi@0 | 119 | c.create(reader); |
aoqi@0 | 120 | } |
aoqi@0 | 121 | |
aoqi@0 | 122 | /** |
aoqi@0 | 123 | * Create contents of a buffer from a XMLStreamWriter. |
aoqi@0 | 124 | * |
aoqi@0 | 125 | * <p> |
aoqi@0 | 126 | * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation. |
aoqi@0 | 127 | * |
aoqi@0 | 128 | * <p> |
aoqi@0 | 129 | * The MutableXMLStreamBuffer is created by consuming events on a XMLStreamWriter using |
aoqi@0 | 130 | * an instance of {@link StreamWriterBufferCreator}. |
aoqi@0 | 131 | */ |
aoqi@0 | 132 | public XMLStreamWriter createFromXMLStreamWriter() { |
aoqi@0 | 133 | reset(); |
aoqi@0 | 134 | return new StreamWriterBufferCreator(this); |
aoqi@0 | 135 | } |
aoqi@0 | 136 | |
aoqi@0 | 137 | /** |
aoqi@0 | 138 | * Create contents of a buffer from a {@link SAXBufferCreator}. |
aoqi@0 | 139 | * |
aoqi@0 | 140 | * <p> |
aoqi@0 | 141 | * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation. |
aoqi@0 | 142 | * |
aoqi@0 | 143 | * <p> |
aoqi@0 | 144 | * The MutableXMLStreamBuffer is created by consuming events from a {@link ContentHandler} using |
aoqi@0 | 145 | * an instance of {@link SAXBufferCreator}. |
aoqi@0 | 146 | * |
aoqi@0 | 147 | * @return The {@link SAXBufferCreator} to create from. |
aoqi@0 | 148 | */ |
aoqi@0 | 149 | public SAXBufferCreator createFromSAXBufferCreator() { |
aoqi@0 | 150 | reset(); |
aoqi@0 | 151 | SAXBufferCreator c = new SAXBufferCreator(); |
aoqi@0 | 152 | c.setBuffer(this); |
aoqi@0 | 153 | return c; |
aoqi@0 | 154 | } |
aoqi@0 | 155 | |
aoqi@0 | 156 | /** |
aoqi@0 | 157 | * Create contents of a buffer from a {@link XMLReader} and {@link InputStream}. |
aoqi@0 | 158 | * |
aoqi@0 | 159 | * <p> |
aoqi@0 | 160 | * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation. |
aoqi@0 | 161 | * |
aoqi@0 | 162 | * <p> |
aoqi@0 | 163 | * The MutableXMLStreamBuffer is created by using an instance of {@link SAXBufferCreator} |
aoqi@0 | 164 | * and registering associated handlers on the {@link XMLReader}. |
aoqi@0 | 165 | * |
aoqi@0 | 166 | * @param reader |
aoqi@0 | 167 | * The {@link XMLReader} to use for parsing. |
aoqi@0 | 168 | * @param in |
aoqi@0 | 169 | * The {@link InputStream} to be parsed. |
aoqi@0 | 170 | */ |
aoqi@0 | 171 | public void createFromXMLReader(XMLReader reader, InputStream in) throws SAXException, IOException { |
aoqi@0 | 172 | createFromXMLReader(reader, in, null); |
aoqi@0 | 173 | } |
aoqi@0 | 174 | |
aoqi@0 | 175 | /** |
aoqi@0 | 176 | * Create contents of a buffer from a {@link XMLReader} and {@link InputStream}. |
aoqi@0 | 177 | * |
aoqi@0 | 178 | * <p> |
aoqi@0 | 179 | * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation. |
aoqi@0 | 180 | * |
aoqi@0 | 181 | * <p> |
aoqi@0 | 182 | * The MutableXMLStreamBuffer is created by using an instance of {@link SAXBufferCreator} |
aoqi@0 | 183 | * and registering associated handlers on the {@link XMLReader}. |
aoqi@0 | 184 | * |
aoqi@0 | 185 | * @param reader |
aoqi@0 | 186 | * The {@link XMLReader} to use for parsing. |
aoqi@0 | 187 | * @param in |
aoqi@0 | 188 | * The {@link InputStream} to be parsed. |
aoqi@0 | 189 | * @param systemId |
aoqi@0 | 190 | * The system ID of the input stream. |
aoqi@0 | 191 | */ |
aoqi@0 | 192 | public void createFromXMLReader(XMLReader reader, InputStream in, String systemId) throws SAXException, IOException { |
aoqi@0 | 193 | reset(); |
aoqi@0 | 194 | SAXBufferCreator c = new SAXBufferCreator(this); |
aoqi@0 | 195 | |
aoqi@0 | 196 | reader.setContentHandler(c); |
aoqi@0 | 197 | reader.setDTDHandler(c); |
aoqi@0 | 198 | reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, c); |
aoqi@0 | 199 | |
aoqi@0 | 200 | c.create(reader, in, systemId); |
aoqi@0 | 201 | } |
aoqi@0 | 202 | |
aoqi@0 | 203 | /** |
aoqi@0 | 204 | * Reset the MutableXMLStreamBuffer. |
aoqi@0 | 205 | * |
aoqi@0 | 206 | * <p> |
aoqi@0 | 207 | * This method will reset the MutableXMLStreamBuffer to a state of being "uncreated" |
aoqi@0 | 208 | * similar to the state of a newly instantiated MutableXMLStreamBuffer. |
aoqi@0 | 209 | * |
aoqi@0 | 210 | * <p> |
aoqi@0 | 211 | * As many Objects as possible will be retained for reuse in future creation. |
aoqi@0 | 212 | */ |
aoqi@0 | 213 | public void reset() { |
aoqi@0 | 214 | // Reset the ptrs in arrays to 0 |
aoqi@0 | 215 | _structurePtr = |
aoqi@0 | 216 | _structureStringsPtr = |
aoqi@0 | 217 | _contentCharactersBufferPtr = |
aoqi@0 | 218 | _contentObjectsPtr = 0; |
aoqi@0 | 219 | |
aoqi@0 | 220 | // Set the first element of structure array to indicate an empty buffer |
aoqi@0 | 221 | // that has not been created |
aoqi@0 | 222 | _structure.getArray()[0] = (byte)AbstractCreatorProcessor.T_END; |
aoqi@0 | 223 | |
aoqi@0 | 224 | // Clean up content objects |
aoqi@0 | 225 | _contentObjects.setNext(null); |
aoqi@0 | 226 | final Object[] o = _contentObjects.getArray(); |
aoqi@0 | 227 | for (int i = 0; i < o.length; i++) { |
aoqi@0 | 228 | if (o[i] != null) { |
aoqi@0 | 229 | o[i] = null; |
aoqi@0 | 230 | } else { |
aoqi@0 | 231 | break; |
aoqi@0 | 232 | } |
aoqi@0 | 233 | } |
aoqi@0 | 234 | |
aoqi@0 | 235 | treeCount = 0; |
aoqi@0 | 236 | |
aoqi@0 | 237 | /* |
aoqi@0 | 238 | * TODO consider truncating the size of _structureStrings and |
aoqi@0 | 239 | * _contentCharactersBuffer to limit the memory used by the buffer |
aoqi@0 | 240 | */ |
aoqi@0 | 241 | } |
aoqi@0 | 242 | |
aoqi@0 | 243 | |
aoqi@0 | 244 | protected void setHasInternedStrings(boolean hasInternedStrings) { |
aoqi@0 | 245 | _hasInternedStrings = hasInternedStrings; |
aoqi@0 | 246 | } |
aoqi@0 | 247 | } |