KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.xml2;
31
32 import javax.xml.namespace.QName JavaDoc;
33
34 /**
35  * Interning names
36  */

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