1 /*
2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.xml.internal.bind.v2.runtime.unmarshaller;
27 
28 import javax.xml.namespace.QName;
29 
30 import com.sun.xml.internal.bind.v2.runtime.Name;
31 
32 import org.xml.sax.Attributes;
33 
34 /**
35  * Represents an XML tag name (and attributes for start tags.)
36  *
37  * <p>
38  * This object is used so reduce the number of method call parameters
39  * among unmarshallers.
40  *
41  * An instance of this is expected to be reused by the caller of
42  * {@link XmlVisitor}. Note that the rest of the unmarshaller may
43  * modify any of the fields while processing an event (such as to
44  * intern strings, replace attributes),
45  * so {@link XmlVisitor} should reset all fields for each use.
46  *
47  * <p>
48  * The 'qname' parameter, which holds the qualified name of the tag
49  * (such as 'foo:bar' or 'zot'), is not used in the typical unmarshalling
50  * route and it's also expensive to compute for some input.
51  * Thus this parameter is computed lazily.
52  *
53  * @author Kohsuke Kawaguchi
54  */
55 @SuppressWarnings({"StringEquality"})
56 public abstract class TagName {
57     /**
58      * URI of the attribute/element name.
59      *
60      * Can be empty, but never null. Interned.
61      */
62     public String uri;
63     /**
64      * Local part of the attribute/element name.
65      *
66      * Never be null. Interned.
67      */
68     public String local;
69 
70     /**
71      * Used only for the enterElement event.
72      * Otherwise the value is undefined.
73      *
74      * This might be {@link AttributesEx}.
75      */
76     public Attributes atts;
77 
TagName()78     public TagName() {
79     }
80 
81     /**
82      * Checks if the given name pair matches this name.
83      */
matches( String nsUri, String local )84     public final boolean matches( String nsUri, String local ) {
85         return this.uri==nsUri && this.local==local;
86     }
87 
88     /**
89      * Checks if the given name pair matches this name.
90      */
matches( Name name )91     public final boolean matches( Name name ) {
92         return this.local==name.localName && this.uri==name.nsUri;
93     }
94 
95 //    /**
96 //     * @return
97 //     *      Can be empty but always non-null. NOT interned.
98 //     */
99 //    public final String getPrefix() {
100 //        int idx = qname.indexOf(':');
101 //        if(idx<0)   return "";
102 //        else        return qname.substring(0,idx);
103 //    }
104 
toString()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      */
getQname()114     public abstract String getQname();
115 
116     /**
117      * Gets the prefix. This is slow.
118      *
119      * @return can be "" but never null.
120      */
getPrefix()121     public String getPrefix() {
122         String qname = getQname();
123         int idx = qname.indexOf(':');
124         if(idx<0)   return "";
125         else        return qname.substring(0,idx);
126     }
127 
128     /**
129      * Creates {@link QName}.
130      */
createQName()131     public QName createQName() {
132         return new QName(uri,local,getPrefix());
133     }
134 }
135