Search in sources :

Example 1 with Attribute

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

the class JpaNamespaceTransformingEventReader method updateElementAttributes.

private List<Attribute> updateElementAttributes(StartElement startElement) {
    // adjust the version attribute
    List<Attribute> newElementAttributeList = new ArrayList<Attribute>();
    Iterator<?> existingAttributesIterator = startElement.getAttributes();
    while (existingAttributesIterator.hasNext()) {
        Attribute attribute = (Attribute) existingAttributesIterator.next();
        if (VERSION_ATTRIBUTE_NAME.equals(attribute.getName().getLocalPart())) {
            if (!DEFAULT_VERSION.equals(attribute.getName().getPrefix())) {
                newElementAttributeList.add(xmlEventFactory.createAttribute(attribute.getName(), DEFAULT_VERSION));
            }
        } else {
            newElementAttributeList.add(attribute);
        }
    }
    return newElementAttributeList;
}
Also used : Attribute(javax.xml.stream.events.Attribute) ArrayList(java.util.ArrayList)

Example 2 with Attribute

use of javax.xml.stream.events.Attribute 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());
            }
        }
    }
}
Also used : Attribute(javax.xml.stream.events.Attribute) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 3 with Attribute

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

the class XmlResourceValues method parseTagAttributes.

public static Map<String, String> parseTagAttributes(StartElement start) {
    // Using a map to deduplicate xmlns declarations on the attributes.
    Map<String, String> attributeMap = new LinkedHashMap<>();
    Iterator<Attribute> attributes = iterateAttributesFrom(start);
    while (attributes.hasNext()) {
        Attribute attribute = attributes.next();
        QName name = attribute.getName();
        // Name used as the resource key, so skip it here.
        if (ATTR_NAME.equals(name)) {
            continue;
        }
        String value = escapeXmlValues(attribute.getValue()).replace("\"", "&quot;");
        if (!name.getNamespaceURI().isEmpty()) {
            attributeMap.put(name.getPrefix() + ":" + attribute.getName().getLocalPart(), value);
        } else {
            attributeMap.put(attribute.getName().getLocalPart(), value);
        }
        Iterator<Namespace> namespaces = iterateNamespacesFrom(start);
        while (namespaces.hasNext()) {
            Namespace namespace = namespaces.next();
            attributeMap.put("xmlns:" + namespace.getPrefix(), namespace.getNamespaceURI());
        }
    }
    return attributeMap;
}
Also used : Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) Namespace(javax.xml.stream.events.Namespace) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with Attribute

use of javax.xml.stream.events.Attribute 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()));
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) ImmutableSet(com.google.common.collect.ImmutableSet) XMLStreamException(javax.xml.stream.XMLStreamException) BufferedInputStream(java.io.BufferedInputStream) Attribute(javax.xml.stream.events.Attribute) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader)

Example 5 with Attribute

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

the class AndroidManifestProcessor method writeManifestPackage.

/**
   * Overwrite the package attribute of {@code <manifest>} in an AndroidManifest.xml file.
   *
   * @param manifest The input manifest.
   * @param customPackage The package to write to the manifest.
   * @param output The output manifest to generate.
   * @return The output manifest if generated or the input manifest if no overwriting is required.
   */
/* TODO(apell): switch from custom xml parsing to Gradle merger with NO_PLACEHOLDER_REPLACEMENT
   * set when android common is updated to version 2.5.0.
   */
public Path writeManifestPackage(Path manifest, String customPackage, Path output) {
    if (Strings.isNullOrEmpty(customPackage)) {
        return manifest;
    }
    try {
        Files.createDirectories(output.getParent());
        XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(Files.newInputStream(manifest), UTF_8.name());
        XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(Files.newOutputStream(output), UTF_8.name());
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        while (reader.hasNext()) {
            XMLEvent event = reader.nextEvent();
            if (event.isStartElement() && event.asStartElement().getName().toString().equalsIgnoreCase("manifest")) {
                StartElement element = event.asStartElement();
                @SuppressWarnings("unchecked") Iterator<Attribute> attributes = element.getAttributes();
                ImmutableList.Builder<Attribute> newAttributes = ImmutableList.builder();
                while (attributes.hasNext()) {
                    Attribute attr = attributes.next();
                    if (attr.getName().toString().equalsIgnoreCase("package")) {
                        newAttributes.add(eventFactory.createAttribute("package", customPackage));
                    } else {
                        newAttributes.add(attr);
                    }
                }
                writer.add(eventFactory.createStartElement(element.getName(), newAttributes.build().iterator(), element.getNamespaces()));
            } else {
                writer.add(event);
            }
        }
        writer.flush();
    } catch (XMLStreamException | FactoryConfigurationError | IOException e) {
        throw new RuntimeException(e);
    }
    return output;
}
Also used : Attribute(javax.xml.stream.events.Attribute) XMLEventFactory(javax.xml.stream.XMLEventFactory) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEventWriter(javax.xml.stream.XMLEventWriter) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) FactoryConfigurationError(javax.xml.stream.FactoryConfigurationError)

Aggregations

Attribute (javax.xml.stream.events.Attribute)140 QName (javax.xml.namespace.QName)71 StartElement (javax.xml.stream.events.StartElement)62 XMLEvent (javax.xml.stream.events.XMLEvent)52 XMLEventReader (javax.xml.stream.XMLEventReader)30 Namespace (javax.xml.stream.events.Namespace)26 EndElement (javax.xml.stream.events.EndElement)25 ArrayList (java.util.ArrayList)23 XMLStreamException (javax.xml.stream.XMLStreamException)20 XMLInputFactory (javax.xml.stream.XMLInputFactory)18 InputStream (java.io.InputStream)14 IOException (java.io.IOException)12 Iterator (java.util.Iterator)11 ByteArrayInputStream (java.io.ByteArrayInputStream)7 FileInputStream (java.io.FileInputStream)7 HashMap (java.util.HashMap)7 Test (org.junit.Test)7 HashSet (java.util.HashSet)6 Characters (javax.xml.stream.events.Characters)5 QNm (org.brackit.xquery.atomic.QNm)5