KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > css > engine > StyleMap


1 /*
2
3    Copyright 2002 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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 package org.apache.batik.css.engine;
19
20 import org.apache.batik.css.engine.value.Value;
21
22 /**
23  * This class represents objects which contains property/value mappings.
24  *
25  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
26  * @version $Id: StyleMap.java,v 1.4 2004/08/20 19:29:47 deweese Exp $
27  */

28 public class StyleMap {
29     
30     //
31
// The masks, still have 2 free bits: 0x0800, & 0x1000, could also
32
// go to int if needed.
33
//
34
public final static short IMPORTANT_MASK = 0x0001;
35     public final static short COMPUTED_MASK = 0x0002;
36     public final static short NULL_CASCADED_MASK = 0x0004;
37     public final static short INHERITED_MASK = 0x0008;
38
39     public final static short LINE_HEIGHT_RELATIVE_MASK = 0x0010;
40     public final static short FONT_SIZE_RELATIVE_MASK = 0x0020;
41     public final static short COLOR_RELATIVE_MASK = 0x0040;
42     public final static short PARENT_RELATIVE_MASK = 0x0080;
43     public final static short BLOCK_WIDTH_RELATIVE_MASK = 0x0100;
44     public final static short BLOCK_HEIGHT_RELATIVE_MASK = 0x0200;
45     public final static short BOX_RELATIVE_MASK = 0x0400;
46     
47     public final static short ORIGIN_MASK = (short)0xE000; // 3 last bits
48

49     //
50
// The origin values.
51
//
52
public final static short USER_AGENT_ORIGIN = 0;
53     public final static short USER_ORIGIN = 0x2000; // 0010
54
public final static short NON_CSS_ORIGIN = 0x4000; // 0100
55
public final static short AUTHOR_ORIGIN = 0x6000; // 0110
56
public final static short INLINE_AUTHOR_ORIGIN = (short)0x8000; // 1000
57

58     /**
59      * The values.
60      */

61     protected Value[] values;
62
63     /**
64      * To store the value masks.
65      */

66     protected short[] masks;
67
68     /**
69      * Whether the values of this map cannot be re-cascaded.
70      */

71     protected boolean fixedCascadedValues;
72
73     /**
74      * Creates a new StyleMap.
75      */

76     public StyleMap(int size) {
77         values = new Value[size];
78         masks = new short[size];
79     }
80
81     /**
82      * Whether this map has fixed cascaded value.
83      */

84     public boolean hasFixedCascadedValues() {
85         return fixedCascadedValues;
86     }
87
88     /**
89      * Sets the fixedCascadedValues property.
90      */

91     public void setFixedCascadedStyle(boolean b) {
92         fixedCascadedValues = b;
93     }
94
95     /**
96      * Returns the value at the given index, null if unspecified.
97      */

98     public Value getValue(int i) {
99         return values[i];
100     }
101
102     /**
103      * Returns the mask of the given property value.
104      */

105     public short getMask(int i) {
106         return masks[i];
107     }
108
109     /**
110      * Tells whether the given property value is important.
111      */

112     public boolean isImportant(int i) {
113         return (masks[i] & IMPORTANT_MASK) != 0;
114     }
115
116     /**
117      * Tells whether the given property value is computed.
118      */

119     public boolean isComputed(int i) {
120         return (masks[i] & COMPUTED_MASK) != 0;
121     }
122
123     /**
124      * Tells whether the given cascaded property value is null.
125      */

126     public boolean isNullCascaded(int i) {
127         return (masks[i] & NULL_CASCADED_MASK) != 0;
128     }
129
130     /**
131      * Tells whether the given cascaded property value was
132      * inherited from it's parent or set locally.
133      */

134     public boolean isInherited(int i) {
135         return (masks[i] & INHERITED_MASK) != 0;
136     }
137
138     /**
139      * Returns the origin value.
140      */

141     public short getOrigin(int i) {
142         return (short)(masks[i] & ORIGIN_MASK);
143     }
144
145     /**
146      * Tells whether the given property value is relative to 'color'.
147      */

148     public boolean isColorRelative(int i) {
149         return (masks[i] & COLOR_RELATIVE_MASK) != 0;
150     }
151
152     /**
153      * Tells whether the given property value is relative to the parent's
154      * property value.
155      */

156     public boolean isParentRelative(int i) {
157         return (masks[i] & PARENT_RELATIVE_MASK) != 0;
158     }
159
160     /**
161      * Tells whether the given property value is relative to 'line-height'.
162      */

163     public boolean isLineHeightRelative(int i) {
164         return (masks[i] & LINE_HEIGHT_RELATIVE_MASK) != 0;
165     }
166
167     /**
168      * Tells whether the given property value is relative to 'font-size'.
169      */

170     public boolean isFontSizeRelative(int i) {
171         return (masks[i] & FONT_SIZE_RELATIVE_MASK) != 0;
172     }
173
174     /**
175      * Tells whether the given property value is relative to the
176      * width of the containing block.
177      */

178     public boolean isBlockWidthRelative(int i) {
179         return (masks[i] & BLOCK_WIDTH_RELATIVE_MASK) != 0;
180     }
181
182     /**
183      * Tells whether the given property value is relative to the
184      * height of the containing block.
185      */

186     public boolean isBlockHeightRelative(int i) {
187         return (masks[i] & BLOCK_HEIGHT_RELATIVE_MASK) != 0;
188     }
189
190     /**
191      * Puts a property value, given the property index.
192      * @param i The property index.
193      * @param v The property value.
194      */

195     public void putValue(int i, Value v) {
196         values[i] = v;
197     }
198
199     /**
200      * Puts a property mask, given the property index.
201      * @param i The property index.
202      * @param m The property mask.
203      */

204     public void putMask(int i, short m) {
205         masks[i] = m;
206     }
207
208     /**
209      * Sets the priority of a property value.
210      */

211     public void putImportant(int i, boolean b) {
212         if (b) masks[i] |= IMPORTANT_MASK;
213         else masks[i] &= ~IMPORTANT_MASK;
214     }
215
216     /**
217      * Sets the origin of the given value.
218      */

219     public void putOrigin(int i, short val) {
220         masks[i] &= ~ORIGIN_MASK;
221         masks[i] |= (short)(val & ORIGIN_MASK);
222     }
223
224     /**
225      * Sets the computed flag of a property value.
226      */

227     public void putComputed(int i, boolean b) {
228         if (b) masks[i] |= COMPUTED_MASK;
229         else masks[i] &= ~COMPUTED_MASK;
230     }
231
232     /**
233      * Sets the null-cascaded flag of a property value.
234      */

235     public void putNullCascaded(int i, boolean b) {
236         if (b) masks[i] |= NULL_CASCADED_MASK;
237         else masks[i] &= ~NULL_CASCADED_MASK;
238     }
239
240     /**
241      * Sets the inherited flag of a property value.
242      * If true this computed value was inherited from it's parent.
243      */

244     public void putInherited(int i, boolean b) {
245         if (b) masks[i] |= INHERITED_MASK;
246         else masks[i] &= ~INHERITED_MASK;
247     }
248
249     /**
250      * Sets the color-relative flag of a property value.
251      */

252     public void putColorRelative(int i, boolean b) {
253         if (b) masks[i] |= COLOR_RELATIVE_MASK;
254         else masks[i] &= ~COLOR_RELATIVE_MASK;
255     }
256
257     /**
258      * Sets the parent-relative flag of a property value.
259      */

260     public void putParentRelative(int i, boolean b) {
261         if (b) masks[i] |= PARENT_RELATIVE_MASK;
262         else masks[i] &= ~PARENT_RELATIVE_MASK;
263     }
264
265     /**
266      * Sets the line-height-relative flag of a property value.
267      */

268     public void putLineHeightRelative(int i, boolean b) {
269         if (b) masks[i] |= LINE_HEIGHT_RELATIVE_MASK;
270         else masks[i] &= ~LINE_HEIGHT_RELATIVE_MASK;
271     }
272
273     /**
274      * Sets the font-size-relative flag of a property value.
275      */

276     public void putFontSizeRelative(int i, boolean b) {
277         if (b) masks[i] |= FONT_SIZE_RELATIVE_MASK;
278         else masks[i] &= ~FONT_SIZE_RELATIVE_MASK;
279     }
280
281     /**
282      * Sets the block-width-relative flag of a property value.
283      */

284     public void putBlockWidthRelative(int i, boolean b) {
285         if (b) masks[i] |= BLOCK_WIDTH_RELATIVE_MASK;
286         else masks[i] &= ~BLOCK_WIDTH_RELATIVE_MASK;
287     }
288
289     /**
290      * Sets the block-height-relative flag of a property value.
291      */

292     public void putBlockHeightRelative(int i, boolean b) {
293         if (b) masks[i] |= BLOCK_HEIGHT_RELATIVE_MASK;
294         else masks[i] &= ~BLOCK_HEIGHT_RELATIVE_MASK;
295     }
296
297     /**
298      * Returns a printable representation of this style map.
299      */

300     public String JavaDoc toString(CSSEngine eng) {
301         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
302         for (int i = 0; i < values.length; i++) {
303             Value v = values[i];
304             if (v == null) continue;
305
306             sb.append(eng.getPropertyName(i));
307             sb.append(": ");
308             sb.append(v);
309             if (isImportant(i)) sb.append(" !important");
310             sb.append(";\n");
311         }
312         return sb.toString();
313     }
314 }
315
Popular Tags