1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * The Apache Software License, Version 1.1
   7  *
   8  *
   9  * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
  10  * reserved.
  11  *
  12  * Redistribution and use in source and binary forms, with or without
  13  * modification, are permitted provided that the following conditions
  14  * are met:
  15  *
  16  * 1. Redistributions of source code must retain the above copyright
  17  *    notice, this list of conditions and the following disclaimer.
  18  *
  19  * 2. Redistributions in binary form must reproduce the above copyright
  20  *    notice, this list of conditions and the following disclaimer in
  21  *    the documentation and/or other materials provided with the
  22  *    distribution.
  23  *
  24  * 3. The end-user documentation included with the redistribution,
  25  *    if any, must include the following acknowledgment:
  26  *       "This product includes software developed by the
  27  *        Apache Software Foundation (http://www.apache.org/)."
  28  *    Alternately, this acknowledgment may appear in the software itself,
  29  *    if and wherever such third-party acknowledgments normally appear.
  30  *
  31  * 4. The names "Xerces" and "Apache Software Foundation" must
  32  *    not be used to endorse or promote products derived from this
  33  *    software without prior written permission. For written
  34  *    permission, please contact apache@apache.org.
  35  *
  36  * 5. Products derived from this software may not be called "Apache",
  37  *    nor may "Apache" appear in their name, without prior written
  38  *    permission of the Apache Software Foundation.
  39  *
  40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  43  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  46  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51  * SUCH DAMAGE.
  52  * ====================================================================
  53  *
  54  * This software consists of voluntary contributions made by many
  55  * individuals on behalf of the Apache Software Foundation and was
  56  * originally based on software copyright (c) 1999, International
  57  * Business Machines, Inc., http://www.apache.org.  For more
  58  * information on the Apache Software Foundation, please see
  59  * <http://www.apache.org/>.
  60  */
  61 
  62 package com.sun.org.apache.xerces.internal.xni;
  63 
  64 /**
  65  * A structure that holds the components of an XML Namespaces qualified
  66  * name.
  67  * <p>
  68  * To be used correctly, the strings must be identical references for
  69  * equal strings. Within the parser, these values are considered symbols
  70  * and should always be retrieved from the <code>SymbolTable</code>.
  71  *
  72  * @see <a href="../../../../../xerces2/com/sun/org/apache/xerces/internal/util/SymbolTable.html">com.sun.org.apache.xerces.internal.util.SymbolTable</a>
  73  *
  74  * @author Andy Clark, IBM
  75  *
  76  * Better performance patch for the equals method by Daniel Petersson: refer to jaxp issue 61;
  77  * == were used to compare strings
  78  * @author Joe Wang, Oracle
  79  *
  80  * @version $Id: QName.java,v 1.6 2010/03/18 19:32:31 joehw Exp $
  81  */
  82 public class QName
  83 implements Cloneable {
  84 
  85 
  86     /**
  87      * The qname prefix. For example, the prefix for the qname "a:foo"
  88      * is "a".
  89      */
  90     public String prefix;
  91 
  92     /**
  93      * The qname localpart. For example, the localpart for the qname "a:foo"
  94      * is "foo".
  95      */
  96     public String localpart;
  97 
  98     /**
  99      * The qname rawname. For example, the rawname for the qname "a:foo"
 100      * is "a:foo".
 101      */
 102     public String rawname;
 103 
 104     /**
 105      * The URI to which the qname prefix is bound. This binding must be
 106      * performed by a XML Namespaces aware processor.
 107      */
 108     public String uri;
 109 
 110     //
 111     // Constructors
 112     //
 113 
 114     /** Default constructor. */
 115     public QName() {
 116         clear();
 117     } // <init>()
 118 
 119     /** Constructs a QName with the specified values. */
 120     public QName(String prefix, String localpart, String rawname, String uri) {
 121         setValues(prefix, localpart, rawname, uri);
 122     } // <init>(String,String,String,String)
 123 
 124     /** Constructs a copy of the specified QName. */
 125     public QName(QName qname) {
 126         setValues(qname);
 127     } // <init>(QName)
 128 
 129     //
 130     // Public methods
 131     //
 132 
 133     /**
 134      * Convenience method to set the values of the qname components.
 135      *
 136      * @param QName The qualified name to be copied.
 137      */
 138     public void setValues(QName qname) {
 139         prefix = qname.prefix;
 140         localpart = qname.localpart;
 141         rawname = qname.rawname;
 142         uri = qname.uri;
 143     } // setValues(QName)
 144 
 145     /**
 146      * Convenience method to set the values of the qname components.
 147      *
 148      * @param prefix    The qname prefix. (e.g. "a")
 149      * @param localpart The qname localpart. (e.g. "foo")
 150      * @param rawname   The qname rawname. (e.g. "a:foo")
 151      * @param uri       The URI binding. (e.g. "http://foo.com/mybinding")
 152      */
 153     public void setValues(String prefix, String localpart, String rawname,
 154     String uri) {
 155         this.prefix = prefix;
 156         this.localpart = localpart;
 157         this.rawname = rawname;
 158         this.uri = uri;
 159     } // setValues(String,String,String,String)
 160 
 161     /** Clears the values of the qname components. */
 162     public void clear() {
 163         prefix = null;
 164         localpart = null;
 165         rawname = null;
 166         uri = null;
 167     } // clear()
 168 
 169     //
 170     // Cloneable methods
 171     //
 172 
 173     /** Returns a clone of this object. */
 174     public Object clone() {
 175         return new QName(this);
 176     } // clone():Object
 177 
 178     //
 179     // Object methods
 180     //
 181 
 182     /** Returns the hashcode for this object. */
 183     public int hashCode() {
 184         if (uri != null) {
 185             return uri.hashCode() +
 186                 ((localpart != null) ? localpart.hashCode() : 0);
 187         }
 188         return (rawname != null) ? rawname.hashCode() : 0;
 189     } // hashCode():int
 190 
 191     /** Returns true if the two objects are equal. */
 192     public boolean equals(Object object) {
 193         if (object == this) {
 194             return true;
 195         }
 196 
 197         if (object != null && object instanceof QName) {
 198             QName qname = (QName)object;
 199             if (qname.uri != null) {
 200                     return qname.localpart.equals(localpart) && qname.uri.equals(uri);
 201             }
 202             else if (uri == null) {
 203                 return rawname.equals(qname.rawname);
 204             }
 205             // fall through and return not equal
 206         }
 207         return false;
 208     } // equals(Object):boolean
 209 
 210     /** Returns a string representation of this object. */
 211     public String toString() {
 212 
 213         StringBuffer str = new StringBuffer();
 214         boolean comma = false;
 215         if (prefix != null) {
 216             str.append("prefix=\""+prefix+'"');
 217             comma = true;
 218         }
 219         if (localpart != null) {
 220             if (comma) {
 221                 str.append(',');
 222             }
 223             str.append("localpart=\""+localpart+'"');
 224             comma = true;
 225         }
 226         if (rawname != null) {
 227             if (comma) {
 228                 str.append(',');
 229             }
 230             str.append("rawname=\""+rawname+'"');
 231             comma = true;
 232         }
 233         if (uri != null) {
 234             if (comma) {
 235                 str.append(',');
 236             }
 237             str.append("uri=\""+uri+'"');
 238         }
 239         return str.toString();
 240 
 241     } // toString():String
 242 
 243 } // class QName