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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 1999-2003,2005 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.MimeConstants;
  35. import org.apache.fop.fo.Constants;
  36. /**
  37. * This class demonstrates the use of the AWT Viewer.
  38. */
  39. public class ExampleAWTViewer {
  40. public void viewFO(File fo)
  41. throws IOException, FOPException, TransformerException {
  42. //Setup FOP
  43. Fop fop = new Fop(MimeConstants.MIME_FOP_AWT_PREVIEW);
  44. try {
  45. //Load XSL-FO file (you can also do an XSL transformation here)
  46. TransformerFactory factory = TransformerFactory.newInstance();
  47. Transformer transformer = factory.newTransformer();
  48. Source src = new StreamSource(fo);
  49. Result res = new SAXResult(fop.getDefaultHandler());
  50. transformer.transform(src, res);
  51. } catch (Exception e) {
  52. if (e instanceof FOPException) {
  53. throw (FOPException)e;
  54. }
  55. throw new FOPException(e);
  56. }
  57. }
  58. public static void main(String[] args) {
  59. try {
  60. System.out.println("FOP ExampleAWTViewer\n");
  61. System.out.println("Preparing...");
  62. //Setup directories
  63. File baseDir = new File(".");
  64. File outDir = new File(baseDir, "out");
  65. outDir.mkdirs();
  66. //Setup input and output files
  67. File fofile = new File(baseDir, "xml/fo/helloworld.fo");
  68. System.out.println("Input: XSL-FO (" + fofile + ")");
  69. System.out.println("Output: AWT Viewer");
  70. System.out.println();
  71. System.out.println("Starting AWT Viewer...");
  72. ExampleAWTViewer app = new ExampleAWTViewer();
  73. app.viewFO(fofile);
  74. System.out.println("Success!");
  75. } catch (Exception e) {
  76. System.err.println(ExceptionUtil.printStackTrace(e));
  77. System.exit(-1);
  78. }
  79. }
  80. }