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 public boolean alwaysDeclare() {
99 return alwaysDeclare;
100 }
101
102
103
104
105
106
107
108 public void setAlwaysDeclare(boolean shouldAlwaysDeclare) {
109 alwaysDeclare = shouldAlwaysDeclare;
110 }
111
112
113 public String toString() {
114 if (nsStr == null) {
115 constructStringRepresentation();
116 }
117
118 return nsStr;
119 }
120
121
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
131
132
133
134
135
136
137
138
139
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
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 }