Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PrintStarter.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. import org.apache.fop.layout.AreaTree;
  29. import org.apache.fop.layout.Page;
  30. import org.apache.fop.messaging.MessageHandler;
  31. /**
  32. * This class prints a xsl-fo dokument without interaction.
  33. * At the moment java has not the possibility to configure the printer and it's
  34. * options without interaction (30.03.2000).
  35. * This class allows to print a set of pages (from-to), even/odd pages and many copies.
  36. * - Print from page xxx: property name - start, value int
  37. * - Print to page xxx: property name - end, value int
  38. * - Print even/odd pages: property name - even, value boolean
  39. * - Print xxx copies: property name - copies, value int
  40. *
  41. */
  42. public class PrintStarter extends CommandLineStarter {
  43. public PrintStarter(CommandLineOptions options) throws FOPException {
  44. super(options);
  45. }
  46. public void run() throws FOPException {
  47. Driver driver = new Driver();
  48. if (errorDump) {
  49. driver.setErrorDump(true);
  50. }
  51. String version = Version.getVersion();
  52. MessageHandler.errorln(version);
  53. XMLReader parser = inputHandler.getParser();
  54. setParserFeatures(parser);
  55. PrinterJob pj = PrinterJob.getPrinterJob();
  56. if(System.getProperty("dialog") != null)
  57. if(!pj.printDialog())
  58. throw new FOPException("Printing cancelled by operator");
  59. PrintRenderer renderer = new PrintRenderer(pj);
  60. int copies = getIntProperty("copies", 1);
  61. pj.setCopies(copies);
  62. //renderer.setCopies(copies);
  63. try {
  64. driver.setRenderer(renderer);
  65. driver.render(parser, inputHandler.getInputSource());
  66. } catch (Exception e) {
  67. if (e instanceof FOPException) {
  68. throw (FOPException)e;
  69. }
  70. throw new FOPException(e);
  71. }
  72. System.exit(0);
  73. }
  74. int getIntProperty(String name, int def) {
  75. String propValue = System.getProperty(name);
  76. if(propValue != null) {
  77. try {
  78. return Integer.parseInt(propValue);
  79. } catch (Exception e) {
  80. return def;
  81. }
  82. } else {
  83. return def;
  84. }
  85. }
  86. class PrintRenderer extends AWTRenderer {
  87. private static final int EVEN_AND_ALL = 0;
  88. private static final int EVEN = 1;
  89. private static final int ODD = 2;
  90. private int startNumber;
  91. private int endNumber;
  92. private int mode = EVEN_AND_ALL;
  93. private int copies = 1;
  94. private PrinterJob printerJob;
  95. PrintRenderer(PrinterJob printerJob) {
  96. super(null);
  97. this.printerJob = printerJob;
  98. startNumber = getIntProperty("start", 1) - 1;
  99. endNumber = getIntProperty("end", -1);
  100. printerJob.setPageable(this);
  101. mode = EVEN_AND_ALL;
  102. String str = System.getProperty("even");
  103. if (str != null) {
  104. try {
  105. mode = Boolean.valueOf(str).booleanValue() ? EVEN : ODD;
  106. } catch (Exception e) {}
  107. }
  108. }
  109. public void stopRenderer(OutputStream outputStream)
  110. throws IOException {
  111. super.stopRenderer();
  112. if(endNumber == -1)
  113. endNumber = getPageCount();
  114. Vector numbers = getInvalidPageNumbers();
  115. for (int i = numbers.size() - 1; i > -1; i--) {
  116. //removePage(Integer.parseInt((String)numbers.elementAt(i)));
  117. }
  118. try {
  119. printerJob.print();
  120. } catch (PrinterException e) {
  121. e.printStackTrace();
  122. throw new IOException(
  123. "Unable to print: " + e.getClass().getName() +
  124. ": " + e.getMessage());
  125. }
  126. }
  127. public void renderPage(Page page) {
  128. pageWidth = (int)((float)page.getWidth() / 1000f);
  129. pageHeight = (int)((float)page.getHeight() / 1000f);
  130. }
  131. private Vector getInvalidPageNumbers() {
  132. Vector vec = new Vector();
  133. int max = getPageCount();
  134. boolean isValid;
  135. for (int i = 0; i < max; i++) {
  136. isValid = true;
  137. if (i < startNumber || i > endNumber) {
  138. isValid = false;
  139. } else if (mode != EVEN_AND_ALL) {
  140. if (mode == EVEN && ((i + 1) % 2 != 0))
  141. isValid = false;
  142. else if (mode == ODD && ((i + 1) % 2 != 1))
  143. isValid = false;
  144. }
  145. if (!isValid)
  146. vec.add(i + "");
  147. }
  148. return vec;
  149. }
  150. /* TODO: I'm totally not sure that this is necessary -Mark
  151. void setCopies(int val) {
  152. copies = val;
  153. Vector copie = tree.getPages();
  154. for (int i = 1; i < copies; i++) {
  155. tree.getPages().addAll(copie);
  156. }
  157. }
  158. */
  159. } // class PrintRenderer
  160. } // class PrintCommandLine