List of usage examples for javax.xml.transform.stream StreamSource StreamSource
public StreamSource(File f)
From source file:XSLTransform.java
public static void main(String[] args) throws TransformerException { // Set up streams for input, stylesheet, and output. // These do not have to come from or go to files. We can also use the // javax.xml.transform.{dom,sax} packages use DOM trees and streams of // SAX events as sources and sinks for documents and stylesheets. StreamSource input = new StreamSource(new File(args[0])); StreamSource stylesheet = new StreamSource(new File(args[1])); StreamResult output = new StreamResult(new File(args[2])); // Get a factory object, create a Transformer from it, and // transform the input document to the output document. TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(stylesheet); transformer.transform(input, output); }
From source file:UseStylesheetPI.java
public static void main(String[] args) throws TransformerException, TransformerConfigurationException { String media = null, title = null, charset = null; try {// w w w . ja va 2 s . c o m TransformerFactory tFactory = TransformerFactory.newInstance(); Source stylesheet = tFactory.getAssociatedStylesheet(new StreamSource("fooX.xml"), media, title, charset); Transformer transformer = tFactory.newTransformer(stylesheet); transformer.transform(new StreamSource("fooX.xml"), new StreamResult(new java.io.FileOutputStream("foo.out"))); System.out.println("************* The result is in foo.out *************"); } catch (Exception e) { e.printStackTrace(); } }
From source file:XMLTransform.java
public static void main(String args[]) { if (args.length != 2) { System.err.println("Usage: java XMLTransform xmlfile.xml stylesheet.xsl"); System.exit(-1);/*from w w w . j a v a2 s .co m*/ } try { StreamSource source = new StreamSource(args[0]); StreamSource stylesource = new StreamSource(args[1]); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(stylesource); StreamResult result = new StreamResult(System.out); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<?xml version='1.0'?><test><test2></test2></test>"; String schemaString = // "<?xml version='1.0'?>"// + "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' elementFormDefault='unqualified' attributeFormDefault='unqualified'>"// + "<xsd:element name='test' type='Test'/>"// + "<xsd:element name='test2' type='Test2'/>"// + "<xsd:complexType name='Test'>"// + "<xsd:sequence>"// + "<xsd:element ref='test2' minOccurs='1' maxOccurs='unbounded'/>"// + "</xsd:sequence>"// + "</xsd:complexType>"// + "<xsd:simpleType name='Test2'>"// + "<xsd:restriction base='xsd:string'><xsd:minLength value='1'/></xsd:restriction>"// + "</xsd:simpleType>"// + "</xsd:schema>"; Source schemaSource = new StreamSource(new StringReader(schemaString)); Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaSource); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true);// w w w . j a va 2 s. c o m factory.setSchema(schema); SAXParser parser = factory.newSAXParser(); MyContentHandler handler = new MyContentHandler(); parser.parse(new InputSource(new StringReader(xml)), handler); }
From source file:UseStylesheetParam.java
public static void main(String[] args) throws TransformerException, TransformerConfigurationException, SAXException, IOException { if (args.length != 1) { System.err.println("Please pass one string to this program"); return;//from w w w .jav a 2 s . c o m } // Get the parameter value from the command line. String paramValue = args[0]; TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(new StreamSource("foo.xsl")); // Set the parameter. I can't get non-null namespaces to work!! transformer.setParameter("param1", /* parameter name */ paramValue /* parameter value */ ); transformer.transform(new StreamSource("foo.xml"), new StreamResult(new OutputStreamWriter(System.out))); }
From source file:SimpleTransform.java
public static void main(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { // 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(); // 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("birds.xsl")); // Use the Transformer to apply the associated Templates object to an XML document // (foo.xml) and write the output to a file (foo.out). transformer.transform(new StreamSource("birds.xml"), new StreamResult(new FileOutputStream("birds.out"))); System.out.println("************* The result is in birds.out *************"); }
From source file:org.jasig.portlet.data.Importer.java
public static void main(String[] args) throws Exception { String dir = args[0];/*w ww . ja va 2 s . c o m*/ String importExportContext = args[1]; String sessionFactoryBeanName = args[2]; String modelClassName = args[3]; String serviceBeanName = args[4]; String serviceBeanMethodName = args[5]; ApplicationContext context = PortletApplicationContextLocator.getApplicationContext(importExportContext); SessionFactory sessionFactory = context.getBean(sessionFactoryBeanName, SessionFactory.class); Class<?> modelClass = Class.forName(modelClassName); Object service = context.getBean(serviceBeanName); JAXBContext jc = JAXBContext.newInstance(modelClass); File folder = new File(dir); File[] files = folder.listFiles(new ImportFileFilter()); for (File f : files) { StreamSource xml = new StreamSource(f.getAbsoluteFile()); Unmarshaller unmarshaller = jc.createUnmarshaller(); JAXBElement je1 = unmarshaller.unmarshal(xml, modelClass); Object object = je1.getValue(); Session session = sessionFactory.getCurrentSession(); Transaction transaction = session.beginTransaction(); Method method = service.getClass().getMethod(serviceBeanMethodName, modelClass); method.invoke(service, object); transaction.commit(); } }
From source file:Pipe.java
public static void main(String[] args) throws TransformerException, TransformerConfigurationException, SAXException, IOException { // Instantiate a TransformerFactory. TransformerFactory tFactory = TransformerFactory.newInstance(); // Determine whether the TransformerFactory supports The use uf SAXSource // and SAXResult if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) { // Cast the TransformerFactory to SAXTransformerFactory. SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory); // Create a TransformerHandler for each stylesheet. TransformerHandler tHandler1 = saxTFactory.newTransformerHandler(new StreamSource("foo1.xsl")); TransformerHandler tHandler2 = saxTFactory.newTransformerHandler(new StreamSource("foo2.xsl")); TransformerHandler tHandler3 = saxTFactory.newTransformerHandler(new StreamSource("foo3.xsl")); // Create an XMLReader. XMLReader reader = XMLReaderFactory.createXMLReader(); reader.setContentHandler(tHandler1); reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler1); tHandler1.setResult(new SAXResult(tHandler2)); tHandler2.setResult(new SAXResult(tHandler3)); // transformer3 outputs SAX events to the serializer. java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml"); xmlProps.setProperty("indent", "yes"); xmlProps.setProperty("standalone", "no"); Serializer serializer = SerializerFactory.getSerializer(xmlProps); serializer.setOutputStream(System.out); tHandler3.setResult(new SAXResult(serializer.asContentHandler())); // Parse the XML input document. The input ContentHandler and output ContentHandler // work in separate threads to optimize performance. reader.parse("foo.xml"); }/* ww w . j a v a 2 s . c o m*/ }
From source file:UseXMLFilters.java
public static void main(String[] args) throws TransformerException, TransformerConfigurationException, SAXException, IOException { // Instantiate a TransformerFactory. TransformerFactory tFactory = TransformerFactory.newInstance(); // Determine whether the TransformerFactory supports The use uf SAXSource // and SAXResult if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) { // Cast the TransformerFactory to SAXTransformerFactory. SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory); // Create an XMLFilter for each stylesheet. XMLFilter xmlFilter1 = saxTFactory.newXMLFilter(new StreamSource("foo1.xsl")); XMLFilter xmlFilter2 = saxTFactory.newXMLFilter(new StreamSource("foo2.xsl")); XMLFilter xmlFilter3 = saxTFactory.newXMLFilter(new StreamSource("foo3.xsl")); // Create an XMLReader. XMLReader reader = XMLReaderFactory.createXMLReader(); // xmlFilter1 uses the XMLReader as its reader. xmlFilter1.setParent(reader);/*from www. ja v a2 s . com*/ // xmlFilter2 uses xmlFilter1 as its reader. xmlFilter2.setParent(xmlFilter1); // xmlFilter3 uses xmlFilter2 as its reader. xmlFilter3.setParent(xmlFilter2); // xmlFilter3 outputs SAX events to the serializer. java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml"); xmlProps.setProperty("indent", "yes"); xmlProps.setProperty("standalone", "no"); Serializer serializer = SerializerFactory.getSerializer(xmlProps); serializer.setOutputStream(System.out); xmlFilter3.setContentHandler(serializer.asContentHandler()); // Perform the series of transformations as follows: // - transformer3 gets its parent (transformer2) as the XMLReader/XMLFilter // and calls transformer2.parse(new InputSource("foo.xml")). // - transformer2 gets its parent (transformer1) as the XMLReader/XMLFilter // and calls transformer1.parse(new InputSource("foo.xml")). // - transformer1 gets its parent (reader, a SAXParser) as the XMLReader // and calls reader.parse(new InputSource("foo.xml")). // - reader parses the XML document and sends the SAX parse events to transformer1, // which performs transformation 1 and sends the output to transformer2. // - transformer2 parses the transformation 1 output, performs transformation 2, and // sends the output to transformer3. // - transformer3 parses the transformation 2 output, performs transformation 3, // and sends the output to the serializer. xmlFilter3.parse(new InputSource("foo.xml")); } }
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 w w. ja v a 2s. com // 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 ****"); }