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