View Javadoc
1   /*
2    * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   *
25   * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
26   */
27  
28  package com.sun.xml.internal.fastinfoset;
29  
30  import javax.xml.namespace.QName;
31  
32  public class QualifiedName {
33      public String prefix;
34      public String namespaceName;
35      public String localName;
36      public String qName;
37      public int index;
38      public int prefixIndex;
39      public int namespaceNameIndex;
40      public int localNameIndex;
41      public int attributeId;
42      public int attributeHash;
43      private QName qNameObject;
44  
45      public QualifiedName() { }
46  
47      public QualifiedName(String prefix, String namespaceName, String localName, String qName) {
48          this.prefix = prefix;
49          this.namespaceName = namespaceName;
50          this.localName = localName;
51          this.qName = qName;
52          this.index = -1;
53          this.prefixIndex = 0;
54          this.namespaceNameIndex = 0;
55          this.localNameIndex = -1;
56      }
57  
58      public void set(String prefix, String namespaceName, String localName, String qName) {
59          this.prefix = prefix;
60          this.namespaceName = namespaceName;
61          this.localName = localName;
62          this.qName = qName;
63          this.index = -1;
64          this.prefixIndex = 0;
65          this.namespaceNameIndex = 0;
66          this.localNameIndex = -1;
67          this.qNameObject = null;
68      }
69  
70      public QualifiedName(String prefix, String namespaceName, String localName, String qName, int index) {
71          this.prefix = prefix;
72          this.namespaceName = namespaceName;
73          this.localName = localName;
74          this.qName = qName;
75          this.index = index;
76          this.prefixIndex = 0;
77          this.namespaceNameIndex = 0;
78          this.localNameIndex = -1;
79      }
80  
81      public final QualifiedName set(String prefix, String namespaceName, String localName, String qName, int index) {
82          this.prefix = prefix;
83          this.namespaceName = namespaceName;
84          this.localName = localName;
85          this.qName = qName;
86          this.index = index;
87          this.prefixIndex = 0;
88          this.namespaceNameIndex = 0;
89          this.localNameIndex = -1;
90          this.qNameObject = null;
91          return this;
92      }
93  
94      public QualifiedName(String prefix, String namespaceName, String localName, String qName, int index,
95              int prefixIndex, int namespaceNameIndex, int localNameIndex) {
96          this.prefix = prefix;
97          this.namespaceName = namespaceName;
98          this.localName = localName;
99          this.qName = qName;
100         this.index = index;
101         this.prefixIndex = prefixIndex + 1;
102         this.namespaceNameIndex = namespaceNameIndex + 1;
103         this.localNameIndex = localNameIndex;
104     }
105 
106     public final QualifiedName set(String prefix, String namespaceName, String localName, String qName, int index,
107             int prefixIndex, int namespaceNameIndex, int localNameIndex) {
108         this.prefix = prefix;
109         this.namespaceName = namespaceName;
110         this.localName = localName;
111         this.qName = qName;
112         this.index = index;
113         this.prefixIndex = prefixIndex + 1;
114         this.namespaceNameIndex = namespaceNameIndex + 1;
115         this.localNameIndex = localNameIndex;
116         this.qNameObject = null;
117         return this;
118     }
119 
120     public QualifiedName(String prefix, String namespaceName, String localName) {
121         this.prefix = prefix;
122         this.namespaceName = namespaceName;
123         this.localName = localName;
124         this.qName = createQNameString(prefix, localName);
125         this.index = -1;
126         this.prefixIndex = 0;
127         this.namespaceNameIndex = 0;
128         this.localNameIndex = -1;
129     }
130 
131     public final QualifiedName set(String prefix, String namespaceName, String localName) {
132         this.prefix = prefix;
133         this.namespaceName = namespaceName;
134         this.localName = localName;
135         this.qName = createQNameString(prefix, localName);
136         this.index = -1;
137         this.prefixIndex = 0;
138         this.namespaceNameIndex = 0;
139         this.localNameIndex = -1;
140         this.qNameObject = null;
141         return this;
142     }
143 
144     public QualifiedName(String prefix, String namespaceName, String localName,
145             int prefixIndex, int namespaceNameIndex, int localNameIndex,
146             char[] charBuffer) {
147         this.prefix = prefix;
148         this.namespaceName = namespaceName;
149         this.localName = localName;
150 
151         if (charBuffer != null) {
152             final int l1 = prefix.length();
153             final int l2 = localName.length();
154             final int total = l1 + l2 + 1;
155             if (total < charBuffer.length) {
156                 prefix.getChars(0, l1, charBuffer, 0);
157                 charBuffer[l1] = ':';
158                 localName.getChars(0, l2, charBuffer, l1 + 1);
159                 this.qName = new String(charBuffer, 0, total);
160             } else {
161                 this.qName = createQNameString(prefix, localName);
162             }
163         } else {
164             this.qName = this.localName;
165         }
166 
167         this.prefixIndex = prefixIndex + 1;
168         this.namespaceNameIndex = namespaceNameIndex + 1;
169         this.localNameIndex = localNameIndex;
170         this.index = -1;
171     }
172 
173     public final QualifiedName set(String prefix, String namespaceName, String localName,
174             int prefixIndex, int namespaceNameIndex, int localNameIndex,
175             char[] charBuffer) {
176         this.prefix = prefix;
177         this.namespaceName = namespaceName;
178         this.localName = localName;
179 
180         if (charBuffer != null) {
181             final int l1 = prefix.length();
182             final int l2 = localName.length();
183             final int total = l1 + l2 + 1;
184             if (total < charBuffer.length) {
185                 prefix.getChars(0, l1, charBuffer, 0);
186                 charBuffer[l1] = ':';
187                 localName.getChars(0, l2, charBuffer, l1 + 1);
188                 this.qName = new String(charBuffer, 0, total);
189             } else {
190                 this.qName = createQNameString(prefix, localName);
191             }
192         } else {
193             this.qName = this.localName;
194         }
195 
196         this.prefixIndex = prefixIndex + 1;
197         this.namespaceNameIndex = namespaceNameIndex + 1;
198         this.localNameIndex = localNameIndex;
199         this.index = -1;
200         this.qNameObject = null;
201         return this;
202     }
203 
204     public QualifiedName(String prefix, String namespaceName, String localName, int index) {
205         this.prefix = prefix;
206         this.namespaceName = namespaceName;
207         this.localName = localName;
208         this.qName = createQNameString(prefix, localName);
209         this.index = index;
210         this.prefixIndex = 0;
211         this.namespaceNameIndex = 0;
212         this.localNameIndex = -1;
213     }
214 
215     public final QualifiedName set(String prefix, String namespaceName, String localName, int index) {
216         this.prefix = prefix;
217         this.namespaceName = namespaceName;
218         this.localName = localName;
219         this.qName = createQNameString(prefix, localName);
220         this.index = index;
221         this.prefixIndex = 0;
222         this.namespaceNameIndex = 0;
223         this.localNameIndex = -1;
224         this.qNameObject = null;
225         return this;
226     }
227 
228     public QualifiedName(String prefix, String namespaceName, String localName, int index,
229             int prefixIndex, int namespaceNameIndex, int localNameIndex) {
230         this.prefix = prefix;
231         this.namespaceName = namespaceName;
232         this.localName = localName;
233         this.qName = createQNameString(prefix, localName);
234         this.index = index;
235         this.prefixIndex = prefixIndex + 1;
236         this.namespaceNameIndex = namespaceNameIndex + 1;
237         this.localNameIndex = localNameIndex;
238     }
239 
240     public final QualifiedName set(String prefix, String namespaceName, String localName, int index,
241             int prefixIndex, int namespaceNameIndex, int localNameIndex) {
242         this.prefix = prefix;
243         this.namespaceName = namespaceName;
244         this.localName = localName;
245         this.qName = createQNameString(prefix, localName);
246         this.index = index;
247         this.prefixIndex = prefixIndex + 1;
248         this.namespaceNameIndex = namespaceNameIndex + 1;
249         this.localNameIndex = localNameIndex;
250         this.qNameObject = null;
251         return this;
252     }
253 
254     // Qualified Name as a Namespace Name
255     public QualifiedName(String prefix, String namespaceName) {
256         this.prefix = prefix;
257         this.namespaceName = namespaceName;
258         this.localName = "";
259         this.qName = "";
260         this.index = -1;
261         this.prefixIndex = 0;
262         this.namespaceNameIndex = 0;
263         this.localNameIndex = -1;
264     }
265 
266     public final QualifiedName set(String prefix, String namespaceName) {
267         this.prefix = prefix;
268         this.namespaceName = namespaceName;
269         this.localName = "";
270         this.qName = "";
271         this.index = -1;
272         this.prefixIndex = 0;
273         this.namespaceNameIndex = 0;
274         this.localNameIndex = -1;
275         this.qNameObject = null;
276         return this;
277     }
278 
279     public final QName getQName() {
280         if (qNameObject == null) {
281             qNameObject = new QName(namespaceName, localName, prefix);
282         }
283 
284         return qNameObject;
285     }
286 
287     public final String getQNameString() {
288         if (this.qName != "") {
289             return this.qName;
290         }
291 
292         return this.qName = createQNameString(prefix, localName);
293     }
294 
295     public final void createAttributeValues(int size) {
296         attributeId = localNameIndex | (namespaceNameIndex << 20);
297         attributeHash = localNameIndex % size;
298     }
299 
300     private final String createQNameString(String p, String l) {
301         if (p != null && p.length() > 0) {
302             final StringBuffer b = new StringBuffer(p);
303             b.append(':');
304             b.append(l);
305             return b.toString();
306         } else {
307             return l;
308         }
309     }
310 }