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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  3. * For details on use and redistribution please refer to the
  4. * LICENSE file included with these sources."
  5. */
  6. package org.apache.fop.apps;
  7. /*
  8. originally contributed by
  9. Stanislav Gorkhover: stanislav.gorkhover@jcatalog.com
  10. jCatalog Software AG
  11. */
  12. import org.xml.sax.XMLReader;
  13. import org.xml.sax.InputSource;
  14. import org.xml.sax.SAXException;
  15. import org.xml.sax.SAXParseException;
  16. import java.awt.Graphics;
  17. import java.awt.print.*;
  18. import java.io.OutputStream;
  19. import java.io.IOException;
  20. import java.util.Vector;
  21. import org.apache.fop.render.awt.AWTRenderer;
  22. import org.apache.fop.layout.AreaTree;
  23. import org.apache.fop.layout.Page;
  24. import org.apache.fop.messaging.MessageHandler;
  25. /**
  26. * This class prints a xsl-fo dokument without interaction.
  27. * At the moment java has not the possibility to configure the printer and it's
  28. * options without interaction (30.03.2000).
  29. * This class allows to print a set of pages (from-to), even/odd pages and many copies.
  30. * - Print from page xxx: property name - start, value int
  31. * - Print to page xxx: property name - end, value int
  32. * - Print even/odd pages: property name - even, value boolean
  33. * - Print xxx copies: property name - copies, value int
  34. *
  35. */
  36. public class PrintStarter extends CommandLineStarter {
  37. public PrintStarter (CommandLineOptions options)
  38. throws FOPException
  39. {
  40. super(options);
  41. }
  42. public void run ()
  43. throws FOPException
  44. {
  45. Driver driver = new Driver();
  46. if (errorDump) {
  47. driver.setErrorDump(true);
  48. }
  49. String version = Version.getVersion();
  50. MessageHandler.errorln(version);
  51. XMLReader parser = inputHandler.getParser();
  52. setParserFeatures(parser);
  53. PrintRenderer renderer = new PrintRenderer();
  54. try {
  55. driver.setRenderer(renderer);
  56. driver.buildFOTree(parser, inputHandler.getInputSource());
  57. driver.format();
  58. driver.render();
  59. } catch (Exception e) {
  60. if (e instanceof FOPException) {
  61. throw (FOPException)e;
  62. }
  63. throw new FOPException(e);
  64. }
  65. int copies = PrintRenderer.getIntProperty("copies", 1);
  66. renderer.setCopies(copies);
  67. PrinterJob pj = PrinterJob.getPrinterJob();
  68. pj.setPageable(renderer);
  69. pj.setCopies(copies);
  70. try {
  71. pj.print();
  72. } catch (PrinterException pe) {
  73. pe.printStackTrace();
  74. }
  75. }
  76. static class PrintRenderer extends AWTRenderer {
  77. static int EVEN_AND_ALL = 0;
  78. static int EVEN = 1;
  79. static int ODD = 2;
  80. int startNumber;
  81. int endNumber;
  82. int mode = EVEN_AND_ALL;
  83. int copies = 1;
  84. PrintRenderer() {
  85. super(null);
  86. startNumber = getIntProperty("start", 1) - 1;
  87. endNumber = getIntProperty("end", -1);
  88. mode = EVEN_AND_ALL;
  89. String str = System.getProperty("even");
  90. if (str != null) {
  91. try {
  92. mode = Boolean.valueOf(str).booleanValue() ? EVEN : ODD;
  93. } catch (Exception e) {
  94. }
  95. }
  96. }
  97. static int getIntProperty(String name, int def) {
  98. String propValue = System.getProperty(name);
  99. if (propValue != null) {
  100. try {
  101. return Integer.parseInt(propValue);
  102. } catch (Exception e) {
  103. return def;
  104. }
  105. } else {
  106. return def;
  107. }
  108. }
  109. public void render(AreaTree areaTree,
  110. OutputStream stream) throws IOException {
  111. tree = areaTree;
  112. if (endNumber == -1) {
  113. endNumber = tree.getPages().size();
  114. }
  115. Vector numbers = getInvalidPageNumbers();
  116. for (int i = numbers.size() - 1; i > -1; i--)
  117. tree.getPages().removeElementAt(
  118. Integer.parseInt((String) numbers.elementAt(i)));
  119. }
  120. public void renderPage(Page page) {
  121. pageWidth = (int)((float) page.getWidth() / 1000f);
  122. pageHeight = (int)((float) page.getHeight() / 1000f);
  123. super.renderPage(page);
  124. }
  125. private Vector getInvalidPageNumbers() {
  126. Vector vec = new Vector();
  127. int max = tree.getPages().size();
  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. void setCopies(int val) {
  145. copies = val;
  146. Vector copie = tree.getPages();
  147. for (int i = 1; i < copies; i++) {
  148. tree.getPages().addAll(copie);
  149. }
  150. }
  151. } // class PrintRenderer
  152. } // class PrintCommandLine