| 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$ */
|
| 19 |
|
| 20 |
package embedding;
|
| 21 |
|
| 22 |
//Java
|
| 23 |
import java.io.File;
|
| 24 |
import java.io.IOException;
|
| 25 |
import java.io.OutputStream;
|
| 26 |
|
| 27 |
//JAXP
|
| 28 |
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 |
/**
|
| 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 xml, File xslt, File fo)
|
| 51 |
throws IOException, TransformerException {
|
| 52 |
|
| 53 |
//Setup output
|
| 54 |
OutputStream out = new java.io.FileOutputStream(fo);
|
| 55 |
try {
|
| 56 |
//Setup XSLT
|
| 57 |
TransformerFactory factory = TransformerFactory.newInstance();
|
| 58 |
Transformer transformer = factory.newTransformer(new StreamSource(xslt));
|
| 59 |
|
| 60 |
//Setup input for XSLT transformation
|
| 61 |
Source src = new StreamSource(xml);
|
| 62 |
|
| 63 |
//Resulting SAX events (the generated FO) must be piped through to FOP
|
| 64 |
Result res = new StreamResult(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[] args) {
|
| 79 |
try {
|
| 80 |
System.out.println("FOP ExampleXML2FO\n");
|
| 81 |
System.out.println("Preparing...");
|
| 82 |
|
| 83 |
//Setup directories
|
| 84 |
File baseDir = new File(".");
|
| 85 |
File outDir = new File(baseDir, "out");
|
| 86 |
outDir.mkdirs();
|
| 87 |
|
| 88 |
//Setup input and output files
|
| 89 |
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 |
}
|