001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/datatypes/QualifiedName.java $ 002 /*---------------- FILE HEADER ------------------------------------------ 003 004 This file is part of deegree. 005 Copyright (C) 2001-2008 by: 006 EXSE, Department of Geography, University of Bonn 007 http://www.giub.uni-bonn.de/deegree/ 008 lat/lon GmbH 009 http://www.lat-lon.de 010 011 This library is free software; you can redistribute it and/or 012 modify it under the terms of the GNU Lesser General Public 013 License as published by the Free Software Foundation; either 014 version 2.1 of the License, or (at your option) any later version. 015 016 This library is distributed in the hope that it will be useful, 017 but WITHOUT ANY WARRANTY; without even the implied warranty of 018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 019 Lesser General Public License for more details. 020 021 You should have received a copy of the GNU Lesser General Public 022 License along with this library; if not, write to the Free Software 023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 024 025 Contact: 026 027 Andreas Poth 028 lat/lon GmbH 029 Aennchenstr. 19 030 53115 Bonn 031 Germany 032 E-Mail: poth@lat-lon.de 033 034 Prof. Dr. Klaus Greve 035 Department of Geography 036 University of Bonn 037 Meckenheimer Allee 166 038 53115 Bonn 039 Germany 040 E-Mail: greve@giub.uni-bonn.de 041 042 ---------------------------------------------------------------------------*/ 043 package org.deegree.datatypes; 044 045 import java.io.Serializable; 046 import java.net.URI; 047 048 import org.deegree.framework.util.StringTools; 049 050 /** 051 * This class represent a qualified name for something. A name is thought to be built from an 052 * optional prefix and/or a local name E.g.: <BR>- deegree - pre:deegree <BR> 053 * a name may be located within a namespace assigned to the names prefix (or as default namespace if 054 * the name has not prefix). 055 * 056 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a> 057 * @author last edited by: $Author: apoth $ 058 * 059 * @version $Revision: 9337 $, $Date: 2007-12-27 13:31:11 +0100 (Do, 27 Dez 2007) $ 060 */ 061 public class QualifiedName implements Serializable { 062 063 private static final long serialVersionUID = 5551137348397905772L; 064 065 private String prefix = null; 066 067 private String localName = null; 068 069 private URI namespace = null; 070 071 private String s = null; 072 073 /** 074 * @param name 075 * local name/simple (without prefix) 076 */ 077 public QualifiedName( String name ) { 078 if ( name.indexOf( ':' ) > -1 ) { 079 String[] tmp = StringTools.toArray( name, ":", false ); 080 if ( tmp.length == 2 ) { 081 prefix = tmp[0]; 082 localName = tmp[1]; 083 } else { 084 localName = name; 085 } 086 } else { 087 this.localName = name; 088 } 089 buildString(); 090 } 091 092 /** 093 * @param name 094 * complete name including a prefix 095 * @param namespace 096 * namespace the name is located within 097 */ 098 public QualifiedName( String name, URI namespace ) { 099 if ( name.indexOf( ':' ) > -1 ) { 100 String[] tmp = StringTools.toArray( name, ":", false ); 101 prefix = tmp[0]; 102 this.localName = tmp[1]; 103 } else { 104 this.localName = name; 105 } 106 this.namespace = namespace; 107 buildString(); 108 } 109 110 /** 111 * @param prefix 112 * @param localName 113 * local/simple name (e.g. deegree) 114 * @param namespace 115 * namespace the name is located within 116 */ 117 public QualifiedName( String prefix, String localName, URI namespace ) { 118 this.prefix = prefix; 119 this.localName = localName; 120 this.namespace = namespace; 121 buildString(); 122 } 123 124 private void buildString() { 125 StringBuffer sb = new StringBuffer( 50 ); 126 if ( prefix != null && prefix.length() != 0 ) { 127 sb.append( prefix ).append( ':' ); 128 } 129 sb.append( localName ); 130 s = sb.toString(); 131 } 132 133 /** 134 * returns a string representation of a QualifiedName. prefix and local name are separated by 135 * ':' 136 * 137 * @return string representation of a QualifiedName 138 * @deprecated use 139 * @see #getFormattedString() or 140 * @see #getPrefixedName() instead 141 */ 142 @Deprecated 143 public String getAsString() { 144 return s; 145 } 146 147 /** 148 * returns a string representation of a QualifiedName. prefix and local name are separated by 149 * ':'. If the Prefix is null, the sole localname will be returned. 150 * 151 * @return string representation of a QualifiedName 152 */ 153 public String getPrefixedName() { 154 return s; 155 } 156 157 /** 158 * @return a QualifiedName as a formatted string. If a QualifiedName has a namespace the 159 * returned format is:<br> 160 * {namespace}:localName. <br> 161 * Otherwise just a String representation of this qualified name will be returned. Which 162 * means, that if the prefix is not null (allthough not bound to namespace) the result 163 * String will be: <br> 164 * PRE_FIX:localName.<br> 165 * If the Prefix is null, the sole localname will be returned. 166 */ 167 public String getFormattedString() { 168 if ( namespace != null ) { 169 return StringTools.concat( 100, "{", namespace, "}:", localName ); 170 } 171 return s; 172 } 173 174 /** 175 * returns the names prefix 176 * 177 * @return the names prefix 178 */ 179 public String getPrefix() { 180 return prefix; 181 } 182 183 /** 184 * returns the local part of the name 185 * 186 * @return the local part of the name 187 */ 188 public String getLocalName() { 189 return localName; 190 } 191 192 /** 193 * returns the namespace the name is located within (may be null) 194 * 195 * @return the namespace the name is located within (may be null) 196 */ 197 public URI getNamespace() { 198 return namespace; 199 } 200 201 /** 202 * @param ns 203 * the namespace to checkfor 204 * @return true if the given namespace equals this qualified name's namespace. If the given ns 205 * is null and the namespace is null, this method will also return true. 206 */ 207 public boolean isInNamespace( URI ns ) { 208 if ( ns == null ) { 209 if ( this.namespace == null ) { 210 return true; 211 } 212 return false; 213 } 214 return ns.equals( this.namespace ); 215 } 216 217 @Override 218 public String toString() { 219 StringBuffer result = new StringBuffer( 150 ); 220 result.append( this.s ); 221 if ( this.prefix != null && this.prefix.length() > 0 ) { 222 result.append( " (" ); 223 result.append( this.prefix ); 224 result.append( "=" ); 225 if ( this.namespace == null || this.namespace.toASCIIString().length() == 0 ) { 226 result.append( "not bound to a namespace" ); 227 } else { 228 result.append( this.namespace.toASCIIString() ); 229 } 230 result.append( ")" ); 231 } 232 return result.toString(); 233 } 234 235 @Override 236 public int hashCode() { 237 return ( this.namespace + this.localName ).hashCode(); 238 } 239 240 @Override 241 public boolean equals( Object o ) { 242 // return false in the case that the object is null 243 // or isn't an instance of QualifiedName 244 if ( o == null || !( o instanceof QualifiedName ) ) { 245 return false; 246 } 247 248 QualifiedName other = (QualifiedName) o; 249 if ( localName.equals( other.getLocalName() ) ) { 250 if ( ( namespace != null && namespace.equals( other.getNamespace() ) ) 251 || ( namespace == null && other.getNamespace() == null ) ) { 252 return true; 253 } 254 } 255 return false; 256 } 257 }