1 51 import java.io.*; 52 import java.util.Vector ; 53 54 import java.awt.print.PrinterJob ; 55 import java.awt.print.PrinterException ; 56 57 import javax.servlet.*; 58 import javax.servlet.http.*; 59 60 import org.xml.sax.InputSource ; 61 62 import org.apache.fop.apps.Driver; 63 import org.apache.fop.layout.Page; 64 import org.apache.fop.apps.XSLTInputHandler; 65 import org.apache.fop.messaging.MessageHandler; 66 67 import org.apache.fop.render.awt.AWTRenderer ; 68 69 import org.apache.avalon.framework.logger.ConsoleLogger; 70 import org.apache.avalon.framework.logger.Logger; 71 72 94 95 public class FopPrintServlet extends HttpServlet { 96 public static final String FO_REQUEST_PARAM = "fo"; 97 public static final String XML_REQUEST_PARAM = "xml"; 98 public static final String XSL_REQUEST_PARAM = "xsl"; 99 Logger log = null; 100 101 public void doGet(HttpServletRequest request, 102 HttpServletResponse response) throws ServletException { 103 if (log == null) { 104 log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN); 105 MessageHandler.setScreenLogger(log); 106 } 107 108 try { 109 String foParam = request.getParameter(FO_REQUEST_PARAM); 110 String xmlParam = request.getParameter(XML_REQUEST_PARAM); 111 String xslParam = request.getParameter(XSL_REQUEST_PARAM); 112 113 if (foParam != null) { 114 FileInputStream file = new FileInputStream(foParam); 115 renderFO(new InputSource (file), response); 116 } else if ((xmlParam != null) && (xslParam != null)) { 117 XSLTInputHandler input = 118 new XSLTInputHandler(new File(xmlParam), 119 new File(xslParam)); 120 renderXML(input, response); 121 } else { 122 response.setContentType ("text/html"); 123 124 PrintWriter out = response.getWriter(); 125 out.println("<html><title>Error</title>\n"+ "<body><h1>FopServlet Error</h1><h3>No 'fo' or 'xml/xsl' "+ 126 "request param given.</h3></body></html>"); 127 } 128 } catch (ServletException ex) { 129 throw ex; 130 } 131 catch (Exception ex) { 132 throw new ServletException(ex); 133 } 134 } 135 136 139 public void renderFO(InputSource foFile, 140 HttpServletResponse response) throws ServletException { 141 try { 142 Driver driver = new Driver(foFile, null); 143 PrinterJob pj = PrinterJob.getPrinterJob(); 144 PrintRenderer renderer = new PrintRenderer(pj); 145 146 driver.setLogger (log); 147 driver.setRenderer(renderer); 148 driver.run(); 149 150 reportOK (response); 151 } catch (Exception ex) { 152 throw new ServletException(ex); 153 } 154 } 155 156 159 public void renderXML(XSLTInputHandler input, 160 HttpServletResponse response) throws ServletException { 161 try { 162 Driver driver = new Driver(); 163 PrinterJob pj = PrinterJob.getPrinterJob(); 164 PrintRenderer renderer = new PrintRenderer(pj); 165 166 pj.setCopies(1); 167 168 driver.setLogger (log); 169 driver.setRenderer (renderer); 170 driver.render (input.getParser(), input.getInputSource()); 171 172 reportOK (response); 173 } catch (Exception ex) { 174 throw new ServletException(ex); 175 } 176 } 177 178 private void reportOK (HttpServletResponse response) 180 throws ServletException { 181 String sMsg = 182 "<html><title>Success</title>\n" + "<body><h1>FopPrintServlet: </h1>" + 183 "<h3>The requested data was printed</h3></body></html>" ; 184 185 response.setContentType ("text/html"); 186 response.setContentLength (sMsg.length()); 187 188 try { 189 PrintWriter out = response.getWriter(); 190 out.println (sMsg); 191 out.flush(); 192 } catch (Exception ex) { 193 throw new ServletException(ex); 194 } 195 } 196 197 class PrintRenderer extends AWTRenderer { 199 200 private static final int EVEN_AND_ALL = 0; 201 private static final int EVEN = 1; 202 private static final int ODD = 2; 203 204 private int startNumber; 205 private int endNumber; 206 private int mode = EVEN_AND_ALL; 207 private int copies = 1; 208 private PrinterJob printerJob; 209 210 PrintRenderer(PrinterJob printerJob) { 211 super(null); 212 213 this.printerJob = printerJob; 214 startNumber = 0 ; 215 endNumber = -1; 216 217 printerJob.setPageable(this); 218 219 mode = EVEN_AND_ALL; 220 String str = System.getProperty("even"); 221 if (str != null) { 222 try { 223 mode = Boolean.valueOf(str).booleanValue() ? EVEN : ODD; 224 } catch (Exception e) {} 225 226 } 227 } 228 229 public void stopRenderer(OutputStream outputStream) 230 throws IOException { 231 super.stopRenderer(outputStream); 232 233 if (endNumber == -1) 234 endNumber = getPageCount(); 235 236 Vector numbers = getInvalidPageNumbers(); 237 for (int i = numbers.size() - 1; i > -1; i--) 238 removePage( 239 Integer.parseInt((String ) numbers.elementAt(i))); 240 241 try { 242 printerJob.print(); 243 } catch (PrinterException e) { 244 e.printStackTrace(); 245 throw new IOException("Unable to print: " + 246 e.getClass().getName() + ": " + e.getMessage()); 247 } 248 } 249 250 public void renderPage(Page page) { 251 pageWidth = (int)((float) page.getWidth() / 1000f); 252 pageHeight = (int)((float) page.getHeight() / 1000f); 253 super.renderPage(page); 254 } 255 256 257 private Vector getInvalidPageNumbers() { 258 259 Vector vec = new Vector (); 260 int max = getPageCount(); 261 boolean isValid; 262 for (int i = 0; i < max; i++) { 263 isValid = true; 264 if (i < startNumber || i > endNumber) { 265 isValid = false; 266 } else if (mode != EVEN_AND_ALL) { 267 if (mode == EVEN && ((i + 1) % 2 != 0)) 268 isValid = false; 269 else if (mode == ODD && ((i + 1) % 2 != 1)) 270 isValid = false; 271 } 272 273 if (!isValid) 274 vec.add(i + ""); 275 } 276 277 return vec; 278 } 279 } 281 } 282 283 | Popular Tags |