KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2002-2003 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 a collection of CSS property values.
24  *
25  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
26  * @version $Id: StyleDeclaration.java,v 1.8 2005/03/29 10:48:02 deweese Exp $
27  */

28 public class StyleDeclaration {
29
30     protected final static int INITIAL_LENGTH = 8;
31     
32     /**
33      * The values.
34      */

35     protected Value[] values = new Value[INITIAL_LENGTH];
36
37     /**
38      * The value indexes.
39      */

40     protected int[] indexes = new int[INITIAL_LENGTH];
41
42     /**
43      * The value priorities.
44      */

45     protected boolean[] priorities = new boolean[INITIAL_LENGTH];
46
47     /**
48      * The number of values in the declaration.
49      */

50     protected int count;
51
52     /**
53      * Returns the number of values in the declaration.
54      */

55     public int size() {
56         return count;
57     }
58
59     /**
60      * Returns the value at the given index.
61      */

62     public Value getValue(int idx) {
63         return values[idx];
64     }
65
66     /**
67      * Returns the property index of a value.
68      */

69     public int getIndex(int idx) {
70         return indexes[idx];
71     }
72
73     /**
74      * Tells whether a value is important.
75      */

76     public boolean getPriority(int idx) {
77         return priorities[idx];
78     }
79
80     /**
81      * Removes the value at the given index.
82      */

83     public void remove(int idx) {
84         count--;
85         for (int i = idx; i < count; i++) {
86             values[i] = values[i + 1];
87             indexes[i] = indexes[i + 1];
88             priorities[i] = priorities[i + 1];
89         }
90     }
91
92     /**
93      * Sets a value within the declaration.
94      */

95     public void put(int idx, Value v, int i, boolean prio) {
96         values[idx] = v;
97         indexes[idx] = i;
98         priorities[idx] = prio;
99     }
100
101     /**
102      * Appends a value to the declaration.
103      */

104     public void append(Value v, int idx, boolean prio) {
105         if (values.length == count) {
106             Value[] newval = new Value[count * 2];
107             int[] newidx = new int[count * 2];
108             boolean[] newprio = new boolean[count * 2];
109             for (int i = 0; i < count; i++) {
110                 newval[i] = values[i];
111                 newidx[i] = indexes[i];
112                 newprio[i] = priorities[i];
113             }
114             values = newval;
115             indexes = newidx;
116             priorities = newprio;
117         }
118         for (int i = 0; i < count; i++) {
119             if (indexes[i] == idx) {
120                 // Replace existing property values,
121
// unless they are important!
122
if (prio || (priorities[i] == prio)) {
123                     values [i] = v;
124                     priorities[i] = prio;
125                 }
126                 return;
127             }
128         }
129         values [count] = v;
130         indexes [count] = idx;
131         priorities[count] = prio;
132         count++;
133     }
134
135     /**
136      * Returns a printable representation of this style rule.
137      */

138     public String JavaDoc toString(CSSEngine eng) {
139         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
140         for (int i = 0; i < count; i++) {
141             sb.append(eng.getPropertyName(indexes[i]));
142             sb.append(": ");
143             sb.append(values[i]);
144             sb.append(";\n");
145         }
146         return sb.toString();
147     }
148 }
149
Popular Tags