KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > util > XSNamedMapImpl


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.xs.util;
18
19 import org.apache.xerces.util.SymbolHash;
20 import org.apache.xerces.xni.QName;
21 import org.apache.xerces.xs.*;
22
23 /**
24  * Containts the map between qnames and XSObject's.
25  *
26  * @xerces.internal
27  *
28  * @author Sandy Gao, IBM
29  *
30  * @version $Id: XSNamedMapImpl.java,v 1.8 2004/10/06 15:14:50 mrglavas Exp $
31  */

32 public class XSNamedMapImpl implements XSNamedMap {
33
34     /**
35      * An immutable empty map.
36      */

37     public static final XSNamedMap EMPTY_MAP = new XSNamedMap () {
38         public int getLength() {
39             return 0;
40         }
41         public XSObject itemByName(String JavaDoc namespace, String JavaDoc localName) {
42             return null;
43         }
44         public XSObject item(int index) {
45             return null;
46         }
47     };
48     
49     // components of these namespaces are stored in this map
50
String JavaDoc[] fNamespaces;
51     // number of namespaces
52
int fNSNum;
53     // each entry contains components in one namespace
54
SymbolHash[] fMaps;
55     // store all components from all namespace.
56
// used when this map is accessed as a list.
57
XSObject[] fArray = null;
58     // store the number of componetns.
59
// used when this map is accessed as a list.
60
int fLength = -1;
61     // temprory QName object
62
QName fName = new QName();
63     
64     /**
65      * Construct an XSNamedMap implmentation for one namespace
66      *
67      * @param namespace the namespace to which the components belong
68      * @param map the map from local names to components
69      */

70     public XSNamedMapImpl(String JavaDoc namespace, SymbolHash map) {
71         fNamespaces = new String JavaDoc[] {namespace};
72         fMaps = new SymbolHash[] {map};
73         fNSNum = 1;
74     }
75
76     /**
77      * Construct an XSNamedMap implmentation for a list of namespaces
78      *
79      * @param namespaces the namespaces to which the components belong
80      * @param maps the maps from local names to components
81      * @param num the number of namespaces
82      */

83     public XSNamedMapImpl(String JavaDoc[] namespaces, SymbolHash[] maps, int num) {
84         fNamespaces = namespaces;
85         fMaps = maps;
86         fNSNum = num;
87     }
88
89     /**
90      * Construct an XSNamedMap implmentation one namespace from an array
91      *
92      * @param array containing all components
93      * @param length number of components
94      */

95     public XSNamedMapImpl(XSObject[] array, int length) {
96         if (length == 0) {
97             fNSNum = 0;
98             fLength = 0;
99             return;
100         }
101         // because all components are from the same target namesapce,
102
// get the namespace from the first one.
103
fNamespaces = new String JavaDoc[]{array[0].getNamespace()};
104         fMaps = null;
105         fNSNum = 1;
106         // copy elements to the Vector
107
fArray = array;
108         fLength = length;
109     }
110
111     /**
112      * The number of <code>XSObjects</code> in the <code>XSObjectList</code>. The
113      * range of valid child node indices is 0 to <code>length-1</code>
114      * inclusive.
115      */

116     public synchronized int getLength() {
117         if (fLength == -1) {
118             fLength = 0;
119             for (int i = 0; i < fNSNum; i++)
120                 fLength += fMaps[i].getLength();
121         }
122         return fLength;
123     }
124
125     /**
126      * Retrieves an <code>XSObject</code> specified by local name and namespace
127      * URI.
128      * @param namespace The namespace URI of the <code>XSObject</code> to
129      * retrieve.
130      * @param localName The local name of the <code>XSObject</code> to retrieve.
131      * @return A <code>XSObject</code> (of any type) with the specified local
132      * name and namespace URI, or <code>null</code> if they do not
133      * identify any <code>XSObject</code> in this map.
134      */

135     public XSObject itemByName(String JavaDoc namespace, String JavaDoc localName) {
136         if (namespace != null)
137             namespace = namespace.intern();
138         for (int i = 0; i < fNSNum; i++) {
139             if (namespace == fNamespaces[i]) {
140                 // when this map is created from SymbolHash's
141
// get the component from SymbolHash
142
if (fMaps != null)
143                     return (XSObject)fMaps[i].get(localName);
144                 // Otherwise (it's created from an array)
145
// go through the array to find a matcing name
146
XSObject ret;
147                 for (int j = 0; j < fLength; j++) {
148                     ret = fArray[j];
149                     if (ret.getName().equals(localName))
150                         return ret;
151                 }
152                 return null;
153             }
154         }
155         return null;
156     }
157
158     /**
159      * Returns the <code>index</code>th item in the map. The index starts at
160      * 0. If <code>index</code> is greater than or equal to the number of
161      * nodes in the list, this returns <code>null</code>.
162      * @param index The position in the map from which the item is to be
163      * retrieved.
164      * @return The <code>XSObject</code> at the <code>index</code>th position
165      * in the <code>XSNamedMap</code>, or <code>null</code> if that is
166      * not a valid index.
167      */

168     public synchronized XSObject item(int index) {
169         if (fArray == null) {
170             // calculate the total number of elements
171
getLength();
172             fArray = new XSObject[fLength];
173             int pos = 0;
174             // get components from all SymbolHash's
175
for (int i = 0; i < fNSNum; i++) {
176                 pos += fMaps[i].getValues(fArray, pos);
177             }
178         }
179         if (index < 0 || index >= fLength)
180             return null;
181         return fArray[index];
182     }
183     
184 } // class XSNamedMapImpl
185
Popular Tags