KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xml > binding > AttributesImpl


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.xml.binding;
8
9 import org.xml.sax.Attributes JavaDoc;
10
11 import java.util.List JavaDoc;
12 import java.util.ArrayList JavaDoc;
13
14 /**
15  * org.xml.sax.Attributes implementation.
16  *
17  * @version <tt>$Revision: 1.5.2.6 $</tt>
18  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
19  */

20 public class AttributesImpl
21    implements Attributes JavaDoc
22 {
23    private final List JavaDoc attrList;
24
25    public AttributesImpl(Attributes JavaDoc attrs)
26    {
27       this(attrs == null ? 0 : attrs.getLength());
28
29       if(attrs != null)
30       {
31          for(int i = 0; i < attrs.getLength(); ++i)
32          {
33             add(
34                attrs.getURI(i),
35                attrs.getLocalName(i),
36                attrs.getQName(i),
37                attrs.getType(i),
38                attrs.getValue(i)
39             );
40          }
41       }
42    }
43
44    public AttributesImpl(int size)
45    {
46       this.attrList = new ArrayList JavaDoc(size);
47    }
48
49    public void add(String JavaDoc namespaceUri, String JavaDoc localName, String JavaDoc qName, String JavaDoc type, String JavaDoc value)
50    {
51       attrList.add(new AttributeImpl(namespaceUri, localName, qName, type, value));
52    }
53
54    public void addAll(Attributes JavaDoc attrs)
55    {
56       for(int i = 0; i < attrs.getLength(); ++i)
57       {
58          add(attrs.getURI(i), attrs.getLocalName(i), attrs.getQName(i), attrs.getType(i), attrs.getValue(i));
59       }
60    }
61
62    // Attributes implementation
63

64    public int getLength()
65    {
66       return attrList.size();
67    }
68
69    public String JavaDoc getURI(int index)
70    {
71       return getAttribute(index).namespaceUri;
72    }
73
74    public String JavaDoc getLocalName(int index)
75    {
76       return getAttribute(index).localName;
77    }
78
79    public String JavaDoc getQName(int index)
80    {
81       return getAttribute(index).qName;
82    }
83
84    public String JavaDoc getType(int index)
85    {
86       return getAttribute(index).type;
87    }
88
89    public String JavaDoc getValue(int index)
90    {
91       return getAttribute(index).value;
92    }
93
94    public int getIndex(String JavaDoc uri, String JavaDoc localName)
95    {
96       int i = 0;
97       while(i < attrList.size())
98       {
99          final AttributeImpl attr = getAttribute(i++);
100          if(
101             (attr.namespaceUri == null ? uri == null : attr.namespaceUri.equals(uri)) &&
102             (attr.localName == null ? localName == null : attr.localName.equals(localName))
103          )
104          {
105             break;
106          }
107       }
108
109       if (i == attrList.size())
110          return -1;
111
112       return i;
113    }
114
115    public int getIndex(String JavaDoc qName)
116    {
117       int i = 0;
118       while(i < attrList.size())
119       {
120          final AttributeImpl attr = getAttribute(i++);
121          if(attr.qName.equals(qName))
122          {
123             break;
124          }
125       }
126
127       if (i == attrList.size())
128          return -1;
129
130       return i;
131    }
132
133    public String JavaDoc getType(String JavaDoc uri, String JavaDoc localName)
134    {
135       AttributeImpl attr = null;
136       int i = 0;
137       while(i < attrList.size())
138       {
139          attr = getAttribute(i++);
140          if(
141             (attr.namespaceUri == null ? uri == null : attr.namespaceUri.equals(uri)) &&
142             (attr.localName == null ? localName == null : attr.localName.equals(localName))
143          )
144          {
145             break;
146          }
147       }
148
149       if (attr == null)
150          return null;
151
152       return attr.type;
153    }
154
155    public String JavaDoc getType(String JavaDoc qName)
156    {
157       AttributeImpl attr = null;
158       int i = 0;
159       while(i < attrList.size())
160       {
161          attr = getAttribute(i++);
162          if(attr.qName.equals(qName))
163          {
164             break;
165          }
166       }
167
168       if (attr == null)
169          return null;
170
171       return attr.type;
172    }
173
174    public String JavaDoc getValue(String JavaDoc uri, String JavaDoc localName)
175    {
176       AttributeImpl attr = null;
177       int i = 0;
178       while(i < attrList.size())
179       {
180          attr = getAttribute(i++);
181          if(
182             (attr.namespaceUri == null ? uri == null : attr.namespaceUri.equals(uri)) &&
183             (attr.localName == null ? localName == null : attr.localName.equals(localName))
184          )
185          {
186             break;
187          }
188       }
189
190       if (attr == null)
191          return null;
192
193       return attr.value;
194    }
195
196    public String JavaDoc getValue(String JavaDoc qName)
197    {
198       AttributeImpl attr = null;
199       int i = 0;
200       while(i < attrList.size())
201       {
202          attr = getAttribute(i++);
203          if(attr.qName.equals(qName))
204          {
205             break;
206          }
207       }
208
209       if (attr == null)
210          return null;
211
212       return attr.value;
213    }
214
215    // Public
216

217    public String JavaDoc toString()
218    {
219       String JavaDoc result;
220       if(this.attrList.isEmpty())
221       {
222          result = "[]";
223       }
224       else
225       {
226          StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
227          sb.append('[');
228          sb.append(getQName(0)).append('=').append(getValue(0));
229          for(int i = 1; i < attrList.size(); ++i)
230          {
231             sb.append(", ").append(getQName(i)).append('=').append(getValue(i));
232          }
233          sb.append(']');
234          result = sb.toString();
235       }
236       return result;
237    }
238
239    // Private
240

241    private AttributeImpl getAttribute(int index)
242    {
243       return (AttributeImpl)attrList.get(index);
244    }
245
246    // Inner
247

248    private static final class AttributeImpl
249    {
250       public final String JavaDoc namespaceUri;
251       public final String JavaDoc localName;
252       public final String JavaDoc qName;
253       public final String JavaDoc type;
254       public final String JavaDoc value;
255
256       public AttributeImpl(String JavaDoc namespaceUri, String JavaDoc localName, String JavaDoc qName, String JavaDoc type, String JavaDoc value)
257       {
258          this.namespaceUri = namespaceUri;
259          this.localName = localName;
260          this.qName = qName;
261          this.type = type;
262          this.value = value;
263       }
264
265       public boolean equals(Object JavaDoc o)
266       {
267          if(this == o) return true;
268          if(!(o instanceof AttributeImpl)) return false;
269
270          final AttributeImpl attribute = (AttributeImpl)o;
271
272          if(localName != null ? !localName.equals(attribute.localName) : attribute.localName != null) return false;
273          if(namespaceUri != null ? !namespaceUri.equals(attribute.namespaceUri) : attribute.namespaceUri != null) return false;
274          if(qName != null ? !qName.equals(attribute.qName) : attribute.qName != null) return false;
275          if(type != null ? !type.equals(attribute.type) : attribute.type != null) return false;
276          if(value != null ? !value.equals(attribute.value) : attribute.value != null) return false;
277
278          return true;
279       }
280
281       public int hashCode()
282       {
283          int result;
284          result = (namespaceUri != null ? namespaceUri.hashCode() : 0);
285          result = 29 * result + (localName != null ? localName.hashCode() : 0);
286          result = 29 * result + (qName != null ? qName.hashCode() : 0);
287          result = 29 * result + (type != null ? type.hashCode() : 0);
288          result = 29 * result + (value != null ? value.hashCode() : 0);
289          return result;
290       }
291    }
292 }
Popular Tags