001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: ExampleXML2FO.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package embedding;
021:
022: //Java
023: import java.io.File;
024: import java.io.IOException;
025: import java.io.OutputStream;
026:
027: //JAXP
028: import javax.xml.transform.Transformer;
029: import javax.xml.transform.TransformerFactory;
030: import javax.xml.transform.TransformerException;
031: import javax.xml.transform.Source;
032: import javax.xml.transform.Result;
033: import javax.xml.transform.stream.StreamResult;
034: import javax.xml.transform.stream.StreamSource;
035:
036: /**
037: * This class demonstrates the conversion of an XML file to an XSL-FO file
038: * using JAXP (XSLT).
039: */
040: public class ExampleXML2FO {
041:
042: /**
043: * Converts an XML file to an XSL-FO file using JAXP (XSLT).
044: * @param xml the XML file
045: * @param xslt the stylesheet file
046: * @param fo the target XSL-FO file
047: * @throws IOException In case of an I/O problem
048: * @throws TransformerException In case of a XSL transformation problem
049: */
050: public void convertXML2FO(File xml, File xslt, File fo)
051: throws IOException, TransformerException {
052:
053: //Setup output
054: OutputStream out = new java.io.FileOutputStream(fo);
055: try {
056: //Setup XSLT
057: TransformerFactory factory = TransformerFactory
058: .newInstance();
059: Transformer transformer = factory
060: .newTransformer(new StreamSource(xslt));
061:
062: //Setup input for XSLT transformation
063: Source src = new StreamSource(xml);
064:
065: //Resulting SAX events (the generated FO) must be piped through to FOP
066: Result res = new StreamResult(out);
067:
068: //Start XSLT transformation and FOP processing
069: transformer.transform(src, res);
070: } finally {
071: out.close();
072: }
073: }
074:
075: /**
076: * Main method.
077: * @param args command-line arguments
078: */
079: public static void main(String[] args) {
080: try {
081: System.out.println("FOP ExampleXML2FO\n");
082: System.out.println("Preparing...");
083:
084: //Setup directories
085: File baseDir = new File(".");
086: File outDir = new File(baseDir, "out");
087: outDir.mkdirs();
088:
089: //Setup input and output files
090: File xmlfile = new File(baseDir, "xml/xml/projectteam.xml");
091: File xsltfile = new File(baseDir,
092: "xml/xslt/projectteam2fo.xsl");
093: File fofile = new File(outDir, "ResultXML2FO.fo");
094:
095: System.out.println("Input: XML (" + xmlfile + ")");
096: System.out.println("Stylesheet: " + xsltfile);
097: System.out.println("Output: XSL-FO (" + fofile + ")");
098: System.out.println();
099: System.out.println("Transforming...");
100:
101: ExampleXML2FO app = new ExampleXML2FO();
102: app.convertXML2FO(xmlfile, xsltfile, fofile);
103:
104: System.out.println("Success!");
105: } catch (Exception e) {
106: e.printStackTrace(System.err);
107: System.exit(-1);
108: }
109: }
110: }
|