001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.runtime;
038:
039: import java.util.HashMap;
040: import java.util.HashSet;
041: import java.util.Map;
042: import java.util.Set;
043:
044: import javax.xml.namespace.QName;
045:
046: import com.sun.xml.bind.v2.util.QNameMap;
047:
048: /**
049: * Creates {@link Name}s and assign index numbers to them.
050: *
051: * <p>
052: * During this process, this class also finds out which namespace URIs
053: * are statically known to be un-bindable as the default namespace.
054: * Those are the namespace URIs that are used by attribute names.
055: *
056: * @author Kohsuke Kawaguchi
057: */
058: @SuppressWarnings({"StringEquality"})
059: public final class NameBuilder {
060: private Map<String, Integer> uriIndexMap = new HashMap<String, Integer>();
061: private Set<String> nonDefaultableNsUris = new HashSet<String>();
062: private Map<String, Integer> localNameIndexMap = new HashMap<String, Integer>();
063: private QNameMap<Integer> elementQNameIndexMap = new QNameMap<Integer>();
064: private QNameMap<Integer> attributeQNameIndexMap = new QNameMap<Integer>();
065:
066: public Name createElementName(QName name) {
067: return createElementName(name.getNamespaceURI(), name
068: .getLocalPart());
069: }
070:
071: public Name createElementName(String nsUri, String localName) {
072: return createName(nsUri, localName, false, elementQNameIndexMap);
073: }
074:
075: public Name createAttributeName(QName name) {
076: return createAttributeName(name.getNamespaceURI(), name
077: .getLocalPart());
078: }
079:
080: public Name createAttributeName(String nsUri, String localName) {
081: assert nsUri.intern() == nsUri;
082: assert localName.intern() == localName;
083:
084: if (nsUri.length() == 0)
085: return new Name(allocIndex(attributeQNameIndexMap, "",
086: localName), -1, nsUri, allocIndex(
087: localNameIndexMap, localName), localName, true);
088: else {
089: nonDefaultableNsUris.add(nsUri);
090: return createName(nsUri, localName, true,
091: attributeQNameIndexMap);
092: }
093: }
094:
095: private Name createName(String nsUri, String localName,
096: boolean isAttribute, QNameMap<Integer> map) {
097: assert nsUri.intern() == nsUri;
098: assert localName.intern() == localName;
099:
100: return new Name(allocIndex(map, nsUri, localName), allocIndex(
101: uriIndexMap, nsUri), nsUri, allocIndex(
102: localNameIndexMap, localName), localName, isAttribute);
103: }
104:
105: private int allocIndex(Map<String, Integer> map, String str) {
106: Integer i = map.get(str);
107: if (i == null) {
108: i = map.size();
109: map.put(str, i);
110: }
111: return i;
112: }
113:
114: private int allocIndex(QNameMap<Integer> map, String nsUri,
115: String localName) {
116: Integer i = map.get(nsUri, localName);
117: if (i == null) {
118: i = map.size();
119: map.put(nsUri, localName, i);
120: }
121: return i;
122: }
123:
124: /**
125: * Wraps up everything and creates {@link NameList}.
126: */
127: public NameList conclude() {
128: boolean[] nsUriCannotBeDefaulted = new boolean[uriIndexMap
129: .size()];
130: for (Map.Entry<String, Integer> e : uriIndexMap.entrySet()) {
131: nsUriCannotBeDefaulted[e.getValue()] = nonDefaultableNsUris
132: .contains(e.getKey());
133: }
134:
135: NameList r = new NameList(list(uriIndexMap),
136: nsUriCannotBeDefaulted, list(localNameIndexMap),
137: elementQNameIndexMap.size(), attributeQNameIndexMap
138: .size());
139: // delete them so that the create method can never be called again
140: uriIndexMap = null;
141: localNameIndexMap = null;
142: return r;
143: }
144:
145: private String[] list(Map<String, Integer> map) {
146: String[] r = new String[map.size()];
147: for (Map.Entry<String, Integer> e : map.entrySet())
148: r[e.getValue()] = e.getKey();
149: return r;
150: }
151: }
|