KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > embedding > ExampleXML2FO


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: ExampleXML2FO.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package embedding;
21
22 //Java
23
import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26
27 //JAXP
28
import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import javax.xml.transform.TransformerException JavaDoc;
31 import javax.xml.transform.Source JavaDoc;
32 import javax.xml.transform.Result JavaDoc;
33 import javax.xml.transform.stream.StreamResult JavaDoc;
34 import javax.xml.transform.stream.StreamSource JavaDoc;
35
36 /**
37  * This class demonstrates the conversion of an XML file to an XSL-FO file
38  * using JAXP (XSLT).
39  */

40 public class ExampleXML2FO {
41
42     /**
43      * Converts an XML file to an XSL-FO file using JAXP (XSLT).
44      * @param xml the XML file
45      * @param xslt the stylesheet file
46      * @param fo the target XSL-FO file
47      * @throws IOException In case of an I/O problem
48      * @throws TransformerException In case of a XSL transformation problem
49      */

50     public void convertXML2FO(File JavaDoc xml, File JavaDoc xslt, File JavaDoc fo)
51                 throws IOException JavaDoc, TransformerException JavaDoc {
52        
53         //Setup output
54
OutputStream JavaDoc out = new java.io.FileOutputStream JavaDoc(fo);
55         try {
56             //Setup XSLT
57
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
58             Transformer JavaDoc transformer = factory.newTransformer(new StreamSource JavaDoc(xslt));
59         
60             //Setup input for XSLT transformation
61
Source JavaDoc src = new StreamSource JavaDoc(xml);
62         
63             //Resulting SAX events (the generated FO) must be piped through to FOP
64
Result JavaDoc res = new StreamResult JavaDoc(out);
65
66             //Start XSLT transformation and FOP processing
67
transformer.transform(src, res);
68         } finally {
69             out.close();
70         }
71     }
72
73
74     /**
75      * Main method.
76      * @param args command-line arguments
77      */

78     public static void main(String JavaDoc[] args) {
79         try {
80             System.out.println("FOP ExampleXML2FO\n");
81             System.out.println("Preparing...");
82
83             //Setup directories
84
File JavaDoc baseDir = new File JavaDoc(".");
85             File JavaDoc outDir = new File JavaDoc(baseDir, "out");
86             outDir.mkdirs();
87
88             //Setup input and output files
89
File JavaDoc xmlfile = new File JavaDoc(baseDir, "xml/xml/projectteam.xml");
90             File JavaDoc xsltfile = new File JavaDoc(baseDir, "xml/xslt/projectteam2fo.xsl");
91             File JavaDoc fofile = new File JavaDoc(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 JavaDoc e) {
104             e.printStackTrace(System.err);
105             System.exit(-1);
106         }
107     }
108 }
109
Popular Tags