class 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) {
            // runtime problem
            throw new RuntimeException(e);
        }

        // root elements
        Document doc = docBuilder.newDocument();

        // creating <resources></resources>
        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("]")) {
                // creating <string-array name="$NAME">
                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);
                    }
                }

                /**
                 * Append plural category items Show the items in predefined
                 * order
                 */
                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 {
                // creating <string name=$NAME>VALUE</string>
                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) {
            // runtime problem
            throw new ResourceFilterException(e);
        }

        // to add the tab spacing to files
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        transformer.setOutputProperty(OutputKeys.STANDALONE, "no");

        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(outStream);

        // write the file
        try {
            transformer.transform(source, result);
        } catch (TransformerException e) {
            // runtime problem?
            throw new ResourceFilterException(e);
        }
    }

}