Search in sources :

Example 16 with StartElement

use of javax.xml.stream.events.StartElement in project bazel by bazelbuild.

the class AttrXmlResourceValue method readSubValues.

private static Map<String, String> readSubValues(XMLEventReader reader, QName subTagType) throws XMLStreamException {
    Builder<String, String> builder = ImmutableMap.builder();
    while (reader.hasNext() && XmlResourceValues.isTag(XmlResourceValues.peekNextTag(reader), subTagType)) {
        StartElement element = reader.nextEvent().asStartElement();
        builder.put(XmlResourceValues.getElementName(element), XmlResourceValues.getElementValue(element));
        XMLEvent endTag = reader.nextEvent();
        if (!XmlResourceValues.isEndTag(endTag, subTagType)) {
            throw new XMLStreamException(String.format("Unexpected [%s]; Expected %s", endTag, "</enum>"), endTag.getLocation());
        }
    }
    return builder.build();
}
Also used : StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 17 with StartElement

use of javax.xml.stream.events.StartElement in project bazel by bazelbuild.

the class DataResourceXml method parse.

/**
   * Parses xml resources from a Path to the provided overwritable and combining collections.
   *
   * <p>This method is a bit tricky in the service of performance -- creating several collections
   * and merging them was more expensive than writing to mutable collections directly.
   *
   * @param xmlInputFactory Used to create an XMLEventReader from the supplied resource path.
   * @param path The path to the xml resource to be parsed.
   * @param fqnFactory Used to create {@link FullyQualifiedName}s from the resource names.
   * @param overwritingConsumer A consumer for overwritable {@link DataResourceXml}s.
   * @param combiningConsumer A consumer for combining {@link DataResourceXml}s.
   * @throws XMLStreamException Thrown with the resource format is invalid.
   * @throws FactoryConfigurationError Thrown with the {@link XMLInputFactory} is misconfigured.
   * @throws IOException Thrown when there is an error reading a file.
   */
public static void parse(XMLInputFactory xmlInputFactory, Path path, Factory fqnFactory, KeyValueConsumer<DataKey, DataResource> overwritingConsumer, KeyValueConsumer<DataKey, DataResource> combiningConsumer) throws XMLStreamException, FactoryConfigurationError, IOException {
    XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(new BufferedInputStream(Files.newInputStream(path)), StandardCharsets.UTF_8.toString());
    try {
        // TODO(corysmith): Make the xml parsing more readable.
        for (StartElement resources = XmlResourceValues.moveToResources(eventReader); resources != null; resources = XmlResourceValues.moveToResources(eventReader)) {
            // Record attributes on the <resources> tag.
            Iterator<Attribute> attributes = XmlResourceValues.iterateAttributesFrom(resources);
            while (attributes.hasNext()) {
                Attribute attribute = attributes.next();
                Namespaces namespaces = Namespaces.from(attribute.getName());
                String attributeName = attribute.getName().getNamespaceURI().isEmpty() ? attribute.getName().getLocalPart() : attribute.getName().getPrefix() + ":" + attribute.getName().getLocalPart();
                overwritingConsumer.consume(fqnFactory.create(VirtualType.RESOURCES_ATTRIBUTE, attributeName), DataResourceXml.createWithNamespaces(path, ResourcesAttribute.of(attributeName, attribute.getValue()), namespaces));
            }
            // Process resource declarations.
            for (StartElement start = XmlResourceValues.findNextStart(eventReader); start != null; start = XmlResourceValues.findNextStart(eventReader)) {
                Namespaces.Collector namespacesCollector = Namespaces.collector();
                if (XmlResourceValues.isEatComment(start) || XmlResourceValues.isSkip(start)) {
                    continue;
                }
                ResourceType resourceType = getResourceType(start);
                if (resourceType == null) {
                    throw new XMLStreamException(path + " contains an unrecognized resource type: " + start, start.getLocation());
                }
                if (resourceType == DECLARE_STYLEABLE) {
                    // Styleables are special, as they produce multiple overwrite and combining values,
                    // so we let the value handle the assignments.
                    XmlResourceValues.parseDeclareStyleable(fqnFactory, path, overwritingConsumer, combiningConsumer, eventReader, start);
                } else {
                    // Of simple resources, only IDs and Public are combining.
                    KeyValueConsumer<DataKey, DataResource> consumer = (resourceType == ID || resourceType == PUBLIC) ? combiningConsumer : overwritingConsumer;
                    String elementName = XmlResourceValues.getElementName(start);
                    if (elementName == null) {
                        throw new XMLStreamException(String.format("resource name is required for %s", resourceType), start.getLocation());
                    }
                    FullyQualifiedName key = fqnFactory.create(resourceType, elementName);
                    XmlResourceValue xmlResourceValue = parseXmlElements(resourceType, eventReader, start, namespacesCollector);
                    consumer.consume(key, DataResourceXml.createWithNamespaces(path, xmlResourceValue, namespacesCollector.toNamespaces()));
                }
            }
        }
    } catch (XMLStreamException e) {
        throw new XMLStreamException(path + ": " + e.getMessage(), e.getLocation(), e);
    } catch (RuntimeException e) {
        throw new RuntimeException("Error parsing " + path, e);
    }
}
Also used : Namespaces(com.google.devtools.build.android.xml.Namespaces) Attribute(javax.xml.stream.events.Attribute) ResourcesAttribute(com.google.devtools.build.android.xml.ResourcesAttribute) ResourceType(com.android.resources.ResourceType) StartElement(javax.xml.stream.events.StartElement) StyleableXmlResourceValue(com.google.devtools.build.android.xml.StyleableXmlResourceValue) PublicXmlResourceValue(com.google.devtools.build.android.xml.PublicXmlResourceValue) SimpleXmlResourceValue(com.google.devtools.build.android.xml.SimpleXmlResourceValue) StyleXmlResourceValue(com.google.devtools.build.android.xml.StyleXmlResourceValue) ArrayXmlResourceValue(com.google.devtools.build.android.xml.ArrayXmlResourceValue) IdXmlResourceValue(com.google.devtools.build.android.xml.IdXmlResourceValue) AttrXmlResourceValue(com.google.devtools.build.android.xml.AttrXmlResourceValue) PluralXmlResourceValue(com.google.devtools.build.android.xml.PluralXmlResourceValue) XMLStreamException(javax.xml.stream.XMLStreamException) BufferedInputStream(java.io.BufferedInputStream) XMLEventReader(javax.xml.stream.XMLEventReader)

