List of usage examples for javax.xml.transform TransformerException toString
public String toString()
From source file:org.jenkinsci.plugins.pipeline.maven.util.XmlUtils.java
@Nonnull public static String toString(@Nullable Node node) { try {//from w ww .ja va2 s . c o m StringWriter out = new StringWriter(); Transformer identityTransformer = TransformerFactory.newInstance().newTransformer(); identityTransformer.transform(new DOMSource(node), new StreamResult(out)); return out.toString(); } catch (TransformerException e) { LOGGER.log(Level.WARNING, "Exception dumping node " + node, e); return e.toString(); } }
From source file:org.kuali.test.utils.Utils.java
public static String printElement(Element node) { String retval = ""; try {/*w w w. j a v a 2 s.com*/ TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, " "); transformer.transform(new DOMSource(node), new StreamResult(buffer)); retval = buffer.toString(); } catch (TransformerException ex) { retval = ex.toString(); } return retval; }
From source file:org.smartfrog.avalanche.client.sf.apps.gt4.javawscore.DeployToTomcat.java
/** * This method is valid for both Tomcat 4.1.x and 5.x * /*from w w w . ja va 2 s . c o m*/ * Edits the file $<tomcat.dir>/conf/server.xml to include the appropriate * connectors. * If default port (8443) is not used, then edits the file * $<tomcat.dir>/webapps/wsrf/WEB-INF/web.xml to include the non-default port. * @param connectionAttrs * @param valueAttrs * @return */ public void editXMLFiles(String version, Hashtable connectionAttrs, Hashtable valveAttrs) throws WSCoreException { String fileName = new String(tomcatDirectory.concat("/conf/server.xml")); fileName = fileName.replace('\\', File.separatorChar); fileName = fileName.replace('/', File.separatorChar); EditXML xmlHelper; EditXML webXMLHelper; boolean defaultPort = true; Element engine = null; log.info("Entered editXMLFiles... with tomcat version : " + version); try { log.info("server.xml file location : " + fileName); xmlHelper = new EditXML(fileName); log.info("EditXML object created"); if (version.startsWith("4.1")) { addConnectorForTomcat4_1_x(xmlHelper, connectionAttrs); engine = xmlHelper.getElementByTagNameAttrName("Engine", "Standalone"); } else if (version.startsWith("5.")) { log.info("Before adding connector"); addConnectorForTomcat5_x(xmlHelper, connectionAttrs); log.info("After adding connector"); engine = xmlHelper.getElementByTagNameAttrName("Engine", "Catalina"); } else { log.error("The version " + version + " is not supported"); throw new WSCoreException("The version " + version + " is not supported"); } log.info("Before adding Valve"); Element valve = xmlHelper.createElementWithAttrs("Valve", valveAttrs); xmlHelper.addFirstChild(engine, valve); log.info("After adding Valve"); if (!((String) connectionAttrs.get("port")).equals("8443")) { defaultPort = false; String webXMLFile = new String( tomcatDirectory.concat("/webapps/" + webappName + "/WEB-INF/web.xml")); webXMLFile = webXMLFile.replace('/', File.separatorChar); webXMLFile = webXMLFile.replace('\\', File.separatorChar); webXMLHelper = new EditXML(webXMLFile); Element servlet = webXMLHelper.getElementByTagName("servlet"); if (null == servlet) { log.error("Cannot find the node 'servlet' in the file " + webXMLFile); throw new WSCoreException("Cannot find the node 'servlet' in the file " + webXMLFile); } /* * adding the following xml elements * <init-param> * <param-name>defaultProtocol</param-name> * <param-value>https</param-value> * </init-param> */ Element protocol = webXMLHelper.createElement("init-param", null); Element protocolName = webXMLHelper.createElement("param-name", "defaultProtocol"); Element protocolValue = webXMLHelper.createElement("param-value", "https"); webXMLHelper.addLastChild(protocol, protocolName); webXMLHelper.addLastChild(protocol, protocolValue); webXMLHelper.addBeforeLastChild(servlet, protocol); /* * Adds the following elements to web.xml * <init-param> * <param-name>defaultPort</param-name> * <param-value>443</param-value> * </init-param> */ Element portElement = webXMLHelper.createElement("init-param", null); Element portName = webXMLHelper.createElement("param-name", "defaultPort"); Element portValue = webXMLHelper.createElement("param-value", (String) connectionAttrs.get("port")); webXMLHelper.addLastChild(portElement, portName); webXMLHelper.addLastChild(portElement, portValue); webXMLHelper.addBeforeLastChild(servlet, portElement); xmlHelper.commitChanges(); if (!defaultPort) { webXMLHelper.commitChanges(); } } } catch (ParserConfigurationException pce) { throw new WSCoreException(pce.toString()); } catch (IOException ioe) { throw new WSCoreException(ioe.toString()); } catch (SAXException se) { throw new WSCoreException(se.toString()); } catch (TransformerException te) { throw new WSCoreException(te.toString()); } /*finally { try { xmlHelper.commitChanges(); if (!defaultPort) { webXMLHelper.commitChanges(); } }catch (FileNotFoundException fnf) { log.error(fnf); }catch (TransformerException te) { log.error(te); } }*/ }
From source file:org.smartfrog.avalanche.client.sf.apps.gt4wscore.DeployToTomcat.java
/** * This method is valid for both Tomcat 4.1.x and 5.x * /*from w w w.j ava2 s. co m*/ * Edits the file $<tomcat.dir>/conf/server.xml to include the appropriate * connectors. * If default port (8443) is not used, then edits the file * $<tomcat.dir>/webapps/wsrf/WEB-INF/web.xml to include the non-default port. * @param connectionAttrs * @param valueAttrs * @return */ public boolean editXMLFiles(String version, Hashtable connectionAttrs, Hashtable valveAttrs) throws TomcatConfigException { String fileName = new String(tomcatDirectory.concat("/conf/server.xml")); fileName = fileName.replace('\\', File.separatorChar); fileName = fileName.replace('/', File.separatorChar); EditXML xmlHelper = null; EditXML webXMLHelper = null; boolean defaultPort = true; Element engine = null; try { xmlHelper = new EditXML(fileName); try { if (version.startsWith("4.1")) { if (!addConnectorForTomcat4_1_x(xmlHelper, connectionAttrs)) { return false; } engine = xmlHelper.getElementByTagNameAttrName("Engine", "Standalone"); } else if (version.startsWith("5.")) { if (!addConnectorForTomcat5_x(xmlHelper, connectionAttrs)) { return false; } engine = xmlHelper.getElementByTagNameAttrName("Engine", "Catalina"); } else { log.error("The version " + version + " is not supported"); return false; } } catch (TransformerException te) { throw new TomcatConfigException(te.toString()); } Element valve = xmlHelper.createElementWithAttrs("Valve", valveAttrs); xmlHelper.addFirstChild(engine, valve); if (!((String) connectionAttrs.get("port")).equals("8443")) { defaultPort = false; String webXMLFile = new String( tomcatDirectory.concat("/webapps/" + webappName + "/WEB-INF/web.xml")); webXMLFile = webXMLFile.replace('/', File.separatorChar); webXMLFile = webXMLFile.replace('\\', File.separatorChar); webXMLHelper = new EditXML(webXMLFile); Element servlet = webXMLHelper.getElementByTagName("servlet"); if (null == servlet) { log.error("Cannot find the node 'servlet' in the file " + webXMLFile); return false; } /* * adding the following xml elements * <init-param> * <param-name>defaultProtocol</param-name> * <param-value>https</param-value> * </init-param> */ Element protocol = webXMLHelper.createElement("init-param", null); Element protocolName = webXMLHelper.createElement("param-name", "defaultProtocol"); Element protocolValue = webXMLHelper.createElement("param-value", "https"); webXMLHelper.addLastChild(protocol, protocolName); webXMLHelper.addLastChild(protocol, protocolValue); webXMLHelper.addBeforeLastChild(servlet, protocol); /* * Adds the following elements to web.xml * <init-param> * <param-name>defaultPort</param-name> * <param-value>443</param-value> * </init-param> */ Element portElement = webXMLHelper.createElement("init-param", null); Element portName = webXMLHelper.createElement("param-name", "defaultPort"); Element portValue = webXMLHelper.createElement("param-value", (String) connectionAttrs.get("port")); webXMLHelper.addLastChild(portElement, portName); webXMLHelper.addLastChild(portElement, portValue); webXMLHelper.addBeforeLastChild(servlet, portElement); } } catch (ParserConfigurationException pce) { throw new TomcatConfigException(pce.toString()); } catch (IOException ioe) { throw new TomcatConfigException(ioe.toString()); } catch (SAXException se) { throw new TomcatConfigException(se.toString()); } finally { try { xmlHelper.commitChanges(); if (!defaultPort) { webXMLHelper.commitChanges(); } } catch (FileNotFoundException fnf) { log.error(fnf); return false; } catch (TransformerException te) { log.error(te); return false; } } return true; }
From source file:org.toobsframework.transformpipeline.domain.TransletTransformer.java
@SuppressWarnings("unchecked") protected void doTransform(String xslTranslet, StreamSource xmlSource, Map params, IXMLTransformerHelper transformerHelper, StreamResult xmlResult) throws XMLTransformerException { try {/* ww w . j a va 2s . c om*/ // Dont rely on the system property to get the right transformer TransformerFactory tFactory = new org.apache.xalan.xsltc.trax.TransformerFactoryImpl(); // set the URI Resolver for the transformer factory setFactoryResolver(tFactory); Source source = uriResolver.resolve(xslTranslet + ".xsl", ""); String tPkg = source.getSystemId().substring(0, source.getSystemId().lastIndexOf("/")) .replaceAll("/", ".").replaceAll("-", "_"); try { //tFactory.setAttribute("use-classpath", Boolean.TRUE); tFactory.setAttribute("auto-translet", Boolean.TRUE); tFactory.setAttribute("package-name", tPkg); } catch (IllegalArgumentException iae) { log.error("Error setting XSLTC specific attribute", iae); throw new XMLTransformerException(iae); } Transformer transformer = tFactory.newTransformer(source); // 2.2 Set character encoding for all transforms to UTF-8. transformer.setOutputProperty("encoding", "UTF-8"); transformer.setErrorListener(tFactory.getErrorListener()); // 2.5 Set Parameters necessary for transformation. if (params != null) { Iterator paramIt = params.entrySet().iterator(); while (paramIt.hasNext()) { Map.Entry thisParam = (Map.Entry) paramIt.next(); transformer.setParameter((String) thisParam.getKey(), (String) thisParam.getValue()); } } if (transformerHelper != null) { transformer.setParameter(TRANSFORMER_HELPER, transformerHelper); } // 3. Use the Transformer to transform an XML Source and send the // output to a Result object. transformer.transform(xmlSource, xmlResult); } catch (TransformerConfigurationException tce) { log.error(tce.toString(), tce); throw new XMLTransformerException(tce); } catch (TransformerException te) { log.error(te.toString(), te); throw new XMLTransformerException(te); } }