001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.bind.v2.runtime.unmarshaller;
027:
028: import javax.xml.namespace.QName;
029:
030: import com.sun.xml.internal.bind.v2.runtime.Name;
031:
032: import org.xml.sax.Attributes;
033:
034: /**
035: * Represents an XML tag name (and attributes for start tags.)
036: *
037: * <p>
038: * This object is used so reduce the number of method call parameters
039: * among unmarshallers.
040: *
041: * An instance of this is expected to be reused by the caller of
042: * {@link XmlVisitor}. Note that the rest of the unmarshaller may
043: * modify any of the fields while processing an event (such as to
044: * intern strings, replace attributes),
045: * so {@link XmlVisitor} should reset all fields for each use.
046: *
047: * <p>
048: * The 'qname' parameter, which holds the qualified name of the tag
049: * (such as 'foo:bar' or 'zot'), is not used in the typical unmarshalling
050: * route and it's also expensive to compute for some input.
051: * Thus this parameter is computed lazily.
052: *
053: * @author Kohsuke Kawaguchi
054: */
055: @SuppressWarnings({"StringEquality"})
056: public abstract class TagName {
057: /**
058: * URI of the attribute/element name.
059: *
060: * Can be empty, but never null. Interned.
061: */
062: public String uri;
063: /**
064: * Local part of the attribute/element name.
065: *
066: * Never be null. Interned.
067: */
068: public String local;
069:
070: /**
071: * Used only for the enterElement event.
072: * Otherwise the value is undefined.
073: *
074: * This might be {@link AttributesEx}.
075: */
076: public Attributes atts;
077:
078: public TagName() {
079: }
080:
081: /**
082: * Checks if the given name pair matches this name.
083: */
084: public final boolean matches(String nsUri, String local) {
085: return this .uri == nsUri && this .local == local;
086: }
087:
088: /**
089: * Checks if the given name pair matches this name.
090: */
091: public final boolean matches(Name name) {
092: return this .local == name.localName && this .uri == name.nsUri;
093: }
094:
095: // /**
096: // * @return
097: // * Can be empty but always non-null. NOT interned.
098: // */
099: // public final String getPrefix() {
100: // int idx = qname.indexOf(':');
101: // if(idx<0) return "";
102: // else return qname.substring(0,idx);
103: // }
104:
105: public String toString() {
106: return '{' + uri + '}' + local;
107: }
108:
109: /**
110: * Gets the qualified name of the tag.
111: *
112: * @return never null.
113: */
114: public abstract String getQname();
115:
116: /**
117: * Gets the prefix. This is slow.
118: *
119: * @return can be "" but never null.
120: */
121: public String getPrefix() {
122: String qname = getQname();
123: int idx = qname.indexOf(':');
124: if (idx < 0)
125: return "";
126: else
127: return qname.substring(0, idx);
128: }
129:
130: /**
131: * Creates {@link QName}.
132: */
133: public QName createQName() {
134: return new QName(uri, local, getPrefix());
135: }
136: }
|