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 * @deprecated use appropriate methods on the XMLObject's {@link NamespaceManager}.
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 * @deprecated use appropriate methods on the XMLObject's {@link NamespaceManager}.
111 */
112 public void setAlwaysDeclare(boolean shouldAlwaysDeclare) {
113 alwaysDeclare = shouldAlwaysDeclare;
114 }
115
116 /** {@inheritDoc} */
117 public String toString() {
118 if (nsStr == null) {
119 constructStringRepresentation();
120 }
121
122 return nsStr;
123 }
124
125 /** {@inheritDoc} */
126 public int hashCode() {
127 int hash = 1;
128 hash = hash * 31 + toString().hashCode();
129 hash = hash * 31 + (alwaysDeclare ? 0 : 1);
130 return hash;
131 }
132
133 /**
134 * Checks if the given object is the same as this Namespace. This is true if:
135 * <ul>
136 * <li>The given object is of type {@link Namespace}</li>
137 * <li>The given object's namespace URI is the same as this object's namespace URI</li>
138 * <li>The given object's namespace prefix is the same as this object's namespace prefix</li>
139 * </ul>
140 *
141 * @param obj {@inheritDoc}
142 *
143 * @return {@inheritDoc}
144 */
145 public boolean equals(Object obj) {
146 if(obj == this){
147 return true;
148 }
149
150 if (obj instanceof Namespace) {
151 Namespace otherNamespace = (Namespace) obj;
152 if (DatatypeHelper.safeEquals(otherNamespace.getNamespaceURI(), getNamespaceURI())){
153 if (DatatypeHelper.safeEquals(otherNamespace.getNamespacePrefix(), getNamespacePrefix())){
154 return otherNamespace.alwaysDeclare() == alwaysDeclare();
155 }
156 }
157 }
158
159 return false;
160 }
161
162 /** Constructs an XML namespace declaration string representing this namespace. */
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 }