List of usage examples for javax.xml.transform TransformerConfigurationException getMessage
public String getMessage()
From source file:Main.java
/** * Writes the XML document to the particular file specified as argument * /* w w w . j a va 2 s .c om*/ * @param doc the document * @param filename the path to the file in which to write the XML data writing * operation fails */ public static void writeXMLDocument(Document doc, String filename) { try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer; transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(filename)); transformer.transform(source, result); log.fine("writing operation to " + filename + " successful!"); } catch (TransformerConfigurationException e) { log.warning(e.getMessage()); } catch (TransformerException e) { log.warning(e.getMessage()); } }
From source file:Main.java
public static void writeXmlFile(Document doc, String filename) { try {// w w w.ja v a 2 s. c om // Prepare the DOM document for writing Source source = new DOMSource(doc); // Prepare the output file File file = new File(filename); Result result = new StreamResult(file); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); } catch (TransformerConfigurationException ex) { System.out.println(ex.getMessage()); } catch (TransformerException ex) { System.out.println(ex.getMessage()); } }
From source file:Main.java
public static void printDOM(OutputStream ostr, Document doc, String encoding) { try {/* www . j a v a2 s . co m*/ Transformer tr = TransformerFactory.newInstance().newTransformer(); if (encoding != null) tr.setOutputProperty(OutputKeys.ENCODING, encoding); tr.setOutputProperty(OutputKeys.INDENT, "yes"); tr.setOutputProperty(OutputKeys.METHOD, "xml"); tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); tr.transform(new DOMSource(doc), new StreamResult(ostr)); } catch (TransformerConfigurationException ex) { System.err.println(ex.getMessage()); } catch (TransformerException ex) { System.err.println(ex.getMessage()); } }
From source file:Main.java
/** Write a DOM to a file. */ public static void writeXML(Document document, File file) throws IOException { // save the DOM to file StreamResult result = new StreamResult(new BufferedOutputStream(new FileOutputStream(file))); TransformerFactory transFactory = TransformerFactory.newInstance(); try {// w w w .ja v a2 s. c o m Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); transformer.transform(new DOMSource(document), result); } catch (TransformerConfigurationException ex) { throw new IOException(ex.getMessage()); } catch (TransformerException ex) { throw new IOException(ex.getMessage()); } result.getOutputStream().close(); }
From source file:Main.java
public static void refreshTransformer() { if (transformerFactory == null) { refreshTransformerFactory();/*from www .ja va 2s. c om*/ } try { transformer = transformerFactory.newTransformer(); } catch (TransformerConfigurationException e) { System.out.println("Transformer configuration error: " + e.getMessage()); } }
From source file:Main.java
public static String XMLDocumentToString(Document _doc) { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans;/* w w w .j av a2s . c o m*/ String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; try { trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(_doc); trans.transform(source, result); xmlString += sw.toString(); } catch (TransformerConfigurationException e) { System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage()); return null; } catch (TransformerException e) { System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage()); return null; } return xmlString; }
From source file:Main.java
/** * This method performs XSL Transformation. <br> * <b>Deprecated use XmlTransformer.transform</b> * /*from w w w.j a v a 2s . c o m*/ * @param source * The input XML document * @param stylesheet * The XSL stylesheet * @param params * parameters to apply to the XSL Stylesheet * @param outputProperties * properties to use for the xsl transform. Will overload the xsl output definition. * @return The output document transformed * @throws Exception * The exception */ @Deprecated public static String transform(Source source, Source stylesheet, Map<String, String> params, Properties outputProperties) throws Exception { try { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(stylesheet); if (outputProperties != null) { transformer.setOutputProperties(outputProperties); } if (params != null) { transformer.clearParameters(); for (Entry<String, String> entry : params.entrySet()) { String name = entry.getKey(); String value = entry.getValue(); transformer.setParameter(name, value); } } StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); transformer.transform(source, result); return sw.toString(); } catch (TransformerConfigurationException e) { String strMessage = e.getMessage(); if (e.getLocationAsString() != null) { strMessage += ("- location : " + e.getLocationAsString()); } throw new Exception("Error transforming document XSLT : " + strMessage, e.getCause()); } catch (TransformerFactoryConfigurationError e) { throw new Exception("Error transforming document XSLT : " + e.getMessage(), e); } catch (TransformerException e) { String strMessage = e.getMessage(); if (e.getLocationAsString() != null) { strMessage += ("- location : " + e.getLocationAsString()); } throw new Exception("Error transforming document XSLT : " + strMessage, e.getCause()); } catch (Exception e) { throw new Exception("Error transforming document XSLT : " + e.getMessage(), e); } }
From source file:com.casewaresa.framework.util.LegacyJasperInputStream.java
public static String addDocTypeAndConvertDOMToString(final Document document) { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = null;// www.ja v a 2 s . c om try { trans = transfac.newTransformer(); } catch (TransformerConfigurationException ex) { log.error(ex.getMessage(), ex); } trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "//JasperReports//DTD Report Design//EN"); trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(document); try { trans.transform(source, result); } catch (TransformerException ex) { log.error(ex.getMessage(), ex); } return sw.toString(); }
From source file:Main.java
public static void writeXMLDocToFile(Document outputDoc, String outFileName) { try {//from w w w. j a v a2s . c om //FileWriter writer = new FileWriter( outFileName ); final String encoding = "UTF-8"; OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFileName), encoding); // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(outputDoc); StreamResult result = new StreamResult(writer); transformer.transform(source, result); writer.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Could not create output XML file: " + outFileName); } catch (TransformerConfigurationException tce) { // Error generated by the parser System.out.println("* Transformer Factory error"); System.out.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 parser System.out.println("* Transformation error"); System.out.println(" " + te.getMessage()); // Use the contained exception, if any Throwable x = te; if (te.getException() != null) x = te.getException(); x.printStackTrace(); } }
From source file:com.aurel.track.exchange.docx.exporter.CustomXML.java
/** * @param items/*from w ww.j a va 2s .c om*/ * @return */ public static void convertToXml(OutputStream outputStream, Document dom) { Transformer transformer = null; try { TransformerFactory factory = TransformerFactory.newInstance(); transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); } catch (TransformerConfigurationException e) { LOGGER.error( "Creating the transformer failed with TransformerConfigurationException: " + e.getMessage()); return; } try { transformer.transform(new DOMSource(dom), new StreamResult(outputStream)); } catch (TransformerException e) { LOGGER.error("Transform failed with TransformerException: " + e.getMessage()); } }