Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2005, 2014, 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.SAXBufferProcessor; |
aoqi@0 | 29 | import com.sun.xml.internal.stream.buffer.stax.StreamReaderBufferProcessor; |
aoqi@0 | 30 | import com.sun.xml.internal.stream.buffer.stax.StreamWriterBufferProcessor; |
aoqi@0 | 31 | import java.io.IOException; |
aoqi@0 | 32 | import java.io.InputStream; |
aoqi@0 | 33 | import java.util.Collections; |
aoqi@0 | 34 | import java.util.Map; |
aoqi@0 | 35 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 36 | import javax.xml.stream.XMLStreamReader; |
aoqi@0 | 37 | import javax.xml.stream.XMLStreamWriter; |
aoqi@0 | 38 | import javax.xml.transform.TransformerFactory; |
aoqi@0 | 39 | import javax.xml.transform.Transformer; |
aoqi@0 | 40 | import javax.xml.transform.TransformerException; |
aoqi@0 | 41 | import javax.xml.transform.dom.DOMResult; |
aoqi@0 | 42 | |
aoqi@0 | 43 | import org.xml.sax.ContentHandler; |
aoqi@0 | 44 | import org.xml.sax.DTDHandler; |
aoqi@0 | 45 | import org.xml.sax.ErrorHandler; |
aoqi@0 | 46 | import org.xml.sax.SAXException; |
aoqi@0 | 47 | import org.xml.sax.XMLReader; |
aoqi@0 | 48 | import org.xml.sax.ext.LexicalHandler; |
aoqi@0 | 49 | import org.w3c.dom.Node; |
aoqi@0 | 50 | |
aoqi@0 | 51 | /** |
aoqi@0 | 52 | * An immutable stream-based buffer of an XML infoset. |
aoqi@0 | 53 | * |
aoqi@0 | 54 | * <p> |
aoqi@0 | 55 | * A XMLStreamBuffer is an abstract class. It is immutable with |
aoqi@0 | 56 | * respect to the methods on the class, which are non-modifying in terms |
aoqi@0 | 57 | * of state. |
aoqi@0 | 58 | * |
aoqi@0 | 59 | * <p> |
aoqi@0 | 60 | * A XMLStreamBuffer can be processed using specific SAX and StAX-based |
aoqi@0 | 61 | * processors. Utility methods on XMLStreamBuffer are provided for |
aoqi@0 | 62 | * such functionality that utilize SAX and StAX-based processors. |
aoqi@0 | 63 | * The same instance of a XMLStreamBuffer may be processed |
aoqi@0 | 64 | * multiple times and concurrently by more than one processor. |
aoqi@0 | 65 | * |
aoqi@0 | 66 | * <p> |
aoqi@0 | 67 | * There are two concrete implementations of XMLStreamBuffer. |
aoqi@0 | 68 | * The first, {@link MutableXMLStreamBuffer}, can be instantiated for the creation |
aoqi@0 | 69 | * of a buffer using SAX and StAX-based creators, and from which may be |
aoqi@0 | 70 | * processed as an XMLStreamBuffer. The second, |
aoqi@0 | 71 | * {@link XMLStreamBufferMark}, can be instantiated to mark into an existing |
aoqi@0 | 72 | * buffer that is being created or processed. This allows a subtree of |
aoqi@0 | 73 | * {@link XMLStreamBuffer} to be treated as its own {@link XMLStreamBuffer}. |
aoqi@0 | 74 | * |
aoqi@0 | 75 | * <p> |
aoqi@0 | 76 | * A XMLStreamBuffer can represent a complete XML infoset or a subtree |
aoqi@0 | 77 | * of an XML infoset. It is also capable of representing a "forest", |
aoqi@0 | 78 | * where the buffer represents multiple adjacent XML elements, although |
aoqi@0 | 79 | * in this mode there are restrictions about how you can consume such |
aoqi@0 | 80 | * forest, because not all XML APIs handle forests very well. |
aoqi@0 | 81 | */ |
aoqi@0 | 82 | public abstract class XMLStreamBuffer { |
aoqi@0 | 83 | |
aoqi@0 | 84 | /** |
aoqi@0 | 85 | * In scope namespaces on a fragment |
aoqi@0 | 86 | */ |
aoqi@0 | 87 | protected Map<String,String> _inscopeNamespaces = Collections.emptyMap(); |
aoqi@0 | 88 | |
aoqi@0 | 89 | /** |
aoqi@0 | 90 | * True if the buffer was created from a parser that interns Strings |
aoqi@0 | 91 | * as specified by the SAX interning features |
aoqi@0 | 92 | */ |
aoqi@0 | 93 | protected boolean _hasInternedStrings; |
aoqi@0 | 94 | |
aoqi@0 | 95 | /** |
aoqi@0 | 96 | * Fragmented array to hold structural information |
aoqi@0 | 97 | */ |
aoqi@0 | 98 | protected FragmentedArray<byte[]> _structure; |
aoqi@0 | 99 | protected int _structurePtr; |
aoqi@0 | 100 | |
aoqi@0 | 101 | /** |
aoqi@0 | 102 | * Fragmented array to hold structural information as strings |
aoqi@0 | 103 | */ |
aoqi@0 | 104 | protected FragmentedArray<String[]> _structureStrings; |
aoqi@0 | 105 | protected int _structureStringsPtr; |
aoqi@0 | 106 | |
aoqi@0 | 107 | /** |
aoqi@0 | 108 | * Fragmented array to hold content information in a shared char[] |
aoqi@0 | 109 | */ |
aoqi@0 | 110 | protected FragmentedArray<char[]> _contentCharactersBuffer; |
aoqi@0 | 111 | protected int _contentCharactersBufferPtr; |
aoqi@0 | 112 | |
aoqi@0 | 113 | /** |
aoqi@0 | 114 | * Fragmented array to hold content information as objects |
aoqi@0 | 115 | */ |
aoqi@0 | 116 | protected FragmentedArray<Object[]> _contentObjects; |
aoqi@0 | 117 | protected int _contentObjectsPtr; |
aoqi@0 | 118 | |
aoqi@0 | 119 | /** |
aoqi@0 | 120 | * Number of trees in this stream buffer. |
aoqi@0 | 121 | * |
aoqi@0 | 122 | * <p> |
aoqi@0 | 123 | * 1 if there's only one, which is the normal case. When the buffer |
aoqi@0 | 124 | * holds a forest, this value is greater than 1. If the buffer is empty, then 0. |
aoqi@0 | 125 | * |
aoqi@0 | 126 | * <p> |
aoqi@0 | 127 | * Notice that we cannot infer this value by looking at the {@link FragmentedArray}s, |
aoqi@0 | 128 | * because this {@link XMLStreamBuffer} maybe a view of a portion of another bigger |
aoqi@0 | 129 | * {@link XMLStreamBuffer}. |
aoqi@0 | 130 | */ |
aoqi@0 | 131 | protected int treeCount; |
aoqi@0 | 132 | |
aoqi@0 | 133 | /** |
aoqi@0 | 134 | * The system identifier associated with the buffer |
aoqi@0 | 135 | */ |
aoqi@0 | 136 | protected String systemId; |
aoqi@0 | 137 | |
aoqi@0 | 138 | /** |
aoqi@0 | 139 | * Is the buffer created by creator. |
aoqi@0 | 140 | * |
aoqi@0 | 141 | * @return |
aoqi@0 | 142 | * <code>true</code> if the buffer has been created. |
aoqi@0 | 143 | */ |
aoqi@0 | 144 | public final boolean isCreated() { |
aoqi@0 | 145 | return _structure.getArray()[0] != AbstractCreatorProcessor.T_END; |
aoqi@0 | 146 | } |
aoqi@0 | 147 | |
aoqi@0 | 148 | /** |
aoqi@0 | 149 | * Is the buffer a representation of a fragment of an XML infoset. |
aoqi@0 | 150 | * |
aoqi@0 | 151 | * @return |
aoqi@0 | 152 | * <code>true</code> if the buffer is a representation of a fragment |
aoqi@0 | 153 | * of an XML infoset. |
aoqi@0 | 154 | */ |
aoqi@0 | 155 | public final boolean isFragment() { |
aoqi@0 | 156 | return (isCreated() && (_structure.getArray()[_structurePtr] & AbstractCreatorProcessor.TYPE_MASK) |
aoqi@0 | 157 | != AbstractCreatorProcessor.T_DOCUMENT); |
aoqi@0 | 158 | } |
aoqi@0 | 159 | |
aoqi@0 | 160 | /** |
aoqi@0 | 161 | * Is the buffer a representation of a fragment of an XML infoset |
aoqi@0 | 162 | * that is an element (and its contents). |
aoqi@0 | 163 | * |
aoqi@0 | 164 | * @return |
aoqi@0 | 165 | * <code>true</code> if the buffer a representation |
aoqi@0 | 166 | * of a fragment of an XML infoset that is an element (and its contents). |
aoqi@0 | 167 | */ |
aoqi@0 | 168 | public final boolean isElementFragment() { |
aoqi@0 | 169 | return (isCreated() && (_structure.getArray()[_structurePtr] & AbstractCreatorProcessor.TYPE_MASK) |
aoqi@0 | 170 | == AbstractCreatorProcessor.T_ELEMENT); |
aoqi@0 | 171 | } |
aoqi@0 | 172 | |
aoqi@0 | 173 | /** |
aoqi@0 | 174 | * Returns ture if this buffer represents a forest, which is |
aoqi@0 | 175 | * are more than one adjacent XML elements. |
aoqi@0 | 176 | */ |
aoqi@0 | 177 | public final boolean isForest() { |
aoqi@0 | 178 | return isCreated() && treeCount>1; |
aoqi@0 | 179 | } |
aoqi@0 | 180 | |
aoqi@0 | 181 | /** |
aoqi@0 | 182 | * Get the system identifier associated with the buffer. |
aoqi@0 | 183 | * @return The system identifier. |
aoqi@0 | 184 | */ |
aoqi@0 | 185 | public final String getSystemId() { |
aoqi@0 | 186 | return systemId; |
aoqi@0 | 187 | } |
aoqi@0 | 188 | |
aoqi@0 | 189 | /** |
aoqi@0 | 190 | * Get the in-scope namespaces. |
aoqi@0 | 191 | * |
aoqi@0 | 192 | * <p> |
aoqi@0 | 193 | * |
aoqi@0 | 194 | * The in-scope namespaces will be empty if the buffer is not a |
aoqi@0 | 195 | * fragment ({@link #isFragment} returns <code>false</code>). |
aoqi@0 | 196 | * |
aoqi@0 | 197 | * The in-scope namespace will correspond to the in-scope namespaces of the |
aoqi@0 | 198 | * fragment if the buffer is a fragment ({@link #isFragment} |
aoqi@0 | 199 | * returns <code>false</code>). The in-scope namespaces will include any |
aoqi@0 | 200 | * namespace delcarations on an element if the fragment correspond to that |
aoqi@0 | 201 | * of an element ({@link #isElementFragment} returns <code>false</code>). |
aoqi@0 | 202 | * |
aoqi@0 | 203 | * @return |
aoqi@0 | 204 | * The in-scope namespaces of the XMLStreamBuffer. |
aoqi@0 | 205 | * Prefix to namespace URI. |
aoqi@0 | 206 | */ |
aoqi@0 | 207 | public final Map<String,String> getInscopeNamespaces() { |
aoqi@0 | 208 | return _inscopeNamespaces; |
aoqi@0 | 209 | } |
aoqi@0 | 210 | |
aoqi@0 | 211 | /** |
aoqi@0 | 212 | * Has the buffer been created using Strings that have been interned |
aoqi@0 | 213 | * for certain properties of information items. The Strings that are interned |
aoqi@0 | 214 | * are those that correspond to Strings that are specified by the SAX API |
aoqi@0 | 215 | * "string-interning" property |
aoqi@0 | 216 | * (see <a href="http://java.sun.com/j2se/1.5.0/docs/api/org/xml/sax/package-summary.html#package_description">here</a>). |
aoqi@0 | 217 | * |
aoqi@0 | 218 | * <p> |
aoqi@0 | 219 | * An buffer may have been created, for example, from an XML document parsed |
aoqi@0 | 220 | * using the Xerces SAX parser. The Xerces SAX parser will have interned certain Strings |
aoqi@0 | 221 | * according to the SAX string interning property. |
aoqi@0 | 222 | * This method enables processors to avoid the duplication of |
aoqi@0 | 223 | * String interning if such a feature is required by a procesing application and the |
aoqi@0 | 224 | * buffer being processed was created using Strings that have been interned. |
aoqi@0 | 225 | * |
aoqi@0 | 226 | * @return |
aoqi@0 | 227 | * <code>true</code> if the buffer has been created using Strings that |
aoqi@0 | 228 | * have been interned. |
aoqi@0 | 229 | */ |
aoqi@0 | 230 | public final boolean hasInternedStrings() { |
aoqi@0 | 231 | return _hasInternedStrings; |
aoqi@0 | 232 | } |
aoqi@0 | 233 | |
aoqi@0 | 234 | /** |
aoqi@0 | 235 | * Read the contents of the buffer as a {@link XMLStreamReader}. |
aoqi@0 | 236 | * |
aoqi@0 | 237 | * @return |
aoqi@0 | 238 | * A an instance of a {@link StreamReaderBufferProcessor}. Always non-null. |
aoqi@0 | 239 | */ |
aoqi@0 | 240 | public final StreamReaderBufferProcessor readAsXMLStreamReader() throws XMLStreamException { |
aoqi@0 | 241 | return new StreamReaderBufferProcessor(this); |
aoqi@0 | 242 | } |
aoqi@0 | 243 | |
aoqi@0 | 244 | /** |
aoqi@0 | 245 | * Write the contents of the buffer to an XMLStreamWriter. |
aoqi@0 | 246 | * |
aoqi@0 | 247 | * <p> |
aoqi@0 | 248 | * The XMLStreamBuffer will be written out to the XMLStreamWriter using |
aoqi@0 | 249 | * an instance of {@link StreamWriterBufferProcessor}. |
aoqi@0 | 250 | * |
aoqi@0 | 251 | * @param writer |
aoqi@0 | 252 | * A XMLStreamWriter to write to. |
aoqi@0 | 253 | * @param writeAsFragment |
aoqi@0 | 254 | * If true, {@link XMLStreamWriter} will not receive {@link XMLStreamWriter#writeStartDocument()} |
aoqi@0 | 255 | * nor {@link XMLStreamWriter#writeEndDocument()}. This is desirable behavior when |
aoqi@0 | 256 | * you are writing the contents of a buffer into a bigger document. |
aoqi@0 | 257 | */ |
aoqi@0 | 258 | public final void writeToXMLStreamWriter(XMLStreamWriter writer, boolean writeAsFragment) throws XMLStreamException { |
aoqi@0 | 259 | StreamWriterBufferProcessor p = new StreamWriterBufferProcessor(this,writeAsFragment); |
aoqi@0 | 260 | p.process(writer); |
aoqi@0 | 261 | } |
aoqi@0 | 262 | |
aoqi@0 | 263 | /** |
aoqi@0 | 264 | * @deprecated |
aoqi@0 | 265 | * Use {@link #writeToXMLStreamWriter(XMLStreamWriter, boolean)} |
aoqi@0 | 266 | */ |
aoqi@0 | 267 | public final void writeToXMLStreamWriter(XMLStreamWriter writer) throws XMLStreamException { |
aoqi@0 | 268 | writeToXMLStreamWriter(writer, this.isFragment()); |
aoqi@0 | 269 | } |
aoqi@0 | 270 | |
aoqi@0 | 271 | /** |
aoqi@0 | 272 | * Reads the contents of the buffer from a {@link XMLReader}. |
aoqi@0 | 273 | * |
aoqi@0 | 274 | * @return |
aoqi@0 | 275 | * A an instance of a {@link SAXBufferProcessor}. |
aoqi@0 | 276 | * @deprecated |
aoqi@0 | 277 | * Use {@link #readAsXMLReader(boolean)} |
aoqi@0 | 278 | */ |
aoqi@0 | 279 | public final SAXBufferProcessor readAsXMLReader() { |
aoqi@0 | 280 | return new SAXBufferProcessor(this,isFragment()); |
aoqi@0 | 281 | } |
aoqi@0 | 282 | |
aoqi@0 | 283 | /** |
aoqi@0 | 284 | * Reads the contents of the buffer from a {@link XMLReader}. |
aoqi@0 | 285 | * |
aoqi@0 | 286 | * @param produceFragmentEvent |
aoqi@0 | 287 | * True to generate fragment SAX events without start/endDocument. |
aoqi@0 | 288 | * False to generate a full document SAX events. |
aoqi@0 | 289 | * @return |
aoqi@0 | 290 | * A an instance of a {@link SAXBufferProcessor}. |
aoqi@0 | 291 | */ |
aoqi@0 | 292 | public final SAXBufferProcessor readAsXMLReader(boolean produceFragmentEvent) { |
aoqi@0 | 293 | return new SAXBufferProcessor(this,produceFragmentEvent); |
aoqi@0 | 294 | } |
aoqi@0 | 295 | |
aoqi@0 | 296 | /** |
aoqi@0 | 297 | * Write the contents of the buffer to a {@link ContentHandler}. |
aoqi@0 | 298 | * |
aoqi@0 | 299 | * <p> |
aoqi@0 | 300 | * If the <code>handler</code> is also an instance of other SAX-based |
aoqi@0 | 301 | * handlers, such as {@link LexicalHandler}, than corresponding SAX events |
aoqi@0 | 302 | * will be reported to those handlers. |
aoqi@0 | 303 | * |
aoqi@0 | 304 | * @param handler |
aoqi@0 | 305 | * The ContentHandler to receive SAX events. |
aoqi@0 | 306 | * @param produceFragmentEvent |
aoqi@0 | 307 | * True to generate fragment SAX events without start/endDocument. |
aoqi@0 | 308 | * False to generate a full document SAX events. |
aoqi@0 | 309 | * |
aoqi@0 | 310 | * @throws SAXException |
aoqi@0 | 311 | * if a parsing fails, or if {@link ContentHandler} throws a {@link SAXException}. |
aoqi@0 | 312 | */ |
aoqi@0 | 313 | public final void writeTo(ContentHandler handler, boolean produceFragmentEvent) throws SAXException { |
aoqi@0 | 314 | SAXBufferProcessor p = readAsXMLReader(produceFragmentEvent); |
aoqi@0 | 315 | p.setContentHandler(handler); |
aoqi@0 | 316 | if (p instanceof LexicalHandler) { |
aoqi@0 | 317 | p.setLexicalHandler((LexicalHandler)handler); |
aoqi@0 | 318 | } |
aoqi@0 | 319 | if (p instanceof DTDHandler) { |
aoqi@0 | 320 | p.setDTDHandler((DTDHandler)handler); |
aoqi@0 | 321 | } |
aoqi@0 | 322 | if (p instanceof ErrorHandler) { |
aoqi@0 | 323 | p.setErrorHandler((ErrorHandler)handler); |
aoqi@0 | 324 | } |
aoqi@0 | 325 | p.process(); |
aoqi@0 | 326 | } |
aoqi@0 | 327 | |
aoqi@0 | 328 | /** |
aoqi@0 | 329 | * @deprecated |
aoqi@0 | 330 | * Use {@link #writeTo(ContentHandler,boolean)} |
aoqi@0 | 331 | */ |
aoqi@0 | 332 | public final void writeTo(ContentHandler handler) throws SAXException { |
aoqi@0 | 333 | writeTo(handler,isFragment()); |
aoqi@0 | 334 | } |
aoqi@0 | 335 | |
aoqi@0 | 336 | /** |
aoqi@0 | 337 | * Write the contents of the buffer to a {@link ContentHandler} with errors |
aoqi@0 | 338 | * report to a {@link ErrorHandler}. |
aoqi@0 | 339 | * |
aoqi@0 | 340 | * <p> |
aoqi@0 | 341 | * If the <code>handler</code> is also an instance of other SAX-based |
aoqi@0 | 342 | * handlers, such as {@link LexicalHandler}, than corresponding SAX events |
aoqi@0 | 343 | * will be reported to those handlers. |
aoqi@0 | 344 | * |
aoqi@0 | 345 | * @param handler |
aoqi@0 | 346 | * The ContentHandler to receive SAX events. |
aoqi@0 | 347 | * @param errorHandler |
aoqi@0 | 348 | * The ErrorHandler to receive error events. |
aoqi@0 | 349 | * |
aoqi@0 | 350 | * @throws SAXException |
aoqi@0 | 351 | * if a parsing fails and {@link ErrorHandler} throws a {@link SAXException}, |
aoqi@0 | 352 | * or if {@link ContentHandler} throws a {@link SAXException}. |
aoqi@0 | 353 | */ |
aoqi@0 | 354 | public final void writeTo(ContentHandler handler, ErrorHandler errorHandler, boolean produceFragmentEvent) throws SAXException { |
aoqi@0 | 355 | SAXBufferProcessor p = readAsXMLReader(produceFragmentEvent); |
aoqi@0 | 356 | p.setContentHandler(handler); |
aoqi@0 | 357 | if (p instanceof LexicalHandler) { |
aoqi@0 | 358 | p.setLexicalHandler((LexicalHandler)handler); |
aoqi@0 | 359 | } |
aoqi@0 | 360 | if (p instanceof DTDHandler) { |
aoqi@0 | 361 | p.setDTDHandler((DTDHandler)handler); |
aoqi@0 | 362 | } |
aoqi@0 | 363 | |
aoqi@0 | 364 | p.setErrorHandler(errorHandler); |
aoqi@0 | 365 | |
aoqi@0 | 366 | p.process(); |
aoqi@0 | 367 | } |
aoqi@0 | 368 | |
aoqi@0 | 369 | public final void writeTo(ContentHandler handler, ErrorHandler errorHandler) throws SAXException { |
aoqi@0 | 370 | writeTo(handler, errorHandler, isFragment()); |
aoqi@0 | 371 | } |
aoqi@0 | 372 | |
aoqi@0 | 373 | private static final ContextClassloaderLocal<TransformerFactory> trnsformerFactory = new ContextClassloaderLocal<TransformerFactory>() { |
aoqi@0 | 374 | @Override |
aoqi@0 | 375 | protected TransformerFactory initialValue() throws Exception { |
aoqi@0 | 376 | return TransformerFactory.newInstance(); |
aoqi@0 | 377 | } |
aoqi@0 | 378 | }; |
aoqi@0 | 379 | |
aoqi@0 | 380 | /** |
aoqi@0 | 381 | * Writes out the contents of this buffer as DOM node and append that to the given node. |
aoqi@0 | 382 | * |
aoqi@0 | 383 | * Faster implementation would be desirable. |
aoqi@0 | 384 | * |
aoqi@0 | 385 | * @return |
aoqi@0 | 386 | * The newly added child node. |
aoqi@0 | 387 | */ |
aoqi@0 | 388 | public final Node writeTo(Node n) throws XMLStreamBufferException { |
aoqi@0 | 389 | try { |
aoqi@0 | 390 | Transformer t = trnsformerFactory.get().newTransformer(); |
aoqi@0 | 391 | t.transform(new XMLStreamBufferSource(this), new DOMResult(n)); |
aoqi@0 | 392 | return n.getLastChild(); |
aoqi@0 | 393 | } catch (TransformerException e) { |
aoqi@0 | 394 | throw new XMLStreamBufferException(e); |
aoqi@0 | 395 | } |
aoqi@0 | 396 | } |
aoqi@0 | 397 | |
aoqi@0 | 398 | /** |
aoqi@0 | 399 | * Create a new buffer from a XMLStreamReader. |
aoqi@0 | 400 | * |
aoqi@0 | 401 | * @param reader |
aoqi@0 | 402 | * A XMLStreamReader to read from to create. |
aoqi@0 | 403 | * @return XMLStreamBuffer the created buffer |
aoqi@0 | 404 | * @see MutableXMLStreamBuffer#createFromXMLStreamReader(XMLStreamReader) |
aoqi@0 | 405 | */ |
aoqi@0 | 406 | public static XMLStreamBuffer createNewBufferFromXMLStreamReader(XMLStreamReader reader) |
aoqi@0 | 407 | throws XMLStreamException { |
aoqi@0 | 408 | MutableXMLStreamBuffer b = new MutableXMLStreamBuffer(); |
aoqi@0 | 409 | b.createFromXMLStreamReader(reader); |
aoqi@0 | 410 | return b; |
aoqi@0 | 411 | } |
aoqi@0 | 412 | |
aoqi@0 | 413 | /** |
aoqi@0 | 414 | * Create a new buffer from a {@link XMLReader} and {@link InputStream}. |
aoqi@0 | 415 | * |
aoqi@0 | 416 | * @param reader |
aoqi@0 | 417 | * The {@link XMLReader} to use for parsing. |
aoqi@0 | 418 | * @param in |
aoqi@0 | 419 | * The {@link InputStream} to be parsed. |
aoqi@0 | 420 | * @return XMLStreamBuffer the created buffer |
aoqi@0 | 421 | * @see MutableXMLStreamBuffer#createFromXMLReader(XMLReader, InputStream) |
aoqi@0 | 422 | */ |
aoqi@0 | 423 | public static XMLStreamBuffer createNewBufferFromXMLReader(XMLReader reader, InputStream in) throws SAXException, IOException { |
aoqi@0 | 424 | MutableXMLStreamBuffer b = new MutableXMLStreamBuffer(); |
aoqi@0 | 425 | b.createFromXMLReader(reader, in); |
aoqi@0 | 426 | return b; |
aoqi@0 | 427 | } |
aoqi@0 | 428 | |
aoqi@0 | 429 | /** |
aoqi@0 | 430 | * Create a new buffer from a {@link XMLReader} and {@link InputStream}. |
aoqi@0 | 431 | * |
aoqi@0 | 432 | * @param reader |
aoqi@0 | 433 | * The {@link XMLReader} to use for parsing. |
aoqi@0 | 434 | * @param in |
aoqi@0 | 435 | * The {@link InputStream} to be parsed. |
aoqi@0 | 436 | * @param systemId |
aoqi@0 | 437 | * The system ID of the input stream. |
aoqi@0 | 438 | * @return XMLStreamBuffer the created buffer |
aoqi@0 | 439 | * @see MutableXMLStreamBuffer#createFromXMLReader(XMLReader, InputStream, String) |
aoqi@0 | 440 | */ |
aoqi@0 | 441 | public static XMLStreamBuffer createNewBufferFromXMLReader(XMLReader reader, InputStream in, |
aoqi@0 | 442 | String systemId) throws SAXException, IOException { |
aoqi@0 | 443 | MutableXMLStreamBuffer b = new MutableXMLStreamBuffer(); |
aoqi@0 | 444 | b.createFromXMLReader(reader, in, systemId); |
aoqi@0 | 445 | return b; |
aoqi@0 | 446 | } |
aoqi@0 | 447 | |
aoqi@0 | 448 | protected final FragmentedArray<byte[]> getStructure() { |
aoqi@0 | 449 | return _structure; |
aoqi@0 | 450 | } |
aoqi@0 | 451 | |
aoqi@0 | 452 | protected final int getStructurePtr() { |
aoqi@0 | 453 | return _structurePtr; |
aoqi@0 | 454 | } |
aoqi@0 | 455 | |
aoqi@0 | 456 | protected final FragmentedArray<String[]> getStructureStrings() { |
aoqi@0 | 457 | return _structureStrings; |
aoqi@0 | 458 | } |
aoqi@0 | 459 | |
aoqi@0 | 460 | protected final int getStructureStringsPtr() { |
aoqi@0 | 461 | return _structureStringsPtr; |
aoqi@0 | 462 | } |
aoqi@0 | 463 | |
aoqi@0 | 464 | protected final FragmentedArray<char[]> getContentCharactersBuffer() { |
aoqi@0 | 465 | return _contentCharactersBuffer; |
aoqi@0 | 466 | } |
aoqi@0 | 467 | |
aoqi@0 | 468 | protected final int getContentCharactersBufferPtr() { |
aoqi@0 | 469 | return _contentCharactersBufferPtr; |
aoqi@0 | 470 | } |
aoqi@0 | 471 | |
aoqi@0 | 472 | protected final FragmentedArray<Object[]> getContentObjects() { |
aoqi@0 | 473 | return _contentObjects; |
aoqi@0 | 474 | } |
aoqi@0 | 475 | |
aoqi@0 | 476 | protected final int getContentObjectsPtr() { |
aoqi@0 | 477 | return _contentObjectsPtr; |
aoqi@0 | 478 | } |
aoqi@0 | 479 | } |