Example usage for org.jdom2.transform JDOMResult getFactory

List of usage examples for org.jdom2.transform JDOMResult getFactory

Introduction

In this page you can find the example usage for org.jdom2.transform JDOMResult getFactory.

Prototype

public JDOMFactory getFactory() 

Source Link

Document

Returns the custom JDOMFactory used to build the transformation result.

Usage

From source file:org.mycore.common.content.transformer.MCRXSL2XMLTransformer.java

License:Open Source License

private Document getDocument(JDOMResult result) {
    Document resultDoc = result.getDocument();
    if (resultDoc == null) {
        //Sometimes a transformation produces whitespace strings
        //JDOM would produce a empty document if it detects those
        //So we remove them, if they exists.
        List<Content> transformResult = result.getResult();
        int origSize = transformResult.size();
        Iterator<Content> iterator = transformResult.iterator();
        while (iterator.hasNext()) {
            Content content = iterator.next();
            if (content instanceof Text) {
                String trimmedText = ((Text) content).getTextTrim();
                if (trimmedText.length() == 0) {
                    iterator.remove();/*from  w w w.  j  a v a  2s. c  om*/
                }
            }
        }
        if (transformResult.size() < origSize) {
            JDOMFactory f = result.getFactory();
            if (f == null) {
                f = new DefaultJDOMFactory();
            }
            resultDoc = f.document(null);
            resultDoc.setContent(transformResult);
        }
    }
    return resultDoc;
}