You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExampleXML2FO.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /* $Id$ */
  18. package embedding;
  19. //Java
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. //JAXP
  24. import javax.xml.transform.Transformer;
  25. import javax.xml.transform.TransformerFactory;
  26. import javax.xml.transform.TransformerException;
  27. import javax.xml.transform.Source;
  28. import javax.xml.transform.Result;
  29. import javax.xml.transform.stream.StreamResult;
  30. import javax.xml.transform.stream.StreamSource;
  31. /**
  32. * This class demonstrates the conversion of an XML file to an XSL-FO file
  33. * using JAXP (XSLT).
  34. */
  35. public class ExampleXML2FO {
  36. /**
  37. * Converts an XML file to an XSL-FO file using JAXP (XSLT).
  38. * @param xml the XML file
  39. * @param xslt the stylesheet file
  40. * @param fo the target XSL-FO file
  41. * @throws IOException In case of an I/O problem
  42. * @throws TransformerException In case of a XSL transformation problem
  43. */
  44. public void convertXML2FO(File xml, File xslt, File fo)
  45. throws IOException, TransformerException {
  46. //Setup output
  47. OutputStream out = new java.io.FileOutputStream(fo);
  48. try {
  49. //Setup XSLT
  50. TransformerFactory factory = TransformerFactory.newInstance();
  51. Transformer transformer = factory.newTransformer(new StreamSource(xslt));
  52. //Setup input for XSLT transformation
  53. Source src = new StreamSource(xml);
  54. //Resulting SAX events (the generated FO) must be piped through to FOP
  55. Result res = new StreamResult(out);
  56. //Start XSLT transformation and FOP processing
  57. transformer.transform(src, res);
  58. } finally {
  59. out.close();
  60. }
  61. }
  62. /**
  63. * Main method.
  64. * @param args command-line arguments
  65. */
  66. public static void main(String[] args) {
  67. try {
  68. System.out.println("FOP ExampleXML2FO\n");
  69. System.out.println("Preparing...");
  70. //Setup directories
  71. File baseDir = new File(".");
  72. File outDir = new File(baseDir, "out");
  73. outDir.mkdirs();
  74. //Setup input and output files
  75. File xmlfile = new File(baseDir, "xml/xml/projectteam.xml");
  76. File xsltfile = new File(baseDir, "xml/xslt/projectteam2fo.xsl");
  77. File fofile = new File(outDir, "ResultXML2FO.fo");
  78. System.out.println("Input: XML (" + xmlfile + ")");
  79. System.out.println("Stylesheet: " + xsltfile);
  80. System.out.println("Output: XSL-FO (" + fofile + ")");
  81. System.out.println();
  82. System.out.println("Transforming...");
  83. ExampleXML2FO app = new ExampleXML2FO();
  84. app.convertXML2FO(xmlfile, xsltfile, fofile);
  85. System.out.println("Success!");
  86. } catch (Exception e) {
  87. e.printStackTrace(System.err);
  88. System.exit(-1);
  89. }
  90. }
  91. }