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);
}
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);
}
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);
}
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);
}
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());
}
}
Aggregations