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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 1999-2003,2005-2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package embedding;
  18. //Java
  19. import java.io.File;
  20. import java.io.IOException;
  21. //JAXP
  22. import javax.xml.transform.Transformer;
  23. import javax.xml.transform.TransformerFactory;
  24. import javax.xml.transform.TransformerException;
  25. import javax.xml.transform.Source;
  26. import javax.xml.transform.Result;
  27. import javax.xml.transform.stream.StreamSource;
  28. import javax.xml.transform.sax.SAXResult;
  29. //Avalon
  30. import org.apache.avalon.framework.ExceptionUtil;
  31. //FOP
  32. import org.apache.fop.apps.FOPException;
  33. import org.apache.fop.apps.Fop;
  34. import org.apache.fop.apps.FopFactory;
  35. import org.apache.fop.apps.MimeConstants;
  36. /**
  37. * This class demonstrates the use of the AWT Viewer.
  38. */
  39. public class ExampleAWTViewer {
  40. // configure fopFactory as desired
  41. private FopFactory fopFactory = FopFactory.newInstance();
  42. /**
  43. * Display an FO file in the AWT Preview.
  44. * @param fo the FO file
  45. * @throws IOException In case of an I/O problem
  46. * @throws FOPException In case of a problem during layout
  47. * @throws TransformerException In case of a problem during XML processing
  48. */
  49. public void viewFO(File fo)
  50. throws IOException, FOPException, TransformerException {
  51. //Setup FOP
  52. Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AWT_PREVIEW);
  53. try {
  54. //Load XSL-FO file (you can also do an XSL transformation here)
  55. TransformerFactory factory = TransformerFactory.newInstance();
  56. Transformer transformer = factory.newTransformer();
  57. Source src = new StreamSource(fo);
  58. Result res = new SAXResult(fop.getDefaultHandler());
  59. transformer.transform(src, res);
  60. } catch (Exception e) {
  61. if (e instanceof FOPException) {
  62. throw (FOPException)e;
  63. }
  64. throw new FOPException(e);
  65. }
  66. }
  67. /**
  68. * Main method.
  69. * @param args the command-line arguments
  70. */
  71. public static void main(String[] args) {
  72. try {
  73. System.out.println("FOP ExampleAWTViewer\n");
  74. System.out.println("Preparing...");
  75. //Setup directories
  76. File baseDir = new File(".");
  77. File outDir = new File(baseDir, "out");
  78. outDir.mkdirs();
  79. //Setup input and output files
  80. File fofile = new File(baseDir, "xml/fo/helloworld.fo");
  81. System.out.println("Input: XSL-FO (" + fofile + ")");
  82. System.out.println("Output: AWT Viewer");
  83. System.out.println();
  84. System.out.println("Starting AWT Viewer...");
  85. ExampleAWTViewer app = new ExampleAWTViewer();
  86. app.viewFO(fofile);
  87. System.out.println("Success!");
  88. } catch (Exception e) {
  89. System.err.println(ExceptionUtil.printStackTrace(e));
  90. System.exit(-1);
  91. }
  92. }
  93. }