List of usage examples for javax.xml.stream XMLEventReader nextEvent
public XMLEvent nextEvent() throws XMLStreamException;
From source file:Main.java
/** * Copy an XML event stream.// ww w .j av a 2 s . co m * @param reader The event reader. * @param writer The event writer. * @param omitDoc if true, ignore start/end document events. * @throws XMLStreamException For errors writing to the XML event writer. */ public static void copyXMLEventStream(final XMLEventReader reader, final XMLEventWriter writer, final boolean omitDoc) throws XMLStreamException { if (omitDoc) { while (reader.hasNext()) { final XMLEvent event = reader.nextEvent(); final int type = event.getEventType(); if ((type != XMLStreamConstants.START_DOCUMENT) && (type != XMLStreamConstants.END_DOCUMENT)) { writer.add(event); } } } else { writer.add(reader); } writer.flush(); }
From source file:com.hazelcast.simulator.probes.probes.ProbesResultXmlReader.java
private static String parseCharsAndEndCurrentElement(XMLEventReader reader) throws XMLStreamException { XMLEvent xmlEvent = reader.nextEvent(); if (!xmlEvent.isCharacters()) { throw new XMLStreamException("Unexpected event " + xmlEvent); }//from ww w . j a v a2 s . c o m String data = xmlEvent.asCharacters().getData(); while (reader.hasNext()) { xmlEvent = reader.nextEvent(); if (xmlEvent.isEndElement()) { return data; } } throw new XMLStreamException("Unexpected end of the document"); }
From source file:Main.java
/** * 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.//from w w w .j a va2s .com * * @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; }
From source file:Main.java
/** * Advances the event stream until it encounters a start or end tag, but * does not actaully read the event./*from w w w . j a v a 2 s. c o m*/ * * @param reader The reader to peek. * @return The next StartElement or EndElement event, retrieved using * <code>peek()</code>, or <code>null</code> if the end of the * stream was encountered before any tag event. * @throws XMLStreamException If an error occurs reading the stream. */ public static final XMLEvent nextTag(XMLEventReader reader) throws XMLStreamException { while (reader.hasNext()) { XMLEvent nextEvent = reader.peek(); if (nextEvent.isStartElement() || nextEvent.isEndElement()) { return nextEvent; } else { // eat the event. reader.nextEvent(); } } return null; }
From source file:com.predic8.membrane.core.util.SOAPUtil.java
public static boolean isSOAP(XMLInputFactory xmlInputFactory, XOPReconstitutor xopr, Message msg) { try {//from w ww . jav a 2s . c o m XMLEventReader parser; synchronized (xmlInputFactory) { parser = xmlInputFactory.createXMLEventReader(xopr.reconstituteIfNecessary(msg)); } while (parser.hasNext()) { XMLEvent event = parser.nextEvent(); if (event.isStartElement()) { QName name = ((StartElement) event).getName(); return (Constants.SOAP11_NS.equals(name.getNamespaceURI()) || Constants.SOAP12_NS.equals(name.getNamespaceURI())) && "Envelope".equals(name.getLocalPart()); } } } catch (Exception e) { log.warn("Ignoring exception: ", e); } return false; }
From source file:Main.java
private static String printEvents(XMLEvent start, XMLEventReader xml, int indent) throws XMLStreamException { String ret = "\n"; for (int i = 0; i < indent; i++) { ret += " "; }/*w w w.j a va2 s.c o m*/ ret += start + "\n"; XMLEvent next = null; // Keep processing events... while (xml.hasNext()) { next = xml.nextEvent(); // Delegate start tags to a new indentation level if (next.isStartElement()) { printEvents(next, xml, indent + 4); } else if (next.isEndElement()) { // We've found our end element. Add it and be done. for (int i = 0; i < indent; i++) { ret += " "; } ret += next + "\n"; return ret; } else if (next.isCharacters() && next.asCharacters().isWhiteSpace()) { // Skip whitespace continue; } else { // Print the contents of this tag for (int i = 0; i < indent + 4; i++) { ret += " "; } ret += next + "\n"; } } // Oh wow, we ran out of XML. Uh, return then? return ret; }
From source file:com.hazelcast.simulator.probes.probes.ProbesResultXmlReader.java
private static void parseBuckets(XMLEventReader reader, LinearHistogram histogram) throws XMLStreamException { while (reader.hasNext()) { XMLEvent xmlEvent = reader.nextEvent(); if (xmlEvent.isStartElement()) { StartElement startElement = xmlEvent.asStartElement(); if (LATENCY_DIST_BUCKET.matches(startElement.getName().getLocalPart())) { parseBucket(reader, startElement, histogram); }//from ww w . ja v a 2s . co m } else if (xmlEvent.isEndElement()) { EndElement endElement = xmlEvent.asEndElement(); if (!LATENCY_DIST_BUCKETS.matches(endElement.getName().getLocalPart())) { throw new XMLStreamException("Unexpected end element " + endElement.getName()); } return; } } }
From source file:com.hazelcast.simulator.probes.probes.ProbesResultXmlReader.java
public static Map<String, Result> read(InputStream inputStream) { Map<String, Result> result = new HashMap<String, Result>(); XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); try {/* w w w . j a va 2s . c o m*/ XMLEventReader reader = xmlInputFactory.createXMLEventReader(inputStream); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { StartElement startElement = event.asStartElement(); if (PROBES_RESULT.matches(startElement.getName().getLocalPart())) { parseProbesResult(reader, result); } } } } catch (XMLStreamException e) { e.printStackTrace(); } return result; }
From source file:com.hazelcast.simulator.probes.probes.ProbesResultXmlReader.java
private static Result parseMaxLatencyResult(XMLEventReader reader) throws XMLStreamException { Long maxLatency = null;/* ww w . j a va 2s . c o m*/ while (reader.hasNext()) { XMLEvent xmlEvent = reader.nextEvent(); if (xmlEvent.isCharacters()) { maxLatency = Long.parseLong(xmlEvent.asCharacters().getData()); } else if (xmlEvent.isEndElement()) { EndElement endElement = xmlEvent.asEndElement(); if (!MAX_LATENCY.matches(endElement.getName().getLocalPart())) { throw new XMLStreamException("Unexpected end element " + endElement.getName()); } if (maxLatency == null) { throw new XMLStreamException("Unexpected end element " + MAX_LATENCY.getName()); } return new MaxLatencyResult(maxLatency); } } throw new XMLStreamException("Unexpected end of stream"); }
From source file:com.predic8.membrane.core.util.SOAPUtil.java
public static boolean isFault(XMLInputFactory xmlInputFactory, XOPReconstitutor xopr, Message msg) { int state = 0; /*/*from w w w .j a v a 2 s . com*/ * 0: waiting for "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" * 1: waiting for "<soapenv:Body>" (skipping any "<soapenv:Header>") * 2: waiting for "<soapenv:Fault>" */ try { XMLEventReader parser; synchronized (xmlInputFactory) { parser = xmlInputFactory.createXMLEventReader(xopr.reconstituteIfNecessary(msg)); } while (parser.hasNext()) { XMLEvent event = parser.nextEvent(); if (event.isStartElement()) { QName name = ((StartElement) event).getName(); if (!Constants.SOAP11_NS.equals(name.getNamespaceURI()) && !Constants.SOAP12_NS.equals(name.getNamespaceURI())) return false; if ("Header".equals(name.getLocalPart())) { // skip header int stack = 0; while (parser.hasNext()) { event = parser.nextEvent(); if (event.isStartElement()) stack++; if (event.isEndElement()) if (stack == 0) break; else stack--; } continue; } String expected; switch (state) { case 0: expected = "Envelope"; break; case 1: expected = "Body"; break; case 2: expected = "Fault"; break; default: return false; } if (expected.equals(name.getLocalPart())) { if (state == 2) return true; else state++; } else return false; } if (event.isEndElement()) return false; } } catch (Exception e) { log.warn("Ignoring exception: ", e); } return false; }