001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.schema.types;
016
017import java.io.Serializable;
018
019/**
020 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
021 */
022public class QName implements Serializable {
023
024    private static final long serialVersionUID = 5259846002688485463L;
025
026    final String prefix;
027
028    final String localName;
029
030    final String prefixedName;
031
032    /**
033     * Creates a QName without prefix (e.g. with the default prefix : "").
034     *
035     * @param localName the local name
036     */
037    public QName(String localName) {
038        this(localName, null);
039    }
040
041    /**
042     * Creates a QName having the given local name and prefix.
043     *
044     * @param localName
045     * @param prefix
046     */
047    public QName(String localName, String prefix) {
048        this.localName = localName.intern();
049        if (prefix == null || prefix.length() == 0) {
050            this.prefix = "";
051            prefixedName = this.localName;
052        } else {
053            this.prefix = prefix.intern();
054            prefixedName = (prefix + ':' + localName).intern();
055        }
056    }
057
058    public final String getLocalName() {
059        return localName;
060    }
061
062    public final String getPrefixedName() {
063        return prefixedName;
064    }
065
066    public final String getPrefix() {
067        return prefix;
068    }
069
070    @Override
071    public int hashCode() {
072        return prefixedName.hashCode();
073    }
074
075    @Override
076    public String toString() {
077        return prefixedName;
078    }
079
080    @Override
081    public boolean equals(Object obj) {
082        if (obj == this) {
083            return true;
084        }
085        if (obj instanceof QName) {
086            return ((QName) obj).prefixedName.equals(prefixedName);
087        }
088        return false;
089    }
090
091    /**
092     * Parses the given name and creates the corresponding QName.
093     * <p>
094     * If the given name is not prefixed then the default prefix (e.g. "") will be used (i.e. the name will remain
095     * unprefixed).
096     *
097     * @param name the name in the prefixed form
098     * @return the qname
099     */
100    public static QName valueOf(String name) {
101        return valueOf(name, "");
102    }
103
104    /**
105     * Parses the given name and create the corresponding QName.
106     * <p>
107     * If the given name is not prefixed then the given prefix will be used.
108     *
109     * @param name
110     * @param prefix
111     * @return
112     */
113    public static QName valueOf(String name, String prefix) {
114        int p = name.indexOf(':');
115        if (p > -1) {
116            return new QName(name.substring(p + 1), name.substring(0, p));
117        } else {
118            return new QName(name, prefix);
119        }
120    }
121
122}