AndroidStringsResource extends ResourceFilter { @Override public void write(OutputStream outStream, LanguageBundle languageBundle, FilterOptions options) throws IOException, ResourceFilterException { List<ResourceString> resStrings = languageBundle.getSortedResourceStrings(); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = null; try { docBuilder = docFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement(RESOURCES_STRING); for (ResourceString resString : resStrings) { String value = resString.getValue(); Map<String, String> plural_categories = null; if (value.startsWith("[") && value.endsWith("]")) { Element child = doc.createElement(STR_ARRAY); Attr attr = doc.createAttribute(NAME_STRING); attr.setValue(resString.getKey()); child.setAttributeNode(attr); int startIndex = 0; int endIndex = -1; while (endIndex < value.length() - 1) { endIndex = value.indexOf(',', startIndex); if (endIndex == -1) { endIndex = value.length() - 1; } String itemValue = value.substring(startIndex + 1, endIndex); Element arrayChild = doc.createElement("item"); arrayChild.setTextContent(itemValue); child.appendChild(arrayChild); startIndex = endIndex + 1; } rootElement.appendChild(child); } else if (!(plural_categories = getPluralCategories(value)).isEmpty()) { Element child = doc.createElement(PLURALS_STRING); Attr attr = doc.createAttribute(NAME_STRING); attr.setValue(resString.getKey()); child.setAttributeNode(attr); if (!resString.getNotes().isEmpty()) { List<String> comments = resString.getNotes(); for (String comment : comments) { Comment d_comment = doc.createComment(comment); child.appendChild(d_comment); } } for (String pKey : Constants.PLURAL_CATEGORIES) { if (plural_categories.containsKey(pKey)) { String pValue = plural_categories.get(pKey); Element item = doc.createElement(ITEM_STRING); Attr pAttr = doc.createAttribute(QUANTITY_STRING); pAttr.setValue(pKey); item.setAttributeNode(pAttr); item.setTextContent(pValue); child.appendChild(item); } } rootElement.appendChild(child); } else { Element child = doc.createElement(STR_STRING); Attr attr = doc.createAttribute(NAME_STRING); attr.setValue(resString.getKey()); child.setAttributeNode(attr); child.setTextContent(value); rootElement.appendChild(child); } } doc.appendChild(rootElement); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = null; try { transformer = transformerFactory.newTransformer(); } catch (TransformerConfigurationException e) { throw new ResourceFilterException(e); } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http: transformer.setOutputProperty(OutputKeys.STANDALONE, "no"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(outStream); try { transformer.transform(source, result); } catch (TransformerException e) { throw new ResourceFilterException(e); } }  @Override LanguageBundle parse(InputStream inStream, FilterOptions options); @Override void write(OutputStream outStream, LanguageBundle languageBundle, FilterOptions options); @Override void merge(InputStream baseStream, OutputStream outStream, LanguageBundle languageBundle,
            FilterOptions options);  }
@Test public void testWrite() throws IOException, ResourceFilterException{ File tempFile = File.createTempFile(this.getClass().getSimpleName(), ".xml"); tempFile.deleteOnExit(); try (OutputStream os = new FileOutputStream(tempFile)) { res.write(os, WRITE_BUNDLE, null); os.flush(); assertTrue(ResourceTestUtil.compareFiles(EXPECTED_WRITE_FILE, tempFile, 1)); } }