1 51 package org.apache.fop.apps; 52 53 63 64 import org.xml.sax.XMLReader ; 65 66 import java.awt.print.*; 67 import java.io.OutputStream ; 68 import java.io.IOException ; 69 import java.util.ArrayList ; 70 71 import org.apache.fop.render.awt.AWTRenderer; 72 import org.apache.fop.layout.Page; 73 74 import org.apache.avalon.framework.logger.ConsoleLogger; 76 77 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 102 driver.setLogger (new ConsoleLogger(ConsoleLogger.LEVEL_INFO)) ; 103 log.info (Version.getVersion()) ; 104 105 XMLReader 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 118 try { 119 driver.setRenderer(renderer); 120 driver.render(parser, inputHandler.getInputSource()); 121 } catch (Exception 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 name, int def) { 131 String propValue = System.getProperty(name); 132 if(propValue != null) { 133 try { 134 return Integer.parseInt(propValue); 135 } catch (Exception 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 str = System.getProperty("even"); 166 if (str != null) { 167 try { 168 mode = Boolean.valueOf(str).booleanValue() ? EVEN : ODD; 169 } catch (Exception e) {} 170 171 } 172 173 } 174 175 public void stopRenderer(OutputStream outputStream) 176 throws IOException { 177 super.stopRenderer(outputStream); 178 179 if(endNumber == -1) 180 endNumber = getPageCount(); 181 182 ArrayList numbers = getInvalidPageNumbers(); 183 for (int i = numbers.size() - 1; i > -1; i--) 184 removePage(Integer.parseInt((String )numbers.get(i))); 185 186 try { 187 printerJob.print(); 188 } catch (PrinterException e) { 189 e.printStackTrace(); 190 throw new IOException ( 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 getInvalidPageNumbers() { 204 ArrayList vec = new ArrayList (); 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 235 } } 238 239 240 | Popular Tags |