aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.runtime; aoqi@0: aoqi@0: import java.util.HashMap; aoqi@0: import java.util.HashSet; aoqi@0: import java.util.Map; aoqi@0: import java.util.Set; aoqi@0: aoqi@0: import javax.xml.namespace.QName; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.v2.util.QNameMap; aoqi@0: aoqi@0: /** aoqi@0: * Creates {@link Name}s and assign index numbers to them. aoqi@0: * aoqi@0: *

aoqi@0: * During this process, this class also finds out which namespace URIs aoqi@0: * are statically known to be un-bindable as the default namespace. aoqi@0: * Those are the namespace URIs that are used by attribute names. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: @SuppressWarnings({"StringEquality"}) aoqi@0: public final class NameBuilder { aoqi@0: private Map uriIndexMap = new HashMap(); aoqi@0: private Set nonDefaultableNsUris = new HashSet(); aoqi@0: private Map localNameIndexMap = new HashMap(); aoqi@0: private QNameMap elementQNameIndexMap = new QNameMap(); aoqi@0: private QNameMap attributeQNameIndexMap = new QNameMap(); aoqi@0: aoqi@0: public Name createElementName(QName name) { aoqi@0: return createElementName(name.getNamespaceURI(),name.getLocalPart()); aoqi@0: } aoqi@0: aoqi@0: public Name createElementName(String nsUri, String localName) { aoqi@0: return createName(nsUri, localName, false, elementQNameIndexMap); aoqi@0: } aoqi@0: aoqi@0: public Name createAttributeName(QName name) { aoqi@0: return createAttributeName(name.getNamespaceURI(),name.getLocalPart()); aoqi@0: } aoqi@0: aoqi@0: public Name createAttributeName(String nsUri, String localName) { aoqi@0: assert nsUri.intern()==nsUri; aoqi@0: assert localName.intern()==localName; aoqi@0: aoqi@0: if(nsUri.length()==0) aoqi@0: return new Name( aoqi@0: allocIndex(attributeQNameIndexMap,"",localName), aoqi@0: -1, aoqi@0: nsUri, aoqi@0: allocIndex(localNameIndexMap,localName), aoqi@0: localName, aoqi@0: true); aoqi@0: else { aoqi@0: nonDefaultableNsUris.add(nsUri); aoqi@0: return createName(nsUri,localName, true, attributeQNameIndexMap); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private Name createName(String nsUri, String localName, boolean isAttribute, QNameMap map) { aoqi@0: assert nsUri.intern()==nsUri; aoqi@0: assert localName.intern()==localName; aoqi@0: aoqi@0: return new Name( aoqi@0: allocIndex(map,nsUri,localName), aoqi@0: allocIndex(uriIndexMap,nsUri), aoqi@0: nsUri, aoqi@0: allocIndex(localNameIndexMap,localName), aoqi@0: localName, aoqi@0: isAttribute ); aoqi@0: } aoqi@0: aoqi@0: private int allocIndex(Map map, String str) { aoqi@0: Integer i = map.get(str); aoqi@0: if(i==null) { aoqi@0: i = map.size(); aoqi@0: map.put(str,i); aoqi@0: } aoqi@0: return i; aoqi@0: } aoqi@0: aoqi@0: private int allocIndex(QNameMap map, String nsUri, String localName) { aoqi@0: Integer i = map.get(nsUri,localName); aoqi@0: if(i==null) { aoqi@0: i = map.size(); aoqi@0: map.put(nsUri,localName,i); aoqi@0: } aoqi@0: return i; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Wraps up everything and creates {@link NameList}. aoqi@0: */ aoqi@0: public NameList conclude() { aoqi@0: boolean[] nsUriCannotBeDefaulted = new boolean[uriIndexMap.size()]; aoqi@0: for (Map.Entry e : uriIndexMap.entrySet()) { aoqi@0: nsUriCannotBeDefaulted[e.getValue()] = nonDefaultableNsUris.contains(e.getKey()); aoqi@0: } aoqi@0: aoqi@0: NameList r = new NameList( aoqi@0: list(uriIndexMap), aoqi@0: nsUriCannotBeDefaulted, aoqi@0: list(localNameIndexMap), aoqi@0: elementQNameIndexMap.size(), aoqi@0: attributeQNameIndexMap.size() ); aoqi@0: // delete them so that the create method can never be called again aoqi@0: uriIndexMap = null; aoqi@0: localNameIndexMap = null; aoqi@0: return r; aoqi@0: } aoqi@0: aoqi@0: private String[] list(Map map) { aoqi@0: String[] r = new String[map.size()]; aoqi@0: for (Map.Entry e : map.entrySet()) aoqi@0: r[e.getValue()] = e.getKey(); aoqi@0: return r; aoqi@0: } aoqi@0: }