KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > util > QName


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id$ */
19
20 package org.apache.fop.util;
21
22 import java.io.Serializable JavaDoc;
23
24 /**
25  * Represents a qualified name of an XML element or an XML attribute.
26  * <p>
27  * Note: This class allows to carry a namespace prefix but it is not used in the equals() and
28  * hashCode() methods.
29  */

30 public class QName implements Serializable JavaDoc {
31
32     private static final long serialVersionUID = -5225376740044770690L;
33     
34     private String JavaDoc namespaceURI;
35     private String JavaDoc localName;
36     private String JavaDoc prefix;
37     private int hashCode;
38     
39     /**
40      * Main constructor.
41      * @param namespaceURI the namespace URI
42      * @param prefix the namespace prefix, may be null
43      * @param localName the local name
44      */

45     public QName(String JavaDoc namespaceURI, String JavaDoc prefix, String JavaDoc localName) {
46         if (localName == null) {
47             throw new NullPointerException JavaDoc("Parameter localName must not be null");
48         }
49         if (localName.length() == 0) {
50             throw new IllegalArgumentException JavaDoc("Parameter localName must not be empty");
51         }
52         this.namespaceURI = namespaceURI;
53         this.prefix = prefix;
54         this.localName = localName;
55         this.hashCode = toHashString().hashCode();
56     }
57     
58     /**
59      * Main constructor.
60      * @param namespaceURI the namespace URI
61      * @param qName the qualified name
62      */

63     public QName(String JavaDoc namespaceURI, String JavaDoc qName) {
64         if (qName == null) {
65             throw new NullPointerException JavaDoc("Parameter localName must not be null");
66         }
67         if (qName.length() == 0) {
68             throw new IllegalArgumentException JavaDoc("Parameter localName must not be empty");
69         }
70         this.namespaceURI = namespaceURI;
71         int p = qName.indexOf(':');
72         if (p > 0) {
73             this.prefix = qName.substring(0, p);
74             this.localName = qName.substring(p + 1);
75         } else {
76             this.prefix = null;
77             this.localName = qName;
78         }
79         this.hashCode = toHashString().hashCode();
80     }
81     
82     /** @return the namespace URI */
83     public String JavaDoc getNamespaceURI() {
84         return this.namespaceURI;
85     }
86     
87     /** @return the namespace prefix */
88     public String JavaDoc getPrefix() {
89         return this.prefix;
90     }
91     
92     /** @return the local name */
93     public String JavaDoc getLocalName() {
94         return this.localName;
95     }
96     
97     /** @return the fully qualified name */
98     public String JavaDoc getQName() {
99         return getPrefix() != null ? getPrefix() + ':' + getLocalName() : getLocalName();
100     }
101
102     /** @see java.lang.Object#hashCode() */
103     public int hashCode() {
104         return this.hashCode;
105     }
106
107     /** @see java.lang.Object#equals(java.lang.Object) */
108     public boolean equals(Object JavaDoc obj) {
109         if (obj == null) {
110             return false;
111         } else if (obj == this) {
112             return true;
113         } else {
114             if (obj instanceof QName) {
115                 QName other = (QName)obj;
116                 if ((getNamespaceURI() == null && other.getNamespaceURI() == null)
117                         || getNamespaceURI().equals(other.getNamespaceURI())) {
118                     return getLocalName().equals(other.getLocalName());
119                 }
120             }
121         }
122         return false;
123     }
124
125     /** @see java.lang.Object#toString() */
126     public String JavaDoc toString() {
127         return prefix != null
128                 ? (prefix + ":" + localName)
129                 : toHashString();
130     }
131
132     private String JavaDoc toHashString() {
133         return (namespaceURI != null
134                 ? ("{" + namespaceURI + "}" + localName)
135                 : localName);
136     }
137
138 }
139
Popular Tags