Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ExampleConcat.java 8.0KB

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