use of javax.xml.stream.events.XMLEvent in project bazel by bazelbuild.
the class AttrXmlResourceValue method from.
/**
* Creates a new {@link AttrXmlResourceValue}. Returns null if there are no formats.
*/
@Nullable
public static XmlResourceValue from(StartElement attr, @Nullable String format, XMLEventReader eventReader) throws XMLStreamException {
Set<String> formatNames = new HashSet<>();
if (format != null) {
Collections.addAll(formatNames, format.split("\\|"));
}
XMLEvent nextTag = XmlResourceValues.peekNextTag(eventReader);
if (nextTag != null && nextTag.isStartElement()) {
QName tagName = nextTag.asStartElement().getName();
if (TAG_FLAG.equals(tagName)) {
formatNames.add(FLAGS);
} else {
formatNames.add(tagName.getLocalPart().toLowerCase());
}
}
Builder<String, ResourceXmlAttrValue> formats = ImmutableMap.builder();
for (String formatName : formatNames) {
switch(formatName) {
case FLAGS:
Map<String, String> flags = readSubValues(eventReader, TAG_FLAG);
endAttrElement(eventReader);
formats.put(formatName, FlagResourceXmlAttrValue.of(flags));
break;
case ENUM:
Map<String, String> enums = readSubValues(eventReader, TAG_ENUM);
endAttrElement(eventReader);
formats.put(formatName, EnumResourceXmlAttrValue.of(enums));
break;
case REFERENCE:
formats.put(formatName, ReferenceResourceXmlAttrValue.of());
break;
case COLOR:
formats.put(formatName, ColorResourceXmlAttrValue.of());
break;
case BOOLEAN:
formats.put(formatName, BooleanResourceXmlAttrValue.of());
break;
case DIMENSION:
formats.put(formatName, DimensionResourceXmlAttrValue.of());
break;
case FLOAT:
formats.put(formatName, FloatResourceXmlAttrValue.of());
break;
case INTEGER:
formats.put(formatName, IntegerResourceXmlAttrValue.of());
break;
case STRING:
formats.put(formatName, StringResourceXmlAttrValue.of());
break;
case FRACTION:
formats.put(formatName, FractionResourceXmlAttrValue.of());
break;
default:
throw new XMLStreamException(String.format("Unexpected attr format: %S", formatName), attr.getLocation());
}
}
return of(formats.build());
}
use of javax.xml.stream.events.XMLEvent in project bazel by bazelbuild.
the class XmlResourceValues method parseDeclareStyleable.
static void parseDeclareStyleable(FullyQualifiedName.Factory fqnFactory, Path path, KeyValueConsumer<DataKey, DataResource> overwritingConsumer, KeyValueConsumer<DataKey, DataResource> combiningConsumer, XMLEventReader eventReader, StartElement start) throws XMLStreamException {
Map<FullyQualifiedName, Boolean> members = new LinkedHashMap<>();
for (XMLEvent element = nextTag(eventReader); !isEndTag(element, TAG_DECLARE_STYLEABLE); element = nextTag(eventReader)) {
if (isStartTag(element, TAG_ATTR)) {
StartElement attr = element.asStartElement();
FullyQualifiedName attrName = fqnFactory.create(ResourceType.ATTR, getElementName(attr));
// Without those, it will be an attr reference.
if (XmlResourceValues.getElementAttributeByName(attr, ATTR_FORMAT) != null || (XmlResourceValues.peekNextTag(eventReader) != null && XmlResourceValues.peekNextTag(eventReader).isStartElement())) {
overwritingConsumer.consume(attrName, DataResourceXml.createWithNoNamespace(path, parseAttr(eventReader, attr)));
members.put(attrName, Boolean.TRUE);
} else {
members.put(attrName, Boolean.FALSE);
}
}
}
combiningConsumer.consume(fqnFactory.create(ResourceType.STYLEABLE, getElementName(start)), DataResourceXml.createWithNoNamespace(path, StyleableXmlResourceValue.of(members)));
}
use of javax.xml.stream.events.XMLEvent in project bazel by bazelbuild.
the class XmlResourceValues method nextTag.
public static XMLEvent nextTag(XMLEventReader eventReader) throws XMLStreamException {
while (eventReader.hasNext() && !(eventReader.peek().isEndElement() || eventReader.peek().isStartElement())) {
XMLEvent nextEvent = eventReader.nextEvent();
if (nextEvent.isCharacters() && !nextEvent.asCharacters().isIgnorableWhiteSpace()) {
Characters characters = nextEvent.asCharacters();
// TODO(corysmith): Turn into a warning with the Path is available to add to it.
// This case is when unexpected characters are thrown into the xml. Best case, it's a
// incorrect comment type...
logger.fine(String.format("Invalid characters [%s] found at %s", characters.getData(), characters.getLocation().getLineNumber()));
}
}
return eventReader.nextEvent();
}
use of javax.xml.stream.events.XMLEvent 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()));
}
}
use of javax.xml.stream.events.XMLEvent 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