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.

ExampleAWTViewer.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. //JAXP
  23. import javax.xml.transform.Transformer;
  24. import javax.xml.transform.TransformerFactory;
  25. import javax.xml.transform.TransformerException;
  26. import javax.xml.transform.Source;
  27. import javax.xml.transform.Result;
  28. import javax.xml.transform.stream.StreamSource;
  29. import javax.xml.transform.sax.SAXResult;
  30. //Avalon
  31. import org.apache.avalon.framework.ExceptionUtil;
  32. //FOP
  33. import org.apache.fop.apps.FOPException;
  34. import org.apache.fop.apps.Fop;
  35. import org.apache.fop.apps.FopFactory;
  36. import org.apache.fop.apps.MimeConstants;
  37. /**
  38. * This class demonstrates the use of the AWT Viewer.
  39. */
  40. public class ExampleAWTViewer {
  41. // configure fopFactory as desired
  42. private FopFactory fopFactory = FopFactory.newInstance();
  43. /**
  44. * Display an FO file in the AWT Preview.
  45. * @param fo the FO file
  46. * @throws IOException In case of an I/O problem
  47. * @throws FOPException In case of a problem during layout
  48. * @throws TransformerException In case of a problem during XML processing
  49. */
  50. public void viewFO(File fo)
  51. throws IOException, FOPException, TransformerException {
  52. //Setup FOP
  53. Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AWT_PREVIEW);
  54. try {
  55. //Load XSL-FO file (you can also do an XSL transformation here)
  56. TransformerFactory factory = TransformerFactory.newInstance();
  57. Transformer transformer = factory.newTransformer();
  58. Source src = new StreamSource(fo);
  59. Result res = new SAXResult(fop.getDefaultHandler());
  60. transformer.transform(src, res);
  61. } catch (Exception e) {
  62. if (e instanceof FOPException) {
  63. throw (FOPException)e;
  64. }
  65. throw new FOPException(e);
  66. }
  67. }
  68. /**
  69. * Main method.
  70. * @param args the command-line arguments
  71. */
  72. public static void main(String[] args) {
  73. try {
  74. System.out.println("FOP ExampleAWTViewer\n");
  75. System.out.println("Preparing...");
  76. //Setup directories
  77. File baseDir = new File(".");
  78. File outDir = new File(baseDir, "out");
  79. outDir.mkdirs();
  80. //Setup input and output files
  81. File fofile = new File(baseDir, "xml/fo/helloworld.fo");
  82. System.out.println("Input: XSL-FO (" + fofile + ")");
  83. System.out.println("Output: AWT Viewer");
  84. System.out.println();
  85. System.out.println("Starting AWT Viewer...");
  86. ExampleAWTViewer app = new ExampleAWTViewer();
  87. app.viewFO(fofile);
  88. System.out.println("Success!");
  89. } catch (Exception e) {
  90. System.err.println(ExceptionUtil.printStackTrace(e));
  91. System.exit(-1);
  92. }
  93. }
  94. }