class XLIFFResource extends ResourceFilter {

    @Override
    public void write(OutputStream outStream, LanguageBundle languageBundle,
            FilterOptions options) throws IOException, ResourceFilterException {

        List<ResourceString> resStrings = languageBundle.getSortedResourceStrings();

        String targetLanguage = languageBundle.getEmbeddedLanguageCode();
        if (targetLanguage == null || targetLanguage.isEmpty()) {
            throw new ResourceFilterException("Target language is not specified.");
        }

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

        Element xliff = doc.createElement(XLIFF_STRING);
        xliff.setAttribute(VERSION_STRING, VERSION_NUMBER_STRING);
        xliff.setAttribute(XMLNS_STRING,XMLNS_VALUE_STRING);
        xliff.setAttribute(XSI_STRING,XSI_VALUE_STRING);

        Element file = doc.createElement(FILE_STRING);
        file.setAttribute(ORIGINAL_STRING, GLOBAL_STRING);
        file.setAttribute(DATATYPE_STRING, PLAINTEXT_STRING);
        // TODO: Support source languages other than English
        file.setAttribute(SOURCE_LANGUAGE_STRING, ENGLISH);
        file.setAttribute(TARGET_LANGUAGE_STRING, targetLanguage);
        xliff.appendChild(file);

        Element body = doc.createElement(BODY_STRING);
        file.appendChild(body);

        for (ResourceString resString : resStrings) {
            Element trans_unit = doc.createElement(UNIT_STRING);
            trans_unit.setAttribute(ID_STRING, resString.getKey());
            Element source = doc.createElement(SOURCE_STRING);
            source.setTextContent(resString.getSourceValue());
            trans_unit.appendChild(source);
            Element target = doc.createElement(TARGET_STRING);
            target.setTextContent(resString.getValue());
            trans_unit.appendChild(target);
            body.appendChild(trans_unit);
        }

        doc.appendChild(xliff);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = null;
        try {
            transformer = transformerFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            // runtime problem
            throw new RuntimeException(e);
        }

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

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

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

}