    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements.  See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache License, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License.  You may obtain a copy of the License at
    008     *
    009     *     http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the License for the specific language governing permissions and
    015     * limitations under the License.
    016     */
    017    package org.apache.commons.jxpath.ri;
    018
    019    import java.io.Serializable;
    020
    021
    022    /**
    023     * A qualified name: a combination of an optional namespace prefix
    024     * and an local name.
    025     *
    026     * @author Dmitri Plotnikov
    027     * @version $Revision: 652925 $ $Date: 2008-05-02 17:05:41 -0500 (Fri, 02 May 2008) $
    028     */
    029    public class QName implements Serializable {
    030        private static final long serialVersionUID = 7616199282015091496L;
    031
    032        private String prefix;
    033        private String name;
    034        private String qualifiedName;
    035
    036        /**
    037         * Create a new QName.
    038         * @param qualifiedName value
    039         */
    040        public QName(String qualifiedName) {
    041            this.qualifiedName = qualifiedName;
    042            int index = qualifiedName.indexOf(':');
    043            prefix = index < 0 ? null : qualifiedName.substring(0, index);
    044            name = index < 0 ? qualifiedName : qualifiedName.substring(index + 1);
    045        }
    046
    047        /**
    048         * Create a new QName.
    049         * @param prefix ns
    050         * @param localName String
    051         */
    052        public QName(String prefix, String localName) {
    053            this.prefix = prefix;
    054            this.name = localName;
    055            this.qualifiedName = prefix == null ? localName : prefix + ':' + localName;
    056        }
    057
    058        /**
    059         * Get the prefix of this QName.
    060         * @return String
    061         */
    062        public String getPrefix() {
    063            return prefix;
    064        }
    065
    066        /**
    067         * Get the local name.
    068         * @return String
    069         */
    070        public String getName() {
    071            return name;
    072        }
    073
    074        public String toString() {
    075            return qualifiedName;
    076        }
    077
    078        public int hashCode() {
    079            return name.hashCode();
    080        }
    081
    082        public boolean equals(Object object) {
    083            if (this == object) {
    084                return true;
    085            }
    086            if (!(object instanceof QName)) {
    087                return false;
    088            }
    089            return qualifiedName.equals(((QName) object).qualifiedName);
    090        }
    091    }