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

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