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.Source;
027: import javax.xml.transform.Transformer;
028: import javax.xml.transform.TransformerException;
029: import javax.xml.transform.TransformerFactory;
030: import javax.xml.transform.sax.SAXResult;
031: import javax.xml.transform.stream.StreamSource;
032:
033: import org.apache.fop.apps.FOUserAgent;
034: import org.apache.fop.apps.FopFactory;
035: import org.apache.fop.apps.MimeConstants;
036: import org.apache.fop.area.AreaTreeModel;
037: import org.apache.fop.area.AreaTreeParser;
038: import org.apache.fop.area.RenderPagesModel;
039: import org.apache.fop.fonts.FontInfo;
040: import org.xml.sax.SAXException;
041:
042: import embedding.ExampleObj2XML;
043: import embedding.model.ProjectTeam;
044:
045: /**
046: * Example for the intermediate format that demonstrates the stamping of a document with some
047: * kind of watermark. The resulting document is then rendered to a PDF file.
048: */
049: public class ExampleStamp {
050:
051: // configure fopFactory as desired
052: private FopFactory fopFactory = FopFactory.newInstance();
053:
054: /**
055: * Stamps an intermediate file and renders it to a PDF file.
056: * @param atfile the intermediate file (area tree XML)
057: * @param stampSheet the stylesheet that does the stamping
058: * @param pdffile the target PDF file
059: * @throws IOException In case of an I/O problem
060: * @throws TransformerException In case of a XSL transformation problem
061: * @throws SAXException In case of an XML-related problem
062: */
063: public void stampToPDF(File atfile, File stampSheet, File pdffile)
064: throws IOException, TransformerException, SAXException {
065: // Setup output
066: OutputStream out = new java.io.FileOutputStream(pdffile);
067: out = new java.io.BufferedOutputStream(out);
068: try {
069: //Setup fonts and user agent
070: FontInfo fontInfo = new FontInfo();
071: FOUserAgent userAgent = fopFactory.newFOUserAgent();
072:
073: //Construct the AreaTreeModel that will received the individual pages
074: AreaTreeModel treeModel = new RenderPagesModel(userAgent,
075: MimeConstants.MIME_PDF, fontInfo, out);
076:
077: //Iterate over all intermediate files
078: AreaTreeParser parser = new AreaTreeParser();
079: Source src = new StreamSource(atfile);
080: Source xslt = new StreamSource(stampSheet);
081:
082: //Setup Transformer for XSLT processing
083: TransformerFactory tFactory = TransformerFactory
084: .newInstance();
085: Transformer transformer = tFactory.newTransformer(xslt);
086:
087: //Send XSLT result to AreaTreeParser
088: SAXResult res = new SAXResult(parser.getContentHandler(
089: treeModel, userAgent));
090:
091: //Start XSLT transformation and area tree parsing
092: transformer.transform(src, res);
093:
094: //Signal the end of the processing. The renderer can finalize the target document.
095: treeModel.endDocument();
096: } finally {
097: out.close();
098: }
099: }
100:
101: /**
102: * Main method.
103: * @param args command-line arguments
104: */
105: public static void main(String[] args) {
106: try {
107: System.out.println("FOP ExampleConcat\n");
108:
109: //Setup directories
110: File baseDir = new File(".");
111: File outDir = new File(baseDir, "out");
112: outDir.mkdirs();
113:
114: //Setup output file
115: File xsltfile = new File(baseDir,
116: "xml/xslt/projectteam2fo.xsl");
117: File atfile = new File(outDir, "team.at.xml");
118: File stampxsltfile = new File(baseDir,
119: "xml/xslt/atstamp.xsl");
120: File pdffile = new File(outDir, "ResultStamped.pdf");
121: System.out.println("Intermediate file : "
122: + atfile.getCanonicalPath());
123: System.out.println("Stamp XSLT: "
124: + stampxsltfile.getCanonicalPath());
125: System.out.println("PDF Output File: "
126: + pdffile.getCanonicalPath());
127: System.out.println();
128:
129: ProjectTeam team1 = ExampleObj2XML
130: .createSampleProjectTeam();
131:
132: //Create intermediate file
133: ExampleConcat concatapp = new ExampleConcat();
134: concatapp.convertToIntermediate(team1
135: .getSourceForProjectTeam(), new StreamSource(
136: xsltfile), atfile);
137:
138: //Stamp document and produce a PDF from the intermediate format
139: ExampleStamp app = new ExampleStamp();
140: app.stampToPDF(atfile, stampxsltfile, pdffile);
141:
142: System.out.println("Success!");
143:
144: } catch (Exception e) {
145: e.printStackTrace(System.err);
146: System.exit(-1);
147: }
148: }
149:
150: }
|