use of javax.xml.transform.dom.DOMResult in project spring-framework by spring-projects.
the class StaxSourceTests method streamReaderSourceToDOMResult.
@Test
public void streamReaderSourceToDOMResult() throws Exception {
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(XML));
StaxSource source = new StaxSource(streamReader);
assertEquals("Invalid streamReader returned", streamReader, source.getXMLStreamReader());
assertNull("EventReader returned", source.getXMLEventReader());
Document expected = documentBuilder.parse(new InputSource(new StringReader(XML)));
Document result = documentBuilder.newDocument();
transformer.transform(source, new DOMResult(result));
assertThat("Invalid result", result, isSimilarTo(expected));
}
use of javax.xml.transform.dom.DOMResult in project spring-framework by spring-projects.
the class JibxMarshaller method marshalDomNode.
// Unsupported marshalling
@Override
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
try {
// JiBX does not support DOM natively, so we write to a buffer first, and transform that to the Node
Result result = new DOMResult(node);
transformAndMarshal(graph, result);
} catch (IOException ex) {
throw new MarshallingFailureException("JiBX marshalling exception", ex);
}
}
use of javax.xml.transform.dom.DOMResult in project spring-framework by spring-projects.
the class AbstractMarshallerTests method marshalDOMResult.
@Test
public void marshalDOMResult() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
Document result = builder.newDocument();
DOMResult domResult = new DOMResult(result);
marshaller.marshal(flights, domResult);
Document expected = builder.newDocument();
Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
Attr namespace = expected.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:tns");
namespace.setNodeValue("http://samples.springframework.org/flight");
flightsElement.setAttributeNode(namespace);
expected.appendChild(flightsElement);
Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
flightsElement.appendChild(flightElement);
Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
flightElement.appendChild(numberElement);
Text text = expected.createTextNode("42");
numberElement.appendChild(text);
assertThat("Marshaller writes invalid DOMResult", result, isSimilarTo(expected));
}
use of javax.xml.transform.dom.DOMResult in project nokogiri by sparklemotion.
the class XsltStylesheet method tryXsltTransformation.
private DOMResult tryXsltTransformation(ThreadContext context, IRubyObject[] args, DOMSource domSource, NokogiriXsltErrorListener elistener) throws TransformerException {
Transformer transf = sheet.newTransformer();
transf.reset();
transf.setErrorListener(elistener);
if (args.length > 1) {
addParametersToTransformer(context, transf, args[1]);
}
DOMResult result = new DOMResult();
transf.transform(domSource, result);
return result;
}
use of javax.xml.transform.dom.DOMResult in project gocd by gocd.
the class XsltStylesheet method transform.
@JRubyMethod(rest = true, required = 1, optional = 2)
public IRubyObject transform(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.getRuntime();
argumentTypeCheck(runtime, args[0]);
NokogiriXsltErrorListener elistener = new NokogiriXsltErrorListener();
DOMSource domSource = new DOMSource(((XmlDocument) args[0]).getDocument());
DOMResult result = null;
String stringResult = null;
try {
// DOMResult
result = tryXsltTransformation(context, args, domSource, elistener);
if (result.getNode().getFirstChild() == null) {
// StreamResult
stringResult = retryXsltTransformation(context, args, domSource, elistener);
}
} catch (TransformerConfigurationException ex) {
throw runtime.newRuntimeError(ex.getMessage());
} catch (TransformerException ex) {
throw runtime.newRuntimeError(ex.getMessage());
} catch (IOException ex) {
throw runtime.newRuntimeError(ex.getMessage());
}
switch(elistener.getErrorType()) {
case ERROR:
case FATAL:
throw runtime.newRuntimeError(elistener.getErrorMessage());
case WARNING:
default:
}
if (stringResult == null) {
return createDocumentFromDomResult(context, runtime, result);
} else {
return createDocumentFromString(context, runtime, stringResult);
}
}
Aggregations