325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.bind.v2.runtime;
325N/A
325N/Aimport java.util.HashMap;
325N/Aimport java.util.HashSet;
325N/Aimport java.util.Map;
325N/Aimport java.util.Set;
325N/A
325N/Aimport javax.xml.namespace.QName;
325N/A
325N/Aimport com.sun.xml.internal.bind.v2.util.QNameMap;
325N/A
325N/A/**
325N/A * Creates {@link Name}s and assign index numbers to them.
325N/A *
325N/A * <p>
325N/A * During this process, this class also finds out which namespace URIs
325N/A * are statically known to be un-bindable as the default namespace.
325N/A * Those are the namespace URIs that are used by attribute names.
325N/A *
325N/A * @author Kohsuke Kawaguchi
325N/A */
325N/A@SuppressWarnings({"StringEquality"})
325N/Apublic final class NameBuilder {
325N/A private Map<String,Integer> uriIndexMap = new HashMap<String, Integer>();
325N/A private Set<String> nonDefaultableNsUris = new HashSet<String>();
325N/A private Map<String,Integer> localNameIndexMap = new HashMap<String, Integer>();
325N/A private QNameMap<Integer> elementQNameIndexMap = new QNameMap<Integer>();
325N/A private QNameMap<Integer> attributeQNameIndexMap = new QNameMap<Integer>();
325N/A
325N/A public Name createElementName(QName name) {
325N/A return createElementName(name.getNamespaceURI(),name.getLocalPart());
325N/A }
325N/A
325N/A public Name createElementName(String nsUri, String localName) {
325N/A return createName(nsUri, localName, false, elementQNameIndexMap);
325N/A }
325N/A
325N/A public Name createAttributeName(QName name) {
325N/A return createAttributeName(name.getNamespaceURI(),name.getLocalPart());
325N/A }
325N/A
325N/A public Name createAttributeName(String nsUri, String localName) {
325N/A assert nsUri.intern()==nsUri;
325N/A assert localName.intern()==localName;
325N/A
325N/A if(nsUri.length()==0)
325N/A return new Name(
325N/A allocIndex(attributeQNameIndexMap,"",localName),
325N/A -1,
325N/A nsUri,
325N/A allocIndex(localNameIndexMap,localName),
325N/A localName,
325N/A true);
325N/A else {
325N/A nonDefaultableNsUris.add(nsUri);
325N/A return createName(nsUri,localName, true, attributeQNameIndexMap);
325N/A }
325N/A }
325N/A
325N/A private Name createName(String nsUri, String localName, boolean isAttribute, QNameMap<Integer> map) {
325N/A assert nsUri.intern()==nsUri;
325N/A assert localName.intern()==localName;
325N/A
325N/A return new Name(
325N/A allocIndex(map,nsUri,localName),
325N/A allocIndex(uriIndexMap,nsUri),
325N/A nsUri,
325N/A allocIndex(localNameIndexMap,localName),
325N/A localName,
325N/A isAttribute );
325N/A }
325N/A
325N/A private int allocIndex(Map<String,Integer> map, String str) {
325N/A Integer i = map.get(str);
325N/A if(i==null) {
325N/A i = map.size();
325N/A map.put(str,i);
325N/A }
325N/A return i;
325N/A }
325N/A
325N/A private int allocIndex(QNameMap<Integer> map, String nsUri, String localName) {
325N/A Integer i = map.get(nsUri,localName);
325N/A if(i==null) {
325N/A i = map.size();
325N/A map.put(nsUri,localName,i);
325N/A }
325N/A return i;
325N/A }
325N/A
325N/A /**
325N/A * Wraps up everything and creates {@link NameList}.
325N/A */
325N/A public NameList conclude() {
325N/A boolean[] nsUriCannotBeDefaulted = new boolean[uriIndexMap.size()];
325N/A for (Map.Entry<String,Integer> e : uriIndexMap.entrySet()) {
325N/A nsUriCannotBeDefaulted[e.getValue()] = nonDefaultableNsUris.contains(e.getKey());
325N/A }
325N/A
325N/A NameList r = new NameList(
325N/A list(uriIndexMap),
325N/A nsUriCannotBeDefaulted,
325N/A list(localNameIndexMap),
325N/A elementQNameIndexMap.size(),
325N/A attributeQNameIndexMap.size() );
325N/A // delete them so that the create method can never be called again
325N/A uriIndexMap = null;
325N/A localNameIndexMap = null;
325N/A return r;
325N/A }
325N/A
325N/A private String[] list(Map<String, Integer> map) {
325N/A String[] r = new String[map.size()];
325N/A for (Map.Entry<String, Integer> e : map.entrySet())
325N/A r[e.getValue()] = e.getKey();
325N/A return r;
325N/A }
325N/A}