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;
}
Aggregations