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