You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PrintStarter.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.apps;
  8. /*
  9. * originally contributed by
  10. * Stanislav Gorkhover: stanislav.gorkhover@jcatalog.com
  11. * jCatalog Software AG
  12. *
  13. * Updated by Mark Lillywhite, mark-fop@inomial.com. Modified to
  14. * handle the print job better, added -Ddialog option, removed
  15. * (apparently) redundant copies code, generally cleaned up, and
  16. * added interfaces to the new Render API.
  17. */
  18. import org.xml.sax.XMLReader;
  19. import org.xml.sax.InputSource;
  20. import org.xml.sax.SAXException;
  21. import org.xml.sax.SAXParseException;
  22. import java.awt.Graphics;
  23. import java.awt.print.*;
  24. import java.io.OutputStream;
  25. import java.io.IOException;
  26. import java.util.Vector;
  27. import org.apache.fop.render.awt.AWTRenderer;
  28. /**
  29. * This class prints a xsl-fo dokument without interaction.
  30. * At the moment java has not the possibility to configure the printer and it's
  31. * options without interaction (30.03.2000).
  32. * This class allows to print a set of pages (from-to), even/odd pages and many copies.
  33. * - Print from page xxx: property name - start, value int
  34. * - Print to page xxx: property name - end, value int
  35. * - Print even/odd pages: property name - even, value boolean
  36. * - Print xxx copies: property name - copies, value int
  37. *
  38. */
  39. public class PrintStarter extends CommandLineStarter {
  40. public PrintStarter(CommandLineOptions options) throws FOPException {
  41. super(options);
  42. }
  43. public void run() throws FOPException {
  44. Driver driver = new Driver();
  45. String version = Version.getVersion();
  46. //log.debug(version);
  47. XMLReader parser = inputHandler.getParser();
  48. setParserFeatures(parser);
  49. PrinterJob pj = PrinterJob.getPrinterJob();
  50. if(System.getProperty("dialog") != null)
  51. if(!pj.printDialog())
  52. throw new FOPException("Printing cancelled by operator");
  53. PrintRenderer renderer = new PrintRenderer(pj);
  54. int copies = getIntProperty("copies", 1);
  55. pj.setCopies(copies);
  56. //renderer.setCopies(copies);
  57. try {
  58. driver.setRenderer(renderer);
  59. driver.render(parser, inputHandler.getInputSource());
  60. } catch (Exception e) {
  61. if (e instanceof FOPException) {
  62. throw (FOPException)e;
  63. }
  64. throw new FOPException(e);
  65. }
  66. System.exit(0);
  67. }
  68. int getIntProperty(String name, int def) {
  69. String propValue = System.getProperty(name);
  70. if(propValue != null) {
  71. try {
  72. return Integer.parseInt(propValue);
  73. } catch (Exception e) {
  74. return def;
  75. }
  76. } else {
  77. return def;
  78. }
  79. }
  80. class PrintRenderer extends AWTRenderer {
  81. private static final int EVEN_AND_ALL = 0;
  82. private static final int EVEN = 1;
  83. private static final int ODD = 2;
  84. private int startNumber;
  85. private int endNumber;
  86. private int mode = EVEN_AND_ALL;
  87. private int copies = 1;
  88. private PrinterJob printerJob;
  89. PrintRenderer(PrinterJob printerJob) {
  90. super(null);
  91. this.printerJob = printerJob;
  92. startNumber = getIntProperty("start", 1) - 1;
  93. endNumber = getIntProperty("end", -1);
  94. printerJob.setPageable(this);
  95. mode = EVEN_AND_ALL;
  96. String str = System.getProperty("even");
  97. if (str != null) {
  98. try {
  99. mode = Boolean.valueOf(str).booleanValue() ? EVEN : ODD;
  100. } catch (Exception e) {}
  101. }
  102. }
  103. public void stopRenderer(OutputStream outputStream)
  104. throws IOException {
  105. super.stopRenderer();
  106. if(endNumber == -1)
  107. endNumber = getPageCount();
  108. Vector numbers = getInvalidPageNumbers();
  109. for (int i = numbers.size() - 1; i > -1; i--) {
  110. //removePage(Integer.parseInt((String)numbers.elementAt(i)));
  111. }
  112. try {
  113. printerJob.print();
  114. } catch (PrinterException e) {
  115. e.printStackTrace();
  116. throw new IOException(
  117. "Unable to print: " + e.getClass().getName() +
  118. ": " + e.getMessage());
  119. }
  120. }
  121. /*public void renderPage(Page page) {
  122. pageWidth = (int)((float)page.getWidth() / 1000f);
  123. pageHeight = (int)((float)page.getHeight() / 1000f);
  124. }*/
  125. private Vector getInvalidPageNumbers() {
  126. Vector vec = new Vector();
  127. int max = getPageCount();
  128. boolean isValid;
  129. for (int i = 0; i < max; i++) {
  130. isValid = true;
  131. if (i < startNumber || i > endNumber) {
  132. isValid = false;
  133. } else if (mode != EVEN_AND_ALL) {
  134. if (mode == EVEN && ((i + 1) % 2 != 0))
  135. isValid = false;
  136. else if (mode == ODD && ((i + 1) % 2 != 1))
  137. isValid = false;
  138. }
  139. if (!isValid)
  140. vec.add(i + "");
  141. }
  142. return vec;
  143. }
  144. /* TODO: I'm totally not sure that this is necessary -Mark
  145. void setCopies(int val) {
  146. copies = val;
  147. Vector copie = tree.getPages();
  148. for (int i = 1; i < copies; i++) {
  149. tree.getPages().addAll(copie);
  150. }
  151. }
  152. */
  153. } // class PrintRenderer
  154. } // class PrintCommandLine