Source Code Cross Referenced for MultipleFO2PDF.java in  » Graphic-Library » fop » embedding » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Graphic Library » fop » embedding 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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: MultipleFO2PDF.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020:        package embedding;
021:
022:        // Java
023:        import java.io.BufferedOutputStream;
024:        import java.io.BufferedReader;
025:        import java.io.File;
026:        import java.io.FileOutputStream;
027:        import java.io.IOException;
028:        import java.io.OutputStream;
029:
030:        //JAXP
031:        import javax.xml.transform.Transformer;
032:        import javax.xml.transform.TransformerException;
033:        import javax.xml.transform.TransformerFactory;
034:        import javax.xml.transform.Source;
035:        import javax.xml.transform.Result;
036:        import javax.xml.transform.stream.StreamSource;
037:        import javax.xml.transform.sax.SAXResult;
038:
039:        // FOP
040:        import org.apache.commons.io.IOUtils;
041:        import org.apache.fop.apps.FOUserAgent;
042:        import org.apache.fop.apps.Fop;
043:        import org.apache.fop.apps.FOPException;
044:        import org.apache.fop.apps.FopFactory;
045:        import org.apache.fop.apps.FormattingResults;
046:        import org.apache.fop.apps.MimeConstants;
047:        import org.apache.fop.apps.PageSequenceResults;
048:
049:        /**
050:         * This class demonstrates the conversion of multiple FO files to PDF using FOP.
051:         * The FopFactory is reused. Its configuration is applied to each rendering run.
052:         * The FOUserAgent and Fop are newly created by the FopFactory for each run.
053:         * The FOUserAgent can be configured differently for each run.
054:         */
055:        public class MultipleFO2PDF {
056:
057:            // configure fopFactory as desired
058:            private FopFactory fopFactory = FopFactory.newInstance();
059:
060:            // JAXP TransformerFactory can be reused, too
061:            private TransformerFactory factory = TransformerFactory
062:                    .newInstance();
063:
064:            /**
065:             * Converts an FO file to a PDF file using FOP
066:             * @param fo the FO file
067:             * @param pdf the target PDF file
068:             * @throws TransformerException in case of a transformation problem 
069:             * @throws IOException in case of an I/O problem
070:             * @throws FOPException in case of a FOP problem
071:             * @return the formatting results of the run
072:             */
073:            public FormattingResults convertFO2PDF(File fo, File pdf)
074:                    throws TransformerException, IOException, FOPException {
075:
076:                OutputStream out = null;
077:                Fop fop;
078:
079:                try {
080:                    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
081:                    // configure foUserAgent as desired
082:
083:                    // Setup output stream.  Note: Using BufferedOutputStream
084:                    // for performance reasons (helpful with FileOutputStreams).
085:                    out = new FileOutputStream(pdf);
086:                    out = new BufferedOutputStream(out);
087:
088:                    // Construct fop with desired output format and output stream
089:                    fop = fopFactory.newFop(MimeConstants.MIME_PDF,
090:                            foUserAgent, out);
091:
092:                    // Setup JAXP using identity transformer
093:                    Transformer transformer = factory.newTransformer(); // identity transformer
094:
095:                    // Setup input stream
096:                    Source src = new StreamSource(fo);
097:
098:                    // Resulting SAX events (the generated FO) must be piped through to FOP
099:                    Result res = new SAXResult(fop.getDefaultHandler());
100:
101:                    // Start XSLT transformation and FOP processing
102:                    transformer.transform(src, res);
103:                } finally {
104:                    IOUtils.closeQuietly(out);
105:                }
106:
107:                return fop.getResults();
108:            }
109:
110:            /** 
111:             * Listens on standard in for names of fo files to be transformed to pdf.
112:             * 'quit' or the null string (for piped input) cause the listener to stop listening.
113:             */
114:            public void listen() {
115:
116:                //Setup directories
117:                File baseDir = new File(".");
118:                File outDir = new File(baseDir, "out");
119:                outDir.mkdirs();
120:                BufferedReader in = new BufferedReader(
121:                        new java.io.InputStreamReader(System.in));
122:
123:                while (true) {
124:                    try {
125:                        // Listen for the input file name            
126:                        System.out
127:                                .print("Input XSL-FO file ('quit' to stop): ");
128:                        String foname = in.readLine();
129:                        if (foname == null) {
130:                            System.out.println("Null input, quitting");
131:                            return;
132:                        }
133:                        foname.trim();
134:                        if (foname.equals("quit")) {
135:                            System.out.println("Quitting");
136:                            return;
137:                        }
138:                        File fofile = new File(baseDir, foname);
139:                        String pdfname = foname;
140:                        int p = foname.lastIndexOf('.');
141:                        pdfname = foname.substring(0, p) + ".pdf";
142:                        File pdffile = new File(outDir, pdfname);
143:
144:                        // transform and render
145:                        System.out.print("Transforming " + fofile
146:                                + " to PDF file " + pdffile + "...");
147:                        FormattingResults foResults = convertFO2PDF(fofile,
148:                                pdffile);
149:                        System.out.println("done!");
150:
151:                        // Result processing
152:                        java.util.List pageSequences = foResults
153:                                .getPageSequences();
154:                        for (java.util.Iterator it = pageSequences.iterator(); it
155:                                .hasNext();) {
156:                            PageSequenceResults pageSequenceResults = (PageSequenceResults) it
157:                                    .next();
158:                            System.out.println("PageSequence "
159:                                    + (String.valueOf(
160:                                            pageSequenceResults.getID())
161:                                            .length() > 0 ? pageSequenceResults
162:                                            .getID() : "<no id>")
163:                                    + " generated "
164:                                    + pageSequenceResults.getPageCount()
165:                                    + " pages.");
166:                        }
167:                        System.out
168:                                .println("Generated "
169:                                        + foResults.getPageCount()
170:                                        + " pages in total.");
171:
172:                    } catch (Exception e) {
173:                        System.out.println("failure!");
174:                        e.printStackTrace(System.out);
175:                    } finally {
176:                        System.out.println("");
177:                    }
178:                }
179:            }
180:
181:            /**
182:             * Main method. Set up the listener.
183:             * @param args command-line arguments
184:             */
185:            public static void main(String[] args) {
186:                System.out.println("FOP MultipleFO2PDF\n");
187:                System.out.println("Preparing...");
188:                MultipleFO2PDF m = new MultipleFO2PDF();
189:                m.listen();
190:            }
191:
192:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.