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.unmarshaller;
038:
039: import javax.xml.namespace.QName;
040:
041: import com.sun.xml.bind.v2.runtime.Name;
042:
043: import org.xml.sax.Attributes;
044:
045: /**
046: * Represents an XML tag name (and attributes for start tags.)
047: *
048: * <p>
049: * This object is used so reduce the number of method call parameters
050: * among unmarshallers.
051: *
052: * An instance of this is expected to be reused by the caller of
053: * {@link XmlVisitor}. Note that the rest of the unmarshaller may
054: * modify any of the fields while processing an event (such as to
055: * intern strings, replace attributes),
056: * so {@link XmlVisitor} should reset all fields for each use.
057: *
058: * <p>
059: * The 'qname' parameter, which holds the qualified name of the tag
060: * (such as 'foo:bar' or 'zot'), is not used in the typical unmarshalling
061: * route and it's also expensive to compute for some input.
062: * Thus this parameter is computed lazily.
063: *
064: * @author Kohsuke Kawaguchi
065: */
066: @SuppressWarnings({"StringEquality"})
067: public abstract class TagName {
068: /**
069: * URI of the attribute/element name.
070: *
071: * Can be empty, but never null. Interned.
072: */
073: public String uri;
074: /**
075: * Local part of the attribute/element name.
076: *
077: * Never be null. Interned.
078: */
079: public String local;
080:
081: /**
082: * Used only for the enterElement event.
083: * Otherwise the value is undefined.
084: *
085: * This might be {@link AttributesEx}.
086: */
087: public Attributes atts;
088:
089: public TagName() {
090: }
091:
092: /**
093: * Checks if the given name pair matches this name.
094: */
095: public final boolean matches(String nsUri, String local) {
096: return this .uri == nsUri && this .local == local;
097: }
098:
099: /**
100: * Checks if the given name pair matches this name.
101: */
102: public final boolean matches(Name name) {
103: return this .local == name.localName && this .uri == name.nsUri;
104: }
105:
106: // /**
107: // * @return
108: // * Can be empty but always non-null. NOT interned.
109: // */
110: // public final String getPrefix() {
111: // int idx = qname.indexOf(':');
112: // if(idx<0) return "";
113: // else return qname.substring(0,idx);
114: // }
115:
116: public String toString() {
117: return '{' + uri + '}' + local;
118: }
119:
120: /**
121: * Gets the qualified name of the tag.
122: *
123: * @return never null.
124: */
125: public abstract String getQname();
126:
127: /**
128: * Gets the prefix. This is slow.
129: *
130: * @return can be "" but never null.
131: */
132: public String getPrefix() {
133: String qname = getQname();
134: int idx = qname.indexOf(':');
135: if (idx < 0)
136: return "";
137: else
138: return qname.substring(0, idx);
139: }
140:
141: /**
142: * Creates {@link QName}.
143: */
144: public QName createQName() {
145: return new QName(uri, local, getPrefix());
146: }
147: }
|