001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.css.engine;
020:
021: import org.apache.batik.css.engine.value.Value;
022:
023: /**
024: * This class represents a collection of CSS property values.
025: *
026: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
027: * @version $Id: StyleDeclaration.java 479673 2006-11-27 16:08:00Z dvholten $
028: */
029: public class StyleDeclaration {
030:
031: protected static final int INITIAL_LENGTH = 8;
032:
033: /**
034: * The values.
035: */
036: protected Value[] values = new Value[INITIAL_LENGTH];
037:
038: /**
039: * The value indexes.
040: */
041: protected int[] indexes = new int[INITIAL_LENGTH];
042:
043: /**
044: * The value priorities.
045: */
046: protected boolean[] priorities = new boolean[INITIAL_LENGTH];
047:
048: /**
049: * The number of values in the declaration.
050: */
051: protected int count;
052:
053: /**
054: * Returns the number of values in the declaration.
055: */
056: public int size() {
057: return count;
058: }
059:
060: /**
061: * Returns the value at the given index.
062: */
063: public Value getValue(int idx) {
064: return values[idx];
065: }
066:
067: /**
068: * Returns the property index of a value.
069: */
070: public int getIndex(int idx) {
071: return indexes[idx];
072: }
073:
074: /**
075: * Tells whether a value is important.
076: */
077: public boolean getPriority(int idx) {
078: return priorities[idx];
079: }
080:
081: /**
082: * Removes the value at the given index.
083: */
084: public void remove(int idx) {
085: count--;
086: int from = idx + 1;
087: int to = idx;
088: int nCopy = count - idx;
089:
090: System.arraycopy(values, from, values, to, nCopy);
091: System.arraycopy(indexes, from, indexes, to, nCopy);
092: System.arraycopy(priorities, from, priorities, to, nCopy);
093:
094: values[count] = null;
095: indexes[count] = 0;
096: priorities[count] = false;
097:
098: // for (int i = idx; i < count; i++) {
099: // values[i] = values[i + 1];
100: // indexes[i] = indexes[i + 1];
101: // priorities[i] = priorities[i + 1];
102: // }
103: }
104:
105: /**
106: * Sets a value within the declaration.
107: */
108: public void put(int idx, Value v, int i, boolean prio) {
109: values[idx] = v;
110: indexes[idx] = i;
111: priorities[idx] = prio;
112: }
113:
114: /**
115: * Appends a value to the declaration.
116: */
117: public void append(Value v, int idx, boolean prio) {
118: if (values.length == count) {
119: Value[] newval = new Value[count * 2];
120: int[] newidx = new int[count * 2];
121: boolean[] newprio = new boolean[count * 2];
122:
123: System.arraycopy(values, 0, newval, 0, count);
124: System.arraycopy(indexes, 0, newidx, 0, count);
125: System.arraycopy(priorities, 0, newprio, 0, count);
126:
127: values = newval;
128: indexes = newidx;
129: priorities = newprio;
130: }
131: for (int i = 0; i < count; i++) {
132: if (indexes[i] == idx) {
133: // Replace existing property values,
134: // unless they are important!
135: if (prio || (priorities[i] == prio)) {
136: values[i] = v;
137: priorities[i] = prio;
138: }
139: return;
140: }
141: }
142: values[count] = v;
143: indexes[count] = idx;
144: priorities[count] = prio;
145: count++;
146: }
147:
148: /**
149: * Returns a printable representation of this style rule.
150: */
151: public String toString(CSSEngine eng) {
152: StringBuffer sb = new StringBuffer(count * 8);
153: for (int i = 0; i < count; i++) {
154: sb.append(eng.getPropertyName(indexes[i]));
155: sb.append(": ");
156: sb.append(values[i]);
157: sb.append(";\n");
158: }
159: return sb.toString();
160: }
161: }
|