~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to src/java/org/apache/xmlgraphics/util/QName.java

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-02-11 14:15:14 UTC
  • mfrom: (8.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110211141514-h67achft6x31gju1
Tags: 1.4.dfsg-3
Uploading to unstable, hoping we won't break too many things ;-)...

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.xmlgraphics.util;
21
 
 
22
 
import java.io.Serializable;
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 {
31
 
 
32
 
    private static final long serialVersionUID = -5225376740044770690L;
33
 
    
34
 
    private String namespaceURI;
35
 
    private String localName;
36
 
    private String 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 namespaceURI, String prefix, String localName) {
46
 
        if (localName == null) {
47
 
            throw new NullPointerException("Parameter localName must not be null");
48
 
        }
49
 
        if (localName.length() == 0) {
50
 
            throw new IllegalArgumentException("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 namespaceURI, String qName) {
64
 
        if (qName == null) {
65
 
            throw new NullPointerException("Parameter localName must not be null");
66
 
        }
67
 
        if (qName.length() == 0) {
68
 
            throw new IllegalArgumentException("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 getNamespaceURI() {
84
 
        return this.namespaceURI;
85
 
    }
86
 
    
87
 
    /** @return the namespace prefix */
88
 
    public String getPrefix() {
89
 
        return this.prefix;
90
 
    }
91
 
    
92
 
    /** @return the local name */
93
 
    public String getLocalName() {
94
 
        return this.localName;
95
 
    }
96
 
    
97
 
    /** @return the fully qualified name */
98
 
    public String 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 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 toString() {
127
 
        return prefix != null
128
 
                ? (prefix + ":" + localName)
129
 
                : toHashString();
130
 
    }
131
 
 
132
 
    private String toHashString() {
133
 
        return (namespaceURI != null 
134
 
                ? ("{" + namespaceURI + "}" + localName) 
135
 
                : localName);
136
 
    }
137
 
 
138
 
}
 
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: QName.java 750418 2009-03-05 11:03:54Z vhennebert $ */
 
19
 
 
20
package org.apache.xmlgraphics.util;
 
21
 
 
22
import java.io.Serializable;
 
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 {
 
31
 
 
32
    private static final long serialVersionUID = -5225376740044770690L;
 
33
 
 
34
    private String namespaceURI;
 
35
    private String localName;
 
36
    private String 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 namespaceURI, String prefix, String localName) {
 
46
        if (localName == null) {
 
47
            throw new NullPointerException("Parameter localName must not be null");
 
48
        }
 
49
        if (localName.length() == 0) {
 
50
            throw new IllegalArgumentException("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 namespaceURI, String qName) {
 
64
        if (qName == null) {
 
65
            throw new NullPointerException("Parameter localName must not be null");
 
66
        }
 
67
        if (qName.length() == 0) {
 
68
            throw new IllegalArgumentException("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 getNamespaceURI() {
 
84
        return this.namespaceURI;
 
85
    }
 
86
 
 
87
    /** @return the namespace prefix */
 
88
    public String getPrefix() {
 
89
        return this.prefix;
 
90
    }
 
91
 
 
92
    /** @return the local name */
 
93
    public String getLocalName() {
 
94
        return this.localName;
 
95
    }
 
96
 
 
97
    /** @return the fully qualified name */
 
98
    public String 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 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 toString() {
 
127
        return prefix != null
 
128
                ? (prefix + ":" + localName)
 
129
                : toHashString();
 
130
    }
 
131
 
 
132
    private String toHashString() {
 
133
        return (namespaceURI != null
 
134
                ? ("{" + namespaceURI + "}" + localName)
 
135
                : localName);
 
136
    }
 
137
 
 
138
}