001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.core.schema.types;
023
024import java.io.Serializable;
025
026/**
027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
028 */
029public class QName implements Serializable {
030
031    private static final long serialVersionUID = 5259846002688485463L;
032
033    final String prefix;
034
035    final String localName;
036
037    final String prefixedName;
038
039    /**
040     * Creates a QName without prefix (e.g. with the default prefix : "").
041     *
042     * @param localName the local name
043     */
044    public QName(String localName) {
045        this(localName, null);
046    }
047
048    /**
049     * Creates a QName having the given local name and prefix.
050     *
051     * @param localName
052     * @param prefix
053     */
054    public QName(String localName, String prefix) {
055        this.localName = localName.intern();
056        if (prefix == null || prefix.length() == 0) {
057            this.prefix = "";
058            prefixedName = this.localName;
059        } else {
060            this.prefix = prefix.intern();
061            prefixedName = (prefix + ':' + localName).intern();
062        }
063    }
064
065    public final String getLocalName() {
066        return localName;
067    }
068
069    public final String getPrefixedName() {
070        return prefixedName;
071    }
072
073    public final String getPrefix() {
074        return prefix;
075    }
076
077    @Override
078    public int hashCode() {
079        return prefixedName.hashCode();
080    }
081
082    @Override
083    public String toString() {
084        return prefixedName;
085    }
086
087    @Override
088    public boolean equals(Object obj) {
089        if (obj == this) {
090            return true;
091        }
092        if (obj instanceof QName) {
093            return ((QName) obj).prefixedName.equals(prefixedName);
094        }
095        return false;
096    }
097
098    /**
099     * Parses the given name and creates the corresponding QName.
100     * <p>
101     * If the given name is not prefixed then the default prefix (e.g. "") will be used (i.e. the name will remain
102     * unprefixed).
103     *
104     * @param name the name in the prefixed form
105     * @return the qname
106     */
107    public static QName valueOf(String name) {
108        return valueOf(name, "");
109    }
110
111    /**
112     * Parses the given name and create the corresponding QName.
113     * <p>
114     * If the given name is not prefixed then the given prefix will be used.
115     *
116     * @param name
117     * @param prefix
118     * @return
119     */
120    public static QName valueOf(String name, String prefix) {
121        int p = name.indexOf(':');
122        if (p > -1) {
123            return new QName(name.substring(p + 1), name.substring(0, p));
124        } else {
125            return new QName(name, prefix);
126        }
127    }
128
129}