1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2002,2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.impl.xs.util;
  22 
  23 import com.sun.org.apache.xerces.internal.util.SymbolHash;
  24 import com.sun.org.apache.xerces.internal.xs.XSObject;
  25 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
  26 
  27 
  28 /**
  29  * Containts the map between qnames and XSObject's.
  30  *
  31  * @xerces.internal
  32  *
  33  * @author Sandy Gao, IBM
  34  *
  35  */
  36 public final class XSNamedMap4Types extends XSNamedMapImpl {
  37 
  38     // the type of component stored here: complex or simple type
  39     private final short fType;
  40 
  41     /**
  42      * Construct an XSNamedMap implementation for one namespace
  43      *
  44      * @param namespace the namespace to which the components belong
  45      * @param map       the map from local names to components
  46      * @param type      the type of components
  47      */
  48     public XSNamedMap4Types(String namespace, SymbolHash map, short type) {
  49         super(namespace, map);
  50         fType = type;
  51     }
  52 
  53     /**
  54      * Construct an XSNamedMap implementation for a list of namespaces
  55      *
  56      * @param namespaces the namespaces to which the components belong
  57      * @param maps       the maps from local names to components
  58      * @param num        the number of namespaces
  59      * @param type      the type of components
  60      */
  61     public XSNamedMap4Types(String[] namespaces, SymbolHash[] maps, int num, short type) {
  62         super(namespaces, maps, num);
  63         fType = type;
  64     }
  65 
  66     /**
  67      * The number of <code>XSObjects</code> in the <code>XSObjectList</code>. The
  68      * range of valid child node indices is 0 to <code>length-1</code>
  69      * inclusive.
  70      */
  71     public synchronized int getLength() {
  72         if (fLength == -1) {
  73             // first get the number of components for all types
  74             int length = 0;
  75             for (int i = 0; i < fNSNum; i++) {
  76                 length += fMaps[i].getLength();
  77             }
  78             // then copy all types to an temporary array
  79             int pos = 0;
  80             XSObject[] array = new XSObject[length];
  81             for (int i = 0; i < fNSNum; i++) {
  82                 pos += fMaps[i].getValues(array, pos);
  83             }
  84             // then copy either simple or complex types to fArray,
  85             // depending on which kind is required
  86             fLength = 0;
  87             fArray = new XSObject[length];
  88             XSTypeDefinition type;
  89             for (int i = 0; i < length; i++) {
  90                 type = (XSTypeDefinition)array[i];
  91                 if (type.getTypeCategory() == fType) {
  92                     fArray[fLength++] = type;
  93                 }
  94             }
  95         }
  96         return fLength;
  97     }
  98 
  99     /**
 100      * Retrieves an <code>XSObject</code> specified by local name and namespace
 101      * URI.
 102      * @param namespace The namespace URI of the <code>XSObject</code> to
 103      *   retrieve.
 104      * @param localName The local name of the <code>XSObject</code> to retrieve.
 105      * @return A <code>XSObject</code> (of any type) with the specified local
 106      *   name and namespace URI, or <code>null</code> if they do not
 107      *   identify any <code>XSObject</code> in this map.
 108      */
 109     public XSObject itemByName(String namespace, String localName) {
 110         for (int i = 0; i < fNSNum; i++) {
 111             if (isEqual(namespace, fNamespaces[i])) {
 112                 XSTypeDefinition type = (XSTypeDefinition)fMaps[i].get(localName);
 113                 // only return it if it matches the required type
 114                 if (type != null && type.getTypeCategory() == fType) {
 115                     return type;
 116                 }
 117                 return null;
 118             }
 119         }
 120         return null;
 121     }
 122 
 123     /**
 124      * Returns the <code>index</code>th item in the map. The index starts at
 125      * 0. If <code>index</code> is greater than or equal to the number of
 126      * nodes in the list, this returns <code>null</code>.
 127      * @param index The position in the map from which the item is to be
 128      *   retrieved.
 129      * @return The <code>XSObject</code> at the <code>index</code>th position
 130      *   in the <code>XSNamedMap</code>, or <code>null</code> if that is
 131      *   not a valid index.
 132      */
 133     public synchronized XSObject item(int index) {
 134         if (fArray == null) {
 135             getLength();
 136         }
 137         if (index < 0 || index >= fLength) {
 138             return null;
 139         }
 140         return fArray[index];
 141     }
 142 
 143 } // class XSNamedMapImpl