KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > stream > StaxIntern


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.stream;
30
31 import javax.xml.namespace.QName JavaDoc;
32
33 /**
34  * Interning names
35  */

36 public class StaxIntern {
37   private static final int SIZE = 203;
38   
39   private final Entry []_entries = new Entry[SIZE];
40
41   private final NamespaceReaderContext _namespaceContext;
42
43   StaxIntern(NamespaceReaderContext namespaceContext)
44   {
45     _namespaceContext = namespaceContext;
46   }
47
48   Entry add(char []buffer, int offset, int length, int colon,
49             boolean isAttribute)
50   {
51     int hash = 0;
52
53     for (int i = length - 1; i >= 0; i--) {
54       hash = 37 * hash + buffer[offset + i];
55     }
56
57     int bucket = (hash & 0x7fffffff) % SIZE;
58
59     Entry entry;
60
61     for (entry = _entries[bucket];
62          entry != null;
63          entry = entry._next) {
64       if (entry.match(buffer, offset, length, isAttribute))
65         return entry;
66     }
67
68     entry = new Entry(_entries[bucket],
69                       buffer, offset, length,
70                       colon,
71                       isAttribute);
72     _entries[bucket] = entry;
73
74     return entry;
75   }
76
77   final class Entry {
78     final Entry _next;
79     
80     final char []_buf;
81     final boolean _isAttribute;
82
83     final String JavaDoc _prefix;
84     final String JavaDoc _localName;
85
86     NamespaceBinding _namespace;
87     int _version;
88     QName JavaDoc _qName;
89
90     Entry(Entry next,
91           char []buf, int offset, int length,
92           int colon,
93           boolean isAttribute)
94     {
95       _next = next;
96
97       _buf = new char[length];
98       System.arraycopy(buf, offset, _buf, 0, length);
99
100       _isAttribute = isAttribute;
101
102       if (colon > 0) {
103         _prefix = new String JavaDoc(buf, 0, colon);
104         _localName = new String JavaDoc(buf, colon + 1, length - colon - 1);
105       }
106       else {
107         _prefix = null;
108         _localName = new String JavaDoc(buf, 0, length);
109       }
110
111       if (_isAttribute)
112         _namespace = _namespaceContext.getAttributeNamespace(_prefix);
113       else
114         _namespace = _namespaceContext.getElementNamespace(_prefix);
115
116       fillQName();
117     }
118
119     public final boolean match(char []buf, int offset, int length,
120                                boolean isAttribute)
121     {
122       if (length != _buf.length || _isAttribute != isAttribute)
123         return false;
124
125       char []entryBuf = _buf;
126
127       for (length--; length >= 0; length--) {
128         if (entryBuf[length] != buf[offset + length])
129           return false;
130       }
131
132       return true;
133     }
134
135     String JavaDoc getLocalName()
136     {
137       return _localName;
138     }
139
140     String JavaDoc getPrefix()
141     {
142       return _prefix;
143     }
144
145     QName JavaDoc getQName()
146     {
147       if (_version != _namespace.getVersion())
148         fillQName();
149
150       return _qName;
151     }
152
153     private void fillQName()
154     {
155       _version = _namespace.getVersion();
156
157       String JavaDoc prefix = _prefix;
158
159       if (prefix == null)
160         prefix = "";
161
162       _qName = new QName JavaDoc(_namespace.getUri(), _localName, prefix);
163     }
164   }
165 }
166
Popular Tags