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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. super(options);
  39. }
  40. public void run () {
  41. Driver driver = new Driver();
  42. if (errorDump) {
  43. driver.setErrorDump(true);
  44. }
  45. String version = Version.getVersion();
  46. MessageHandler.errorln(version);
  47. XMLReader parser = inputHandler.getParser();
  48. setParserFeatures(parser,errorDump);
  49. PrintRenderer renderer = new PrintRenderer();
  50. try {
  51. driver.setRenderer(renderer);
  52. driver.buildFOTree(parser, inputHandler.getInputSource());
  53. driver.format();
  54. driver.render();
  55. } catch (Exception e) {
  56. MessageHandler.errorln("FATAL ERROR: " + e.getMessage());
  57. if (errorDump) {
  58. e.printStackTrace();
  59. }
  60. System.exit(1);
  61. }
  62. int copies = PrintRenderer.getIntProperty("copies", 1);
  63. renderer.setCopies(copies);
  64. PrinterJob pj = PrinterJob.getPrinterJob();
  65. pj.setPageable(renderer);
  66. pj.setCopies(copies);
  67. try {
  68. pj.print();
  69. } catch (PrinterException pe) {
  70. pe.printStackTrace();
  71. }
  72. }
  73. static class PrintRenderer extends AWTRenderer {
  74. static int EVEN_AND_ALL = 0;
  75. static int EVEN = 1;
  76. static int ODD = 2;
  77. int startNumber;
  78. int endNumber;
  79. int mode = EVEN_AND_ALL;
  80. int copies = 1;
  81. PrintRenderer() {
  82. super(null);
  83. startNumber = getIntProperty("start", 1) - 1;
  84. endNumber = getIntProperty("end", -1);
  85. mode = EVEN_AND_ALL;
  86. String str = System.getProperty("even");
  87. if (str != null) {
  88. try {
  89. mode = Boolean.valueOf(str).booleanValue() ? EVEN : ODD;
  90. } catch (Exception e) {
  91. }
  92. }
  93. }
  94. static int getIntProperty(String name, int def) {
  95. String propValue = System.getProperty(name);
  96. if (propValue != null) {
  97. try {
  98. return Integer.parseInt(propValue);
  99. } catch (Exception e) {
  100. return def;
  101. }
  102. } else {
  103. return def;
  104. }
  105. }
  106. public void render(AreaTree areaTree,
  107. OutputStream stream) throws IOException {
  108. tree = areaTree;
  109. if (endNumber == -1) {
  110. endNumber = tree.getPages().size();
  111. }
  112. Vector numbers = getInvalidPageNumbers();
  113. for (int i = numbers.size() - 1; i > -1; i--)
  114. tree.getPages().removeElementAt(
  115. Integer.parseInt((String) numbers.elementAt(i)));
  116. }
  117. public void renderPage(Page page) {
  118. pageWidth = (int)((float) page.getWidth() / 1000f);
  119. pageHeight = (int)((float) page.getHeight() / 1000f);
  120. super.renderPage(page);
  121. }
  122. private Vector getInvalidPageNumbers() {
  123. Vector vec = new Vector();
  124. int max = tree.getPages().size();
  125. boolean isValid;
  126. for (int i = 0; i < max; i++) {
  127. isValid = true;
  128. if (i < startNumber || i > endNumber) {
  129. isValid = false;
  130. } else if (mode != EVEN_AND_ALL) {
  131. if (mode == EVEN && ((i + 1) % 2 != 0))
  132. isValid = false;
  133. else if (mode == ODD && ((i + 1) % 2 != 1))
  134. isValid = false;
  135. }
  136. if (!isValid)
  137. vec.add(i + "");
  138. }
  139. return vec;
  140. }
  141. void setCopies(int val) {
  142. copies = val;
  143. Vector copie = tree.getPages();
  144. for (int i = 1; i < copies; i++) {
  145. tree.getPages().addAll(copie);
  146. }
  147. }
  148. } // class PrintRenderer
  149. } // class PrintCommandLine