From: Peter Bernard West Date: Sun, 15 Sep 2002 05:47:12 +0000 (+0000) Subject: Base class for indirect values. X-Git-Tag: Alt-Design_pre_Properties_split~121 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0e131848bce4b3e65957b618367d694e2a941d9a;p=xmlgraphics-fop.git Base class for indirect values. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/FOP_0-20-0_Alt-Design@195188 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/org/apache/fop/datatypes/IndirectValue.java b/src/org/apache/fop/datatypes/IndirectValue.java new file mode 100644 index 000000000..6b6706139 --- /dev/null +++ b/src/org/apache/fop/datatypes/IndirectValue.java @@ -0,0 +1,186 @@ +package org.apache.fop.datatypes; + +import org.apache.fop.fo.expr.PropertyException; +import org.apache.fop.fo.expr.AbstractPropertyValue; +import org.apache.fop.fo.expr.PropertyValue; +import org.apache.fop.fo.expr.PropertyTriplet; +import org.apache.fop.fo.Properties; +import org.apache.fop.fo.PropertyConsts; +import org.apache.fop.fo.FOTree; + +/* + * $Id$ + * + * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * For details on use and redistribution please refer to the + * LICENSE file included with these sources. + * @author Peter B. West + * @version $Revision$ $Name$ + */ +/** + * A superclass for objects which may have deferred PropertyValue + * resolution. This is because their value is taken from + * another PropertyValue object defined earlier in the FO tree. + * These include Inherit, FromParent and + * FromNearestSpecified objects. If an InheritedValue + * object is defined, it will also extend this class. + *

The required value is usually the computed field of the + * PropertyTriplet for the source property on the source node. This + * property may be different from the property of this object. This class + * provides accessors for the referenced PropertyTriplet and the + * computed value of that triplet. In some cases, the specified value is + * required. It is the responsibility of the subclass to determine and + * act upon these cases. At the time of writing, the only such exception is + * when a line-height is defined as a <number>. + */ + +public class IndirectValue extends AbstractPropertyValue { + + private static final String tag = "$Name$"; + private static final String revision = "$Revision$"; + + /** + * The property from which the inherited value is to be derived. This + * may be different from the target property. + */ + private int sourceProperty; + + /** + * The PropertyTriplet from which this object is being + * inherited. Set when the inheritance cannot be immediately resolved, + * e.g. when the specified value is a percentage. + */ + private PropertyTriplet inheritedValue = null; + + /** + * @param property - the int index of the property on which + * this value is being defined. + * @param type - the type of PropertyValue. + * @param sourceProperty - the int index of the property from + * which the inherited value is derived. + * @exception PropertyException + */ + public IndirectValue(int property, int type, int sourceProperty) + throws PropertyException + { + super(property, type); + this.sourceProperty = sourceProperty; + } + + /** + * @param property the int index of the property on which + * this value is being defined. + * @param type - the type of PropertyValue. + * @exception PropertyException + */ + public IndirectValue(int property, int type) + throws PropertyException + { + this(property, type, property); + } + + /** + * @param propertyName the String name of the property on which + * this value is being defined. + * @param type - the type of PropertyValue. + * @param sourcePropertyName the String name of the property + * from which the inherited value is derived. + * @exception PropertyException + */ + public IndirectValue + (String propertyName, int type, String sourcePropertyName) + throws PropertyException + { + super(propertyName, type); + sourceProperty = PropertyConsts.getPropertyIndex(sourcePropertyName); + } + + /** + * @param propertyName the String name of the property on which + * this value is being defined. + * @param type - the type of PropertyValue. + * @exception PropertyException + */ + public IndirectValue(String propertyName, int type) + throws PropertyException + { + this(propertyName, type, propertyName); + } + + /** + * @return int containing the source property index. + */ + public int getSourceProperty() { + return sourceProperty; + } + + /** + * @return PropertyTriplet which contains or will contain the + * the computed value being inherited. This field will be null except + * when an unresolved computed value is being inherited. If so, + * a null value will be returned. N.B. This triplet will have a + * property value different from this IndirectValue object. + */ + public PropertyTriplet getInheritedTriplet() { + return inheritedValue; + } + + /** + * @return computed PropertyValue field from the + * PropertyTriplet from which this object is inherting. + * If the inheritedValue field is null, no resolution of the + * inheritance has yet been attempted, and a null value is returned. + * If the inheritedValue field is not null, return the + * computed field, which may be null. N.B. This + * PropertyValue may have a property field different from + * this IndirectValue object. The source property field is held in + * the sourceProperty field. + */ + public PropertyValue getInheritedValue() { + if (inheritedValue != null) return inheritedValue.getComputed(); + return null; + } + + /** + * Set the reference to the PropertyTriplet from which the + * value is being inherited. + * @param bequeathed - the PropertyTriplet which contains + * or will contain the the computed value of the percentage being + * inherited. + */ + public void setInheritedTriplet(PropertyTriplet bequeathed) { + inheritedValue = bequeathed; + } + + /** + * Attempt to resove the IndirectValue object. If no bequeathing + * PropertyTriplet is associated with this object, get it + * from the foTree. If the computed value of that triplet is + * null, return this object. If not, return the computed value. + * @param foTree - the FOTree with which this object is + * associated. + * @return - a PropertyValue as described above. A return of + * the same IndirectValue object implies that the inherited + * computed value has not yet been resolved in the ancestor. + */ + public PropertyValue resolve(FOTree foTree) throws PropertyException { + PropertyValue pv; + if (inheritedValue == null) + inheritedValue = foTree.getInheritedTriplet(sourceProperty); + if ((pv = inheritedValue.getComputed()) == null) + return this; + // Check that the property is the same + if (property != pv.getProperty()) { + try { + pv = (PropertyValue)(pv.clone()); + } catch (CloneNotSupportedException e) { + throw new PropertyException(e.getMessage()); + } + } + return pv; + } + + // N.B. no validation on this class - subclasses will validate + // against the interface-defined validate(int) method in the + // superclass. +}