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.

ExampleConcat.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.intermediate;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  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.StreamResult;
  29. import javax.xml.transform.stream.StreamSource;
  30. import org.xml.sax.SAXException;
  31. import org.apache.fop.apps.FOPException;
  32. import org.apache.fop.apps.FOUserAgent;
  33. import org.apache.fop.apps.Fop;
  34. import org.apache.fop.apps.FopFactory;
  35. import org.apache.fop.apps.MimeConstants;
  36. import org.apache.fop.render.intermediate.IFContext;
  37. import org.apache.fop.render.intermediate.IFDocumentHandler;
  38. import org.apache.fop.render.intermediate.IFException;
  39. import org.apache.fop.render.intermediate.IFSerializer;
  40. import org.apache.fop.render.intermediate.IFUtil;
  41. import org.apache.fop.render.intermediate.util.IFConcatenator;
  42. import embedding.ExampleObj2XML;
  43. import embedding.model.ProjectMember;
  44. import embedding.model.ProjectTeam;
  45. /**
  46. * Example for the intermediate format that demonstrates the concatenation of two documents
  47. * rendered to the intermediate format. A single PDF file is generated from the two intermediate
  48. * files.
  49. */
  50. public class ExampleConcat {
  51. // configure fopFactory as desired
  52. private FopFactory fopFactory = FopFactory.newInstance();
  53. /**
  54. * Creates a sample ProjectTeam instance for this demo.
  55. * @return ProjectTeam the newly created ProjectTeam instance
  56. */
  57. public static ProjectTeam createAnotherProjectTeam() {
  58. ProjectTeam team = new ProjectTeam();
  59. team.setProjectName("The Dynamic Duo");
  60. team.addMember(new ProjectMember(
  61. "Batman", "lead", "batman@heroes.org"));
  62. team.addMember(new ProjectMember(
  63. "Robin", "aid", "robin@heroes.org"));
  64. return team;
  65. }
  66. /**
  67. * Converts an XSL-FO document to an intermediate file.
  68. * @param src the source file
  69. * @param xslt the stylesheet file
  70. * @param intermediate the target intermediate file
  71. * @throws IOException In case of an I/O problem
  72. * @throws FOPException In case of a FOP problem
  73. * @throws TransformerException In case of a XSL transformation problem
  74. */
  75. public void convertToIntermediate(Source src, Source xslt, File intermediate)
  76. throws IOException, FOPException, TransformerException {
  77. //Create a user agent
  78. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  79. //Create an instance of the target document handler so the IFSerializer
  80. //can use its font setup
  81. IFDocumentHandler targetHandler = userAgent.getRendererFactory().createDocumentHandler(
  82. userAgent, MimeConstants.MIME_PDF + ";mode=painter");
  83. //Create the IFSerializer to write the intermediate format
  84. IFSerializer ifSerializer = new IFSerializer();
  85. ifSerializer.setContext(new IFContext(userAgent));
  86. //Tell the IFSerializer to mimic the target format
  87. ifSerializer.mimicDocumentHandler(targetHandler);
  88. //Make sure the prepared document handler is used
  89. userAgent.setDocumentHandlerOverride(ifSerializer);
  90. // Setup output
  91. OutputStream out = new java.io.FileOutputStream(intermediate);
  92. out = new java.io.BufferedOutputStream(out);
  93. try {
  94. // Construct FOP (the MIME type here is unimportant due to the override
  95. // on the user agent)
  96. Fop fop = fopFactory.newFop(null, userAgent, out);
  97. // Setup XSLT
  98. TransformerFactory factory = TransformerFactory.newInstance();
  99. Transformer transformer;
  100. if (xslt != null) {
  101. transformer = factory.newTransformer(xslt);
  102. } else {
  103. transformer = factory.newTransformer();
  104. }
  105. // Resulting SAX events (the generated FO) must be piped through to FOP
  106. Result res = new SAXResult(fop.getDefaultHandler());
  107. // Start XSLT transformation and FOP processing
  108. transformer.transform(src, res);
  109. } finally {
  110. out.close();
  111. }
  112. }
  113. /**
  114. * Concatenates an array of intermediate files to a single PDF file.
  115. * @param files the array of intermediate files
  116. * @param pdffile the target PDF file
  117. * @throws IOException In case of an I/O problem
  118. * @throws TransformerException In case of a XSL transformation problem
  119. * @throws SAXException In case of an XML-related problem
  120. * @throws IFException if there was an IF-related error while creating the output file
  121. */
  122. public void concatToPDF(File[] files, File pdffile)
  123. throws IOException, TransformerException, SAXException, IFException {
  124. // Setup output
  125. OutputStream out = new java.io.FileOutputStream(pdffile);
  126. out = new java.io.BufferedOutputStream(out);
  127. try {
  128. //Setup user agent
  129. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  130. //Setup target handler
  131. String mime = MimeConstants.MIME_PDF + ";mode=painter";
  132. IFDocumentHandler targetHandler = fopFactory.getRendererFactory().createDocumentHandler(
  133. userAgent, mime);
  134. //Setup fonts
  135. IFUtil.setupFonts(targetHandler);
  136. targetHandler.setResult(new StreamResult(pdffile));
  137. IFConcatenator concatenator = new IFConcatenator(targetHandler, null);
  138. //Iterate over all intermediate files
  139. for (int i = 0; i < files.length; i++) {
  140. Source src = new StreamSource(files[i]);
  141. concatenator.appendDocument(src);
  142. }
  143. //Signal the end of the processing so the target file can be finalized properly.
  144. concatenator.finish();
  145. } finally {
  146. out.close();
  147. }
  148. }
  149. /**
  150. * Main method.
  151. * @param args command-line arguments
  152. */
  153. public static void main(String[] args) {
  154. try {
  155. System.out.println("FOP ExampleConcat (for the Intermediate Format)\n");
  156. //Setup directories
  157. File baseDir = new File(".");
  158. File outDir = new File(baseDir, "out");
  159. outDir.mkdirs();
  160. //Setup output file
  161. File xsltfile = new File(baseDir, "xml/xslt/projectteam2fo.xsl");
  162. File[] files = new File[] {
  163. new File(outDir, "team1.if.xml"),
  164. new File(outDir, "team2.if.xml")};
  165. File pdffile = new File(outDir, "ResultIFConcat.pdf");
  166. for (int i = 0; i < files.length; i++) {
  167. System.out.println("Intermediate file " + (i + 1) + ": "
  168. + files[i].getCanonicalPath());
  169. }
  170. System.out.println("PDF Output File: " + pdffile.getCanonicalPath());
  171. System.out.println();
  172. ProjectTeam team1 = ExampleObj2XML.createSampleProjectTeam();
  173. ProjectTeam team2 = createAnotherProjectTeam();
  174. ExampleConcat app = new ExampleConcat();
  175. //Create intermediate files
  176. app.convertToIntermediate(
  177. team1.getSourceForProjectTeam(),
  178. new StreamSource(xsltfile), files[0]);
  179. app.convertToIntermediate(
  180. team2.getSourceForProjectTeam(),
  181. new StreamSource(xsltfile), files[1]);
  182. //Concatenate the individual intermediate files to one document
  183. app.concatToPDF(files, pdffile);
  184. System.out.println("Success!");
  185. } catch (Exception e) {
  186. e.printStackTrace(System.err);
  187. System.exit(-1);
  188. }
  189. }
  190. }