KickJava   Java API By Example, From Geeks To Geeks.

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


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.xs.XSObject;
20 import org.apache.xerces.xs.XSTypeDefinition;
21 import org.apache.xerces.util.SymbolHash;
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: XSNamedMap4Types.java,v 1.7 2004/10/06 15:14:50 mrglavas Exp $
31  */

32 public class XSNamedMap4Types extends XSNamedMapImpl {
33
34     // the type of component stored here: complex or simple type
35
short fType;
36     
37     /**
38      * Construct an XSNamedMap implmentation for one namespace
39      *
40      * @param namespace the namespace to which the components belong
41      * @param map the map from local names to components
42      * @param type the type of components
43      */

44     public XSNamedMap4Types(String JavaDoc namespace, SymbolHash map, short type) {
45         super(namespace, map);
46         fType = type;
47     }
48
49     /**
50      * Construct an XSNamedMap implmentation for a list of namespaces
51      *
52      * @param namespaces the namespaces to which the components belong
53      * @param maps the maps from local names to components
54      * @param num the number of namespaces
55      * @param type the type of components
56      */

57     public XSNamedMap4Types(String JavaDoc[] namespaces, SymbolHash[] maps, int num, short type) {
58         super(namespaces, maps, num);
59         fType = type;
60     }
61
62     /**
63      * The number of <code>XSObjects</code> in the <code>XSObjectList</code>. The
64      * range of valid child node indices is 0 to <code>length-1</code>
65      * inclusive.
66      */

67     public synchronized int getLength() {
68         if (fLength == -1) {
69             // first get the number of components for all types
70
int length = 0;
71             for (int i = 0; i < fNSNum; i++)
72                 length += fMaps[i].getLength();
73             // then copy all types to an temporary array
74
int pos = 0;
75             XSObject[] array = new XSObject[length];
76             for (int i = 0; i < fNSNum; i++) {
77                 pos += fMaps[i].getValues(array, pos);
78             }
79             // then copy either simple or complex types to fArray,
80
// depending on which kind is required
81
fLength = 0;
82             fArray = new XSObject[length];
83             XSTypeDefinition type;
84             for (int i = 0; i < length; i++) {
85                 type = (XSTypeDefinition)array[i];
86                 if (type.getTypeCategory() == fType) {
87                     fArray[fLength++] = type;
88                 }
89             }
90         }
91         return fLength;
92     }
93
94     /**
95      * Retrieves an <code>XSObject</code> specified by local name and namespace
96      * URI.
97      * @param namespace The namespace URI of the <code>XSObject</code> to
98      * retrieve.
99      * @param localName The local name of the <code>XSObject</code> to retrieve.
100      * @return A <code>XSObject</code> (of any type) with the specified local
101      * name and namespace URI, or <code>null</code> if they do not
102      * identify any <code>XSObject</code> in this map.
103      */

104     public XSObject itemByName(String JavaDoc namespace, String JavaDoc localName) {
105         if (namespace != null)
106             namespace = namespace.intern();
107         for (int i = 0; i < fNSNum; i++) {
108             if (namespace == fNamespaces[i]) {
109                 XSTypeDefinition type = (XSTypeDefinition)fMaps[i].get(localName);
110                 // only return it if it mataches the required type
111
if (type.getTypeCategory() == fType)
112                     return type;
113                 return null;
114             }
115         }
116         return null;
117     }
118
119     /**
120      * Returns the <code>index</code>th item in the map. The index starts at
121      * 0. If <code>index</code> is greater than or equal to the number of
122      * nodes in the list, this returns <code>null</code>.
123      * @param index The position in the map from which the item is to be
124      * retrieved.
125      * @return The <code>XSObject</code> at the <code>index</code>th position
126      * in the <code>XSNamedMap</code>, or <code>null</code> if that is
127      * not a valid index.
128      */

129     public synchronized XSObject item(int index) {
130         if (fArray == null) {
131             getLength();
132         }
133         if (index < 0 || index >= fLength)
134             return null;
135         return fArray[index];
136     }
137     
138 } // class XSNamedMapImpl
139
Popular Tags