KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > properties > CompoundPropertyMaker


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: CompoundPropertyMaker.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.fo.properties;
21
22 import org.apache.fop.datatypes.CompoundDatatype;
23 import org.apache.fop.fo.Constants;
24 import org.apache.fop.fo.FObj;
25 import org.apache.fop.fo.PropertyList;
26 import org.apache.fop.fo.expr.PropertyException;
27
28 /**
29  * @author me
30  *
31  * To change the template for this generated type comment go to
32  * Window - Preferences - Java - Code Generation - Code and Comments
33  */

34 /**
35  * This class extends Property.Maker with support for sub-properties.
36  */

37 public class CompoundPropertyMaker extends PropertyMaker {
38     /**
39      * The list of subproperty makers supported by this compound maker.
40      */

41     private PropertyMaker[] subproperties =
42                     new PropertyMaker[Constants.COMPOUND_COUNT];
43
44     /**
45      * The first subproperty maker which has a setByShorthand of true.
46      */

47     private PropertyMaker shorthandMaker = null;
48
49     /**
50      * Construct an instance of a CompoundPropertyMaker for the given property.
51      * @param propId The Constant ID of the property to be made.
52      */

53     public CompoundPropertyMaker(int propId) {
54         super(propId);
55     }
56
57     /**
58      * @see org.apache.fop.fo.properties.PropertyMaker#useGeneric(PropertyMaker)
59      */

60     public void useGeneric(PropertyMaker generic) {
61         super.useGeneric(generic);
62         if (generic instanceof CompoundPropertyMaker) {
63             CompoundPropertyMaker compoundGeneric = (CompoundPropertyMaker) generic;
64             for (int i = 0; i < Constants.COMPOUND_COUNT; i++) {
65                 PropertyMaker submaker = compoundGeneric.subproperties[i];
66                 if (submaker != null) {
67                     addSubpropMaker((PropertyMaker) submaker.clone());
68                 }
69             }
70         }
71     }
72     
73     /**
74      * Add a subproperty to this maker.
75      * @param subproperty
76      */

77     public void addSubpropMaker(PropertyMaker subproperty) {
78         // Place the base propId in the propId of the subproperty.
79
subproperty.propId &= Constants.COMPOUND_MASK;
80         subproperty.propId |= propId;
81         
82         subproperties[getSubpropIndex(subproperty.getPropId())] = subproperty;
83
84         // Store the first subproperty with a setByShorthand. That subproperty
85
// will be used for converting a value set on the base property.
86
if (shorthandMaker == null && subproperty.setByShorthand) {
87             shorthandMaker = subproperty;
88         }
89     }
90     
91     
92     /**
93      * Return a Maker object which is used to set the values on components
94      * of compound property types, such as "space".
95      * Overridden by property maker subclasses which handle
96      * compound properties.
97      * @param subpropertyId the id of the component for which a Maker is to
98      * returned, for example CP_OPTIMUM, if the FO attribute is
99      * space.optimum='10pt'.
100      * @return the Maker object specified
101      */

102     public PropertyMaker getSubpropMaker(int subpropertyId) {
103         return subproperties[getSubpropIndex(subpropertyId)];
104     }
105     
106     /**
107      * Calculate the real value of a subproperty by unmasking and shifting
108      * the value into the range [0 - (COMPOUND_COUNT-1)].
109      * The value is used as index into the subproperties array.
110      * @param propId the property id of the sub property.
111      * @return the array index.
112      */

113     private int getSubpropIndex(int subpropertyId) {
114         return ((subpropertyId & Constants.COMPOUND_MASK) >>
115                                     Constants.COMPOUND_SHIFT)-1;
116     }
117
118     /**
119      * For compound properties which can take enumerate values.
120      * Delegate the enumeration check to one of the subpropeties.
121      * @param value the string containing the property value
122      * @return the Property encapsulating the enumerated equivalent of the
123      * input value
124      */

125     protected Property checkEnumValues(String JavaDoc value) {
126         Property result = null;
127         if (shorthandMaker != null) {
128             result = shorthandMaker.checkEnumValues(value);
129         }
130         if (result == null) {
131             result = super.checkEnumValues(value);
132         }
133         return result;
134     }
135
136     /**
137      * Return the property on the current FlowObject. Depending on the passed flags,
138      * this will try to compute it based on other properties, or if it is
139      * inheritable, to return the inherited value. If all else fails, it returns
140      * the default value.
141      * @param subpropertyId The subproperty id of the property being retrieved.
142      * Is 0 when retriving a base property.
143      * @param propertyList The PropertyList object being built for this FO.
144      * @param tryInherit true if inherited properties should be examined.
145      * @param tryDefault true if the default value should be returned.
146      */

147     public Property get(int subpropertyId, PropertyList propertyList,
148                         boolean tryInherit, boolean tryDefault)
149         throws PropertyException
150     {
151         Property p = super.get(subpropertyId, propertyList, tryInherit, tryDefault);
152         if (subpropertyId != 0 && p != null) {
153             p = getSubprop(p, subpropertyId);
154         }
155         return p;
156     }
157    
158     /**
159      * Return a Property object based on the passed Property object.
160      * This method is called if the Property object built by the parser
161      * isn't the right type for this compound property.
162      * @param p The Property object return by the expression parser
163      * @param propertyList The PropertyList object being built for this FO.
164      * @param fo The parent FO for the FO whose property is being made.
165      * @return A Property of the correct type or null if the parsed value
166      * can't be converted to the correct type.
167      * @throws PropertyException for invalid or inconsistent FO input
168      */

169     protected Property convertProperty(Property p,
170                                     PropertyList propertyList,
171                                     FObj fo) throws PropertyException {
172         // Delegate to the subproperty maker to do conversions.
173
p = shorthandMaker.convertProperty(p, propertyList, fo);
174         
175         if (p != null) {
176             Property prop = makeCompound(propertyList, fo);
177             CompoundDatatype pval = (CompoundDatatype) prop.getObject();
178             for (int i = 0; i < Constants.COMPOUND_COUNT; i++) {
179                 PropertyMaker submaker = subproperties[i];
180                 if (submaker != null && submaker.setByShorthand) {
181                     pval.setComponent(submaker.getPropId() & Constants.COMPOUND_MASK, p, false);
182                 }
183             }
184             return prop;
185         }
186         return null;
187     }
188
189     /**
190      * Make a compound property with default values.
191      * @param propertyList The PropertyList object being built for this FO.
192      * @return the Property object corresponding to the parameters
193      * @throws PropertyException for invalid or inconsisten FO input
194      */

195     public Property make(PropertyList propertyList) throws PropertyException {
196         if (defaultValue != null) {
197             return make(propertyList, defaultValue, propertyList.getParentFObj());
198         } else {
199             return makeCompound(propertyList, propertyList.getParentFObj());
200         }
201     }
202     
203     /**
204      * Create a Property object from an attribute specification.
205      * @param propertyList The PropertyList object being built for this FO.
206      * @param value The attribute value.
207      * @param fo The parent FO for the FO whose property is being made.
208      * @return The initialized Property object.
209      * @throws PropertyException for invalid or inconsistent FO input
210      */

211     public Property make(PropertyList propertyList, String JavaDoc value,
212                          FObj fo) throws PropertyException {
213         Property p = super.make(propertyList, value, fo);
214         p = convertProperty(p, propertyList, fo);
215         return p;
216     }
217     
218     /**
219      * Return a property value for a compound property. If the property
220      * value is already partially initialized, this method will modify it.
221      * @param baseProperty The Property object representing the compound property,
222      * for example: SpaceProperty.
223      * @param subpropertyId The Constants ID of the subproperty (component)
224      * whose value is specified.
225      * @param propertyList The propertyList being built.
226      * @param fo The parent FO for the FO whose property is being made.
227      * @param value the value of the
228      * @return baseProperty (or if null, a new compound property object) with
229      * the new subproperty added
230      * @throws PropertyException for invalid or inconsistent FO input
231      */

232     public Property make(Property baseProperty, int subpropertyId,
233                          PropertyList propertyList, String JavaDoc value,
234                          FObj fo) throws PropertyException {
235         if (baseProperty == null) {
236             baseProperty = makeCompound(propertyList, fo);
237         }
238
239         PropertyMaker spMaker = getSubpropMaker(subpropertyId);
240
241         if (spMaker != null) {
242             Property p = spMaker.make(propertyList, value, fo);
243             if (p != null) {
244                 return setSubprop(baseProperty, subpropertyId & Constants.COMPOUND_MASK, p);
245             }
246         } else {
247             //getLogger().error("compound property component "
248
// + partName + " unknown.");
249
}
250         return baseProperty;
251     }
252     
253     /**
254      * Create a empty compound property and fill it with default values for
255      * the subproperties.
256      * @param propertyList The propertyList being built.
257      * @param parentFO The parent FO for the FO whose property is being made.
258      * @return a Property subclass object holding a "compound" property object
259      * initialized to the default values for each component.
260      * @throws PropertyException ...
261      */

262     protected Property makeCompound(PropertyList propertyList, FObj parentFO)
263         throws PropertyException {
264         Property p = makeNewProperty();
265         CompoundDatatype data = (CompoundDatatype) p.getObject();
266         for (int i = 0; i < Constants.COMPOUND_COUNT; i++) {
267             PropertyMaker subpropertyMaker = subproperties[i];
268             if (subpropertyMaker != null) {
269                 Property subproperty = subpropertyMaker.make(propertyList);
270                 data.setComponent(subpropertyMaker.getPropId() & Constants.COMPOUND_MASK, subproperty, true);
271             }
272         }
273         return p;
274     }
275 }
276
Popular Tags