use of javax.xml.transform.Result in project spring-framework by spring-projects.
the class StaxUtilsTests method isStaxResult.
@Test
public void isStaxResult() throws Exception {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
Result result = StaxUtils.createCustomStaxResult(streamWriter);
assertTrue("Not a StAX Result", StaxUtils.isStaxResult(result));
}
use of javax.xml.transform.Result in project spring-framework by spring-projects.
the class MarshallingMessageConverter method marshalToTextMessage.
/**
* Marshal the given object to a {@link TextMessage}.
* @param object the object to be marshalled
* @param session current JMS session
* @param marshaller the marshaller to use
* @return the resulting message
* @throws JMSException if thrown by JMS methods
* @throws IOException in case of I/O errors
* @throws XmlMappingException in case of OXM mapping errors
* @see Session#createTextMessage
* @see Marshaller#marshal(Object, Result)
*/
protected TextMessage marshalToTextMessage(Object object, Session session, Marshaller marshaller) throws JMSException, IOException, XmlMappingException {
StringWriter writer = new StringWriter();
Result result = new StreamResult(writer);
marshaller.marshal(object, result);
return session.createTextMessage(writer.toString());
}
use of javax.xml.transform.Result in project ddf by codice.
the class AbstractFeatureConverterWfs20 method readGml.
protected Object readGml(String xml) {
LOGGER.debug("readGml() input XML: {}", xml);
//Add namespace into XML for processing
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
try {
dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
} catch (ParserConfigurationException e) {
LOGGER.debug("Unable to configure features on document builder.", e);
}
DocumentBuilder dBuilder = null;
Document doc = null;
Object gml = null;
InputStream xmlIs = null;
//Check if GML 3.2.1 namespace exist on XML chunk
try {
dBuilder = dbFactory.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = dBuilder.parse(is);
} catch (ParserConfigurationException | SAXException | IOException e) {
LOGGER.debug(XML_PARSE_FAILURE);
}
if (null != doc) {
String[] namePrefix = doc.getDocumentElement().getNodeName().split(":");
String prefix = "";
if (namePrefix.length < 2) {
LOGGER.debug("Incoming XML has no GML prefix");
} else {
prefix = ":" + namePrefix[0];
}
String xmlNs = doc.getDocumentElement().getAttribute("xmlns" + prefix);
if (xmlNs.equals(Wfs20Constants.GML_3_2_NAMESPACE)) {
LOGGER.debug("Namespace already exists.");
} else {
doc.createElementNS(Wfs20Constants.GML_3_2_NAMESPACE, doc.getDocumentElement().getNodeName());
}
//Convert DOM to InputStream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Source xmlSource = new DOMSource(doc);
Result outputTarget = new StreamResult(outputStream);
try {
TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
} catch (TransformerException | TransformerFactoryConfigurationError e) {
LOGGER.debug(CREATE_TRANSFORMER_FAILURE);
}
xmlIs = new ByteArrayInputStream(outputStream.toByteArray());
//Parse XML into a Geometry object
Configuration configurationG = new org.geotools.gml3.v3_2.GMLConfiguration();
Parser parser = new org.geotools.xml.Parser(configurationG);
parser.setStrict(false);
parser.setValidating(false);
parser.setFailOnValidationError(false);
parser.setForceParserDelegate(false);
try {
gml = parser.parse(xmlIs);
} catch (IOException | SAXException | ParserConfigurationException e) {
LOGGER.debug("{} {}", GML_FAILURE, xml);
}
}
return gml;
}
use of javax.xml.transform.Result in project uPortal by Jasig.
the class ILFBuilder method printNodeToDebug.
private static void printNodeToDebug(Node n, String name) throws TransformerFactoryConfigurationError {
if (!LOG.isDebugEnabled()) {
return;
}
final StringWriter writer = new StringWriter();
try {
final TransformerFactory transFactory = TransformerFactory.newInstance();
final Transformer trans = transFactory.newTransformer();
final Source xmlSource = new DOMSource(n);
final Result transResult = new StreamResult(writer);
trans.transform(xmlSource, transResult);
final String xmlStr = writer.toString();
LOG.debug(name + " DOM Tree:\n\n" + xmlStr);
} catch (Exception e) {
LOG.error("Error printing out " + name + " DOM Tree", e);
final String xmlStr = writer.toString();
LOG.debug("Partial " + name + " DOM Tree:\n\n" + xmlStr);
}
}
use of javax.xml.transform.Result in project stanbol by apache.
the class DOMUtils method printXML.
/**
* This prints the given DOM document to System.out with indentation and
* utf-8 encoding.
*
* @param doc
* a DOM <code>Document</code>
*/
public static void printXML(Document doc) {
try {
// prepare the DOM document for writing
Source source = new DOMSource(doc);
// prepare the output
Result result = new StreamResult(System.out);
// write the DOM document to the file
// get Transformer
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
xformer.setOutputProperty(OutputKeys.METHOD, "xml");
// write to System.out
xformer.transform(source, result);
} catch (TransformerConfigurationException tce) {
// error generated during transformer configuration
System.err.println(tce.getMessage());
// use the contained exception, if any
Throwable x = tce;
if (tce.getException() != null) {
x = tce.getException();
}
x.printStackTrace();
} catch (TransformerException te) {
// error generated by the transformer
System.err.println(te.getMessage());
// use the contained exception, if any
Throwable x = te;
if (te.getException() != null) {
x = te.getException();
}
x.printStackTrace();
}
}
Aggregations