use of com.facebook.buck.android.aapt.RDotTxtEntry.RType in project buck by facebook.
the class MiniAapt method processXmlFile.
@VisibleForTesting
void processXmlFile(ProjectFilesystem filesystem, Path xmlFile, ImmutableSet.Builder<RDotTxtEntry> references) throws IOException, XPathExpressionException, ResourceParseException {
try (InputStream stream = filesystem.newFileInputStream(xmlFile)) {
Document dom = parseXml(xmlFile, stream);
NodeList nodesWithIds = (NodeList) ANDROID_ID_DEFINITION.evaluate(dom, XPathConstants.NODESET);
for (int i = 0; i < nodesWithIds.getLength(); i++) {
String resourceName = nodesWithIds.item(i).getNodeValue();
if (!resourceName.startsWith(ID_DEFINITION_PREFIX)) {
throw new ResourceParseException("Invalid definition of a resource: '%s'", resourceName);
}
Preconditions.checkState(resourceName.startsWith(ID_DEFINITION_PREFIX));
resourceCollector.addIntResourceIfNotPresent(RType.ID, resourceName.substring(ID_DEFINITION_PREFIX.length()));
}
NodeList nodesUsingIds = (NodeList) ANDROID_ID_USAGE.evaluate(dom, XPathConstants.NODESET);
for (int i = 0; i < nodesUsingIds.getLength(); i++) {
String resourceName = nodesUsingIds.item(i).getNodeValue();
int slashPosition = resourceName.indexOf('/');
if (resourceName.charAt(0) != '@' || slashPosition == -1) {
throw new ResourceParseException("Invalid definition of a resource: '%s'", resourceName);
}
String rawRType = resourceName.substring(1, slashPosition);
String name = resourceName.substring(slashPosition + 1);
String nodeName = nodesUsingIds.item(i).getNodeName();
if (name.startsWith("android:") || nodeName.startsWith("tools:")) {
continue;
}
if (!RESOURCE_TYPES.containsKey(rawRType)) {
throw new ResourceParseException("Invalid reference '%s' in '%s'", resourceName, xmlFile);
}
RType rType = Preconditions.checkNotNull(RESOURCE_TYPES.get(rawRType));
references.add(new FakeRDotTxtEntry(IdType.INT, rType, sanitizeName(name)));
}
}
}
use of com.facebook.buck.android.aapt.RDotTxtEntry.RType in project buck by facebook.
the class MiniAapt method getResourceTypes.
private static ImmutableMap<String, RType> getResourceTypes() {
ImmutableMap.Builder<String, RType> types = ImmutableMap.builder();
for (RType rType : RType.values()) {
types.put(rType.toString(), rType);
}
types.put("string-array", RType.ARRAY);
types.put("integer-array", RType.ARRAY);
types.put("declare-styleable", RType.STYLEABLE);
return types.build();
}
use of com.facebook.buck.android.aapt.RDotTxtEntry.RType in project buck by facebook.
the class MiniAapt method processValuesFile.
/**
* Processes an {@code xml} file immediately under a {@code values} directory. See
* <a href="http://developer.android.com/guide/topics/resources/more-resources.html>More Resource
* Types</a> to find out more about how resources are defined.
* <p>
* For an input file with contents like:
* <pre>
* <?xml version="1.0" encoding="utf-8"?>
* <resources>
* <integer name="number">42</integer>
* <dimen name="dimension">10px</dimen>
* <string name="hello">World</string>
* <item name="my_fraction" type="fraction">1.5</item>
* </resources>
* </pre>
* the resulting resources would be:
* <ul>
* <li>R.integer.number</li>
* <li>R.dimen.dimension</li>
* <li>R.string.hello</li>
* <li>R.fraction.my_fraction</li>
* </ul>
*/
@VisibleForTesting
void processValuesFile(ProjectFilesystem filesystem, Path valuesFile) throws IOException, ResourceParseException {
try (InputStream stream = filesystem.newFileInputStream(valuesFile)) {
Document dom = parseXml(valuesFile, stream);
Element root = dom.getDocumentElement();
// rather than being hidden until generated resources are updated.
if (root.getAttribute("exclude-from-buck-resource-map").equals("true")) {
return;
}
for (Node node = root.getFirstChild(); node != null; node = node.getNextSibling()) {
if (node.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
String resourceType = node.getNodeName();
if (resourceType.equals(ITEM_TAG)) {
Node typeNode = node.getAttributes().getNamedItem("type");
if (typeNode == null) {
throw new ResourceParseException("Error parsing file '%s', expected a 'type' attribute in: \n'%s'\n", valuesFile, node.toString());
}
resourceType = typeNode.getNodeValue();
}
if (IGNORED_TAGS.contains(resourceType)) {
continue;
}
if (!RESOURCE_TYPES.containsKey(resourceType)) {
throw new ResourceParseException("Invalid resource type '<%s>' in '%s'.", resourceType, valuesFile);
}
RType rType = Preconditions.checkNotNull(RESOURCE_TYPES.get(resourceType));
addToResourceCollector(node, rType);
}
}
}
use of com.facebook.buck.android.aapt.RDotTxtEntry.RType in project buck by facebook.
the class MiniAapt method processFileNamesInDirectory.
void processFileNamesInDirectory(ProjectFilesystem filesystem, Path dir) throws IOException, ResourceParseException {
String dirname = dir.getFileName().toString();
int dashIndex = dirname.indexOf('-');
if (dashIndex != -1) {
dirname = dirname.substring(0, dashIndex);
}
if (!RESOURCE_TYPES.containsKey(dirname)) {
throw new ResourceParseException("'%s' is not a valid resource sub-directory.", dir);
}
for (Path resourceFile : filesystem.getDirectoryContents(dir)) {
if (shouldIgnoreFile(resourceFile, filesystem)) {
continue;
}
String filename = resourceFile.getFileName().toString();
int dotIndex = filename.indexOf('.');
String resourceName = dotIndex != -1 ? filename.substring(0, dotIndex) : filename;
RType rType = Preconditions.checkNotNull(RESOURCE_TYPES.get(dirname));
if (rType == RType.DRAWABLE) {
processDrawables(filesystem, resourceFile);
} else {
resourceCollector.addIntResourceIfNotPresent(rType, resourceName);
}
}
}
Aggregations