/* Copyright 2002-2004 The Apache Software Foundation. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. * $Id$ * Created: Thu Aug 2 20:29:57 2001 */ package org.apache.fop.fo; import java.awt.Graphics2D; import java.awt.GraphicsEnvironment; import java.awt.font.FontRenderContext; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.fop.apps.Fop; import org.apache.fop.datastructs.Tree; import org.apache.fop.datatypes.Numeric; import org.apache.fop.datatypes.PropertyValue; import org.apache.fop.fo.expr.PropertyException; import org.apache.fop.fo.expr.PropertyParser; import org.apache.fop.xml.XmlEvent; import org.apache.fop.xml.XmlEventReader; /** * FOTree is the class that generates and maintains the FO Tree. * It runs as a thread, so it implements the run() method. * * @author Peter B. West * @version $Id$ */ public class FOTree extends Tree implements Runnable { private static final String tag = "$Name$"; private static final String revision = "$Revision$"; /** Provides a monotonically increasing pageId */ private long nextPageId = 0; /** Locking object for synchronizing getNextPageId method. * The value is irrelevant. */ private Boolean lock = new Boolean(true); public long getNextPageId() { synchronized (lock) { return ++nextPageId; } } /** * The buffer from which the XmlEvents from the parser will * be read. protected so that FONode can access it. */ protected XmlEventReader xmlevents; private Thread parserThread; private boolean errorDump; /** * The PropertyParser which will be used by the FO tree * builder. */ protected PropertyParser exprParser; protected Logger log = Logger.getLogger(Fop.fopPackage); /** * @param xmlevents the buffer from which XmlEvents from the * parser are read. */ public FOTree(XmlEventReader xmlevents) throws PropertyException { super(); Level level = log.getLevel(); if (level.intValue() <= Level.FINE.intValue()) { errorDump = true; } this.xmlevents = xmlevents; exprParser = new PropertyParser(this); // Initialize the FontSize first. Any lengths defined in ems must // be resolved relative to the current font size. This may happen // during setup of initial values. // Set the initial value PropertyValue prop = PropertyConsts.pconsts.getInitialValue(PropNames.FONT_SIZE); if ( ! (prop instanceof Numeric) || ! ((Numeric)prop).isLength()) throw new PropertyException("Initial font-size is not a Length"); // Set up the rendering context } /** The graphics environment in which FOP is operating */ private GraphicsEnvironment gEnv = null; /** * Gets the FOP GraphicsEnvironment * @return the environment */ protected GraphicsEnvironment getGraphicsEnvironment() { return gEnv; } /** The object which controls drawing and text rendering in the page spread */ private Graphics2D g2D = null; /** * Gets the Graphics2D rendering and drawing control object * for area layout * @return */ public Graphics2D getGraphics2D() { return g2D; } /** The FontRenderContext object garnered from the * Graphics2D control object for area layout */ private FontRenderContext frcontext = null; /** * Gets the FontRenderContext derived from the graphics * control object * @return */ public FontRenderContext getFontRenderContext() { return frcontext; } /** * Parser thread notifies itself to FO tree builder by this call. The * purpose of this notification is to allow the FO tree builder thread * to attempt to interrupt the parser thread when the builder * terminates. * @param parserThread - the Thread object of the parser thread. */ public void setParserThread(Thread parserThread) { this.parserThread = parserThread; } /** * Get the xmlevents buffer through which descendents can access * parser events. * @return xmlevents. */ public XmlEventReader getXmlEventReader() { return xmlevents; } /** * The run method() invoked by the call of start * on the thread in which runs off FOTree. */ public void run() { FoRoot foRoot; XmlEvent event; try { // Let the parser look after STARTDOCUMENT and the correct // positioning of the root element event = xmlevents.getStartElement(FObjectNames.ROOT); foRoot = new FoRoot(this, event); foRoot.buildFoTree(); log.info("Back from buildFoTree"); // Clean up the fo:root event event = xmlevents.getEndElement(XmlEventReader.DISCARD_EV, event); // Get the end of document xmlevents.getEndDocument(); } catch (Exception e) { if (errorDump) { e.printStackTrace(); } if (parserThread != null) { try { parserThread.interrupt(); } catch (Exception ex) {} // Ignore } // Now propagate a Runtime exception throw new RuntimeException(e); } log.fine("Elapsed time: " + (System.currentTimeMillis() - org.apache.fop.apps.Fop.startTime)); FONode.PreOrder preorder = foRoot.new PreOrder(); int nodecount = 0; while (preorder.hasNext()) { nodecount++; FONode next = (FONode) preorder.next(); /* PropertyValue[] pvset = next.getSparsePropsSet(); System.out.println("......Number of properties: " + pvset.length); for (int i = 0; i < pvset.length; i++ ) { PropertyValue pv = pvset[i]; System.out.println(pv); } */ } log.fine("# of FONodes: " + nodecount); } /** * Gets the FOTree logger * @return the logger */ public Logger getLogger() { return log; } }// FOTree