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

19 package org.apache.batik.css.engine.value;
20
21 import org.w3c.dom.DOMException;
22 import org.w3c.dom.css.CSSValue;
23
24 /**
25  * This class represents a list of values.
26  *
27  * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
28  * @version $Id: ListValue.java 1733416 2016-03-03 07:07:13Z gadams $
29  */

30 public class ListValue extends AbstractValue {
31
32     /**
33      * The length of the list.
34      */

35     protected int length;
36
37     /**
38      * The items.
39      */

40     protected Value[] items = new Value[5];
41
42     /**
43      * The list separator.
44      */

45     protected char separator = ',';
46
47     /**
48      * Creates a ListValue.
49      */

50     public ListValue() {
51     }
52
53     /**
54      * Creates a ListValue with the given separator.
55      */

56     public ListValue(char s) {
57         separator = s;
58     }
59
60     /**
61      * Returns the separator used for this list.
62      */

63     public char getSeparatorChar() {
64         return separator;
65     }
66
67     /**
68      * Implements {@link Value#getCssValueType()}.
69      */

70     public short getCssValueType() {
71         return CSSValue.CSS_VALUE_LIST;
72     }
73
74     /**
75      *  A string representation of the current value.
76      */

77     public String getCssText() {
78         StringBuffer sb = new StringBuffer( length * 8 );
79         if (length > 0) {
80             sb.append(items[0].getCssText());
81         }
82         for (int i = 1; i < length; i++) {
83             sb.append(separator);
84             sb.append(items[i].getCssText());
85         }
86         return sb.toString();
87     }
88
89     /**
90      * Implements {@link Value#getLength()}.
91      */

92     public int getLength() throws DOMException {
93         return length;
94     }
95
96     /**
97      * Implements {@link Value#item(int)}.
98      */

99     public Value item(int index) throws DOMException {
100         return items[index];
101     }
102
103     /**
104      * Returns a printable representation of this value.
105      */

106     public String toString() {
107         return getCssText();
108     }
109
110     /**
111      * Appends an item to the list.
112      */

113     public void append(Value v) {
114         if (length == items.length) {
115             Value[] t = new Value[length * 2];
116             System.arraycopy( items, 0, t, 0, length );
117             items = t;
118         }
119         items[length++] = v;
120     }
121 }
122