List of usage examples for javax.xml.transform TransformerConfigurationException getMessage
public String getMessage()
From source file:com.sfs.jbtimporter.JBTImporter.java
/** * Transform the issues to the new XML format. * * @param jbt the jbt processor/* w w w . j a va 2s. co m*/ * @param issues the issues */ private static void transformIssues(final JBTProcessor jbt, final List<JBTIssue> issues) { final File xsltFile = new File(jbt.getXsltFileName()); final Source xsltSource = new StreamSource(xsltFile); final TransformerFactory transFact = TransformerFactory.newInstance(); Transformer trans = null; try { final Templates cachedXSLT = transFact.newTemplates(xsltSource); trans = cachedXSLT.newTransformer(); } catch (TransformerConfigurationException tce) { System.out.println("ERROR configuring XSLT engine: " + tce.getMessage()); } // Enable indenting and UTF8 encoding trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); if (trans != null) { for (JBTIssue issue : issues) { System.out.println("Processing Issue ID: " + issue.getId()); System.out.println("Filename: " + issue.getFullFileName()); // Read the XML file final File xmlFile = new File(issue.getFullFileName()); final File tempFile = new File(issue.getFullFileName() + ".tmp"); final File originalFile = new File(issue.getFullFileName() + ".old"); Source xmlSource = null; if (originalFile.exists()) { // The original file exists, use that as the XML source xmlSource = new StreamSource(originalFile); } else { // No backup exists, use the .xml file. xmlSource = new StreamSource(xmlFile); } // Transform the XML file try { trans.transform(xmlSource, new StreamResult(tempFile)); if (originalFile.exists()) { // Delete the .xml file as it needs to be replaced xmlFile.delete(); } else { // Rename the existing file with the .old extension xmlFile.renameTo(originalFile); } } catch (TransformerException te) { System.out.println("ERROR transforming XML: " + te.getMessage()); } // Read the xmlFile and convert the special characters OutputStreamWriter out = null; try { final BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(tempFile), "UTF8")); out = new OutputStreamWriter(new FileOutputStream(xmlFile), "UTF-8"); int ch = -1; ch = in.read(); while (ch != -1) { final char c = (char) ch; if (jbt.getSpecialCharacterMap().containsKey(c)) { // System.out.println("Replacing character: " + c // + ", " + jbt.getSpecialCharacterMap().get(c)); out.write(jbt.getSpecialCharacterMap().get(c)); } else { out.write(c); } ch = in.read(); } } catch (IOException ie) { System.out.println("ERROR converting special characters: " + ie.getMessage()); } finally { try { if (out != null) { out.close(); } } catch (IOException ie) { System.out.println("ERROR closing the XML file: " + ie.getMessage()); } // Delete the temporary file tempFile.delete(); } System.out.println("-------------------------------------"); } } }
From source file:fr.paris.lutece.plugins.calendar.service.XMLUtils.java
/** * This method performs XSL Transformation. * /* w w w . jav a 2 s . com*/ * @param strXml The input XML document * @param baSource The source input stream * @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 */ public static byte[] transformXMLToXSL(String strXml, InputStream baSource, Map<String, String> params, Properties outputProperties) throws Exception { Source stylesheet = new StreamSource(baSource); StringReader srInputXml = new StringReader(strXml); StreamSource source = new StreamSource(srInputXml); try { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(stylesheet); if (outputProperties != null) { transformer.setOutputProperties(outputProperties); } if (params != null) { transformer.clearParameters(); Set<Entry<String, String>> entries = params.entrySet(); for (Entry<String, String> entry : entries) { String name = entry.getKey(); String value = entry.getValue(); transformer.setParameter(name, value); } } ByteArrayOutputStream out = new ByteArrayOutputStream(); Result result = new StreamResult(out); transformer.transform(source, result); return out.toByteArray(); } 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.aurel.track.admin.customize.category.report.execute.ReportBeansToXML.java
/** * @param items/* w ww. j a v a 2s .c o m*/ * @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()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); return; } try { transformer.transform(new DOMSource(dom), new StreamResult(outputStream)); } catch (TransformerException e) { LOGGER.error("Transform failed with TransformerException: " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } }
From source file:com.aurel.track.report.datasource.faqs.FaqsDatasource.java
/** * @param items/*from www . j av a 2 s . 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"); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//W3C//DTD XHTML 1.0 Transitional//EN"); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"); } 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()); } }
From source file:edu.lternet.pasta.client.SubscriptionUtility.java
/** * Transforms a quality report XML document to an HTML table. * /* www . java 2s . co m*/ * @param xslPath * The path to the quality report XSL stylesheet. * * @return The HTML table as a String object. */ public String xmlToHtml(String xslPath) { String html = null; File styleSheet = new File(xslPath); StringReader stringReader = new StringReader(this.subscription); StringWriter stringWriter = new StringWriter(); StreamSource styleSource = new StreamSource(styleSheet); Result result = new StreamResult(stringWriter); Source source = new StreamSource(stringReader); try { Transformer t = TransformerFactory.newInstance().newTransformer(styleSource); t.transform(source, result); html = stringWriter.toString(); } catch (TransformerConfigurationException e) { logger.error(e.getMessage()); e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { logger.error(e.getMessage()); e.printStackTrace(); } catch (TransformerException e) { logger.error(e.getMessage()); e.printStackTrace(); } return html; }
From source file:edu.lternet.pasta.client.EmlUtility.java
private String emlReferenceExpander(String xslPath) throws ParseException { String xml = null;/* www.jav a 2 s . c o m*/ File styleSheet = new File(xslPath); StringReader stringReader = new StringReader(this.eml); StringWriter stringWriter = new StringWriter(); StreamSource styleSource = new StreamSource(styleSheet); Result result = new StreamResult(stringWriter); Source source = new StreamSource(stringReader); try { Transformer t = TransformerFactory.newInstance().newTransformer(styleSource); t.transform(source, result); xml = stringWriter.toString(); } catch (TransformerConfigurationException e) { logger.error(e.getMessage()); e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { logger.error(e.getMessage()); e.printStackTrace(); } catch (TransformerException e) { logger.error(e.getMessage()); e.printStackTrace(); throw new ParseException("EML Parse Error: " + e.getMessage(), 0); } return xml; }
From source file:net.sf.joost.trax.TrAXFilter.java
/** * Parses the <code>InputSource</code> * @param input A <code>InputSource</code> object. * @throws SAXException/* www. ja va2 s .c o m*/ * @throws IOException */ public void parse(InputSource input) throws SAXException, IOException { Transformer transformer = null; if (DEBUG) { if (log.isDebugEnabled()) log.debug("parsing InputSource " + input.getSystemId()); } try { // get a new Transformer transformer = this.templates.newTransformer(); if (transformer instanceof TransformerImpl) { this.processor = ((TransformerImpl) transformer).getStxProcessor(); } else { String msg = "An error is occured, because the given transformer is " + "not an instance of TransformerImpl"; if (log != null) log.fatal(msg); else System.err.println("Fatal error - " + msg); } XMLReader parent = this.getParent(); if (parent == null) { parent = XMLReaderFactory.createXMLReader(); setParent(parent); } ContentHandler handler = this.getContentHandler(); if (handler == null) { handler = parent.getContentHandler(); } if (handler == null) { throw new SAXException("no ContentHandler registered"); } //init StxEmitter StxEmitter out = null; //SAX specific Implementation out = new SAXEmitter(handler); if (this.processor != null) { this.processor.setContentHandler(out); this.processor.setLexicalHandler(out); } else { throw new SAXException("Joost-Processor is not correct configured."); } if (parent == null) { throw new SAXException("No parent for filter"); } parent.setContentHandler(this.processor); parent.setProperty("http://xml.org/sax/properties/lexical-handler", this.processor); parent.setEntityResolver(this); parent.setDTDHandler(this); parent.setErrorHandler(this); parent.parse(input); } catch (TransformerConfigurationException tE) { try { configErrListener.fatalError(tE); } catch (TransformerConfigurationException innerE) { throw new SAXException(innerE.getMessage(), innerE); } } catch (SAXException sE) { try { configErrListener.fatalError(new TransformerConfigurationException(sE.getMessage(), sE)); } catch (TransformerConfigurationException innerE) { throw new SAXException(innerE.getMessage(), innerE); } } catch (IOException iE) { try { configErrListener.fatalError(new TransformerConfigurationException(iE.getMessage(), iE)); } catch (TransformerConfigurationException innerE) { throw new IOException(innerE.getMessage()); } } }
From source file:eu.scape_project.planning.xml.PlanMigrator.java
public boolean transformXmlData(final String fromFile, final String toFile, final String xslFile) throws IOException { try {//w w w. j av a 2 s. com InputStream xsl = Thread.currentThread().getContextClassLoader().getResourceAsStream(xslFile); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(new StreamSource(xsl)); OutputStream transformedOut = new FileOutputStream(toFile); Result outputTarget = new StreamResult(transformedOut); Source xmlSource = new StreamSource(new FileInputStream(fromFile)); transformer.transform(xmlSource, outputTarget); transformedOut.close(); return true; } catch (TransformerConfigurationException e) { log.debug(e.getMessage(), e); } catch (TransformerFactoryConfigurationError e) { log.debug(e.getMessage(), e); } catch (TransformerException e) { log.debug(e.getMessage(), e); } return false; }
From source file:org.lareferencia.backend.indexer.IntelligoIndexer.java
private Transformer buildTransformer() throws IndexerException { Transformer trf;/* w ww . j a va 2 s .c om*/ try { StreamSource stylesource = new StreamSource(stylesheet); trf = xformFactory.newTransformer(stylesource); trf = MedatadaDOMHelper.buildXSLTTransformer(stylesheet); trf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trf.setOutputProperty(OutputKeys.INDENT, "yes"); trf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); } catch (TransformerConfigurationException e) { throw new IndexerException(e.getMessage(), e.getCause()); } return trf; }
From source file:fr.adfab.magebeans.processes.CreateModuleProcess.java
protected void _writeConfigXmlFile() { try {// ww w. j a v a2s. co m String configFile = this.moduleDir + "/" + this.CONFIG_PATH; Transformer tr = TransformerFactory.newInstance().newTransformer(); tr.setOutputProperty(OutputKeys.INDENT, "yes"); tr.setOutputProperty(OutputKeys.METHOD, "xml"); tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); tr.transform(new DOMSource(dom), new StreamResult(new FileOutputStream(configFile))); } catch (TransformerConfigurationException ex) { System.out.println(ex.getMessage()); } catch (TransformerException | FileNotFoundException ex) { System.out.println(ex.getMessage()); } }