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.

FOTree.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. Copyright 2002-2004 The Apache Software Foundation.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. * $Id$
  13. * Created: Thu Aug 2 20:29:57 2001
  14. */
  15. package org.apache.fop.fo;
  16. import java.awt.Graphics2D;
  17. import java.awt.GraphicsEnvironment;
  18. import java.awt.font.FontRenderContext;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import org.apache.fop.apps.Fop;
  22. import org.apache.fop.datastructs.Tree;
  23. import org.apache.fop.datatypes.Numeric;
  24. import org.apache.fop.datatypes.PropertyValue;
  25. import org.apache.fop.fo.expr.PropertyException;
  26. import org.apache.fop.fo.expr.PropertyParser;
  27. import org.apache.fop.xml.XmlEvent;
  28. import org.apache.fop.xml.XmlEventReader;
  29. /**
  30. * <tt>FOTree</tt> is the class that generates and maintains the FO Tree.
  31. * It runs as a thread, so it implements the <tt>run()</tt> method.
  32. *
  33. * @author <a href="mailto:pbwest@powerup.com.au">Peter B. West</a>
  34. * @version $Id$
  35. */
  36. public class FOTree extends Tree implements Runnable {
  37. private static final String tag = "$Name$";
  38. private static final String revision = "$Revision$";
  39. /** Provides a monotonically increasing pageId */
  40. private long nextPageId = 0;
  41. /** Locking object for synchronizing <code>getNextPageId</code> method.
  42. * The value is irrelevant. */
  43. private Boolean lock = new Boolean(true);
  44. public long getNextPageId() {
  45. synchronized (lock) {
  46. return ++nextPageId;
  47. }
  48. }
  49. /**
  50. * The buffer from which the <tt>XmlEvent</tt>s from the parser will
  51. * be read. <tt>protected</tt> so that FONode can access it.
  52. */
  53. protected XmlEventReader xmlevents;
  54. private Thread parserThread;
  55. private boolean errorDump;
  56. /**
  57. * The <tt>PropertyParser</tt> which will be used by the FO tree
  58. * builder.
  59. */
  60. protected PropertyParser exprParser;
  61. protected Logger log = Logger.getLogger(Fop.fopPackage);
  62. /**
  63. * @param xmlevents the buffer from which <tt>XmlEvent</tt>s from the
  64. * parser are read.
  65. */
  66. public FOTree(XmlEventReader xmlevents)
  67. throws PropertyException
  68. {
  69. super();
  70. Level level = log.getLevel();
  71. if (level.intValue() <= Level.FINE.intValue()) {
  72. errorDump = true;
  73. }
  74. this.xmlevents = xmlevents;
  75. exprParser = new PropertyParser(this);
  76. // Initialize the FontSize first. Any lengths defined in ems must
  77. // be resolved relative to the current font size. This may happen
  78. // during setup of initial values.
  79. // Set the initial value
  80. PropertyValue prop =
  81. PropertyConsts.pconsts.getInitialValue(PropNames.FONT_SIZE);
  82. if ( ! (prop instanceof Numeric) || ! ((Numeric)prop).isLength())
  83. throw new PropertyException("Initial font-size is not a Length");
  84. // Set up the rendering context
  85. }
  86. /** The graphics environment in which FOP is operating */
  87. private GraphicsEnvironment gEnv = null;
  88. /**
  89. * Gets the FOP <code>GraphicsEnvironment</code>
  90. * @return the environment
  91. */
  92. protected GraphicsEnvironment getGraphicsEnvironment() {
  93. return gEnv;
  94. }
  95. /** The object which controls drawing and text rendering in the page spread
  96. */
  97. private Graphics2D g2D = null;
  98. /**
  99. * Gets the <code>Graphics2D</code> rendering and drawing control object
  100. * for area layout
  101. * @return
  102. */
  103. public Graphics2D getGraphics2D() {
  104. return g2D;
  105. }
  106. /** The <code>FontRenderContext</code> object garnered from the
  107. * <code>Graphics2D</code> control object for area layout
  108. */
  109. private FontRenderContext frcontext = null;
  110. /**
  111. * Gets the <code>FontRenderContext</code> derived from the graphics
  112. * control object
  113. * @return
  114. */
  115. public FontRenderContext getFontRenderContext() {
  116. return frcontext;
  117. }
  118. /**
  119. * Parser thread notifies itself to FO tree builder by this call. The
  120. * purpose of this notification is to allow the FO tree builder thread
  121. * to attempt to interrupt the parser thread when the builder
  122. * terminates.
  123. * @param parserThread - the <tt>Thread</tt> object of the parser thread.
  124. */
  125. public void setParserThread(Thread parserThread) {
  126. this.parserThread = parserThread;
  127. }
  128. /**
  129. * Get the <i>xmlevents</i> buffer through which descendents can access
  130. * parser events.
  131. * @return <i>xmlevents</i>.
  132. */
  133. public XmlEventReader getXmlEventReader() {
  134. return xmlevents;
  135. }
  136. /**
  137. * The <tt>run</tt> method() invoked by the call of <tt>start</tt>
  138. * on the thread in which runs off FOTree.
  139. */
  140. public void run() {
  141. FoRoot foRoot;
  142. XmlEvent event;
  143. try {
  144. // Let the parser look after STARTDOCUMENT and the correct
  145. // positioning of the root element
  146. event = xmlevents.getStartElement(FObjectNames.ROOT);
  147. foRoot = new FoRoot(this, event);
  148. foRoot.buildFoTree();
  149. log.info("Back from buildFoTree");
  150. // Clean up the fo:root event
  151. event = xmlevents.getEndElement(XmlEventReader.DISCARD_EV, event);
  152. // Get the end of document
  153. xmlevents.getEndDocument();
  154. } catch (Exception e) {
  155. if (errorDump) {
  156. e.printStackTrace();
  157. }
  158. if (parserThread != null) {
  159. try {
  160. parserThread.interrupt();
  161. } catch (Exception ex) {} // Ignore
  162. }
  163. // Now propagate a Runtime exception
  164. throw new RuntimeException(e);
  165. }
  166. log.fine("Elapsed time: " +
  167. (System.currentTimeMillis() -
  168. org.apache.fop.apps.Fop.startTime));
  169. FONode.PreOrder preorder = foRoot.new PreOrder();
  170. int nodecount = 0;
  171. while (preorder.hasNext()) {
  172. nodecount++;
  173. FONode next = (FONode) preorder.next();
  174. /*
  175. PropertyValue[] pvset = next.getSparsePropsSet();
  176. System.out.println("......Number of properties: " + pvset.length);
  177. for (int i = 0; i < pvset.length; i++ ) {
  178. PropertyValue pv = pvset[i];
  179. System.out.println(pv);
  180. }
  181. */
  182. }
  183. log.fine("# of FONodes: " + nodecount);
  184. }
  185. /**
  186. * Gets the <code>FOTree</code> logger
  187. * @return the logger
  188. */
  189. public Logger getLogger() {
  190. return log;
  191. }
  192. }// FOTree