KickJava   Java API By Example, From Geeks To Geeks.

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