List of usage examples for javax.xml.stream XMLEventReader nextEvent
public XMLEvent nextEvent() throws XMLStreamException;
From source file:com.hazelcast.simulator.probes.probes.ProbesResultXmlReader.java
private static Result parseHdrLatencyProbeResult(XMLEventReader reader) throws XMLStreamException { String encodedData = null;/*from ww w.j ava2 s . c o m*/ while (reader.hasNext()) { XMLEvent xmlEvent = reader.nextEvent(); if (xmlEvent.isCharacters()) { encodedData = xmlEvent.asCharacters().getData(); } else if (xmlEvent.isEndElement()) { EndElement endElement = xmlEvent.asEndElement(); if (!HDR_LATENCY_DATA.matches(endElement.getName().getLocalPart()) || encodedData == null) { throw new XMLStreamException("Unexpected end element " + endElement.getName()); } try { byte[] bytes = Base64.decodeBase64(encodedData); Histogram histogram = Histogram.decodeFromCompressedByteBuffer(ByteBuffer.wrap(bytes), 0); return new HdrLatencyDistributionResult(histogram); } catch (DataFormatException e) { throw new RuntimeException(e); } } } throw new XMLStreamException("Unexpected end of stream"); }
From source file:com.hazelcast.simulator.probes.probes.ProbesResultXmlReader.java
private static void parseProbesResult(XMLEventReader reader, Map<String, Result> result) throws XMLStreamException { while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { StartElement startElement = event.asStartElement(); if (PROBE.matches(startElement.getName().getLocalPart())) { parseProbe(reader, startElement, result); }/*from w w w .j a v a2s. c o m*/ } else if (event.isEndElement()) { EndElement endElement = event.asEndElement(); boolean isProbeEnd = PROBE.matches(endElement.getName().getLocalPart()); boolean isProbeResultEnd = PROBES_RESULT.matches(endElement.getName().getLocalPart()); if (!isProbeEnd && !isProbeResultEnd) { throw new XMLStreamException("Unexpected end element " + endElement.getName()); } if (isProbeResultEnd) { return; } } } }
From source file:com.hazelcast.simulator.probes.probes.ProbesResultXmlReader.java
private static Result parseLatencyDistributionResult(XMLEventReader reader) throws XMLStreamException { Integer step = null;/*from ww w.j a va 2 s .c om*/ Integer maxValue = null; while (reader.hasNext()) { XMLEvent xmlEvent = reader.nextEvent(); if (xmlEvent.isStartElement()) { StartElement startElement = xmlEvent.asStartElement(); if (LATENCY_DIST_STEP.matches(startElement.getName().getLocalPart())) { if (step != null) { throw new XMLStreamException("Unexpected element " + LATENCY_DIST_STEP.getName()); } step = Integer.parseInt(parseCharsAndEndCurrentElement(reader)); } else if (LATENCY_DIST_MAX_VALUE.matches(startElement.getName().getLocalPart())) { if (maxValue != null) { throw new XMLStreamException("Unexpected element " + LATENCY_DIST_MAX_VALUE.getName()); } maxValue = Integer.parseInt(parseCharsAndEndCurrentElement(reader)); } else if (LATENCY_DIST_BUCKETS.matches(startElement.getName().getLocalPart())) { if (step == null || maxValue == null) { throw new XMLStreamException("Unexpected element " + LATENCY_DIST_BUCKETS.getName()); } LinearHistogram histogram = new LinearHistogram(maxValue, step); parseBuckets(reader, histogram); return new LatencyDistributionResult(histogram); } } } throw new XMLStreamException("Unexpected end of the document"); }
From source file:com.hazelcast.simulator.probes.probes.ProbesResultXmlReader.java
private static Result parseOperationsPerSecResult(XMLEventReader reader) throws XMLStreamException { Long invocations = null;/* www. j a v a2 s .c om*/ Double operationsPerSecond = null; while (reader.hasNext()) { XMLEvent xmlEvent = reader.nextEvent(); if (xmlEvent.isStartElement()) { StartElement startElement = xmlEvent.asStartElement(); if (INVOCATIONS.matches(startElement.getName().getLocalPart())) { if (invocations != null) { throw new XMLStreamException( "Unexpected element " + INVOCATIONS.getName() + " (has been already defined)"); } invocations = Long.parseLong(parseCharsAndEndCurrentElement(reader)); } else if (OPERATIONS_PER_SECOND.matches(startElement.getName().getLocalPart())) { if (operationsPerSecond != null) { throw new XMLStreamException("Unexpected element " + OPERATIONS_PER_SECOND.getName() + " (has been already defined)"); } operationsPerSecond = Double.parseDouble(parseCharsAndEndCurrentElement(reader)); } if (invocations != null && operationsPerSecond != null) { return new OperationsPerSecResult(invocations, operationsPerSecond); } } } throw new XMLStreamException("Unexpected end of stream"); }
From source file:com.predic8.membrane.core.interceptor.rest.XML2HTTP.java
private static String slurpCharacterData(XMLEventReader parser, StartElement sevent) throws XMLStreamException, XML2HTTPException { String name = sevent.getName().getLocalPart(); StringBuilder value = new StringBuilder(); while (parser.hasNext()) { XMLEvent event = parser.nextEvent(); if (event.isCharacters()) { value.append(event.asCharacters().getData()); } else if (event.isEndElement()) { break; } else {/*w w w . j ava 2 s . c o m*/ throw new XML2HTTPException("XML-HTTP doc <" + name + "> element contains non-character data."); } } return value.toString(); }
From source file:Main.java
/** * Copies an element and all its content from the provided event reader, to * the provided event consumer. The event reader must be positioned before a * start element event, or this method has no effect. * /*from w w w .j a va 2s. c o m*/ * @param reader The reader from which to read the events. * @param consumer The destination for read events, or <code>null</code> to * ignore all events. * @throws XMLStreamException If an error occurs reading or writing the * events. */ public static final void copyElement(XMLEventReader reader, XMLEventConsumer consumer) throws XMLStreamException { if (!reader.hasNext()) return; XMLEvent event = reader.peek(); if (!event.isStartElement()) return; int depth = 0; do { XMLEvent currEvt = reader.nextEvent(); if (currEvt.isStartElement()) { depth++; } else if (currEvt.isEndElement()) { depth--; } if (consumer != null) { consumer.add(currEvt); } } while (depth > 0 && reader.hasNext()); }
From source file:Main.java
public static String getXMLContent(XMLEventReader reader, StartElement element, boolean decodeCharacters) throws XMLStreamException { String rootElementName = getLocalName(element); StringWriter buffer = new StringWriter(1024); StartElement pendingElement = null; String pendingElementName = null; while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (pendingElement != null) { boolean skip = false; if (event.isEndElement() && pendingElementName.equals(getLocalName(event.asEndElement()))) { writeAsEncodedUnicode(pendingElement, buffer, true); // empty tag skip = true; // skip this end tag } else { writeAsEncodedUnicode(pendingElement, buffer, false); }//w w w .j a va 2 s .c om pendingElement = null; pendingElementName = null; if (skip) continue; } if (event.isEndElement()) { EndElement endElement = event.asEndElement(); String name = getLocalName(endElement); if (rootElementName.equals(name)) return buffer.toString(); writeAsEncodedUnicode(endElement, buffer); } else if (event.isStartElement()) { pendingElement = event.asStartElement(); pendingElementName = getLocalName(pendingElement); } else if (event.isCharacters() && decodeCharacters) { buffer.append(event.asCharacters().getData()); } else { event.writeAsEncodedUnicode(buffer); } } throw new XMLStreamException(format("Missing closing tag for '" + rootElementName + "' element", element)); }
From source file:com.predic8.membrane.core.interceptor.rest.XML2HTTP.java
private static String slurpXMLData(XMLEventReader parser, StartElement sevent) throws XML2HTTPException, XMLStreamException { StringWriter bodyStringWriter = new StringWriter(); XMLEventWriter bodyWriter = null; int depth = 0; synchronized (xmlOutputFactory) { bodyWriter = xmlOutputFactory.createXMLEventWriter(bodyStringWriter); }/*from w w w . j av a 2 s .co m*/ while (parser.hasNext()) { XMLEvent event = parser.nextEvent(); if (event.isEndElement() && depth == 0) { bodyWriter.flush(); return bodyStringWriter.toString(); } bodyWriter.add(event); if (event.isStartElement()) depth++; else if (event.isEndElement()) depth--; } throw new XML2HTTPException("Early end of file while reading inner XML document."); }
From source file:edu.jhu.hlt.concrete.ingesters.bolt.BoltForumPostIngester.java
private static Section handleHeadline(final XMLEventReader rdr, final String content) throws XMLStreamException, ConcreteException { // The first type is always a document start event. Skip it. rdr.nextEvent(); // The second type is a document ID block. Skip it. rdr.nextEvent();/*from w ww . j a va 2 s . c o m*/ // The third type is a whitespace block. Skip it. rdr.nextEvent(); // The next type is a headline start tag. XMLEvent hl = rdr.nextEvent(); StartElement hlse = hl.asStartElement(); QName hlqn = hlse.getName(); final String hlPart = hlqn.getLocalPart(); LOGGER.debug("QN: {}", hlPart); int hlPartOff = hlse.getLocation().getCharacterOffset(); LOGGER.debug("HL part offset: {}", hlPartOff); // Text of the headline. This would be useful for purely getting // the content, but for offsets, it's not that useful. Characters cc = rdr.nextEvent().asCharacters(); int charOff = cc.getLocation().getCharacterOffset(); int clen = cc.getData().length(); // The next part is the headline end element. Skip. rdr.nextEvent(); // Whitespace. Skip. rdr.nextEvent(); // Reader is now pointing at the first post. // Construct section, text span, etc. final int charOffPlusLen = charOff + clen; // Strip whitespace off TextSpan ts; if (STRIP_WHITESPACE_OFF_HEADLINE) { final String hlText = content.substring(charOff, charOffPlusLen); SimpleImmutableEntry<Integer, Integer> pads = trimSpacing(hlText); ts = new TextSpan(charOff + pads.getKey(), charOffPlusLen - pads.getValue()); } else { ts = new TextSpan(charOff, charOffPlusLen); } assert ts.getStart() <= ts.getEnding() : "ts=" + ts; Section s = new Section(); s.setKind("headline"); s.setTextSpan(ts); List<Integer> intList = new ArrayList<>(); intList.add(0); s.setNumberList(intList); return s; }
From source file:Main.java
/** * 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 if it matches the specified QName, but if it doesn't match, an * end tag is encountered, or the stream ends, <code>null</code> will be * returned. After returning, the stream will be positioned just before the * start element. The start element will not be consumed by this method. * //from ww w. j a v a2 s . c o m * @param reader The event stream from which to read. * @param name The name of the element to read, or <code>null</code> to * read any start tag. * @return The StartElement read from the stream, or <code>null</code> if * the encountered start tag didn't match the specified QName, 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, QName name) throws XMLStreamException { while (reader.hasNext()) { XMLEvent nextEvent = reader.peek(); if (nextEvent.isStartElement()) { StartElement start = nextEvent.asStartElement(); if (name == null || start.getName().equals(name)) { return start; } else { break; } } else if (nextEvent.isEndElement()) { break; } else { // consume the event. reader.nextEvent(); } } return null; }