Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGPE / src / org / gvsig / xmlschema / utils / QName.java @ 18770

History | View | Annotate | Download (6.55 KB)

1
package org.gvsig.xmlschema.utils;
2

    
3
        /* JBoss, the OpenSource WebOS
4
         *
5
         * Distributable under LGPL license.
6
         * See terms of license at gnu.org.
7
         */
8

    
9
        // $Id: QName.java,v 1.2.6.8 2005/04/12 03:35:26 starksm Exp $
10

    
11
        import java.io.Serializable;
12
        import java.util.StringTokenizer;
13
        
14
        /** QName represents an immutable qualified name.
15
         * The value of a QName contains a Namespace URI, local part and prefix.
16
         * 
17
         * The prefix is included in QName to retain lexical information when present
18
         * in an XML input source. The prefix is NOT used in QName.equals(Object) or
19
         * to compute the QName.hashCode(). Equality and the hash code are defined
20
         * using only the Namespace URI and local part.
21
         * 
22
         * If not specified, the Namespace URI is set to "" (the empty string).
23
         * If not specified, the prefix is set to "" (the empty string).
24
         * 
25
         * @author Scott.Stark@jboss.org
26
         * @author Thomas.Diesler@jboss.org
27
         * @author Jeff Suttor (javadoc)
28
         * @version $Revision: 1.2.6.8 $
29
         */
30
        public class QName implements Serializable
31
        {
32
           /** @since 4.0.2, compatible with jdk5 by default */
33
           final static long serialVersionUID;
34
           static
35
           {
36
                 serialVersionUID = -3852060120346905000L;
37
           }
38

    
39
           private String namespaceURI;
40
           private String localPart;
41
           private String prefix;
42

    
43
           /** QName derived from parsing the formatted String.
44
            * If the String is null or does not conform to QName.toString() formatting,
45
            * an IllegalArgumentException is thrown.
46
            * 
47
            * The String MUST be in the form returned by QName.toString(). There is NO
48
            * standard specification for representing a QName as a String. The String
49
            * format is NOT portable across implementations and will change when a
50
            * standard String representation is defined. This implementation currently
51
            * parses a String formatted as: "{" + Namespace URI + "}" + local part. If
52
            * the Namespace URI .equals(""), only the local part should be provided.
53
            * 
54
            * The prefix value CANNOT be represented in the String and will be set to ""
55
            * 
56
            * This method does not do full validation of the resulting QName. In
57
            * particular, the local part is not validated as a NCName as specified in
58
            * Namespaces in XML.
59
            * 
60
            * @see #toString()
61
            * @param toStringName - a QName string in the format of toString().
62
            * @return QName for the toStringName
63
            */
64
           public static QName valueOf(String toStringName)
65
           {
66
              String uri = null;
67
              String localPart = null;
68

    
69
              StringTokenizer tokenizer = new StringTokenizer(toStringName, "{}");
70
              int tokenCount = tokenizer.countTokens();
71

    
72
              if (tokenCount < 1 || tokenCount > 2)
73
                 throw new IllegalArgumentException("Invalid QName string: " + toStringName);
74

    
75
              if (tokenCount > 1)
76
                 uri = tokenizer.nextToken();
77

    
78
              localPart = tokenizer.nextToken();
79
              return new QName(uri, localPart);
80
           }
81

    
82
           public QName(String localPart)
83
           {
84
              this(null, localPart);
85
           }
86

    
87
           public QName(String namespaceURI, String localPart)
88
           {
89
              this(namespaceURI, localPart, "");
90
           }
91

    
92
           /** QName constructor specifying the Namespace URI, local part and prefix.
93
            * If the Namespace URI is null, it is set to "". This value represents no
94
            * explicitly defined Namespace as defined by the Namespaces in XML
95
            * specification. This action preserves compatible behavior with QName 1.0.
96
            * 
97
            * If the local part is null, an IllegalArgumentException is thrown.
98
            * 
99
            * If the prefix is null, an IllegalArgumentException is thrown. Use "" to
100
            * explicitly indicate that no prefix is present or the prefix is not
101
            * relevant.
102
            * 
103
            * @param namespaceURI - Namespace URI of the QName
104
            * @param localPart - local part of the QName
105
            * @param prefix - prefix of the QName
106
            */
107
           public QName(String namespaceURI, String localPart, String prefix)
108
           {
109
              this.namespaceURI = namespaceURI;
110
              if (this.namespaceURI == null)
111
                 this.namespaceURI = "";
112

    
113
              if (localPart == null)
114
                 throw new IllegalArgumentException("localPart cannot be null");
115

    
116
              if (localPart.startsWith(":"))
117
                 throw new IllegalArgumentException("Illegal localPart: " + localPart);
118

    
119
              this.localPart = localPart;
120

    
121
              this.prefix = prefix;
122
              if (this.prefix == null)
123
                 this.prefix = "";
124
           }
125

    
126
           public String getNamespaceURI()
127
           {
128
              return namespaceURI;
129
           }
130

    
131
           public String getLocalPart()
132
           {
133
              return localPart;
134
           }
135

    
136
           public String getPrefix()
137
           {
138
              return prefix;
139
           }
140

    
141
           /** There is NO standard specification for representing a QName as a String.
142
            * The returned String is not portable across implementations and will change when a standard String representation is defined.
143
            * This implementation currently represents a QName as: "{" + Namespace URI + "}" + local part.
144
            * If the Namespace URI .equals(""), only the local part is returned.
145
            * An appropriate use of this method is for debugging or logging for human consumption.
146
            *
147
            * Note the prefix value is NOT returned as part of the String representation.
148
            *
149
            * @return '{' + namespaceURI + '}' + localPart
150
            */
151
           public String toString()
152
           {
153
              if (namespaceURI.equals(""))
154
                 return localPart;
155
              else
156
                 return '{' + namespaceURI + '}' + localPart;
157
           }
158

    
159
           /** Equality is based on the namespaceURI and localPart
160
            * @param obj the QName to compare too
161
            * @return true if both namespaceURI and localPart, false otherwise
162
            */
163
           public boolean equals(Object obj)
164
           {
165
              if (obj instanceof QName)
166
              {
167
                 QName qn = (QName)obj;
168
                 boolean equals = namespaceURI.equals(qn.namespaceURI);
169
                 return equals && localPart.equals(qn.localPart);
170
              }
171
              return false;
172
           }
173

    
174
           /** Calculate the hash of namespaceURI and localPart 
175
            * @return namespaceURI.hashCode() + localPart.hashCode()
176
            */
177
           public int hashCode()
178
           {
179
              int hashCode = namespaceURI.hashCode() + localPart.hashCode();
180
              return hashCode;
181
           }
182

    
183
           /**
184
            * Compares this object with the specified object for order.  Returns a
185
            * negative integer, zero, or a positive integer as this object is less
186
            * than, equal to, or greater than the specified object.<p>
187
            */
188
           public int compareTo(Object o)
189
           {
190
              QName other = (QName)o;
191
              return toString().compareTo(other.toString());
192
           }
193
        }
194