1 17 18 19 20 package embedding; 21 22 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 27 import javax.xml.transform.Transformer ; 29 import javax.xml.transform.TransformerFactory ; 30 import javax.xml.transform.TransformerException ; 31 import javax.xml.transform.Source ; 32 import javax.xml.transform.Result ; 33 import javax.xml.transform.stream.StreamResult ; 34 import javax.xml.transform.stream.StreamSource ; 35 36 40 public class ExampleXML2FO { 41 42 50 public void convertXML2FO(File xml, File xslt, File fo) 51 throws IOException , TransformerException { 52 53 OutputStream out = new java.io.FileOutputStream (fo); 55 try { 56 TransformerFactory factory = TransformerFactory.newInstance(); 58 Transformer transformer = factory.newTransformer(new StreamSource (xslt)); 59 60 Source src = new StreamSource (xml); 62 63 Result res = new StreamResult (out); 65 66 transformer.transform(src, res); 68 } finally { 69 out.close(); 70 } 71 } 72 73 74 78 public static void main(String [] args) { 79 try { 80 System.out.println("FOP ExampleXML2FO\n"); 81 System.out.println("Preparing..."); 82 83 File baseDir = new File ("."); 85 File outDir = new File (baseDir, "out"); 86 outDir.mkdirs(); 87 88 File xmlfile = new File (baseDir, "xml/xml/projectteam.xml"); 90 File xsltfile = new File (baseDir, "xml/xslt/projectteam2fo.xsl"); 91 File fofile = new File (outDir, "ResultXML2FO.fo"); 92 93 System.out.println("Input: XML (" + xmlfile + ")"); 94 System.out.println("Stylesheet: " + xsltfile); 95 System.out.println("Output: XSL-FO (" + fofile + ")"); 96 System.out.println(); 97 System.out.println("Transforming..."); 98 99 ExampleXML2FO app = new ExampleXML2FO(); 100 app.convertXML2FO(xmlfile, xsltfile, fofile); 101 102 System.out.println("Success!"); 103 } catch (Exception e) { 104 e.printStackTrace(System.err); 105 System.exit(-1); 106 } 107 } 108 } 109
| Popular Tags
|