List of usage examples for javax.xml.stream XMLEventReader peek
public XMLEvent peek() throws XMLStreamException;
From source file:org.geoserver.backuprestore.reader.CatalogFileReader.java
/** * Responsible for moving the cursor before the StartElement of the fragment root. * //from w w w. j a v a2s . co m * This implementation simply looks for the next corresponding element, it does not care about element nesting. You will need to override this * method to correctly handle composite fragments. * * @return <code>true</code> if next fragment was found, <code>false</code> otherwise. * * @throws NonTransientResourceException if the cursor could not be moved. This will be treated as fatal and subsequent calls to read will return * null. */ protected boolean moveCursorToNextFragment(XMLEventReader reader) { try { while (true) { while (reader.peek() != null && !reader.peek().isStartElement()) { reader.nextEvent(); } if (reader.peek() == null) { return false; } QName startElementName = ((StartElement) reader.peek()).getName(); if (isFragmentRootElementName(startElementName)) { return true; } reader.nextEvent(); } } catch (XMLStreamException e) { return logValidationExceptions((T) null, new NonTransientResourceException("Error while reading from event reader", e)); } }
From source file:org.mcisb.subliminal.SubliminalUtils.java
/** * /*from www . j a v a 2 s . c o m*/ * @param elementName * @param is * @param onlyValues * @return Collection * @throws XMLStreamException * @throws UnsupportedEncodingException */ private static Collection<String> getElements(final String elementName, final InputStream is, final boolean onlyValues) throws XMLStreamException, UnsupportedEncodingException { final Collection<String> elements = new ArrayList<>(); final ByteArrayOutputStream os = new ByteArrayOutputStream(); final XMLEventReader reader = XMLInputFactory.newInstance() .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name())); final XMLEventWriter writer = XMLOutputFactory.newInstance() .createXMLEventWriter(new OutputStreamWriter(os, Charset.defaultCharset().name())); boolean read = false; String characters = null; while (reader.peek() != null) { final XMLEvent event = (XMLEvent) reader.next(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.END_DOCUMENT: { // Ignore. break; } case XMLStreamConstants.START_ELEMENT: { read = read || elementName.equals(event.asStartElement().getName().getLocalPart()); if (read && !onlyValues) { writer.add(event); } break; } case XMLStreamConstants.ATTRIBUTE: { if (read && !onlyValues) { writer.add(event); } break; } case XMLStreamConstants.CHARACTERS: { if (read && !onlyValues) { writer.add(event); } characters = event.asCharacters().getData(); break; } case XMLStreamConstants.END_ELEMENT: { if (read && !onlyValues) { writer.add(event); } if (elementName.equals(event.asEndElement().getName().getLocalPart())) { writer.flush(); if (characters != null) { elements.add(characters); } os.reset(); read = false; } break; } default: { // Ignore break; } } } return elements; }
From source file:org.qi4j.valueserialization.stax.StaxValueDeserializer.java
@Override protected Node readObjectTree(XMLEventReader input) throws Exception { XMLEvent peek = input.peek(); if (peek.isStartElement() && "null".equals(peek.asStartElement().getName().getLocalPart())) { input.nextTag();// <null> input.nextTag();// </null> return null; }/*from w w w .j a va 2 s. co m*/ String elementBody = readElementBody(input); Transformer transformer = transformerFactory.newTransformer(); DOMResult domResult = new DOMResult(); transformer.transform(new StreamSource(new StringReader(elementBody)), domResult); return ((Document) domResult.getNode()).getDocumentElement(); }
From source file:org.qi4j.valueserialization.stax.StaxValueDeserializer.java
private static String readElementBody(XMLEventReader input) throws XMLStreamException { StringWriter buf = new StringWriter(1024); int depth = 0; while (input.hasNext()) { // peek event XMLEvent xmlEvent = input.peek(); if (xmlEvent.isStartElement()) { ++depth;/*from w w w .j ava 2 s . co m*/ } else if (xmlEvent.isEndElement()) { --depth; // reached END_ELEMENT tag? // break loop, leave event in stream if (depth < 0) { break; } } // consume event xmlEvent = input.nextEvent(); // print out event xmlEvent.writeAsEncodedUnicode(buf); } return buf.getBuffer().toString(); }
From source file:org.springframework.batch.item.xml.StaxEventItemReader.java
/** * Responsible for moving the cursor before the StartElement of the fragment root. * /*from w w w.j a v a 2 s . c o m*/ * This implementation simply looks for the next corresponding element, it does not care about element nesting. You * will need to override this method to correctly handle composite fragments. * * @return <code>true</code> if next fragment was found, <code>false</code> otherwise. * * @throws NonTransientResourceException if the cursor could not be moved. This will be treated as fatal and * subsequent calls to read will return null. */ protected boolean moveCursorToNextFragment(XMLEventReader reader) throws NonTransientResourceException { try { while (true) { while (reader.peek() != null && !reader.peek().isStartElement()) { reader.nextEvent(); } if (reader.peek() == null) { return false; } QName startElementName = ((StartElement) reader.peek()).getName(); if (isFragmentRootElementName(startElementName)) { return true; } reader.nextEvent(); } } catch (XMLStreamException e) { throw new NonTransientResourceException("Error while reading from event reader", e); } }
From source file:org.xmlsh.commands.internal.xml2json.java
private void readToEnd(XMLEventReader reader) throws XMLStreamException { while (reader.hasNext() && !reader.peek().isEndElement()) reader.nextEvent();//from ww w .j a va 2 s .c o m if (reader.hasNext()) reader.nextEvent(); }
From source file:org.xmlsh.commands.internal.xml2json.java
private String readChars(XMLEventReader reader) throws XMLStreamException { StringBuffer sb = new StringBuffer(); while (reader.hasNext() && reader.peek().isCharacters()) { Characters ch = reader.nextEvent().asCharacters(); sb.append(ch.getData());// w w w . ja v a2s .c o m } return sb.toString(); }