Example 18 with StartElement

use of javax.xml.stream.events.StartElement in project hibernate-orm by hibernate.

the class FilteringXMLEventReader method internalNext.

protected final XMLEvent internalNext(boolean peek) throws XMLStreamException {
    XMLEvent event = null;
    if (peekedEvent != null) {
        event = peekedEvent;
        peekedEvent = null;
        return event;
    }
    do {
        event = super.getParent().nextEvent();
        //If there are pruned elements in the queue filtering events is still needed
        if (!prunedElements.isEmpty()) {
            //If another start element add it to the queue
            if (event.isStartElement()) {
                final StartElement startElement = event.asStartElement();
                prunedElements.push(startElement.getName());
            } else //If end element pop the newest name of the queue and double check that the start/end elements match up
            if (event.isEndElement()) {
                final QName startElementName = prunedElements.pop();
                final EndElement endElement = event.asEndElement();
                final QName endElementName = endElement.getName();
                if (!startElementName.equals(endElementName)) {
                    throw new IllegalArgumentException("Malformed XMLEvent stream. Expected end element for " + startElementName + " but found end element for " + endElementName);
                }
            }
            event = null;
        } else {
            final XMLEvent filteredEvent = this.filterEvent(event, peek);
            //end element need to be removed as well
            if (filteredEvent == null && event.isStartElement()) {
                final StartElement startElement = event.asStartElement();
                final QName name = startElement.getName();
                prunedElements.push(name);
            }
            event = filteredEvent;
        }
    } while (event == null);
    return event;
}
Also used : StartElement(javax.xml.stream.events.StartElement) EndElement(javax.xml.stream.events.EndElement) QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 19 with StartElement

use of javax.xml.stream.events.StartElement in project wildfly by wildfly.

the class SchemaLocationsRewriteTestCase method getAttributeValues.

private List<String> getAttributeValues(URL url, String localPart) throws Exception {
    String document = IOUtils.toString(url.openStream());
    log.trace(document);
    List<String> values = new ArrayList<>();
    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    XMLEventReader eventReader = xmlif.createXMLEventReader(new ByteArrayInputStream(document.getBytes()));
    while (eventReader.hasNext()) {
        XMLEvent xmlEvent = eventReader.nextEvent();
        if (xmlEvent.getEventType() == XMLStreamConstants.START_ELEMENT) {
            StartElement startElement = xmlEvent.asStartElement();
            Attribute attribute = startElement.getAttributeByName(new QName("", localPart));
            if (attribute != null) {
                values.add(attribute.getValue());
            }
        }
    }
    return values;
}
Also used : StartElement(javax.xml.stream.events.StartElement) ByteArrayInputStream(java.io.ByteArrayInputStream) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 20 with StartElement

use of javax.xml.stream.events.StartElement in project uPortal by Jasig.

the class ImmutableUserLayoutXMLEventReader method filterEvent.

@Override
protected XMLEvent filterEvent(XMLEvent event, boolean peek) {
    if (event.isStartElement()) {
        final StartElement startElement = event.asStartElement();
        final QName name = startElement.getName();
        final String localPart = name.getLocalPart();
        if ("channel".equals(localPart) || "folder".equals(localPart)) {
            return new ImmutableStartElementWrapper(startElement, "folder".equals(localPart));
        }
    }
    return event;
}
Also used : StartElement(javax.xml.stream.events.StartElement) QName(javax.xml.namespace.QName)

Aggregations

StartElement (javax.xml.stream.events.StartElement)30 XMLEvent (javax.xml.stream.events.XMLEvent)20 QName (javax.xml.namespace.QName)11 XMLEventReader (javax.xml.stream.XMLEventReader)10 XMLStreamException (javax.xml.stream.XMLStreamException)9 Attribute (javax.xml.stream.events.Attribute)8 EndElement (javax.xml.stream.events.EndElement)8 XMLInputFactory (javax.xml.stream.XMLInputFactory)7 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 InputStream (java.io.InputStream)3 LinkedList (java.util.LinkedList)3 JAXBContext (javax.xml.bind.JAXBContext)3 Unmarshaller (javax.xml.bind.Unmarshaller)3 EventFilter (javax.xml.stream.EventFilter)3 Characters (javax.xml.stream.events.Characters)3 IPortletWindow (org.apereo.portal.portlet.om.IPortletWindow)3 AbstractGCEvent (com.tagtraum.perf.gcviewer.model.AbstractGCEvent)2 GCEvent (com.tagtraum.perf.gcviewer.model.GCEvent)2 BufferedInputStream (java.io.BufferedInputStream)2