KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > xml > Namespace


1 package org.sapia.util.xml;
2
3
4 // Import of Sun's JDK classes
5
// ---------------------------
6
import java.io.Serializable JavaDoc;
7
8
9 /**
10  * The <CODE>Namespace</CODE> class is an object representation of a XML namespace
11  * definition. It has two arguments: a URI and a prefix.
12  *
13  * @author Jean-Cedric Desrochers
14  * <dl>
15  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
16  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
17  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
18  * </dl>
19  */

20 public class Namespace implements Serializable JavaDoc {
21   /////////////////////////////////////////////////////////////////////////////////////////
22
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
23
/////////////////////////////////////////////////////////////////////////////////////////
24

25   /** The URI of this namespace. */
26   private String JavaDoc _theURI;
27
28   /** The prefix associated to this namespace. */
29   private String JavaDoc _thePrefix;
30
31   /** The hash code of this namespace. */
32   private int _theHashCode;
33
34   /////////////////////////////////////////////////////////////////////////////////////////
35
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
36
/////////////////////////////////////////////////////////////////////////////////////////
37

38   /**
39    * Creates a new Namespace instance.
40    */

41   public Namespace() {
42     generateHashCode();
43   }
44
45   /**
46    * Creates a new Namespace instance with the passed in arguments.
47    *
48    * @param anURI The URI of this namespace.
49    * @param aPrefix The prefix of this namespace.
50    */

51   public Namespace(String JavaDoc anURI, String JavaDoc aPrefix) {
52     _theURI = anURI;
53     _thePrefix = aPrefix;
54     generateHashCode();
55   }
56
57   /////////////////////////////////////////////////////////////////////////////////////////
58
////////////////////////////////// ACCESSOR METHODS ///////////////////////////////////
59
/////////////////////////////////////////////////////////////////////////////////////////
60

61   /**
62    * Returns the URI of this namespace.
63    *
64    * @return The URI of this namespace.
65    */

66   public String JavaDoc getURI() {
67     return _theURI;
68   }
69
70   /**
71    * Returns the prefix of this namespace.
72    *
73    * @return The prefix of this namespace.
74    */

75   public String JavaDoc getPrefix() {
76     return _thePrefix;
77   }
78
79   /////////////////////////////////////////////////////////////////////////////////////////
80
/////////////////////////////////// MUTATOR METHODS ///////////////////////////////////
81
/////////////////////////////////////////////////////////////////////////////////////////
82

83   /**
84    * Changes the URI of this namespace.
85    *
86    * @param anURI The new URI of this namespace.
87    */

88   public void setURI(String JavaDoc anURI) {
89     _theURI = anURI;
90     generateHashCode();
91   }
92
93   /**
94    * Changes the prefix of this namespace.
95    *
96    * @param aPrefix The new prefix of this namespace
97    */

98   public void setPrefix(String JavaDoc aPrefix) {
99     _thePrefix = aPrefix;
100     generateHashCode();
101   }
102
103   /**
104    * Utility method that generates the hashcode value of this namespace.
105    */

106   private void generateHashCode() {
107     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
108     aBuffer.append(_theURI).append(_thePrefix);
109     _theHashCode = aBuffer.toString().hashCode();
110   }
111
112   /////////////////////////////////////////////////////////////////////////////////////////
113
////////////////////////////////// OVERRIDEN METHODS //////////////////////////////////
114
/////////////////////////////////////////////////////////////////////////////////////////
115

116   /**
117    * Returns the hash code of this namespace.
118    *
119    * @return The hash code of this namespace.
120    */

121   public int hashCode() {
122     return _theHashCode;
123   }
124
125   /**
126    * Returns true if the object passed in is of type <CODE>Namespace</CODE> and
127    * has the same URI and prefix as this namespace instance.
128    *
129    * @param anObject The object to compare for equality.
130    * @return True if the object is equals, false otherwise.
131    */

132   public boolean equals(Object JavaDoc anObject) {
133     if (anObject == this) {
134       return true;
135     } else if (!(anObject instanceof Namespace)) {
136       return false;
137     } else {
138       Namespace aNamespace = (Namespace) anObject;
139
140       return ((((_theURI == null) && (aNamespace._theURI == null)) ||
141       ((_theURI != null) && (aNamespace._theURI != null) &&
142       _theURI.equals(aNamespace._theURI))) &&
143       (((_thePrefix == null) && (aNamespace._thePrefix == null)) ||
144       ((_thePrefix != null) && (aNamespace._thePrefix != null) &&
145       _thePrefix.equals(aNamespace._thePrefix))));
146     }
147   }
148
149   /**
150    * Returns a string representation of this object.
151    *
152    * @return A string representation of this object.
153    */

154   public String JavaDoc toString() {
155     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc(super.toString());
156     aBuffer.append("[uri=").append(_theURI).append(" prefix=").append(_thePrefix)
157            .append("]");
158
159     return aBuffer.toString();
160   }
161 }
162
Popular Tags