List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:MainClass.java
public static void main(String[] args) throws Exception { SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); SOAPHeader soapHeader = soapEnvelope.getHeader(); SOAPHeaderElement headerElement = soapHeader.addHeaderElement(soapEnvelope.createName("Signature", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12")); SOAPBody soapBody = soapEnvelope.getBody(); soapBody.addAttribute(/* w ww . j a v a2 s . c om*/ soapEnvelope.createName("id", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"), "Body"); Name bodyName = soapEnvelope.createName("FooBar", "z", "http://example.com"); SOAPBodyElement gltp = soapBody.addBodyElement(bodyName); Source source = soapPart.getContent(); Node root = null; if (source instanceof DOMSource) { root = ((DOMSource) source).getNode(); } else if (source instanceof SAXSource) { InputSource inSource = ((SAXSource) source).getInputSource(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = null; db = dbf.newDocumentBuilder(); Document doc = db.parse(inSource); root = (Node) doc.getDocumentElement(); } Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(root), new StreamResult(System.out)); }
From source file:Main.java
public static void main(String[] args) throws Exception { Main example = new Main(); example.data.put("France", "Paris"); example.data.put("Japan", "Tokyo"); JAXBContext context = JAXBContext.newInstance(Main.class); Marshaller marshaller = context.createMarshaller(); DOMResult result = new DOMResult(); marshaller.marshal(example, result); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); Document document = (Document) result.getNode(); XPathExpression expression = xpath.compile("//map/entry"); NodeList nodes = (NodeList) expression.evaluate(document, XPathConstants.NODESET); expression = xpath.compile("//map"); Node oldMap = (Node) expression.evaluate(document, XPathConstants.NODE); Element newMap = document.createElement("map"); for (int index = 0; index < nodes.getLength(); index++) { Element element = (Element) nodes.item(index); newMap.setAttribute(element.getAttribute("key"), element.getAttribute("value")); }//from w ww. j ava 2 s. co m expression = xpath.compile("//map/.."); Node parent = (Node) expression.evaluate(document, XPathConstants.NODE); parent.replaceChild(newMap, oldMap); TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(System.out)); }
From source file:Trace.java
public static void main(String[] args) throws java.io.IOException, TransformerException, TransformerConfigurationException, java.util.TooManyListenersException, org.xml.sax.SAXException { String fileName = "foo"; if (args.length > 0) fileName = args[0];//from w ww .jav a2 s.c om // Set up a PrintTraceListener object to print to a file. java.io.FileWriter fw = new java.io.FileWriter("events.log"); java.io.PrintWriter pw = new java.io.PrintWriter(fw, true); PrintTraceListener ptl = new PrintTraceListener(pw); // Print information as each node is 'executed' in the stylesheet. ptl.m_traceElements = true; // Print information after each result-tree generation event. ptl.m_traceGeneration = true; // Print information after each selection event. ptl.m_traceSelection = true; // Print information whenever a template is invoked. ptl.m_traceTemplates = true; // Print information whenever an extension call is made. ptl.m_traceExtension = true; // Set up the transformation TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(new StreamSource(fileName + ".xsl")); // Cast the Transformer object to TransformerImpl. if (transformer instanceof TransformerImpl) { TransformerImpl transformerImpl = (TransformerImpl) transformer; // Register the TraceListener with a TraceManager associated // with the TransformerImpl. TraceManager trMgr = transformerImpl.getTraceManager(); trMgr.addTraceListener(ptl); // Perform the transformation --printing information to // the events log during the process. transformer.transform(new StreamSource(fileName + ".xml"), new StreamResult(new java.io.FileWriter(fileName + ".out"))); } // Close the PrintWriter and FileWriter. pw.close(); fw.close(); System.out.println("**The output is in " + fileName + ".out; the log is in events.log ****"); }
From source file:ExternalConnection.java
public static void main(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { // Create a connection to the database server // Up the connection pool count for testing DefaultConnectionPool cp = new DefaultConnectionPool(); cp.setDriver("org.apache.derby.jdbc.EmbeddedDriver"); cp.setURL("jdbc:derby:sampleDB"); //cp.setUser("sa"); //cp.setPassword(""); cp.setMinConnections(10);/*ww w .ja v a 2 s. c o m*/ cp.setPoolEnabled(true); // Now let's register our connection pool so we can use // in a stylesheet ConnectionPoolManager pm = new ConnectionPoolManager(); pm.registerPool("extpool", cp); // Use the static TransformerFactory.newInstance() method to instantiate // a TransformerFactory. The javax.xml.transform.TransformerFactory // system property setting determines the actual class to instantiate -- // org.apache.xalan.transformer.TransformerImpl. TransformerFactory tFactory = TransformerFactory.newInstance(); // Grab the Name of the Stylesheet from the commad line if (args.length == 0) { System.out.println("You must provide the path and name to a stylesheet to process"); System.exit(0); } String stylesheet = args[0]; System.out.println("Transforming Stylesheet " + stylesheet); // Use the TransformerFactory to instantiate a Transformer that will work with // the stylesheet you specify. This method call also processes the stylesheet // into a compiled Templates object. Transformer transformer = tFactory.newTransformer(new StreamSource(stylesheet)); // For this transformation, all the required information is in the stylesheet, so generate // a minimal XML source document for the input. // Note: the command-line processor (org.apache.xalan.xslt.Process) uses this strategy when // the user does not provide an -IN parameter. StringReader reader = new StringReader("<?xml version=\"1.0\"?> <doc/>"); // Use the Transformer to apply the associated Templates object to an XML document // and write the output to a file. transformer.transform(new StreamSource(reader), new StreamResult(new FileOutputStream("dbtest-out.html"))); System.out.println("************* The result is in dbtest-out.html *************"); cp.setPoolEnabled(false); }
From source file:JAXPTransletOneTransformation.java
public static void main(String argv[]) throws TransformerException, TransformerConfigurationException, IOException, SAXException, ParserConfigurationException, FileNotFoundException { // Set the TransformerFactory system property to generate and use a translet. // Note: To make this sample more flexible, load properties from a properties file. // The setting for the Xalan Transformer is "org.apache.xalan.processor.TransformerFactoryImpl" String key = "javax.xml.transform.TransformerFactory"; String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl"; Properties props = System.getProperties(); props.put(key, value);//from w w w . jav a 2 s. co m System.setProperties(props); String xslInURI = "todo.xsl"; String xmlInURI = "todo.xml"; String htmlOutURI = "todo.html"; try { // Instantiate the TransformerFactory, and use it along with a SteamSource // XSL stylesheet to create a Transformer. TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(new StreamSource(xslInURI)); // Perform the transformation from a StreamSource to a StreamResult; transformer.transform(new StreamSource(xmlInURI), new StreamResult(new FileOutputStream(htmlOutURI))); System.out.println("Produced todo.html"); } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(); } }
From source file:JAXPTransletMultipleTransformations.java
public static void main(String argv[]) { // Set the TransformerFactory system property to generate and use translets. // Note: To make this sample more flexible, load properties from a properties file. // The setting for the Xalan Transformer is "org.apache.xalan.processor.TransformerFactoryImpl" String key = "javax.xml.transform.TransformerFactory"; String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl"; Properties props = System.getProperties(); props.put(key, value);//w ww. jav a 2s.c o m System.setProperties(props); String xslInURI = "todo.xsl"; try { // Instantiate the TransformerFactory, and use it along with a SteamSource // XSL stylesheet to create a translet as a Templates object. TransformerFactory tFactory = TransformerFactory.newInstance(); Templates translet = tFactory.newTemplates(new StreamSource(xslInURI)); // Perform each transformation doTransform(translet, "todo.xml", "todo.html"); System.out.println("Produced todo.html"); doTransform(translet, "todotoo.xml", "todotoo.html"); System.out.println("Produced todotoo.html"); } catch (Exception e) { e.printStackTrace(); } }
From source file:net.fenyo.gnetwatch.Documentation.java
/** * General entry point./*from w w w . j av a 2s . co m*/ * @param args command line arguments. * @return void. * @throws IOException io exception. */ public static void main(String[] args) throws IOException, TransformerConfigurationException, TransformerException, FileNotFoundException, FOPException { final String docbook_stylesheets_path = args[0]; // Get configuration properties. final Config config = new Config(); // Read general logging rules. GenericTools.initLogEngine(config); log.info(config.getString("log_engine_initialized")); log.info(config.getString("begin")); Transformer docbookTransformerHTML = TransformerFactory.newInstance() .newTransformer(new StreamSource(new File(docbook_stylesheets_path + "/html/docbook.xsl"))); // docbookTransformerHTML.setParameter("draft.mode", "no"); docbookTransformerHTML.transform(new StreamSource(new FileReader(new File("gnetwatch-documentation.xml"))), new StreamResult(new FileWriter(new File("c:/temp/gnetwatch-documentation.html")))); Transformer docbookTransformerFO = TransformerFactory.newInstance() .newTransformer(new StreamSource(new File(docbook_stylesheets_path + "/fo/docbook.xsl"))); // docbookTransformerFO.setParameter("draft.mode", "no"); docbookTransformerFO.transform(new StreamSource(new FileReader(new File("gnetwatch-documentation.xml"))), new StreamResult(new FileWriter(new File("c:/temp/gnetwatch-documentation.fo")))); // for very old FOP version (0.20): // Driver driver = new Driver(); // driver.setRenderer(Driver.RENDER_PDF); // driver.setInputSource(new InputSource(new FileReader(new File("c:/temp/gnetwatch-documentation.fo")))); // driver.setOutputStream(new FileOutputStream(new File("c:/temp/gnetwatch-documentation.pdf"))); // driver.run(); // with new FOP version: OutputStream outStream = new BufferedOutputStream( new FileOutputStream("c:/temp/gnetwatch-documentation.pdf")); final FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()); Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outStream); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); Source source = new StreamSource(new FileReader(new File("c:/temp/gnetwatch-documentation.fo"))); Result result = new SAXResult(fop.getDefaultHandler()); transformer.transform(source, result); outStream.close(); }
From source file:com.semsaas.jsonxml.tools.JsonXpath.java
public static void main(String[] args) { /*/*from www . jav a 2 s. co m*/ * Process options */ LinkedList<String> files = new LinkedList<String>(); LinkedList<String> expr = new LinkedList<String>(); boolean help = false; String activeOption = null; String error = null; for (int i = 0; i < args.length && error == null && !help; i++) { if (activeOption != null) { if (activeOption.equals("-e")) { expr.push(args[i]); } else if (activeOption.equals("-h")) { help = true; } else { error = "Unknown option " + activeOption; } activeOption = null; } else { if (args[i].startsWith("-")) { activeOption = args[i]; } else { files.push(args[i]); } } } if (error != null) { System.err.println(error); showHelp(); } else if (help) { showHelp(); } else { try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); for (String f : files) { System.out.println("*** " + f + " ***"); try { // Create a JSON XML reader XMLReader reader = XMLReaderFactory.createXMLReader("com.semsaas.jsonxml.JsonXMLReader"); // Prepare a reader with the JSON file as input InputStreamReader stringReader = new InputStreamReader(new FileInputStream(f)); SAXSource saxSource = new SAXSource(reader, new InputSource(stringReader)); // Prepare a DOMResult which will hold the DOM of the xjson DOMResult domResult = new DOMResult(); // Run SAX processing through a transformer // (This could be done more simply, but we have here the opportunity to pass our xjson through // an XSLT and get a legacy XML output ;) ) transformer.transform(saxSource, domResult); Node dom = domResult.getNode(); XPathFactory xpathFactory = XPathFactory.newInstance(); for (String x : expr) { try { XPath xpath = xpathFactory.newXPath(); xpath.setNamespaceContext(new NamespaceContext() { public Iterator getPrefixes(String namespaceURI) { return null; } public String getPrefix(String namespaceURI) { return null; } public String getNamespaceURI(String prefix) { if (prefix == null) { return XJSON.XMLNS; } else if ("j".equals(prefix)) { return XJSON.XMLNS; } else { return null; } } }); NodeList nl = (NodeList) xpath.evaluate(x, dom, XPathConstants.NODESET); System.out.println("-- Found " + nl.getLength() + " nodes for xpath '" + x + "' in file '" + f + "'"); for (int i = 0; i < nl.getLength(); i++) { System.out.println(" +(" + i + ")+ "); XMLJsonGenerator handler = new XMLJsonGenerator(); StringWriter buffer = new StringWriter(); handler.setOutputWriter(buffer); SAXResult result = new SAXResult(handler); transformer.transform(new DOMSource(nl.item(i)), result); System.out.println(buffer.toString()); } } catch (XPathExpressionException e) { System.err.println("-- Error evaluating '" + x + "' on file '" + f + "'"); e.printStackTrace(); } catch (TransformerException e) { System.err.println("-- Error evaluating '" + x + "' on file '" + f + "'"); e.printStackTrace(); } } } catch (FileNotFoundException e) { System.err.println("File '" + f + "' was not found"); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } } catch (TransformerConfigurationException e) { e.printStackTrace(); } } }
From source file:Main.java
public static void refreshTransformerFactory() { transformerFactory = TransformerFactory.newInstance(); }
From source file:Main.java
public static synchronized TransformerFactory getTransformerFactory() { if (TRANSFORMER_FACTORY == null) { TRANSFORMER_FACTORY = TransformerFactory.newInstance(); }/*from w w w .j a va 2 s . com*/ return TRANSFORMER_FACTORY; }