/** * Copies the content read from a TrAX {@link Source} to a StAX * {@link XMLStreamWriter}. * * @param source The content source. * @param writer The destination stream. * @throws XMLStreamException If an error occurs copying the content to the * stream. */ public static final void copy(Source source, XMLStreamWriter writer) throws XMLStreamException { XMLStreamReader reader = inputFactory.createXMLStreamReader(source); copy(reader, writer); }
/** * Copies the content read from a TrAX {@link Source} to a StAX * {@link XMLEventWriter}. * * @param source The content source. * @param writer The destination event stream. * @throws XMLStreamException If an error occurs copying the content to the * event stream. */ public static final void copy(Source source, XMLEventWriter writer) throws XMLStreamException { XMLEventReader reader = inputFactory.createXMLEventReader(source); copy(reader, writer); }
/** * Copies the content read from a StAX {@link XMLStreamReader} to a TrAX * {@link Result}. * * @param reader The source stream. * @param result The destination {@link Result}. * @throws XMLStreamException If an error occurs copying the content to the * result. */ public static final void copy(XMLStreamReader reader, Result result) throws XMLStreamException { XMLStreamWriter writer = outputFactory.createXMLStreamWriter(result); copy(reader, writer); // force any cached content to the result writer.flush(); }
/** * Copies the content read from the specified source stream to the provided * result stream. This method is exactly the same as calling * {@link XMLEventWriter#add(XMLEventReader)}, and is provided only for * completeness. * * @param reader The source stream. * @param consumer The destination stream. * @throws XMLStreamException If an error occurs copying the stream * contents. */ public static final void copy(XMLEventReader reader, XMLEventConsumer consumer) throws XMLStreamException { if (consumer instanceof XMLEventWriter) { copy(reader, (XMLEventWriter) consumer); } else { while (reader.hasNext()) { consumer.add(reader.nextEvent()); } } }
/** * Copies the subtree that the reader is currently position at to the * given writer. The reader must be positioned at a START_ELEMENT * event, and when finished, will be positioned on the corresponding * END_ELEMENT event. * <p> * <b>NOTE:</b> Even if successful, this does not currently ensure that * the output has all used namespaces declared. In particular, if a * namespace was previously declared in the XML that the reader saw, * then used within the scope of the elements being copied but no * re-declared within them, the output will not be self-contained. * * @param source the reader whose subtree should be copied. * @param sink the writer to copy to. It will be left open when finished. * @throws XMLStreamException if anything goes wrong during the copy. */ public static void copy(XMLStreamReader source, XMLStreamWriter sink) throws XMLStreamException { sink.setNamespaceContext(source.getNamespaceContext()); // TODO: Make a version of XMLStreamUtils that automatically writes // namespace declarations for any namespaces that aren't in it's // (new) context yet, but are in the reader's context (currently set // above, but it should probably not be...) XMLStreamUtils.copy(new BalancedXMLStreamReader(source), sink); }
/** * Copies the content read from a StAX {@link XMLEventReader} to a TrAX * {@link Result}. * * @param reader The source event stream. * @param result The destination {@link Result}. * @throws XMLStreamException If an error occurs copying the content to the * result. */ public static final void copy(XMLEventReader reader, Result result) throws XMLStreamException { XMLEventWriter writer = outputFactory.createXMLEventWriter(result); copy(reader, writer); // force any cached events to the result writer.flush(); }
private static void copyXML(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter, boolean indent) throws XMLStreamException { try { WhiteSpaceRemovingDelegate xmlStreamReader2 = new XMLUtil.WhiteSpaceRemovingDelegate(xmlStreamReader); XMLStreamWriter xmlStreamWriter2 = xmlStreamWriter; if (indent) { xmlStreamWriter2 = new IndentingXMLStreamWriter(xmlStreamWriter); } XMLStreamUtils.copy(xmlStreamReader2, xmlStreamWriter2); } finally { if (xmlStreamReader != null) { try { xmlStreamReader.close(); } catch (XMLStreamException e) { /* ignore */ } } if (xmlStreamWriter != null) { try { xmlStreamWriter.close(); } catch (XMLStreamException e) { /* ignore */ } } } }