Search in sources :

Example 1 with Gender

use of com.facebook.buck.android.StringResources.Gender in project buck by facebook.

the class CompileStringsStep method getGender.

/**
   * Returns the Gender present in the passed in element's attribute, defaults to unknown gender
   * @param element the element for which gender attribute is to be determined
   * @return gender present in the element and unknown gender if not
   */
private static Gender getGender(Element element) {
    Gender gender = Gender.unknown;
    boolean hasGender = element.hasAttribute("gender");
    if (hasGender) {
        gender = Gender.valueOf(element.getAttribute("gender"));
    }
    return gender;
}
Also used : Gender(com.facebook.buck.android.StringResources.Gender)

Example 2 with Gender

use of com.facebook.buck.android.StringResources.Gender in project buck by facebook.

the class CompileStringsStepTest method testScrapePluralsNodes.

@Test
public void testScrapePluralsNodes() throws IOException, SAXException {
    String xmlInput = "<plurals name='name1' gender='unknown'>" + "<item quantity='zero'>%d people saw this</item>" + "<item quantity='one'>%d person saw this</item>" + "<item quantity='many'>%d people saw this</item>" + "</plurals>" + "<plurals name='name1_f1gender' gender='female'>" + "<item quantity='zero'>%d people saw this f1</item>" + "<item quantity='one'>%d person saw this f1</item>" + "<item quantity='many'>%d people saw this f1</item>" + "</plurals>" + "<plurals name='name2' gender='unknown'>" + "<item quantity='zero'>%d people ate this</item>" + "<item quantity='many'>%d people ate this</item>" + "</plurals>" + "<plurals name='name2_m2gender' gender='male'>" + "<item quantity='zero'>%d people ate this m2</item>" + "<item quantity='many'>%d people ate this m2</item>" + "</plurals>" + // Test empty array.
    "<plurals name='name3' gender='unknown'></plurals>" + // Ignored since "name2" already found.
    "<plurals name='name2' gender='unknown'></plurals>";
    NodeList pluralsNodes = XmlDomParser.parse(createResourcesXml(xmlInput)).getElementsByTagName("plurals");
    EnumMap<Gender, ImmutableMap<String, String>> map1 = Maps.newEnumMap(Gender.class);
    map1.put(Gender.unknown, ImmutableMap.of("zero", "%d people saw this", "one", "%d person saw this", "many", "%d people saw this"));
    map1.put(Gender.female, ImmutableMap.of("zero", "%d people saw this f1", "one", "%d person saw this f1", "many", "%d people saw this f1"));
    EnumMap<Gender, ImmutableMap<String, String>> map2 = Maps.newEnumMap(Gender.class);
    map2.put(Gender.unknown, ImmutableMap.of("zero", "%d people ate this", "many", "%d people ate this"));
    map2.put(Gender.male, ImmutableMap.of("zero", "%d people ate this m2", "many", "%d people ate this m2"));
    EnumMap<Gender, ImmutableMap<String, String>> map3 = Maps.newEnumMap(Gender.class);
    map3.put(Gender.unknown, ImmutableMap.of());
    Map<Integer, EnumMap<Gender, ImmutableMap<String, String>>> pluralsMap = Maps.newHashMap();
    CompileStringsStep step = createNonExecutingStep();
    step.addPluralsResourceNameToIdMap(ImmutableMap.of("name1", 1, "name2", 2, "name3", 3));
    step.scrapePluralsNodes(pluralsNodes, pluralsMap);
    assertEquals("Incorrect map of resource id to plural values.", ImmutableMap.of(1, map1, 2, map2, 3, map3), pluralsMap);
}
Also used : NodeList(org.w3c.dom.NodeList) Gender(com.facebook.buck.android.StringResources.Gender) EnumMap(java.util.EnumMap) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 3 with Gender

use of com.facebook.buck.android.StringResources.Gender in project buck by facebook.

the class CompileStringsStepTest method testScrapeNodesWithSameName.

