List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
From source file:Main.java
/** * Writes XML Document into an xml file. * //from ww w.j a va 2 s. com * @param fileName the target file with the full path * @param document the source document * @return boolean true if the file saved * @throws Exception */ public static boolean writeXmlFile(String fileName, Document document) throws Exception { // creating and writing to xml file File file = new File(fileName); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); // to prevent XML External Entities attack Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(new DOMSource(document), new StreamResult(file)); return true; }
From source file:fr.aliasource.webmail.server.proxy.client.http.DOMUtils.java
public static void serialise(Document doc, OutputStream out) throws TransformerException { TransformerFactory fac = TransformerFactory.newInstance(); Transformer tf = fac.newTransformer(); tf.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ Source input = new DOMSource(doc.getDocumentElement()); Result output = new StreamResult(out); tf.transform(input, output);/*w w w . j av a 2s. co m*/ }
From source file:net.mumie.coursecreator.xml.GraphXMLizer.java
/** * Describe <code>doTransform</code> method here. * * @param document a <code>Node</code> value * @return a <code>ByteArrayOutputStream</code> value * @exception TransformerException if an error occurs * @exception UnsupportedEncodingException if an error occurs */// w ww . ja va 2 s .c om private static ByteArrayOutputStream doTransform(Node document) throws TransformerException, UnsupportedEncodingException { try { // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "US-ASCII"); DOMSource source = new DOMSource(document); ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamWriter out = new OutputStreamWriter(baos, "ASCII"); StreamResult result = new StreamResult(out); transformer.transform(source, result); return baos; } catch (TransformerException te) { // Error generated by the parser CCController.dialogErrorOccured("GraphXMLizer: TransformerException", "GraphXMLizer: TransformerException: " + te, JOptionPane.ERROR_MESSAGE); throw te; } }
From source file:eu.stork.peps.test.simple.SSETestUtils.java
/** * Marshall.//from ww w .j av a2s .c o m * * @param samlToken the SAML token * * @return the byte[] * * @throws MarshallingException the marshalling exception * @throws ParserConfigurationException the parser configuration exception * @throws TransformerException the transformer exception */ public static byte[] marshall(final XMLObject samlToken) throws MarshallingException, ParserConfigurationException, TransformerException { final javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance(); dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); dbf.setNamespaceAware(true); dbf.setIgnoringComments(true); final javax.xml.parsers.DocumentBuilder docBuild = dbf.newDocumentBuilder(); // Get the marshaller factory final MarshallerFactory marshallerFactory = Configuration.getMarshallerFactory(); // Get the Subject marshaller final Marshaller marshaller = marshallerFactory.getMarshaller(samlToken); final Document doc = docBuild.newDocument(); // Marshall the SAML token marshaller.marshall(samlToken, doc); // Obtain a byte array representation of the marshalled SAML object final DOMSource domSource = new DOMSource(doc); final StringWriter writer = new StringWriter(); final StreamResult result = new StreamResult(writer); final TransformerFactory transFact = TransformerFactory.newInstance(); final Transformer transformer = transFact.newTransformer(); transformer.transform(domSource, result); return writer.toString().getBytes(); }
From source file:it.cnr.icar.eric.common.cms.AbstractService.java
protected static void printNodeToConsole(Node n) { try {/*from ww w. j av a 2 s. com*/ TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); // also print to console transformer.transform(new DOMSource(n), new StreamResult(System.err)); } catch (Exception e) { System.err.println(e); } }
From source file:Main.java
/** * DOCUMENT ME!// w w w. j av a 2s . com * * @return DOCUMENT ME! * * @throws TransformerConfigurationException DOCUMENT ME! * @throws TransformerException DOCUMENT ME! * @throws Exception DOCUMENT ME! */ public static byte[] writeToBytes(Document document) throws TransformerConfigurationException, TransformerException, Exception { byte[] ret = null; TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setAttribute("indent-number", new Integer(4)); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(document); ByteArrayOutputStream bos = new ByteArrayOutputStream(); StreamResult result = new StreamResult(bos); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // NOI18N transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); // NOI18N transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); // NOI18N // indent the output to make it more legible... try { transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N } catch (IllegalArgumentException e) { // the JAXP implementation doesn't support indentation, no big deal //e.printStackTrace(); } try { transformer.transform(source, result); ret = bos.toByteArray(); } finally { if (bos != null) { bos.flush(); bos.close(); } } return ret; }
From source file:org.cleverbus.core.common.asynch.msg.MessageTransformer.java
/** * Gets original SOAP envelope.//from www .j av a 2 s .c om * * @param exchange the exchange * @return envelope as string or {@code null} if input message isn't Spring web service message */ @Nullable public static String getSOAPEnvelope(Exchange exchange) { if (!(exchange.getIn() instanceof SpringWebserviceMessage)) { return null; } try { TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); SpringWebserviceMessage inMsg = (SpringWebserviceMessage) exchange.getIn(); Source source = ((SaajSoapMessage) inMsg.getWebServiceMessage()).getEnvelope().getSource(); StringResult strRes = new StringResult(); aTransformer.transform(source, strRes); return strRes.toString(); } catch (Exception ex) { throw new IllegalStateException("Error occurred during conversion SOAP envelope to string", ex); } }
From source file:ac.uk.diamond.sample.HttpClientTest.Utils.java
static String xmlToString(XMLObject aObject) throws IOException { Document doc;//w w w . j a v a 2s . c o m try { doc = Configuration.getMarshallerFactory().getMarshaller(aObject).marshall(aObject).getOwnerDocument(); } catch (MarshallingException e) { throw new IOException(e); } try { Source source = new DOMSource(doc); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerException e) { throw new IOException(e); } }
From source file:eu.eidas.engine.test.simple.SSETestUtils.java
/** * Marshall./*from w w w . j a v a 2 s . c o m*/ * * @param samlToken the SAML token * * @return the byte[] * * @throws MarshallingException the marshalling exception * @throws ParserConfigurationException the parser configuration exception * @throws TransformerException the transformer exception */ public static byte[] marshall(final XMLObject samlToken) throws MarshallingException, ParserConfigurationException, TransformerException { final javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance(); dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); dbf.setNamespaceAware(true); dbf.setIgnoringComments(true); final javax.xml.parsers.DocumentBuilder docBuild = dbf.newDocumentBuilder(); // Get the marshaller factory final MarshallerFactory marshallerFactory = Configuration.getMarshallerFactory(); // Get the Subject marshaller final Marshaller marshaller = marshallerFactory.getMarshaller(samlToken); final Document doc = docBuild.newDocument(); // Marshall the SAML token marshaller.marshall(samlToken, doc); // Obtain a byte array representation of the marshalled SAML object final DOMSource domSource = new DOMSource(doc); ByteArrayOutputStream baos = new ByteArrayOutputStream(); final StreamResult result = new StreamResult(new OutputStreamWriter(baos, Constants.UTF8)); final TransformerFactory transFact = TransformerFactory.newInstance(); final Transformer transformer = transFact.newTransformer(); transformer.transform(domSource, result); return baos.toByteArray(); }
From source file:tud.time4maps.request.CapabilitiesRequest.java
/** * This method is a help method to print xml documents on console. * //from w w w . ja va 2 s . co m * @param doc - the document, that should be printed on console */ public static void printXML(Document doc) { try { DOMSource source = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer; transformer = factory.newTransformer(); transformer.transform(source, result); } catch (TransformerException e) { e.printStackTrace(); } }