KickJava   Java API By Example, From Geeks To Geeks.

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


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
33 import org.xml.sax.Attributes JavaDoc;
34 import javax.xml.namespace.QName JavaDoc;
35
36 class QAttributes implements Attributes JavaDoc {
37   QName JavaDoc []names = new QName JavaDoc[32];
38   String JavaDoc []values = new String JavaDoc[32];
39   int size;
40
41   void clear()
42   {
43     size = 0;
44   }
45
46   void add(QName JavaDoc name, String JavaDoc value)
47   {
48     if (size == names.length) {
49       QName JavaDoc []newNames = new QName JavaDoc[2 * names.length];
50       String JavaDoc []newValues = new String JavaDoc[2 * names.length];
51       System.arraycopy(names, 0, newNames, 0, names.length);
52       System.arraycopy(values, 0, newValues, 0, names.length);
53       names = newNames;
54       values = newValues;
55     }
56     
57     names[size] = name;
58     values[size] = value;
59     size++;
60   }
61
62   public int getLength()
63   {
64     return size;
65   }
66
67   public QName JavaDoc getName(int i)
68   {
69     return names[i];
70   }
71
72   public String JavaDoc getQName(int i)
73   {
74     String JavaDoc prefix = names[i].getPrefix();
75
76     if (prefix != null && prefix.length() > 0)
77       return prefix + ":" + names[i].getLocalPart();
78     else
79       return names[i].getLocalPart();
80   }
81
82   public String JavaDoc getURI(int i)
83   {
84     String JavaDoc uri = names[i].getNamespaceURI();
85
86     if (uri != null)
87       return uri;
88     else
89       return "";
90   }
91
92   public String JavaDoc getLocalName(int i)
93   {
94     String JavaDoc name = names[i].getLocalPart();
95
96     if (name != null)
97       return name;
98     else
99       return "";
100   }
101
102   public String JavaDoc getValue(int i)
103   {
104     return values[i];
105   }
106
107   public String JavaDoc getValue(String JavaDoc qName)
108   {
109     for (int i = 0; i < size; i++) {
110       if (qName.equals(names[i].getLocalPart()))
111         return values[i];
112     }
113
114     return null;
115   }
116
117   public String JavaDoc getValue(String JavaDoc uri, String JavaDoc localName)
118   {
119     for (int i = 0; i < size; i++) {
120       String JavaDoc testURI = names[i].getNamespaceURI();
121
122       if (testURI == null)
123     testURI = "";
124       
125       if (uri.equals(testURI) && localName.equals(names[i].getLocalPart()))
126         return values[i];
127     }
128
129     return null;
130   }
131
132   public int getIndex(String JavaDoc qName)
133   {
134     for (int i = 0; i < size; i++) {
135       if (qName.equals(names[i].getLocalPart()))
136         return i;
137     }
138
139     return -1;
140   }
141
142   public int getIndex(String JavaDoc uri, String JavaDoc localName)
143   {
144     for (int i = 0; i < size; i++) {
145       if (uri.equals(names[i].getNamespaceURI()) &&
146           localName.equals(names[i].getLocalPart()))
147         return i;
148     }
149
150     return -1;
151   }
152
153   public String JavaDoc getType(int i)
154   {
155     return "CDATA";
156   }
157
158   public String JavaDoc getType(String JavaDoc uri, String JavaDoc localName)
159   {
160     return "CDATA";
161   }
162
163   public String JavaDoc getType(String JavaDoc qName)
164   {
165     return "CDATA";
166   }
167
168   public String JavaDoc toString()
169   {
170     StringBuilder JavaDoc cb = new StringBuilder JavaDoc();
171     cb.append("QAttributes[");
172     for (int i = 0; i < size; i++) {
173       cb.append(" ");
174       cb.append(names[i]);
175       cb.append("=\"");
176       cb.append(values[i]);
177       cb.append("\"");
178     }
179     cb.append("]");
180     
181     return cb.toString();
182   }
183 }
184
Popular Tags