286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2002,2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xerces.internal.impl.xs.util;
286N/A
286N/Aimport java.util.AbstractMap;
286N/Aimport java.util.AbstractSet;
286N/Aimport java.util.Iterator;
286N/Aimport java.util.Map;
286N/Aimport java.util.NoSuchElementException;
286N/Aimport java.util.Set;
286N/A
286N/Aimport javax.xml.XMLConstants;
286N/Aimport javax.xml.namespace.QName;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.util.SymbolHash;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSNamedMap;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSObject;
286N/A
286N/A/**
286N/A * Containts the map between qnames and XSObject's.
286N/A *
286N/A * @xerces.internal
286N/A *
286N/A * @author Sandy Gao, IBM
286N/A *
286N/A * @version $Id: XSNamedMapImpl.java,v 1.7 2010-11-01 04:40:06 joehw Exp $
286N/A */
286N/Apublic class XSNamedMapImpl extends AbstractMap implements XSNamedMap {
286N/A
286N/A /**
286N/A * An immutable empty map.
286N/A */
286N/A public static final XSNamedMapImpl EMPTY_MAP = new XSNamedMapImpl(new XSObject[0], 0);
286N/A
286N/A // components of these namespaces are stored in this map
286N/A final String[] fNamespaces;
286N/A // number of namespaces
286N/A final int fNSNum;
286N/A // each entry contains components in one namespace
286N/A final SymbolHash[] fMaps;
286N/A // store all components from all namespace.
286N/A // used when this map is accessed as a list.
286N/A XSObject[] fArray = null;
286N/A // store the number of components.
286N/A // used when this map is accessed as a list.
286N/A int fLength = -1;
286N/A // Set of Map.Entry<QName,XSObject> for the java.util.Map methods
286N/A private Set fEntrySet = null;
286N/A
286N/A /**
286N/A * Construct an XSNamedMap implementation for one namespace
286N/A *
286N/A * @param namespace the namespace to which the components belong
286N/A * @param map the map from local names to components
286N/A */
286N/A public XSNamedMapImpl(String namespace, SymbolHash map) {
286N/A fNamespaces = new String[] {namespace};
286N/A fMaps = new SymbolHash[] {map};
286N/A fNSNum = 1;
286N/A }
286N/A
286N/A /**
286N/A * Construct an XSNamedMap implementation for a list of namespaces
286N/A *
286N/A * @param namespaces the namespaces to which the components belong
286N/A * @param maps the maps from local names to components
286N/A * @param num the number of namespaces
286N/A */
286N/A public XSNamedMapImpl(String[] namespaces, SymbolHash[] maps, int num) {
286N/A fNamespaces = namespaces;
286N/A fMaps = maps;
286N/A fNSNum = num;
286N/A }
286N/A
286N/A /**
286N/A * Construct an XSNamedMap implementation one namespace from an array
286N/A *
286N/A * @param array containing all components
286N/A * @param length number of components
286N/A */
286N/A public XSNamedMapImpl(XSObject[] array, int length) {
286N/A if (length == 0) {
286N/A fNamespaces = null;
286N/A fMaps = null;
286N/A fNSNum = 0;
286N/A fArray = array;
286N/A fLength = 0;
286N/A return;
286N/A }
286N/A // because all components are from the same target namesapce,
286N/A // get the namespace from the first one.
286N/A fNamespaces = new String[]{array[0].getNamespace()};
286N/A fMaps = null;
286N/A fNSNum = 1;
286N/A // copy elements to the Vector
286N/A fArray = array;
286N/A fLength = length;
286N/A }
286N/A
286N/A /**
286N/A * The number of <code>XSObjects</code> in the <code>XSObjectList</code>.
286N/A * The range of valid child object indices is 0 to <code>length-1</code>
286N/A * inclusive.
286N/A */
286N/A public synchronized int getLength() {
286N/A if (fLength == -1) {
286N/A fLength = 0;
286N/A for (int i = 0; i < fNSNum; i++) {
286N/A fLength += fMaps[i].getLength();
286N/A }
286N/A }
286N/A return fLength;
286N/A }
286N/A
286N/A /**
286N/A * Retrieves an <code>XSObject</code> specified by local name and
286N/A * namespace URI.
286N/A * <br>Per XML Namespaces, applications must use the value <code>null</code> as the
286N/A * <code>namespace</code> parameter for methods if they wish to specify
286N/A * no namespace.
286N/A * @param namespace The namespace URI of the <code>XSObject</code> to
286N/A * retrieve, or <code>null</code> if the <code>XSObject</code> has no
286N/A * namespace.
286N/A * @param localName The local name of the <code>XSObject</code> to
286N/A * retrieve.
286N/A * @return A <code>XSObject</code> (of any type) with the specified local
286N/A * name and namespace URI, or <code>null</code> if they do not
286N/A * identify any object in this map.
286N/A */
286N/A public XSObject itemByName(String namespace, String localName) {
286N/A for (int i = 0; i < fNSNum; i++) {
286N/A if (isEqual(namespace, fNamespaces[i])) {
286N/A // when this map is created from SymbolHash's
286N/A // get the component from SymbolHash
286N/A if (fMaps != null) {
286N/A return (XSObject)fMaps[i].get(localName);
286N/A }
286N/A // Otherwise (it's created from an array)
286N/A // go through the array to find a matching name
286N/A XSObject ret;
286N/A for (int j = 0; j < fLength; j++) {
286N/A ret = fArray[j];
286N/A if (ret.getName().equals(localName)) {
286N/A return ret;
286N/A }
286N/A }
286N/A return null;
286N/A }
286N/A }
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * Returns the <code>index</code>th item in the collection or
286N/A * <code>null</code> if <code>index</code> is greater than or equal to
286N/A * the number of objects in the list. The index starts at 0.
286N/A * @param index index into the collection.
286N/A * @return The <code>XSObject</code> at the <code>index</code>th
286N/A * position in the <code>XSObjectList</code>, or <code>null</code> if
286N/A * the index specified is not valid.
286N/A */
286N/A public synchronized XSObject item(int index) {
286N/A if (fArray == null) {
286N/A // calculate the total number of elements
286N/A getLength();
286N/A fArray = new XSObject[fLength];
286N/A int pos = 0;
286N/A // get components from all SymbolHashes
286N/A for (int i = 0; i < fNSNum; i++) {
286N/A pos += fMaps[i].getValues(fArray, pos);
286N/A }
286N/A }
286N/A if (index < 0 || index >= fLength) {
286N/A return null;
286N/A }
286N/A return fArray[index];
286N/A }
286N/A
286N/A static boolean isEqual(String one, String two) {
286N/A return (one != null) ? one.equals(two) : (two == null);
286N/A }
286N/A
286N/A /*
286N/A * java.util.Map methods
286N/A */
286N/A
286N/A public boolean containsKey(Object key) {
286N/A return (get(key) != null);
286N/A }
286N/A
286N/A public Object get(Object key) {
286N/A if (key instanceof QName) {
286N/A final QName name = (QName) key;
286N/A String namespaceURI = name.getNamespaceURI();
286N/A if (XMLConstants.NULL_NS_URI.equals(namespaceURI)) {
286N/A namespaceURI = null;
286N/A }
286N/A String localPart = name.getLocalPart();
286N/A return itemByName(namespaceURI, localPart);
286N/A }
286N/A return null;
286N/A }
286N/A
286N/A public int size() {
286N/A return getLength();
286N/A }
286N/A
286N/A public synchronized Set entrySet() {
286N/A // Defer creation of the entry set until it is actually needed.
286N/A if (fEntrySet == null) {
286N/A final int length = getLength();
286N/A final XSNamedMapEntry[] entries = new XSNamedMapEntry[length];
286N/A for (int i = 0; i < length; ++i) {
286N/A XSObject xso = item(i);
286N/A entries[i] = new XSNamedMapEntry(new QName(xso.getNamespace(), xso.getName()), xso);
286N/A }
286N/A // Create a view of this immutable map.
286N/A fEntrySet = new AbstractSet() {
286N/A public Iterator iterator() {
286N/A return new Iterator() {
286N/A private int index = 0;
286N/A public boolean hasNext() {
286N/A return (index < length);
286N/A }
286N/A public Object next() {
286N/A if (index < length) {
286N/A return entries[index++];
286N/A }
286N/A throw new NoSuchElementException();
286N/A }
286N/A public void remove() {
286N/A throw new UnsupportedOperationException();
286N/A }
286N/A };
286N/A }
286N/A public int size() {
286N/A return length;
286N/A }
286N/A };
286N/A }
286N/A return fEntrySet;
286N/A }
286N/A
286N/A /** An entry in the XSNamedMap. **/
286N/A private static final class XSNamedMapEntry implements Map.Entry {
286N/A private final QName key;
286N/A private final XSObject value;
286N/A public XSNamedMapEntry(QName key, XSObject value) {
286N/A this.key = key;
286N/A this.value = value;
286N/A }
286N/A public Object getKey() {
286N/A return key;
286N/A }
286N/A public Object getValue() {
286N/A return value;
286N/A }
286N/A public Object setValue(Object value) {
286N/A throw new UnsupportedOperationException();
286N/A }
286N/A public boolean equals(Object o) {
286N/A if (o instanceof Map.Entry) {
286N/A Map.Entry e = (Map.Entry) o;
286N/A Object otherKey = e.getKey();
286N/A Object otherValue = e.getValue();
286N/A return (key == null ? otherKey == null : key.equals(otherKey)) &&
286N/A (value == null ? otherValue == null : value.equals(otherValue));
286N/A }
286N/A return false;
286N/A }
286N/A public int hashCode() {
286N/A return (key == null ? 0 : key.hashCode())
286N/A ^ (value == null ? 0 : value.hashCode());
286N/A }
286N/A public String toString() {
286N/A StringBuffer buffer = new StringBuffer();
286N/A buffer.append(String.valueOf(key));
286N/A buffer.append('=');
286N/A buffer.append(String.valueOf(value));
286N/A return buffer.toString();
286N/A }
286N/A }
286N/A
286N/A} // class XSNamedMapImpl