KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb.binding;
23
24 import org.xml.sax.Attributes JavaDoc;
25
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28
29 /**
30  * org.xml.sax.Attributes implementation.
31  *
32  * @version <tt>$Revision: 1958 $</tt>
33  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
34  */

35 public class AttributesImpl
36    implements Attributes JavaDoc
37 {
38    private final List JavaDoc attrList;
39
40    public AttributesImpl(Attributes JavaDoc attrs)
41    {
42       this(attrs == null ? 0 : attrs.getLength());
43
44       if(attrs != null)
45       {
46          for(int i = 0; i < attrs.getLength(); ++i)
47          {
48             add(
49                attrs.getURI(i),
50                attrs.getLocalName(i),
51                attrs.getQName(i),
52                attrs.getType(i),
53                attrs.getValue(i)
54             );
55          }
56       }
57    }
58
59    public AttributesImpl(int size)
60    {
61       this.attrList = new ArrayList JavaDoc(size);
62    }
63
64    public void add(String JavaDoc namespaceUri, String JavaDoc localName, String JavaDoc qName, String JavaDoc type, String JavaDoc value)
65    {
66       attrList.add(new AttributeImpl(namespaceUri, localName, qName, type, value));
67    }
68
69    public void addAll(Attributes JavaDoc attrs)
70    {
71       for(int i = 0; i < attrs.getLength(); ++i)
72       {
73          add(attrs.getURI(i), attrs.getLocalName(i), attrs.getQName(i), attrs.getType(i), attrs.getValue(i));
74       }
75    }
76
77    // Attributes implementation
78

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

232    public String JavaDoc toString()
233    {
234       String JavaDoc result;
235       if(this.attrList.isEmpty())
236       {
237          result = "[]";
238       }
239       else
240       {
241          StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
242          sb.append('[');
243          sb.append(getQName(0)).append('=').append(getValue(0));
244          for(int i = 1; i < attrList.size(); ++i)
245          {
246             sb.append(", ").append(getQName(i)).append('=').append(getValue(i));
247          }
248          sb.append(']');
249          result = sb.toString();
250       }
251       return result;
252    }
253
254    // Private
255

256    private AttributeImpl getAttribute(int index)
257    {
258       return (AttributeImpl)attrList.get(index);
259    }
260
261    // Inner
262

263    private static final class AttributeImpl
264    {
265       public final String JavaDoc namespaceUri;
266       public final String JavaDoc localName;
267       public final String JavaDoc qName;
268       public final String JavaDoc type;
269       public final String JavaDoc value;
270
271       public AttributeImpl(String JavaDoc namespaceUri, String JavaDoc localName, String JavaDoc qName, String JavaDoc type, String JavaDoc value)
272       {
273          this.namespaceUri = namespaceUri;
274          this.localName = localName;
275          this.qName = qName;
276          this.type = type;
277          this.value = value;
278       }
279
280       public boolean equals(Object JavaDoc o)
281       {
282          if(this == o) return true;
283          if(!(o instanceof AttributeImpl)) return false;
284
285          final AttributeImpl attribute = (AttributeImpl)o;
286
287          if(localName != null ? !localName.equals(attribute.localName) : attribute.localName != null) return false;
288          if(namespaceUri != null ? !namespaceUri.equals(attribute.namespaceUri) : attribute.namespaceUri != null) return false;
289          if(qName != null ? !qName.equals(attribute.qName) : attribute.qName != null) return false;
290          if(type != null ? !type.equals(attribute.type) : attribute.type != null) return false;
291          if(value != null ? !value.equals(attribute.value) : attribute.value != null) return false;
292
293          return true;
294       }
295
296       public int hashCode()
297       {
298          int result;
299          result = (namespaceUri != null ? namespaceUri.hashCode() : 0);
300          result = 29 * result + (localName != null ? localName.hashCode() : 0);
301          result = 29 * result + (qName != null ? qName.hashCode() : 0);
302          result = 29 * result + (type != null ? type.hashCode() : 0);
303          result = 29 * result + (value != null ? value.hashCode() : 0);
304          return result;
305       }
306    }
307 }
Popular Tags