001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/datatypes/QualifiedName.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2006 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: 6822 $, $Date: 2007-05-04 16:42:27 +0200 (Fr, 04 Mai 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                prefix = tmp[0];
081                this.localName = tmp[1];
082            } else {
083                this.localName = name;
084            }
085            buildString();
086        }
087    
088        /**
089         * @param name
090         *            complete name including a prefix
091         * @param namespace
092         *            namespace the name is located within
093         */
094        public QualifiedName( String name, URI namespace ) {
095            if ( name.indexOf( ':' ) > -1 ) {
096                String[] tmp = StringTools.toArray( name, ":", false );
097                prefix = tmp[0];
098                this.localName = tmp[1];
099            } else {
100                this.localName = name;
101            }
102            this.namespace = namespace;
103            buildString();
104        }
105    
106        /**
107         * @param prefix
108         * @param localName
109         *            local/simple name (e.g. deegree)
110         * @param namespace
111         *            namespace the name is located within
112         */
113        public QualifiedName( String prefix, String localName, URI namespace ) {
114            this.prefix = prefix;
115            this.localName = localName;
116            this.namespace = namespace;
117            buildString();
118        }
119    
120        private void buildString() {
121            StringBuffer sb = new StringBuffer( 50 );
122            if ( prefix != null && prefix.length() != 0 ) {
123                sb.append( prefix ).append( ':' );
124            }
125            sb.append( localName );
126            s = sb.toString();
127        }
128    
129        /**
130         * returns a string representation of a QualifiedName. prefix and local name are separated by
131         * ':'
132         * 
133         * @return string representation of a QualifiedName
134         * @deprecated use
135         * @see #getFormattedString() or @see #getPrefixedName() instead
136         */
137        @Deprecated
138        public String getAsString() {
139            return s;
140        }
141        
142        /**
143         * returns a string representation of a QualifiedName. prefix and local name are separated by
144         * ':'. If the Prefix is null, the sole localname will be returned.
145         * @return string representation of a QualifiedName
146         */
147        public String getPrefixedName() {
148            return s;
149        }
150    
151        /**
152         * @return a QualifiedName as a formatted string. If a QualifiedName has a namespace the
153         *         returned format is:<br>
154         *         {namespace}:localName. <br>
155         *         Otherwise just a String representation of this qualified name will be returned. Which
156         *         means, that if the prefix is not null (allthough not bound to namespace) the result
157         *         String will be: <br>
158         *         PRE_FIX:localName.<br>
159         *         If the Prefix is null, the sole localname will be returned.
160         */
161        public String getFormattedString() {
162            if ( namespace != null ) {
163                return StringTools.concat( 100, "{", namespace, "}:", localName );
164            }
165            return s;
166        }
167    
168        /**
169         * returns the names prefix
170         * 
171         * @return the names prefix
172         */
173        public String getPrefix() {
174            return prefix;
175        }
176    
177        /**
178         * returns the local part of the name
179         * 
180         * @return the local part of the name
181         */
182        public String getLocalName() {
183            return localName;
184        }
185    
186        /**
187         * returns the namespace the name is located within (may be null)
188         * 
189         * @return the namespace the name is located within (may be null)
190         */
191        public URI getNamespace() {
192            return namespace;
193        }
194    
195        /**
196         * @param ns
197         *            the namespace to checkfor
198         * @return true if the given namespace equals this qualified name's namespace. If the given ns
199         *         is null and the namespace is null, this method will also return true.
200         */
201        public boolean isInNamespace( URI ns ) {
202            if ( ns == null ) {
203                if ( this.namespace == null ) {
204                    return true;
205                }
206                return false;
207            }
208            return ns.equals( this.namespace );
209        }
210    
211        @Override
212        public String toString() {
213            StringBuffer result = new StringBuffer( 150 );
214            result.append( this.s );
215            if ( this.prefix != null && this.prefix.length() > 0 ) {
216                result.append( " (" );
217                result.append( this.prefix );
218                result.append( "=" );
219                if ( this.namespace == null || this.namespace.toASCIIString().length() == 0 ) {
220                    result.append( "not bound to a namespace" );
221                } else {
222                    result.append( this.namespace.toASCIIString() );
223                }
224                result.append( ")" );
225            }
226            return result.toString();
227        }
228    
229        @Override
230        public int hashCode() {
231            return ( this.namespace + this.localName ).hashCode();
232        }
233    
234        @Override
235        public boolean equals( Object o ) {
236            // return false in the case that the object is null
237            // or isn't an instance of QualifiedName
238            if ( o == null || !( o instanceof QualifiedName ) ) {
239                return false;
240            }
241    
242            QualifiedName other = (QualifiedName) o;
243            if ( localName.equals( other.getLocalName() ) ) {
244                if ( ( namespace != null && namespace.equals( other.getNamespace() ) )
245                     || ( namespace == null && other.getNamespace() == null ) ) {
246                    return true;
247                }
248            }
249            return false;
250        }
251    }