KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > apps > PrintStarter


1 /*
2  * $Id: PrintStarter.java,v 1.7.2.5 2003/02/25 10:18:32 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.apps;
52
53 /*
54  * originally contributed by
55  * Stanislav Gorkhover: stanislav.gorkhover@jcatalog.com
56  * jCatalog Software AG
57  *
58  * Updated by Mark Lillywhite, mark-fop@inomial.com. Modified to
59  * handle the print job better, added -Ddialog option, removed
60  * (apparently) redundant copies code, generally cleaned up, and
61  * added interfaces to the new Render API.
62  */

63
64 import org.xml.sax.XMLReader JavaDoc;
65
66 import java.awt.print.*;
67 import java.io.OutputStream JavaDoc;
68 import java.io.IOException JavaDoc;
69 import java.util.ArrayList JavaDoc;
70
71 import org.apache.fop.render.awt.AWTRenderer;
72 import org.apache.fop.layout.Page;
73
74 // Avalon
75
import org.apache.avalon.framework.logger.ConsoleLogger;
76
77 /**
78  * This class prints a xsl-fo dokument without interaction.
79  * At the moment java has not the possibility to configure the printer and it's
80  * options without interaction (30.03.2000).
81  * This class allows to print a set of pages (from-to), even/odd pages and many copies.
82  * - Print from page xxx: property name - start, value int
83  * - Print to page xxx: property name - end, value int
84  * - Print even/odd pages: property name - even, value boolean
85  * - Print xxx copies: property name - copies, value int
86  *
87  */

88 public class PrintStarter extends CommandLineStarter {
89
90     public PrintStarter(CommandLineOptions options) throws FOPException {
91         super(options);
92     }
93
94     public void run() throws FOPException {
95         Driver driver = new Driver();
96         if (errorDump) {
97             driver.setErrorDump(true);
98         }
99
100         // Nov 18, 02 eliminates spurious [ERROR] message to logger
101

102         driver.setLogger (new ConsoleLogger(ConsoleLogger.LEVEL_INFO)) ;
103         log.info (Version.getVersion()) ;
104
105         XMLReader JavaDoc parser = inputHandler.getParser();
106
107         PrinterJob pj = PrinterJob.getPrinterJob();
108         if(System.getProperty("dialog") != null)
109             if(!pj.printDialog())
110                 throw new FOPException("Printing cancelled by operator");
111
112         PrintRenderer renderer = new PrintRenderer(pj);
113         int copies = getIntProperty("copies", 1);
114         pj.setCopies(copies);
115
116         //renderer.setCopies(copies);
117

118         try {
119             driver.setRenderer(renderer);
120             driver.render(parser, inputHandler.getInputSource());
121         } catch (Exception JavaDoc e) {
122             if (e instanceof FOPException) {
123                 throw (FOPException)e;
124             }
125             throw new FOPException(e);
126         }
127
128         System.exit(0);
129     }
130     int getIntProperty(String JavaDoc name, int def) {
131         String JavaDoc propValue = System.getProperty(name);
132         if(propValue != null) {
133             try {
134                 return Integer.parseInt(propValue);
135             } catch (Exception JavaDoc e) {
136                 return def;
137             }
138         } else {
139             return def;
140         }
141     }
142
143     class PrintRenderer extends AWTRenderer {
144
145         private static final int EVEN_AND_ALL = 0;
146         private static final int EVEN = 1;
147         private static final int ODD = 2;
148
149         private int startNumber;
150         private int endNumber;
151         private int mode = EVEN_AND_ALL;
152         private int copies = 1;
153         private PrinterJob printerJob;
154
155         PrintRenderer(PrinterJob printerJob) {
156             super(null);
157
158             this.printerJob = printerJob;
159             startNumber = getIntProperty("start", 1) - 1;
160             endNumber = getIntProperty("end", -1);
161
162             printerJob.setPageable(this);
163
164             mode = EVEN_AND_ALL;
165             String JavaDoc str = System.getProperty("even");
166             if (str != null) {
167                 try {
168                     mode = Boolean.valueOf(str).booleanValue() ? EVEN : ODD;
169                 } catch (Exception JavaDoc e) {}
170
171             }
172
173         }
174
175         public void stopRenderer(OutputStream JavaDoc outputStream)
176         throws IOException JavaDoc {
177             super.stopRenderer(outputStream);
178
179             if(endNumber == -1)
180                 endNumber = getPageCount();
181
182             ArrayList JavaDoc numbers = getInvalidPageNumbers();
183             for (int i = numbers.size() - 1; i > -1; i--)
184                 removePage(Integer.parseInt((String JavaDoc)numbers.get(i)));
185
186             try {
187                 printerJob.print();
188             } catch (PrinterException e) {
189                 e.printStackTrace();
190                 throw new IOException JavaDoc(
191                     "Unable to print: " + e.getClass().getName() +
192                     ": " + e.getMessage());
193             }
194         }
195
196         public void renderPage(Page page) {
197             pageWidth = (int)((float)page.getWidth() / 1000f);
198             pageHeight = (int)((float)page.getHeight() / 1000f);
199             super.renderPage(page);
200         }
201
202
203         private ArrayList JavaDoc getInvalidPageNumbers() {
204             ArrayList JavaDoc vec = new ArrayList JavaDoc();
205             int max = getPageCount();
206             boolean isValid;
207             for (int i = 0; i < max; i++) {
208                 isValid = true;
209                 if (i < startNumber || i > endNumber) {
210                     isValid = false;
211                 } else if (mode != EVEN_AND_ALL) {
212                     if (mode == EVEN && ((i + 1) % 2 != 0))
213                         isValid = false;
214                     else if (mode == ODD && ((i + 1) % 2 != 1))
215                         isValid = false;
216                 }
217
218                 if (!isValid)
219                     vec.add(i + "");
220             }
221
222             return vec;
223         }
224
225         /* TODO: I'm totally not sure that this is necessary -Mark
226         void setCopies(int val) {
227             copies = val;
228             ArrayList copie = tree.getPages();
229             for (int i = 1; i < copies; i++) {
230                 tree.getPages().addAll(copie);
231             }
232
233     }
234         */

235     } // class PrintRenderer
236
} // class PrintCommandLine
237

238
239
240
Popular Tags