KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml2 > AttributesImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xml2;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.xml.QName;
33
34 import org.xml.sax.Attributes JavaDoc;
35
36 /**
37  * Implements the SAX Attributes class.
38  */

39 class AttributesImpl implements Attributes JavaDoc {
40   private InternQName []_names = new InternQName[32];
41   private String JavaDoc []_values = new String JavaDoc[32];
42   private int _size;
43
44   /**
45    * Clears the attributes.
46    */

47   void clear()
48   {
49     _size = 0;
50   }
51
52   /**
53    * Adds a new attribute name and value.
54    */

55   void add(InternQName name, String JavaDoc value)
56   {
57     if (_size == _names.length) {
58       InternQName []newNames = new InternQName[2 * _names.length];
59       String JavaDoc []newValues = new String JavaDoc[2 * _names.length];
60       System.arraycopy(_names, 0, newNames, 0, _names.length);
61       System.arraycopy(_values, 0, newValues, 0, _names.length);
62       _names = newNames;
63       _values = newValues;
64     }
65     
66     _names[_size] = name;
67     _values[_size] = value;
68     _size++;
69   }
70
71   /**
72    * Returns the number of attributes.
73    */

74   public int getLength()
75   {
76     return _size;
77   }
78
79   /**
80    * Returns the indexed name.
81    */

82   /*
83   public QName getName(int i)
84   {
85     return _names[i];
86   }
87   */

88
89   /**
90    * Returns the name.
91    */

92   public String JavaDoc getQName(int i)
93   {
94     return _names[i].getName();
95   }
96
97   /**
98    * Returns the namespace URI.
99    */

100   public String JavaDoc getURI(int i)
101   {
102     throw new UnsupportedOperationException JavaDoc();
103     //return _names[i].getNamespaceURI();
104
}
105
106   /**
107    * Returns the local name.
108    */

109   public String JavaDoc getLocalName(int i)
110   {
111     return _names[i].getLocalName();
112   }
113
114   /**
115    * Returns the value.
116    */

117   public String JavaDoc getValue(int i)
118   {
119     return _values[i];
120   }
121
122   /**
123    * Returns the value with the given name.
124    */

125   public String JavaDoc getValue(String JavaDoc qName)
126   {
127     for (int i = _size - 1; i >= 0; i--) {
128       if (qName.equals(_names[i].getName()))
129         return _values[i];
130     }
131
132     return null;
133   }
134
135   /**
136    * Returns the value for hte uri and local name.
137    */

138   public String JavaDoc getValue(String JavaDoc uri, String JavaDoc localName)
139   {
140     for (int i = _size - 1; i >= 0; i--) {
141       if (uri.equals(_names[i].getNamespaceURI()) &&
142           localName.equals(_names[i].getLocalName()))
143         return _values[i];
144     }
145
146     return null;
147   }
148
149   /**
150    * Returns the index of the given name.
151    */

152   public int getIndex(String JavaDoc qName)
153   {
154     for (int i = _size - 1; i >= 0; i--) {
155       if (qName.equals(_names[i].getName()))
156         return i;
157     }
158
159     return -1;
160   }
161
162   /**
163    * Returns the index of the matching name.
164    */

165   public int getIndex(String JavaDoc uri, String JavaDoc localName)
166   {
167     for (int i = _size -1; i >= 0; i--) {
168       if (uri.equals(_names[i].getNamespaceURI()) &&
169           localName.equals(_names[i].getLocalName()))
170         return i;
171     }
172
173     return -1;
174   }
175
176   /**
177    * Returns the set type.
178    */

179   public String JavaDoc getType(int i)
180   {
181     return "CDATA";
182   }
183
184   /**
185    * Returns the set type.
186    */

187   public String JavaDoc getType(String JavaDoc uri, String JavaDoc localName)
188   {
189     return "CDATA";
190   }
191
192   /**
193    * Returns the set type.
194    */

195   public String JavaDoc getType(String JavaDoc qName)
196   {
197     return "CDATA";
198   }
199
200   /**
201    * Returns a printable version.
202    */

203   public String JavaDoc toString()
204   {
205     CharBuffer cb = new CharBuffer();
206     cb.append("AttributesImpl[");
207     for (int i = 0; i < _size; i++) {
208       cb.append(" ");
209       cb.append(_names[i]);
210       cb.append("=\"");
211       cb.append(_values[i]);
212       cb.append("\"");
213     }
214     cb.append("]");
215     
216     return cb.close();
217   }
218 }
219
Popular Tags