Search in sources :

Example 1 with RType

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)));
        }
    }
}
Also used : InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) RType(com.facebook.buck.android.aapt.RDotTxtEntry.RType) Document(org.w3c.dom.Document) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with RType

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();
}
Also used : RType(com.facebook.buck.android.aapt.RDotTxtEntry.RType) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with RType

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);
        }
    }
}
Also used : InputStream(java.io.InputStream) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) RType(com.facebook.buck.android.aapt.RDotTxtEntry.RType) Document(org.w3c.dom.Document) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with 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);
        }
    }
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) RType(com.facebook.buck.android.aapt.RDotTxtEntry.RType)

Aggregations

RType (com.facebook.buck.android.aapt.RDotTxtEntry.RType)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 InputStream (java.io.InputStream)2 Document (org.w3c.dom.Document)2 SourcePath (com.facebook.buck.rules.SourcePath)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Path (java.nio.file.Path)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1