Search in sources :

Example 1 with EnumMap

use of java.util.EnumMap in project buck by facebook.

the class CompileStringsStep method compileStringFiles.

private StringResources compileStringFiles(ProjectFilesystem filesystem, Collection<Path> filepaths) throws IOException, SAXException {
    TreeMap<Integer, EnumMap<Gender, String>> stringsMap = Maps.newTreeMap();
    TreeMap<Integer, EnumMap<Gender, ImmutableMap<String, String>>> pluralsMap = Maps.newTreeMap();
    TreeMap<Integer, EnumMap<Gender, ImmutableList<String>>> arraysMap = Maps.newTreeMap();
    for (Path stringFilePath : filepaths) {
        Document dom = XmlDomParser.parse(filesystem.getPathForRelativePath(stringFilePath));
        NodeList stringNodes = dom.getElementsByTagName("string");
        scrapeStringNodes(stringNodes, stringsMap);
        NodeList pluralNodes = dom.getElementsByTagName("plurals");
        scrapePluralsNodes(pluralNodes, pluralsMap);
        NodeList arrayNodes = dom.getElementsByTagName("string-array");
        scrapeStringArrayNodes(arrayNodes, arraysMap);
    }
    return new StringResources(stringsMap, pluralsMap, arraysMap);
}
Also used : Path(java.nio.file.Path) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) EnumMap(java.util.EnumMap)

Example 2 with EnumMap

use of java.util.EnumMap 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 EnumMap

use of java.util.EnumMap 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 EnumMap

use of java.util.EnumMap 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 EnumMap

use of java.util.EnumMap in project buck by facebook.

the class StringResources method writeArrays.

/**
   * Writes the metadata and strings in the following format to the output stream:
   * [Int: # of arrays]
   * [Int: Smallest resource id among arrays]
   * [[Short: resource id delta] [Byte: #genders] [[Byte: gender enum ordinal] [Int: #elements]
   *  [[Short: length of element] x # of elements]] x # of genders] x # of arrays
   * [Byte array of string value] x Summation of genders over array elements over # of arrays
   * @param outputStream
   * @throws IOException
   */
private void writeArrays(DataOutputStream outputStream) throws IOException {
    outputStream.writeInt(arrays.keySet().size());
    if (arrays.keySet().isEmpty()) {
        return;
    }
    int previousResourceId = arrays.firstKey();
    outputStream.writeInt(previousResourceId);
    try (ByteArrayOutputStream dataStream = new ByteArrayOutputStream()) {
        for (Map.Entry<Integer, EnumMap<Gender, ImmutableList<String>>> entry : arrays.entrySet()) {
            writeShort(outputStream, entry.getKey() - previousResourceId);
            EnumMap<Gender, ImmutableList<String>> genderMap = entry.getValue();
            outputStream.writeByte(genderMap.size());
            for (Map.Entry<Gender, ImmutableList<String>> gender : genderMap.entrySet()) {
                outputStream.writeByte(gender.getKey().ordinal());
                ImmutableList<String> arrayElements = gender.getValue();
                outputStream.writeByte(arrayElements.size());
                for (String arrayValue : arrayElements) {
                    byte[] byteValue = getUnescapedStringBytes(arrayValue);
                    writeShort(outputStream, byteValue.length);
                    dataStream.write(byteValue);
                }
            }
            previousResourceId = entry.getKey();
        }
        outputStream.write(dataStream.toByteArray());
    }
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImmutableMap(com.google.common.collect.ImmutableMap) EnumMap(java.util.EnumMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap) EnumMap(java.util.EnumMap)

Aggregations

EnumMap (java.util.EnumMap)389 Map (java.util.Map)73 ArrayList (java.util.ArrayList)70 List (java.util.List)61 HashMap (java.util.HashMap)60 Test (org.junit.Test)46 IOException (java.io.IOException)38 Collection (java.util.Collection)35 DecodeHintType (com.google.zxing.DecodeHintType)30 HashSet (java.util.HashSet)26 Set (java.util.Set)26 EncodeHintType (com.google.zxing.EncodeHintType)17 File (java.io.File)17 BitMatrix (com.google.zxing.common.BitMatrix)15 BookID (biblemulticonverter.data.BookID)14 Iterator (java.util.Iterator)14 URL (java.net.URL)12 TreeMap (java.util.TreeMap)12 Header (com.jsql.model.bean.util.Header)10 Request (com.jsql.model.bean.util.Request)10