View Javadoc

1   /*
2    * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.xml;
18  
19  import org.opensaml.xml.util.DatatypeHelper;
20  import org.opensaml.xml.util.XMLConstants;
21  
22  /**
23   * Data structure for representing XML namespace attributes.
24   */
25  public class Namespace {
26  
27      /** URI of the namespace. */
28      private String namespaceURI;
29  
30      /** Prefix of the namespace. */
31      private String namespacePrefix;
32  
33      /** Always declare this namespace while marshalling? */
34      private boolean alwaysDeclare;
35  
36      /** String representation of this namespace. */
37      private String nsStr;
38  
39      /** Constructor. */
40      public Namespace() {
41  
42      }
43  
44      /**
45       * Constructor.
46       * 
47       * @param uri the URI of the namespace
48       * @param prefix the prefix of the namespace
49       */
50      public Namespace(String uri, String prefix) {
51          namespaceURI = DatatypeHelper.safeTrimOrNullString(uri);
52          namespacePrefix = DatatypeHelper.safeTrimOrNullString(prefix);
53          nsStr = null;
54      }
55  
56      /**
57       * Gets the prefix of the namespace.
58       * 
59       * @return the prefix of the namespace
60       */
61      public String getNamespacePrefix() {
62          return namespacePrefix;
63      }
64  
65      /**
66       * Sets the prefix of the namespace.
67       * 
68       * @param newPrefix the prefix of the namespace
69       */
70      public void setNamespacePrefix(String newPrefix) {
71          namespacePrefix = DatatypeHelper.safeTrimOrNullString(newPrefix);
72          nsStr = null;
73      }
74  
75      /**
76       * Gets the URI of the namespace.
77       * 
78       * @return the URI of the namespace
79       */
80      public String getNamespaceURI() {
81          return namespaceURI;
82      }
83  
84      /**
85       * Sets the URI of the namespace.
86       * 
87       * @param newURI the URI of the namespace
88       */
89      public void setNamespaceURI(String newURI) {
90          namespaceURI = DatatypeHelper.safeTrimOrNullString(newURI);
91          nsStr = null;
92      }
93  
94      /**
95       * Gets wether this namespace should always be declared when marshalling, even if it was already declared on an
96       * ancestral element.
97       * 
98       * @return true if this namespace should always be declared, false if not
99       */
100     public boolean alwaysDeclare() {
101         return alwaysDeclare;
102     }
103 
104     /**
105      * Sets wether this namespace should always be declared when marshalling, even if it was already declared on an
106      * ancestral element.
107      * 
108      * @param shouldAlwaysDeclare true if this namespace should always be declared, false if not
109      */
110     public void setAlwaysDeclare(boolean shouldAlwaysDeclare) {
111         alwaysDeclare = shouldAlwaysDeclare;
112     }
113 
114     /** {@inheritDoc} */
115     public String toString() {
116         if (nsStr == null) {
117             constructStringRepresentation();
118         }
119 
120         return nsStr;
121     }
122 
123     /** {@inheritDoc} */
124     public int hashCode() {
125         int hash = 1;
126         hash = hash * 31 + toString().hashCode();
127         hash = hash * 31 + (alwaysDeclare ? 0 : 1);
128         return hash;
129     }
130 
131     /**
132      * Checks if the given object is the same as this Namespace. This is true if:
133      * <ul>
134      * <li>The given object is of type {@link Namespace}</li>
135      * <li>The given object's namespace URI is the same as this object's namespace URI</li>
136      * <li>The given object's namespace prefix is the same as this object's namespace prefix</li>
137      * </ul>
138      * 
139      * @param obj {@inheritDoc}
140      * 
141      * @return {@inheritDoc}
142      */
143     public boolean equals(Object obj) {    
144         if(obj == this){
145             return true;
146         }
147         
148         if (obj instanceof Namespace) {
149             Namespace otherNamespace = (Namespace) obj;
150             if (DatatypeHelper.safeEquals(otherNamespace.getNamespaceURI(), getNamespaceURI())){
151                 if (DatatypeHelper.safeEquals(otherNamespace.getNamespacePrefix(), getNamespacePrefix())){
152                     return otherNamespace.alwaysDeclare() == alwaysDeclare();
153                 }
154             }
155         }
156 
157         return false;
158     }
159 
160     /**
161      * Constructs an XML namespace declaration string representing this namespace.
162      */
163     protected void constructStringRepresentation() {
164         StringBuffer stringRep = new StringBuffer();
165 
166         stringRep.append(XMLConstants.XMLNS_PREFIX);
167 
168         if (namespacePrefix != null) {
169             stringRep.append(":");
170             stringRep.append(namespacePrefix);
171         }
172 
173         stringRep.append("=\"");
174 
175         if (namespaceURI != null) {
176             stringRep.append(namespaceURI);
177         }
178 
179         stringRep.append("\"");
180 
181         nsStr = stringRep.toString();
182     }
183 }