KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.impl.xs.util;
59
60 import com.sun.org.apache.xerces.internal.util.SymbolHash;
61 import com.sun.org.apache.xerces.internal.xni.QName;
62 import com.sun.org.apache.xerces.internal.xs.*;
63
64 /**
65  * Containts the map between qnames and XSObject's.
66  *
67  * @author Sandy Gao, IBM
68  *
69  * @version $Id: XSNamedMapImpl.java,v 1.5 2003/11/11 20:15:00 sandygao Exp $
70  */

71 public class XSNamedMapImpl implements XSNamedMap {
72
73     // components of these namespaces are stored in this map
74
String JavaDoc[] fNamespaces;
75     // number of namespaces
76
int fNSNum;
77     // each entry contains components in one namespace
78
SymbolHash[] fMaps;
79     // store all components from all namespace.
80
// used when this map is accessed as a list.
81
XSObject[] fArray = null;
82     // store the number of componetns.
83
// used when this map is accessed as a list.
84
int fLength = -1;
85     // temprory QName object
86
QName fName = new QName();
87     
88     /**
89      * Construct an XSNamedMap implmentation for one namespace
90      *
91      * @param namespace the namespace to which the components belong
92      * @param map the map from local names to components
93      */

94     public XSNamedMapImpl(String JavaDoc namespace, SymbolHash map) {
95         fNamespaces = new String JavaDoc[] {namespace};
96         fMaps = new SymbolHash[] {map};
97         fNSNum = 1;
98     }
99
100     /**
101      * Construct an XSNamedMap implmentation for a list of namespaces
102      *
103      * @param namespaces the namespaces to which the components belong
104      * @param maps the maps from local names to components
105      * @param num the number of namespaces
106      */

107     public XSNamedMapImpl(String JavaDoc[] namespaces, SymbolHash[] maps, int num) {
108         fNamespaces = namespaces;
109         fMaps = maps;
110         fNSNum = num;
111     }
112
113     /**
114      * Construct an XSNamedMap implmentation one namespace from an array
115      *
116      * @param array containing all components
117      * @param length number of components
118      */

119     public XSNamedMapImpl(XSObject[] array, int length) {
120         if (length == 0) {
121             fNSNum = 0;
122             fLength = 0;
123             return;
124         }
125         // because all components are from the same target namesapce,
126
// get the namespace from the first one.
127
fNamespaces = new String JavaDoc[]{array[0].getNamespace()};
128         fMaps = null;
129         fNSNum = 1;
130         // copy elements to the Vector
131
fArray = array;
132         fLength = length;
133     }
134
135     /**
136      * The number of <code>XSObjects</code> in the <code>XSObjectList</code>. The
137      * range of valid child node indices is 0 to <code>length-1</code>
138      * inclusive.
139      */

140     public synchronized int getLength() {
141         if (fLength == -1) {
142             fLength = 0;
143             for (int i = 0; i < fNSNum; i++)
144                 fLength += fMaps[i].getLength();
145         }
146         return fLength;
147     }
148
149     /**
150      * Retrieves an <code>XSObject</code> specified by local name and namespace
151      * URI.
152      * @param namespace The namespace URI of the <code>XSObject</code> to
153      * retrieve.
154      * @param localName The local name of the <code>XSObject</code> to retrieve.
155      * @return A <code>XSObject</code> (of any type) with the specified local
156      * name and namespace URI, or <code>null</code> if they do not
157      * identify any <code>XSObject</code> in this map.
158      */

159     public XSObject itemByName(String JavaDoc namespace, String JavaDoc localName) {
160         if (namespace != null)
161             namespace = namespace.intern();
162         for (int i = 0; i < fNSNum; i++) {
163             if (namespace == fNamespaces[i]) {
164                 // when this map is created from SymbolHash's
165
// get the component from SymbolHash
166
if (fMaps != null)
167                     return (XSObject)fMaps[i].get(localName);
168                 // Otherwise (it's created from an array)
169
// go through the array to find a matcing name
170
XSObject ret;
171                 for (int j = 0; j < fLength; j++) {
172                     ret = fArray[j];
173                     if (ret.getName().equals(localName))
174                         return ret;
175                 }
176                 return null;
177             }
178         }
179         return null;
180     }
181
182     /**
183      * Returns the <code>index</code>th item in the map. The index starts at
184      * 0. If <code>index</code> is greater than or equal to the number of
185      * nodes in the list, this returns <code>null</code>.
186      * @param index The position in the map from which the item is to be
187      * retrieved.
188      * @return The <code>XSObject</code> at the <code>index</code>th position
189      * in the <code>XSNamedMap</code>, or <code>null</code> if that is
190      * not a valid index.
191      */

192     public synchronized XSObject item(int index) {
193         if (fArray == null) {
194             // calculate the total number of elements
195
getLength();
196             fArray = new XSObject[fLength];
197             int pos = 0;
198             // get components from all SymbolHash's
199
for (int i = 0; i < fNSNum; i++) {
200                 pos += fMaps[i].getValues(fArray, pos);
201             }
202         }
203         if (index < 0 || index >= fLength)
204             return null;
205         return fArray[index];
206     }
207     
208 } // class XSNamedMapImpl
209
Popular Tags