1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 public class Namespace {
24
25
26 private String namespaceURI;
27
28
29 private String namespacePrefix;
30
31
32 private boolean alwaysDeclare;
33
34
35 private String nsStr;
36
37
38 public Namespace() {
39
40 }
41
42
43
44
45
46
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
56
57
58
59 public String getNamespacePrefix() {
60 return namespacePrefix;
61 }
62
63
64
65
66
67
68 public void setNamespacePrefix(String newPrefix) {
69 namespacePrefix = DatatypeHelper.safeTrimOrNullString(newPrefix);
70 nsStr = null;
71 }
72
73
74
75
76
77
78 public String getNamespaceURI() {
79 return namespaceURI;
80 }
81
82
83
84
85
86
87 public void setNamespaceURI(String newURI) {
88 namespaceURI = DatatypeHelper.safeTrimOrNullString(newURI);
89 nsStr = null;
90 }
91
92
93
94
95
96
97
98
99
100 public boolean alwaysDeclare() {
101 return alwaysDeclare;
102 }
103
104
105
106
107
108
109
110
111
112 public void setAlwaysDeclare(boolean shouldAlwaysDeclare) {
113 alwaysDeclare = shouldAlwaysDeclare;
114 }
115
116
117 public String toString() {
118 if (nsStr == null) {
119 constructStringRepresentation();
120 }
121
122 return nsStr;
123 }
124
125
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
135
136
137
138
139
140
141
142
143
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
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 }