List of usage examples for javax.xml.stream XMLEventReader hasNext
@Override public boolean hasNext();
From source file:com.predic8.membrane.core.util.SOAPUtil.java
public static boolean isSOAP(XMLInputFactory xmlInputFactory, XOPReconstitutor xopr, Message msg) { try {//from ww w . ja va2 s . 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: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); }//w ww. j av a 2 s. 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 v a 2s . c om*/ 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;/*from www .ja v a 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 ww w.j a v a2 s. c om*/ * 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; }
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 += " "; }/* ww w . ja v a 2 s .co 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 Result parseHdrLatencyProbeResult(XMLEventReader reader) throws XMLStreamException { String encodedData = null;/*from w w w. j a v a 2s .c om*/ 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 ww.j a va2 s. 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 w w w . ja v a2s . c o m*/ 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.predic8.membrane.core.util.TextUtil.java
/** * Checks whether s is a valid (well-formed and balanced) XML snippet. * * Note that attributes escaped by single quotes are accepted (which is illegal by spec). *//*from w w w . jav a 2s . c o m*/ public static boolean isValidXMLSnippet(String s) { try { XMLEventReader parser; synchronized (xmlInputFactory) { parser = xmlInputFactory.createXMLEventReader(new StringReader("<a>" + s + "</a>")); } XMLEvent event = null; while (parser.hasNext()) { event = (XMLEvent) parser.next(); } return event != null && event.isEndDocument(); } catch (Exception e) { log.error("", e); return false; } }