@Test
public void testScrapeNodesWithSameName() throws IOException, SAXException {
    String xmlInput = "<string name='name1' gender='unknown'>1</string>" + "<string name='name1_f1gender' gender='female'>1 f1</string>" + "<plurals name='name1' gender='unknown'>" + "<item quantity='one'>2</item>" + "<item quantity='other'>3</item>" + "</plurals>" + "<plurals name='name1_f1gender' gender='female'>" + "<item quantity='one'>2 f1</item>" + "<item quantity='other'>3 f1</item>" + "</plurals>" + "<string-array name='name1' gender='unknown'>" + "<item>4</item>" + "<item>5</item>" + "</string-array>" + "<string-array name='name1_f1gender' gender='female'>" + "<item>4 f1</item>" + "<item>5 f1</item>" + "</string-array>";
    NodeList stringNodes = XmlDomParser.parse(createResourcesXml(xmlInput)).getElementsByTagName("string");
    NodeList pluralsNodes = XmlDomParser.parse(createResourcesXml(xmlInput)).getElementsByTagName("plurals");
    NodeList arrayNodes = XmlDomParser.parse(createResourcesXml(xmlInput)).getElementsByTagName("string-array");
    Map<Integer, EnumMap<Gender, String>> stringMap = Maps.newTreeMap();
    Map<Integer, EnumMap<Gender, ImmutableMap<String, String>>> pluralsMap = Maps.newTreeMap();
    Map<Integer, EnumMap<Gender, ImmutableList<String>>> arraysMap = Maps.newTreeMap();
    EnumMap<Gender, String> map1 = Maps.newEnumMap(Gender.class);
    map1.put(Gender.unknown, "1");
    map1.put(Gender.female, "1 f1");
    EnumMap<Gender, Map<String, String>> map2 = Maps.newEnumMap(Gender.class);
    map2.put(Gender.unknown, ImmutableMap.of("one", "2", "other", "3"));
    map2.put(Gender.female, ImmutableMap.of("one", "2 f1", "other", "3 f1"));
    EnumMap<Gender, ImmutableList<String>> map3 = Maps.newEnumMap(Gender.class);
    map3.put(Gender.unknown, ImmutableList.of("4", "5"));
    map3.put(Gender.female, ImmutableList.of("4 f1", "5 f1"));
    CompileStringsStep step = createNonExecutingStep();
    step.addStringResourceNameToIdMap(ImmutableMap.of("name1", 1));
    step.addPluralsResourceNameToIdMap(ImmutableMap.of("name1", 2));
    step.addArrayResourceNameToIdMap(ImmutableMap.of("name1", 3));
    step.scrapeStringNodes(stringNodes, stringMap);
    step.scrapePluralsNodes(pluralsNodes, pluralsMap);
    step.scrapeStringArrayNodes(arrayNodes, arraysMap);
    assertEquals("Incorrect map of resource id to string.", ImmutableMap.of(1, map1), stringMap);
    assertEquals("Incorrect map of resource id to plurals.", ImmutableMap.of(2, map2), pluralsMap);
    assertEquals("Incorrect map of resource id to string arrays.", ImmutableMap.of(3, map3), arraysMap);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) NodeList(org.w3c.dom.NodeList) Gender(com.facebook.buck.android.StringResources.Gender) EnumMap(java.util.EnumMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) EnumMap(java.util.EnumMap) Test(org.junit.Test)

Example 4 with Gender

use of com.facebook.buck.android.StringResources.Gender in project buck by facebook.

the class CompileStringsStepTest method testScrapeStringArrayNodes.

