Java Code Examples for javax.xml.stream.XMLStreamReader#hasName()
The following examples show how to use
javax.xml.stream.XMLStreamReader#hasName() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StreamReaderTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * CR 6631264 / sjsxp Issue 45: * https://sjsxp.dev.java.net/issues/show_bug.cgi?id=45 * XMLStreamReader.hasName() should return false for ENTITY_REFERENCE */ @Test public void testHasNameOnEntityEvent() throws Exception { XMLInputFactory xif = XMLInputFactory.newInstance(); xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false); XMLStreamReader r = xif.createXMLStreamReader( this.getClass().getResourceAsStream("ExternalDTD.xml")); while (r.next() != XMLStreamConstants.ENTITY_REFERENCE) { System.out.println("event type: " + r.getEventType()); continue; } if (r.hasName()) { System.out.println("hasName returned true on ENTITY_REFERENCE event."); } Assert.assertFalse(r.hasName()); // fails }
Example 2
Source File: XmlErrorDocumentConsumer.java From olingo-odata2 with Apache License 2.0 | 6 votes |
private void handleInnerError(final XMLStreamReader reader, final ODataErrorContext errorContext) throws XMLStreamException { StringBuilder sb = new StringBuilder(); while (notFinished(reader, FormatXml.M_INNER_ERROR)) { if (reader.hasName() && !FormatXml.M_INNER_ERROR.equals(reader.getLocalName())) { sb.append("<"); if (reader.isEndElement()) { sb.append("/"); } sb.append(reader.getLocalName()).append(">"); } else if (reader.isCharacters()) { sb.append(reader.getText()); } reader.next(); } errorContext.setInnerError(sb.toString()); }
Example 3
Source File: XmlErrorDocumentDeserializer.java From olingo-odata2 with Apache License 2.0 | 6 votes |
private void handleInnerError(final XMLStreamReader reader, final ODataErrorContext errorContext) throws XMLStreamException { StringBuilder sb = new StringBuilder(); while (notFinished(reader, FormatXml.M_INNER_ERROR)) { if (reader.hasName() && !FormatXml.M_INNER_ERROR.equals(reader.getLocalName())) { sb.append("<"); if (reader.isEndElement()) { sb.append("/"); } sb.append(reader.getLocalName()).append(">"); } else if (reader.isCharacters()) { sb.append(reader.getText()); } reader.next(); } errorContext.setInnerError(sb.toString()); }
Example 4
Source File: AntXmlParser.java From rich-test-results with Apache License 2.0 | 6 votes |
private ImmutableList<TestSuite> parseSuites(XMLStreamReader xmlStreamReader) throws XMLStreamException, XmlParseException { String tagName = null; ImmutableList.Builder<TestSuite> testSuites = ImmutableList.builder(); do { xmlStreamReader.next(); if (!xmlStreamReader.hasName()) { continue; } tagName = xmlStreamReader.getName().toString(); if (xmlStreamReader.isStartElement()) { switch (tagName) { case "testsuite": testSuites.add(parseSuite(xmlStreamReader)); break; default: handleUnsupportedElement("testsuites", tagName); } } } while (!xmlStreamReader.isEndElement() || !"testsuites".equals(tagName)); return testSuites.build(); }
Example 5
Source File: MetaMapConceptProvider.java From bioasq with Apache License 2.0 | 5 votes |
private List<String> splitResponseByMMO(String response) throws XMLStreamException, TransformerException { XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(new StringReader(response)); while (!reader.hasName() || !"MMOs".equals(reader.getLocalName())) { reader.next(); } List<String> mmoStrings = new ArrayList<>(); while (reader.nextTag() == XMLStreamConstants.START_ELEMENT) { StringWriter buffer = new StringWriter(); transformer.transform(new StAXSource(reader), new StreamResult(buffer)); mmoStrings.add(buffer.toString()); } return mmoStrings; }
Example 6
Source File: XmlEntryConsumer.java From olingo-odata2 with Apache License 2.0 | 5 votes |
/** * Skip the tag to which the {@link XMLStreamReader} currently points. * Therefore it is read until an end element tag with current local name is found. * * @param reader * @throws XMLStreamException */ private void skipStartedTag(final XMLStreamReader reader) throws XMLStreamException { final String name = reader.getLocalName(); int read = 1; while (read > 0 && reader.hasNext()) { reader.next(); if (reader.hasName() && name.equals(reader.getLocalName())) { if (reader.isEndElement()) { read--; } else if (reader.isStartElement()) { read++; } } } }
Example 7
Source File: XmlEntryDeserializer.java From olingo-odata2 with Apache License 2.0 | 5 votes |
/** * Skip the tag to which the {@link XMLStreamReader} currently points. * Therefore it is read until an end element tag with current local name is found. * * @param reader * @throws XMLStreamException */ private void skipStartedTag(final XMLStreamReader reader) throws XMLStreamException { final String name = reader.getLocalName(); int read = 1; while (read > 0 && reader.hasNext()) { reader.next(); if (reader.hasName() && name.equals(reader.getLocalName())) { if (reader.isEndElement()) { read--; } else if (reader.isStartElement()) { read++; } } } }
Example 8
Source File: BaseStreamTest.java From woodstox with Apache License 2.0 | 5 votes |
/** * Method that will iterate through contents of an XML document * using specified stream reader; will also access some of data * to make sure reader reads most of lazy-loadable data. * Method is usually called to try to get an exception for invalid * content. * * @return Dummy value calculated on contents; used to make sure * no dead code is eliminated */ protected int streamThrough(XMLStreamReader sr) throws XMLStreamException { int result = 0; assertNotNull(sr); try { while (sr.hasNext()) { int type = sr.next(); result += type; if (sr.hasText()) { /* will also do basic verification for text content, to * see that all text accessor methods return same content */ result += getAndVerifyText(sr).hashCode(); } if (sr.hasName()) { QName n = sr.getName(); assertNotNull(n); result += n.hashCode(); } } } catch (RuntimeException rex) { // Let's try to find a nested XMLStreamException, if possible Throwable t = rex; while (t != null) { t = t.getCause(); if (t instanceof XMLStreamException) { throw (XMLStreamException) t; } } // Nope, just a runtime exception throw rex; } return result; }
Example 9
Source File: AntXmlParser.java From rich-test-results with Apache License 2.0 | 5 votes |
/** * Returns the list of {@link TestSuite} objects parsed from the Ant XML format input stream. */ public ImmutableList<TestSuite> parse(InputStream in, Charset encoding) throws XmlParseException { try { XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(in, encoding.name()); try { while (xmlStreamReader.hasNext()) { int next = xmlStreamReader.next(); if (next == XMLStreamConstants.END_DOCUMENT) { break; } if (!xmlStreamReader.hasName()) { continue; } String tagName = xmlStreamReader.getName().toString(); if (xmlStreamReader.isStartElement()) { switch (tagName) { case "testsuites": return parseSuites(xmlStreamReader); case "testsuite": return ImmutableList.of(parseSuite(xmlStreamReader)); default: handleUnsupportedElement("root", tagName); } } } } finally { xmlStreamReader.close(); } } catch (XMLStreamException e) { if (e.getLocation() != null) { throw new XmlParseException(e.getMessage(), e); } else { throw new RuntimeException(e); } } throw new XmlParseException("No testsuites or testsuite element found."); }
Example 10
Source File: AntXmlParser.java From rich-test-results with Apache License 2.0 | 5 votes |
private void parseProperties(XMLStreamReader xmlStreamReader, TestSuite.Builder suiteBuilder) throws XMLStreamException { String tagName = null; do { xmlStreamReader.next(); if (!xmlStreamReader.hasName()) { continue; } tagName = xmlStreamReader.getName().toString(); if (xmlStreamReader.isStartElement()) { switch (tagName) { case "property": Builder builder = suiteBuilder.addPropertyBuilder(); for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) { String attributeValue = xmlStreamReader.getAttributeValue(i); switch (xmlStreamReader.getAttributeName(i).toString()) { case "name": builder.setName(attributeValue); break; case "value": builder.setValue(attributeValue); break; } } break; } } else if (xmlStreamReader.isEndElement() && "properties".equals(tagName)) { break; } } while (!xmlStreamReader.isEndElement() || !"properties".equals(tagName)); }
Example 11
Source File: AntXmlParser.java From rich-test-results with Apache License 2.0 | 5 votes |
private void skipElement(XMLStreamReader xmlStreamReader, String elementName) throws XMLStreamException, XmlParseException { String tagName = null; do { xmlStreamReader.next(); if (!xmlStreamReader.hasName()) { continue; } tagName = xmlStreamReader.getName().toString(); } while (!xmlStreamReader.isEndElement() || !elementName.equals(tagName)); }
Example 12
Source File: AntXmlParser.java From rich-test-results with Apache License 2.0 | 5 votes |
private String getElementContent(XMLStreamReader xmlStreamReader, String elementName) throws XMLStreamException { String tagName = null; StringBuilder stringBuilder = new StringBuilder(); do { xmlStreamReader.next(); if (xmlStreamReader.hasName()) { tagName = xmlStreamReader.getName().toString(); } else if (xmlStreamReader.isCharacters()) { String text = xmlStreamReader.getText(); stringBuilder.append(text); } } while (!xmlStreamReader.isEndElement() || !elementName.equals(tagName)); return stringBuilder.toString(); }
Example 13
Source File: XmlEntryConsumer.java From cloud-odata-java with Apache License 2.0 | 5 votes |
/** * Skip the tag to which the {@link XMLStreamReader} currently points. * Therefore it is read until an end element tag with current local name is found. * * @param reader * @throws XMLStreamException */ private void skipStartedTag(final XMLStreamReader reader) throws XMLStreamException { final String name = reader.getLocalName(); int read = 1; while (read > 0 && reader.hasNext()) { reader.next(); if (reader.hasName() && name.equals(reader.getLocalName())) { if (reader.isEndElement()) { read--; } else if (reader.isStartElement()) { read++; } } } }
Example 14
Source File: ParsingUtils.java From galleon with Apache License 2.0 | 4 votes |
public static XMLStreamException unexpectedContent(final XMLStreamReader reader) { final String kind; switch (reader.getEventType()) { case XMLStreamConstants.ATTRIBUTE: kind = "attribute"; break; case XMLStreamConstants.CDATA: kind = "cdata"; break; case XMLStreamConstants.CHARACTERS: kind = "characters"; break; case XMLStreamConstants.COMMENT: kind = "comment"; break; case XMLStreamConstants.DTD: kind = "dtd"; break; case XMLStreamConstants.END_DOCUMENT: kind = "document end"; break; case XMLStreamConstants.END_ELEMENT: kind = "element end"; break; case XMLStreamConstants.ENTITY_DECLARATION: kind = "entity declaration"; break; case XMLStreamConstants.ENTITY_REFERENCE: kind = "entity ref"; break; case XMLStreamConstants.NAMESPACE: kind = "namespace"; break; case XMLStreamConstants.NOTATION_DECLARATION: kind = "notation declaration"; break; case XMLStreamConstants.PROCESSING_INSTRUCTION: kind = "processing instruction"; break; case XMLStreamConstants.SPACE: kind = "whitespace"; break; case XMLStreamConstants.START_DOCUMENT: kind = "document start"; break; case XMLStreamConstants.START_ELEMENT: kind = "element start"; break; default: kind = "unknown"; break; } return new XMLStreamException("unexpected content: " + kind + (reader.hasName() ? reader.getName() : null) + (reader.hasText() ? reader.getText() : null), reader.getLocation()); }
Example 15
Source File: AntXmlParser.java From rich-test-results with Apache License 2.0 | 4 votes |
private TestSuite parseSuite(XMLStreamReader xmlStreamReader) throws XMLStreamException, XmlParseException { TestSuite.Builder builder = TestSuite.newBuilder(); for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) { String attributeValue = xmlStreamReader.getAttributeValue(i); switch (xmlStreamReader.getAttributeName(i).toString()) { case "name": builder.setName(attributeValue); break; case "tests": builder.setTotalCount(Integer.parseInt(attributeValue)); break; case "time": builder.setElapsedTimeMillis((long) (Float.parseFloat(attributeValue) * 1000)); break; case "errors": builder.setErrorCount(Integer.parseInt(attributeValue)); break; case "failures": builder.setFailureCount(Integer.parseInt(attributeValue)); break; case "skipped": builder.setSkippedCount(Integer.parseInt(attributeValue)); break; } } String tagName = null; do { xmlStreamReader.next(); if (!xmlStreamReader.hasName()) { continue; } tagName = xmlStreamReader.getName().toString(); if (xmlStreamReader.isStartElement()) { switch (tagName) { case "properties": parseProperties(xmlStreamReader, builder); break; case "testcase": parseTestCase(xmlStreamReader, builder); break; case "system-out": skipElement(xmlStreamReader, "system-out"); break; case "system-err": skipElement(xmlStreamReader, "system-err"); break; default: handleUnsupportedElement("testsuite", tagName); } } } while (!xmlStreamReader.isEndElement() || !"testsuite".equals(tagName)); return builder.build(); }
Example 16
Source File: AntXmlParser.java From rich-test-results with Apache License 2.0 | 4 votes |
private void parseTestCase(XMLStreamReader xmlStreamReader, TestSuite.Builder suiteBuilder) throws XMLStreamException, XmlParseException { TestCase.Builder builder = suiteBuilder.addTestCaseBuilder(); builder.setStatus(TestStatus.PASSED); for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) { String attributeValue = xmlStreamReader.getAttributeValue(i); switch (xmlStreamReader.getAttributeName(i).toString()) { case "name": builder.setName(attributeValue); break; case "classname": builder.setClassName(attributeValue); break; case "time": builder.setElapsedTimeMillis((long) (Float.parseFloat(attributeValue) * 1000)); break; } } String tagName = null; do { xmlStreamReader.next(); if (!xmlStreamReader.hasName()) { continue; } tagName = xmlStreamReader.getName().toString(); if (xmlStreamReader.isStartElement()) { switch (tagName) { case "failure": builder.setStatus(TestStatus.FAILED); parseStackTrace(xmlStreamReader, builder.addFailureBuilder(), "failure"); break; case "error": builder.setStatus(TestStatus.ERROR); parseStackTrace(xmlStreamReader, builder.getErrorBuilder(), "error"); break; case "skipped": builder.setStatus(TestStatus.SKIPPED); builder.setSkippedMessage(getElementContent(xmlStreamReader, "skipped")); break; case "system-out": skipElement(xmlStreamReader, "system-out"); break; case "system-err": skipElement(xmlStreamReader, "system-err"); break; default: handleUnsupportedElement("testcase", tagName); } } } while (!xmlStreamReader.isEndElement() || !"testcase".equals(tagName)); }