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

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