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  /** Data structure for representing XML namespace attributes. */
23  public class Namespace {
24  
25      /** URI of the namespace. */
26      private String namespaceURI;
27  
28      /** Prefix of the namespace. */
29      private String namespacePrefix;
30  
31      /** Always declare this namespace while marshalling? */
32      private boolean alwaysDeclare;
33  
34      /** String representation of this namespace. */
35      private String nsStr;
36  
37      /** Constructor. */
38      public Namespace() {
39  
40      }
41  
42      /**
43       * Constructor.
44       * 
45       * @param uri the URI of the namespace
46       * @param prefix the prefix of the namespace
47       */
48      public Namespace(String uri, String prefix) {
49          namespaceURI = DatatypeHelper.safeTrimOrNullString(uri);
50          namespacePrefix = DatatypeHelper.safeTrimOrNullString(prefix);
51          nsStr = null;
52      }
53  
54      /**
55       * Gets the prefix of the namespace.
56       * 
57       * @return the prefix of the namespace, may be null if this is a default namespace
58       */
59      public String getNamespacePrefix() {
60          return namespacePrefix;
61      }
62  
63      /**
64       * Sets the prefix of the namespace.
65       * 
66       * @param newPrefix the prefix of the namespace
67       */
68      public void setNamespacePrefix(String newPrefix) {
69          namespacePrefix = DatatypeHelper.safeTrimOrNullString(newPrefix);
70          nsStr = null;
71      }
72  
73      /**
74       * Gets the URI of the namespace.
75       * 
76       * @return the URI of the namespace
77       */
78      public String getNamespaceURI() {
79          return namespaceURI;
80      }
81  
82      /**
83       * Sets the URI of the namespace.
84       * 
85       * @param newURI the URI of the namespace
86       */
87      public void setNamespaceURI(String newURI) {
88          namespaceURI = DatatypeHelper.safeTrimOrNullString(newURI);
89          nsStr = null;
90      }
91  
92      /**
93       * Gets wether this namespace should always be declared when marshalling, even if it was already declared on an
94       * ancestral element.
95       * 
96       * @return true if this namespace should always be declared, false if not
97       */
98      public boolean alwaysDeclare() {
99          return alwaysDeclare;
100     }
101 
102     /**
103      * Sets wether this namespace should always be declared when marshalling, even if it was already declared on an
104      * ancestral element.
105      * 
106      * @param shouldAlwaysDeclare true if this namespace should always be declared, false if not
107      */
108     public void setAlwaysDeclare(boolean shouldAlwaysDeclare) {
109         alwaysDeclare = shouldAlwaysDeclare;
110     }
111 
112     /** {@inheritDoc} */
113     public String toString() {
114         if (nsStr == null) {
115             constructStringRepresentation();
116         }
117 
118         return nsStr;
119     }
120 
121     /** {@inheritDoc} */
122     public int hashCode() {
123         int hash = 1;
124         hash = hash * 31 + toString().hashCode();
125         hash = hash * 31 + (alwaysDeclare ? 0 : 1);
126         return hash;
127     }
128 
129     /**
130      * Checks if the given object is the same as this Namespace. This is true if:
131      * <ul>
132      * <li>The given object is of type {@link Namespace}</li>
133      * <li>The given object's namespace URI is the same as this object's namespace URI</li>
134      * <li>The given object's namespace prefix is the same as this object's namespace prefix</li>
135      * </ul>
136      * 
137      * @param obj {@inheritDoc}
138      * 
139      * @return {@inheritDoc}
140      */
141     public boolean equals(Object obj) {    
142         if(obj == this){
143             return true;
144         }
145         
146         if (obj instanceof Namespace) {
147             Namespace otherNamespace = (Namespace) obj;
148             if (DatatypeHelper.safeEquals(otherNamespace.getNamespaceURI(), getNamespaceURI())){
149                 if (DatatypeHelper.safeEquals(otherNamespace.getNamespacePrefix(), getNamespacePrefix())){
150                     return otherNamespace.alwaysDeclare() == alwaysDeclare();
151                 }
152             }
153         }
154 
155         return false;
156     }
157 
158     /** Constructs an XML namespace declaration string representing this namespace. */
159     protected void constructStringRepresentation() {
160         StringBuffer stringRep = new StringBuffer();
161 
162         stringRep.append(XMLConstants.XMLNS_PREFIX);
163 
164         if (namespacePrefix != null) {
165             stringRep.append(":");
166             stringRep.append(namespacePrefix);
167         }
168 
169         stringRep.append("=\"");
170 
171         if (namespaceURI != null) {
172             stringRep.append(namespaceURI);
173         }
174 
175         stringRep.append("\"");
176 
177         nsStr = stringRep.toString();
178     }
179 }