/** * 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); }
/** * Skips all events within a single element, including its start and end * tags. The provided reader must be positioned directly in front of a * <code>StartElement</code> event or it will have no effect. After this * method completes, the reader will be positioned before the event * following the end tag (the end tag will have been read). * * @param reader The event stream to read. * @throws XMLStreamException If an error occurs reading events. */ public static final void skipElement(XMLEventReader reader) throws XMLStreamException { copyElement(reader, null); }
/** * Skips all events within a <code>StartElement</code> until the matching * <code>EndElement</code> is reached. This method assumes that the reader * is positioned after the <code>StartElement</code> event, and when the * method completes, the stream will be positioned before the * <code>EndElement</code> event, but it will not consume the end tag. * * @param reader The event stream to read, positioned after the * <code>StartElement</code> * @throws XMLStreamException If an error occurs reading events. */ public static final void skipElementContent(XMLEventReader reader) throws XMLStreamException { copyElementContent(reader, null); }
/** * Returns the name of the current event type. * * @return The name of the current event type. */ public String getEventTypeName() { return XMLStreamUtils.getEventTypeName(getEventType()); }
/** * Reads the events from the provided event stream until either a start or * end tag is encountered. In the former case, the start tag will be * returned, but if an end tag is encountered, <code>null</code> will be * returned. After returning, the stream will be positioned just before the * returned start element. The start element will not be consumed by this * method. * * @param reader The event stream from which to read. * @return The StartElement read from the stream, or <code>null</code> if * an end tag was found first, or the stream ended before a start * element was found. * @throws XMLStreamException If an error occurs reading the stream. */ public static final StartElement nextElement(XMLEventReader reader) throws XMLStreamException { return nextElement(reader, null); }
/** * Skips the complete content of the element at the specified reader's * cursor. The reader's current event type must be START_ELEMENT, otherwise * this method will have no effect. Upon completion, the reader's cursor * will be at the END_ELEMENT event for the skipped element. * * @param reader An XML stream reader currently in the START_ELEMENT event. */ public static final void skipElement(XMLStreamReader reader) throws XMLStreamException { if (reader.isStartElement()) { skipElementContent(reader); } }
/** * Reads the text content of an element. The reader should be positioned in * front of a StartElement event, and will be read up to and including the * end element tag. * * @param reader The event stream from which to read the element text. * @param elemName The optional name of the element being read. If this * paramter is non- <code>null</code> then an exception will * be thrown if the element read doesn't have the same name. * @return The text read from the element. * @throws XMLStreamException If an error occurs reading the stream, or if * the read element doesn't match the provided QName. */ public static final String readTextElement(XMLEventReader reader, QName elemName) throws XMLStreamException { if (elemName != null) { requireStartElement(reader, elemName); } // read text String text = reader.getElementText(); // consume the end tag reader.nextEvent(); return text; }
public void require(int type, String namespaceURI, String localName) throws XMLStreamException { int currType = getEventType(); if (currType != type) { throw new XMLStreamException("Expected " + XMLStreamUtils.getEventTypeName(type) + " but found " + XMLStreamUtils.getEventTypeName(currType), getStableLocation()); } }
/** * 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); }
+ XMLStreamUtils.getEventTypeName(eventType) + "): " + event);
/** * 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(); }
+ XMLStreamUtils.getEventTypeName(eventType) + "): " + event);
/** * 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 */ } } } }