@Test
public void testScrapeStringArrayNodes() throws IOException, SAXException {
    String xmlInput = "<string-array name='name1' gender='unknown'>" + "<item>Value12</item>" + "<item>Value11</item>" + "</string-array>" + "<string-array name='name1_f1gender' gender='female'>" + "<item>Value12 f1</item>" + "<item>Value11 f1</item>" + "</string-array>" + "<string-array name='name2' gender='unknown'>" + "<item>Value21</item>" + "</string-array>" + "<string-array name='name2_m2gender' gender='male'>" + "<item>Value21 m2</item>" + "</string-array>" + "<string-array name='name3' gender='unknown'></string-array>" + "<string-array name='name2' gender='unknown'>" + // Ignored because "name2" already found above.
    "<item>ignored</item>" + "</string-array>";
    EnumMap<Gender, List<String>> map1 = Maps.newEnumMap(Gender.class);
    map1.put(Gender.unknown, ImmutableList.of("Value12", "Value11"));
    map1.put(Gender.female, ImmutableList.of("Value12 f1", "Value11 f1"));
    EnumMap<Gender, List<String>> map2 = Maps.newEnumMap(Gender.class);
    map2.put(Gender.unknown, ImmutableList.of("Value21"));
    map2.put(Gender.male, ImmutableList.of("Value21 m2"));
    NodeList arrayNodes = XmlDomParser.parse(createResourcesXml(xmlInput)).getElementsByTagName("string-array");
    Map<Integer, EnumMap<Gender, ImmutableList<String>>> arraysMap = Maps.newTreeMap();
    CompileStringsStep step = createNonExecutingStep();
    step.addArrayResourceNameToIdMap(ImmutableMap.of("name1", 1, "name2", 2, "name3", 3));
    step.scrapeStringArrayNodes(arrayNodes, arraysMap);
    assertEquals("Incorrect map of resource id to string arrays.", ImmutableMap.of(1, map1, 2, map2), arraysMap);
}
Also used : NodeList(org.w3c.dom.NodeList) ImmutableList(com.google.common.collect.ImmutableList) NodeList(org.w3c.dom.NodeList) List(java.util.List) Gender(com.facebook.buck.android.StringResources.Gender) EnumMap(java.util.EnumMap) Test(org.junit.Test)

Example 5 with Gender

use of com.facebook.buck.android.StringResources.Gender in project buck by facebook.

the class CompileStringsStep method scrapeStringArrayNodes.

/**
   * Similar to {@code scrapeStringNodes}, but for string array nodes.
   */
@VisibleForTesting
void scrapeStringArrayNodes(NodeList arrayNodes, Map<Integer, EnumMap<Gender, ImmutableList<String>>> arraysMap) {
    for (int i = 0; i < arrayNodes.getLength(); ++i) {
        Element element = (Element) arrayNodes.item(i);
        String resourceName = element.getAttribute("name");
        Gender gender = getGender(element);
        Integer resourceId = getResourceId(resourceName, gender, arrayResourceNameToIdMap);
        // Ignore a resource if R.txt does not contain an entry for it.
        if (resourceId == null) {
            continue;
        }
        EnumMap<Gender, ImmutableList<String>> genderMap = arraysMap.get(resourceId);
        if (genderMap == null) {
            genderMap = Maps.newEnumMap(Gender.class);
        } else if (genderMap.containsKey(gender)) {
            // Ignore a resource if it has already been found
            continue;
        }
        ImmutableList.Builder<String> arrayValues = ImmutableList.builder();
        NodeList itemNodes = element.getElementsByTagName("item");
        if (itemNodes.getLength() == 0) {
            continue;
        }
        for (int j = 0; j < itemNodes.getLength(); ++j) {
            arrayValues.add(itemNodes.item(j).getTextContent());
        }
        genderMap.put(gender, arrayValues.build());
        arraysMap.put(resourceId, genderMap);
    }
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Gender(com.facebook.buck.android.StringResources.Gender) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

Gender (com.facebook.buck.android.StringResources.Gender)8 NodeList (org.w3c.dom.NodeList)6 EnumMap (java.util.EnumMap)4 Test (org.junit.Test)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Element (org.w3c.dom.Element)3 List (java.util.List)1 Map (java.util.Map)1 Node (org.w3c.dom.Node)1