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 public QName(String localName, String prefix) { 052 this.localName = localName.intern(); 053 if (prefix == null || prefix.length() == 0) { 054 this.prefix = ""; 055 prefixedName = this.localName; 056 } else { 057 this.prefix = prefix.intern(); 058 prefixedName = (prefix + ':' + localName).intern(); 059 } 060 } 061 062 public final String getLocalName() { 063 return localName; 064 } 065 066 public final String getPrefixedName() { 067 return prefixedName; 068 } 069 070 public final String getPrefix() { 071 return prefix; 072 } 073 074 @Override 075 public int hashCode() { 076 return prefixedName.hashCode(); 077 } 078 079 @Override 080 public String toString() { 081 return prefixedName; 082 } 083 084 @Override 085 public boolean equals(Object obj) { 086 if (obj == this) { 087 return true; 088 } 089 if (obj instanceof QName) { 090 return ((QName) obj).prefixedName.equals(prefixedName); 091 } 092 return false; 093 } 094 095 /** 096 * Parses the given name and creates the corresponding QName. 097 * <p> 098 * If the given name is not prefixed then the default prefix (e.g. "") will be used (i.e. the name will remain 099 * unprefixed). 100 * 101 * @param name the name in the prefixed form 102 * @return the qname 103 */ 104 public static QName valueOf(String name) { 105 return valueOf(name, ""); 106 } 107 108 /** 109 * Parses the given name and create the corresponding QName. 110 * <p> 111 * If the given name is not prefixed then the given prefix will be used. 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}