Search in sources :

Example 1 with JDOMResult

use of org.jdom2.transform.JDOMResult in project webservices-axiom by apache.

the class ClientServerTest method runTest.

@Override
protected void runTest() throws Throwable {
    JDOMSource source = new JDOMSource(new SAXBuilder().build(ClientServerTest.class.getResourceAsStream("request.xml")).getRootElement());
    JDOMResult result = new JDOMResult();
    context.getBean(WebServiceTemplate.class).sendSourceAndReceiveToResult(source, result);
    assertEquals(8.0d, Double.parseDouble(result.getDocument().getRootElement().getText()), 1e-6);
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) JDOMResult(org.jdom2.transform.JDOMResult) JDOMSource(org.jdom2.transform.JDOMSource) WebServiceTemplate(org.springframework.ws.client.core.WebServiceTemplate)

Example 2 with JDOMResult

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

the class MCRJAXBContent method asXML.

/* (non-Javadoc)
     * @see org.mycore.common.content.MCRContent#asXML()
     */
@Override
public Document asXML() throws JDOMException, IOException, SAXParseException {
    JDOMResult result = new JDOMResult();
    try {
        Marshaller marshaller = getMarshaller();
        marshaller.marshal(jaxbObject, result);
    } catch (JAXBException e) {
        throw new IOException(e);
    }
    return result.getDocument();
}
Also used : JDOMResult(org.jdom2.transform.JDOMResult) Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException)

Example 3 with JDOMResult

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

the class MCRObjectCommands method xslt.

private static void xslt(String objectId, String xslFilePath, boolean force) throws IOException, JDOMException, SAXException, URISyntaxException, TransformerException, MCRPersistenceException, MCRAccessException, ParserConfigurationException {
    File xslFile = new File(xslFilePath);
    URL xslURL;
    if (!xslFile.exists()) {
        try {
            xslURL = new URL(xslFilePath);
        } catch (MalformedURLException e) {
            LOGGER.error("XSL parameter is not a file or URL: {}", xslFilePath);
            return;
        }
    } else {
        xslURL = xslFile.toURI().toURL();
    }
    MCRObjectID mcrId = MCRObjectID.getInstance(objectId);
    Document document = MCRXMLMetadataManager.instance().retrieveXML(mcrId);
    // do XSL transform
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setErrorListener(MCRErrorListener.getInstance());
    transformerFactory.setURIResolver(MCRURIResolver.instance());
    XMLReader xmlReader = MCRXMLParserFactory.getNonValidatingParser().getXMLReader();
    xmlReader.setEntityResolver(MCREntityResolver.instance());
    SAXSource styleSource = new SAXSource(xmlReader, new InputSource(xslURL.toURI().toString()));
    Transformer transformer = transformerFactory.newTransformer(styleSource);
    for (Entry<String, String> property : MCRConfiguration.instance().getPropertiesMap().entrySet()) {
        transformer.setParameter(property.getKey(), property.getValue());
    }
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    JDOMResult result = new JDOMResult();
    transformer.transform(new JDOMSource(document), result);
    Document resultDocument = Objects.requireNonNull(result.getDocument(), "Could not get transformation result");
    String originalName = document.getRootElement().getName();
    String resultName = resultDocument.getRootElement().getName();
    if (!force && !originalName.equals(resultName)) {
        LOGGER.error("{}: root name '{}' does not match result name '{}'.", objectId, originalName, resultName);
        return;
    }
    // update on diff
    if (MCRXMLHelper.deepEqual(document, resultDocument)) {
        return;
    }
    if (resultName.equals(MCRObject.ROOT_NAME)) {
        MCRMetadataManager.update(new MCRObject(resultDocument));
    } else if (resultName.equals(MCRDerivate.ROOT_NAME)) {
        MCRMetadataManager.update(new MCRDerivate(resultDocument));
    } else {
        LOGGER.error("Unable to transform '{}' because unknown result root name '{}'.", objectId, resultName);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputSource(org.xml.sax.InputSource) JDOMResult(org.jdom2.transform.JDOMResult) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) JDOMSource(org.jdom2.transform.JDOMSource) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) Document(org.jdom2.Document) URL(java.net.URL) SAXSource(javax.xml.transform.sax.SAXSource) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) File(java.io.File) XMLReader(org.xml.sax.XMLReader)

Example 4 with JDOMResult

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

the class NavigationTest method toXML.

@Test
public void toXML() throws Exception {
    JAXBContext jc = JAXBContext.newInstance(MCRNavigation.class);
    Marshaller m = jc.createMarshaller();
    JDOMResult JDOMResult = new JDOMResult();
    m.marshal(this.navigation, JDOMResult);
    Element navigationElement = JDOMResult.getDocument().getRootElement();
    XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
    out.output(navigationElement, System.out);
    // test attributes
    assertEquals("template_mysample", navigationElement.getAttributeValue("template"));
    assertEquals("/content", navigationElement.getAttributeValue("dir"));
    assertEquals("History Title", navigationElement.getAttributeValue("historyTitle"));
    assertEquals("/content/below/index.xml", navigationElement.getAttributeValue("hrefStartingPage"));
    assertEquals("Main Title", navigationElement.getAttributeValue("mainTitle"));
    // test children
    assertEquals(2, navigationElement.getChildren("menu").size());
    assertEquals(1, navigationElement.getChildren("insert").size());
}
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 5 with JDOMResult

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

the class MCRXSL2XMLTransformer method getTransformedContent.

@Override
protected MCRContent getTransformedContent(MCRContent source, XMLReader reader, TransformerHandler transformerHandler) throws IOException, SAXException {
    JDOMResult result = new JDOMResult();
    transformerHandler.setResult(result);
    // Parse the source XML, and send the parse events to the
    // TransformerHandler.
    reader.parse(source.getInputSource());
    Document resultDoc = getDocument(result);
    if (resultDoc == null) {
        throw new MCRConfigurationException("Stylesheets " + Arrays.asList(templateSources) + " does not return any content for " + source.getSystemId());
    }
    return new MCRJDOMContent(resultDoc);
}
Also used : JDOMResult(org.jdom2.transform.JDOMResult) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) 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