use of javax.xml.stream.XMLEventReader in project spring-framework by spring-projects.
the class StaxEventXMLReaderTests method partial.
@Test
public void partial() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(CONTENT));
// skip to root
eventReader.nextTag();
StaxEventXMLReader xmlReader = new StaxEventXMLReader(eventReader);
ContentHandler contentHandler = mock(ContentHandler.class);
xmlReader.setContentHandler(contentHandler);
xmlReader.parse(new InputSource());
verify(contentHandler).startDocument();
verify(contentHandler).startElement(eq("http://springframework.org/spring-ws"), eq("child"), eq("child"), any(Attributes.class));
verify(contentHandler).endElement("http://springframework.org/spring-ws", "child", "child");
verify(contentHandler).endDocument();
}
use of javax.xml.stream.XMLEventReader in project spring-framework by spring-projects.
the class ListBasedXMLEventReaderTests method readEvents.
private List<XMLEvent> readEvents(String xml) throws XMLStreamException {
XMLEventReader reader = this.inputFactory.createXMLEventReader(new StringReader(xml));
List<XMLEvent> events = new ArrayList<>();
while (reader.hasNext()) {
events.add(reader.nextEvent());
}
return events;
}
use of javax.xml.stream.XMLEventReader in project camel by apache.
the class ScrHelper method getScrProperties.
public static Map<String, String> getScrProperties(String xmlLocation, String componentName) throws Exception {
Map<String, String> result = new HashMap<>();
XMLInputFactory inputFactory = XMLInputFactory.newFactory();
inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
XMLEventReader eventReader = inputFactory.createXMLEventReader(new FileReader(xmlLocation));
boolean collect = false;
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.getEventType() == XMLStreamConstants.START_ELEMENT && event.asStartElement().getName().toString().equals("scr:component") && event.asStartElement().getAttributeByName(QName.valueOf("name")).getValue().equals(componentName)) {
collect = true;
} else if (collect && event.getEventType() == XMLStreamConstants.START_ELEMENT && event.asStartElement().getName().toString().equals("property")) {
result.put(event.asStartElement().getAttributeByName(QName.valueOf("name")).getValue(), event.asStartElement().getAttributeByName(QName.valueOf("value")).getValue());
} else if (collect && event.getEventType() == XMLStreamConstants.END_ELEMENT && event.asEndElement().getName().toString().equals("scr:component")) {
break;
}
}
return result;
}
use of javax.xml.stream.XMLEventReader in project languagetool by languagetool-org.
the class XmlUsageCounter method countElementsAndAttributes.
private void countElementsAndAttributes(InputStream in) throws XMLStreamException {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
String elementName = event.asStartElement().getName().getLocalPart();
add(elementName);
Iterator attributes = event.asStartElement().getAttributes();
while (attributes.hasNext()) {
Attribute att = (Attribute) attributes.next();
add(elementName + "/" + att.getName());
}
}
}
}
use of javax.xml.stream.XMLEventReader in project bazel by bazelbuild.
the class DataValueFileWithIds method parse.
public static void parse(XMLInputFactory xmlInputFactory, Path source, FullyQualifiedName fileKey, FullyQualifiedName.Factory fqnFactory, KeyValueConsumer<DataKey, DataResource> overwritingConsumer, KeyValueConsumer<DataKey, DataResource> combiningConsumer) throws IOException, XMLStreamException {
ImmutableSet.Builder<String> newIds = ImmutableSet.builder();
try (BufferedInputStream inStream = new BufferedInputStream(Files.newInputStream(source))) {
XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(inStream, StandardCharsets.UTF_8.toString());
// forgiving and allow even non-android namespaced attributes to define a new ID.
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
StartElement start = event.asStartElement();
Iterator<Attribute> attributes = XmlResourceValues.iterateAttributesFrom(start);
while (attributes.hasNext()) {
Attribute attribute = attributes.next();
String value = attribute.getValue();
if (value.startsWith(SdkConstants.NEW_ID_PREFIX)) {
String idName = value.substring(SdkConstants.NEW_ID_PREFIX.length());
newIds.add(idName);
}
}
}
}
eventReader.close();
} catch (XMLStreamException e) {
throw new XMLStreamException(source + ": " + e.getMessage(), e.getLocation(), e);
} catch (RuntimeException e) {
throw new RuntimeException("Error parsing " + source, e);
}
ImmutableSet<String> idResources = newIds.build();
overwritingConsumer.consume(fileKey, DataValueFile.of(source));
for (String id : idResources) {
combiningConsumer.consume(fqnFactory.create(ResourceType.ID, id), DataResourceXml.createWithNoNamespace(source, IdXmlResourceValue.of()));
}
}
Aggregations