Search in sources :

Example 6 with JDOMResult

use of org.jdom2.transform.JDOMResult in project mycore by MyCoRe-Org.

the class MCRXSLTransformation method transform.

/**
 * transforms a jdom Document via XSLT.
 *
 * @param in Document input
 * @param transformer Transformer handling the transformation process
 * @return the transformation result as jdom Document
 * @throws TransformerException if transformation fails
 */
public static Document transform(Document in, Transformer transformer) throws TransformerException {
    JDOMResult out = new JDOMResult();
    transformer.transform(new JDOMSource(in), out);
    return out.getDocument();
}
Also used : JDOMResult(org.jdom2.transform.JDOMResult) JDOMSource(org.jdom2.transform.JDOMSource)

Example 7 with JDOMResult

use of org.jdom2.transform.JDOMResult in project mycore by MyCoRe-Org.

the class ItemTest method toXML.

@Test
public void toXML() throws Exception {
    JAXBContext jc = JAXBContext.newInstance(MCRNavigationItem.class);
    Marshaller m = jc.createMarshaller();
    JDOMResult JDOMResult = new JDOMResult();
    m.marshal(this.item, JDOMResult);
    Element itemElement = JDOMResult.getDocument().getRootElement();
    XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
    out.output(itemElement, System.out);
    assertEquals("template_mysample", itemElement.getAttributeValue("template"));
    assertEquals("bold", itemElement.getAttributeValue("style"));
    assertEquals("_self", itemElement.getAttributeValue("target"));
    assertEquals("intern", itemElement.getAttributeValue("type"));
    assertEquals("true", itemElement.getAttributeValue("constrainPopUp"));
    assertEquals("false", itemElement.getAttributeValue("replaceMenu"));
    assertEquals("item.test.key", itemElement.getAttributeValue("i18nKey"));
    Element label1 = itemElement.getChildren().get(0);
    Element label2 = itemElement.getChildren().get(1);
    assertEquals("Deutschland", label1.getValue());
    assertEquals("England", label2.getValue());
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Marshaller(javax.xml.bind.Marshaller) JDOMResult(org.jdom2.transform.JDOMResult) JsonElement(com.google.gson.JsonElement) Element(org.jdom2.Element) JAXBContext(javax.xml.bind.JAXBContext) Test(org.junit.Test)

Example 8 with JDOMResult

use of org.jdom2.transform.JDOMResult in project mycore by MyCoRe-Org.

the class MCRXSL2XMLTransformer method getDocument.

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();
                }
            }
        }
        if (transformResult.size() < origSize) {
            JDOMFactory f = result.getFactory();
            if (f == null) {
                f = new DefaultJDOMFactory();
            }
            resultDoc = f.document(null);
            resultDoc.setContent(transformResult);
        }
    }
    return resultDoc;
}
Also used : DefaultJDOMFactory(org.jdom2.DefaultJDOMFactory) MCRContent(org.mycore.common.content.MCRContent) Content(org.jdom2.Content) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) JDOMFactory(org.jdom2.JDOMFactory) DefaultJDOMFactory(org.jdom2.DefaultJDOMFactory) Text(org.jdom2.Text) Document(org.jdom2.Document)

Aggregations

JDOMResult (org.jdom2.transform.JDOMResult)7 Marshaller (javax.xml.bind.Marshaller)3 Document (org.jdom2.Document)3 JDOMSource (org.jdom2.transform.JDOMSource)3 JsonElement (com.google.gson.JsonElement)2 JAXBContext (javax.xml.bind.JAXBContext)2 Element (org.jdom2.Element)2 XMLOutputter (org.jdom2.output.XMLOutputter)2 Test (org.junit.Test)2 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)2 File (java.io.File)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 JAXBException (javax.xml.bind.JAXBException)1 Transformer (javax.xml.transform.Transformer)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 SAXSource (javax.xml.transform.sax.SAXSource)1 Content (org.jdom2.Content)1 DefaultJDOMFactory (org.jdom2.DefaultJDOMFactory)1