use of com.google.devtools.build.android.xml.Namespaces 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);
}
}
Aggregations