001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: ExampleDOM2PDF.java 332791 2005-11-12 15:58:07Z jeremias $ */
019:
020: package embedding.intermediate;
021:
022: import java.io.File;
023: import java.io.IOException;
024: import java.io.OutputStream;
025:
026: import javax.xml.transform.Result;
027: import javax.xml.transform.Source;
028: import javax.xml.transform.Transformer;
029: import javax.xml.transform.TransformerException;
030: import javax.xml.transform.TransformerFactory;
031: import javax.xml.transform.sax.SAXResult;
032: import javax.xml.transform.stream.StreamSource;
033:
034: import org.apache.fop.apps.FOPException;
035: import org.apache.fop.apps.FOUserAgent;
036: import org.apache.fop.apps.Fop;
037: import org.apache.fop.apps.FopFactory;
038: import org.apache.fop.apps.MimeConstants;
039: import org.apache.fop.area.AreaTreeModel;
040: import org.apache.fop.area.AreaTreeParser;
041: import org.apache.fop.area.RenderPagesModel;
042: import org.apache.fop.fonts.FontInfo;
043: import org.apache.fop.render.Renderer;
044: import org.apache.fop.render.xml.XMLRenderer;
045: import org.xml.sax.SAXException;
046:
047: import embedding.ExampleObj2XML;
048: import embedding.model.ProjectMember;
049: import embedding.model.ProjectTeam;
050:
051: /**
052: * Example for the intermediate format that demonstrates the concatenation of two documents
053: * renderered to the intermediate format. A single PDF file is generated from the two intermediate
054: * files.
055: */
056: public class ExampleConcat {
057:
058: // configure fopFactory as desired
059: private FopFactory fopFactory = FopFactory.newInstance();
060:
061: /**
062: * Creates a sample ProjectTeam instance for this demo.
063: * @return ProjectTeam the newly created ProjectTeam instance
064: */
065: public static ProjectTeam createAnotherProjectTeam() {
066: ProjectTeam team = new ProjectTeam();
067: team.setProjectName("The Dynamic Duo");
068: team.addMember(new ProjectMember("Batman", "lead",
069: "batman@heroes.org"));
070: team.addMember(new ProjectMember("Robin", "aid",
071: "robin@heroes.org"));
072: return team;
073: }
074:
075: /**
076: * Converts an XSL-FO document to an intermediate file.
077: * @param src the source file
078: * @param xslt the stylesheet file
079: * @param intermediate the target intermediate file (area tree XML)
080: * @throws IOException In case of an I/O problem
081: * @throws FOPException In case of a FOP problem
082: * @throws TransformerException In case of a XSL transformation problem
083: */
084: public void convertToIntermediate(Source src, Source xslt,
085: File intermediate) throws IOException, FOPException,
086: TransformerException {
087:
088: //Create a user agent
089: FOUserAgent userAgent = fopFactory.newFOUserAgent();
090:
091: //Create an instance of the target renderer so the XMLRenderer can use its font setup
092: Renderer targetRenderer = userAgent.getRendererFactory()
093: .createRenderer(userAgent, MimeConstants.MIME_PDF);
094:
095: //Create the XMLRenderer to create the intermediate format (area tree XML)
096: XMLRenderer xmlRenderer = new XMLRenderer();
097: xmlRenderer.setUserAgent(userAgent);
098:
099: //Tell the XMLRenderer to mimic the target renderer
100: xmlRenderer.mimicRenderer(targetRenderer);
101:
102: //Make sure the prepared XMLRenderer is used
103: userAgent.setRendererOverride(xmlRenderer);
104:
105: // Setup output
106: OutputStream out = new java.io.FileOutputStream(intermediate);
107: out = new java.io.BufferedOutputStream(out);
108: try {
109: // Construct fop (the MIME type here is unimportant due to the override
110: // on the user agent)
111: Fop fop = fopFactory.newFop(null, userAgent, out);
112:
113: // Setup XSLT
114: TransformerFactory factory = TransformerFactory
115: .newInstance();
116: Transformer transformer;
117: if (xslt != null) {
118: transformer = factory.newTransformer(xslt);
119: } else {
120: transformer = factory.newTransformer();
121: }
122:
123: // Resulting SAX events (the generated FO) must be piped through to FOP
124: Result res = new SAXResult(fop.getDefaultHandler());
125:
126: // Start XSLT transformation and FOP processing
127: transformer.transform(src, res);
128: } finally {
129: out.close();
130: }
131: }
132:
133: /**
134: * Concatenates an array of intermediate files to a single PDF file.
135: * @param files the array of intermediate files (area tree XML)
136: * @param pdffile the target PDF file
137: * @throws IOException In case of an I/O problem
138: * @throws TransformerException In case of a XSL transformation problem
139: * @throws SAXException In case of an XML-related problem
140: */
141: public void concatToPDF(File[] files, File pdffile)
142: throws IOException, TransformerException, SAXException {
143: // Setup output
144: OutputStream out = new java.io.FileOutputStream(pdffile);
145: out = new java.io.BufferedOutputStream(out);
146: try {
147: //Setup fonts and user agent
148: FontInfo fontInfo = new FontInfo();
149: FOUserAgent userAgent = fopFactory.newFOUserAgent();
150:
151: //Construct the AreaTreeModel that will received the individual pages
152: AreaTreeModel treeModel = new RenderPagesModel(userAgent,
153: MimeConstants.MIME_PDF, fontInfo, out);
154:
155: //Iterate over all intermediate files
156: AreaTreeParser parser = new AreaTreeParser();
157: for (int i = 0; i < files.length; i++) {
158: Source src = new StreamSource(files[i]);
159: parser.parse(src, treeModel, userAgent);
160: }
161:
162: //Signal the end of the processing. The renderer can finalize the target document.
163: treeModel.endDocument();
164: } finally {
165: out.close();
166: }
167: }
168:
169: /**
170: * Main method.
171: * @param args command-line arguments
172: */
173: public static void main(String[] args) {
174: try {
175: System.out.println("FOP ExampleConcat\n");
176:
177: //Setup directories
178: File baseDir = new File(".");
179: File outDir = new File(baseDir, "out");
180: outDir.mkdirs();
181:
182: //Setup output file
183: File xsltfile = new File(baseDir,
184: "xml/xslt/projectteam2fo.xsl");
185: File[] files = new File[] {
186: new File(outDir, "team1.at.xml"),
187: new File(outDir, "team2.at.xml") };
188: File pdffile = new File(outDir, "ResultConcat.pdf");
189: for (int i = 0; i < files.length; i++) {
190: System.out.println("Intermediate file " + (i + 1)
191: + ": " + files[i].getCanonicalPath());
192: }
193: System.out.println("PDF Output File: "
194: + pdffile.getCanonicalPath());
195: System.out.println();
196:
197: ProjectTeam team1 = ExampleObj2XML
198: .createSampleProjectTeam();
199: ProjectTeam team2 = createAnotherProjectTeam();
200:
201: ExampleConcat app = new ExampleConcat();
202:
203: //Create intermediate files
204: app.convertToIntermediate(team1.getSourceForProjectTeam(),
205: new StreamSource(xsltfile), files[0]);
206: app.convertToIntermediate(team2.getSourceForProjectTeam(),
207: new StreamSource(xsltfile), files[1]);
208:
209: //Concatenate the individual intermediate files to one document
210: app.concatToPDF(files, pdffile);
211:
212: System.out.println("Success!");
213:
214: } catch (Exception e) {
215: e.printStackTrace(System.err);
216: System.exit(-1);
217: }
218: }
219:
220: }
|