Search in sources :

Example 1 with XMLEventFactory

use of javax.xml.stream.XMLEventFactory 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

ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 FactoryConfigurationError (javax.xml.stream.FactoryConfigurationError)1 XMLEventFactory (javax.xml.stream.XMLEventFactory)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLEventWriter (javax.xml.stream.XMLEventWriter)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 Attribute (javax.xml.stream.events.Attribute)1 StartElement (javax.xml.stream.events.StartElement)1 XMLEvent (javax.xml.stream.events.XMLEvent)1