1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.neethi;
17
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.Map;
21
22 import javax.xml.namespace.QName;
23 import javax.xml.stream.XMLStreamException;
24 import javax.xml.stream.XMLStreamWriter;
25
26
27
28
29
30
31 public class Policy extends All {
32
33 private HashMap attributes = new HashMap();
34
35
36
37
38
39
40
41
42
43
44 public PolicyComponent normalize(boolean deep) {
45 return normalize(null, deep);
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59 public PolicyComponent normalize(PolicyRegistry reg, boolean deep) {
60 return normalize(this, reg, deep);
61 }
62
63
64
65
66
67
68
69
70 public Policy merge(Policy policy) {
71 Policy result = new Policy();
72 result.addPolicyComponents(getPolicyComponents());
73 result.addPolicyComponents(policy.getPolicyComponents());
74 return result;
75 }
76
77
78
79
80 public Policy intersect(Policy policy) {
81 throw new UnsupportedOperationException();
82 }
83
84
85
86
87 public void serialize(XMLStreamWriter writer) throws XMLStreamException {
88 String wspPrefix = writer.getPrefix(Constants.URI_POLICY_NS);
89
90 if (wspPrefix == null) {
91 wspPrefix = Constants.ATTR_WSP;
92 writer.setPrefix(wspPrefix, Constants.URI_POLICY_NS);
93 }
94
95 String wsuPrefix = writer.getPrefix(Constants.URI_WSU_NS);
96 if (wsuPrefix == null) {
97 wsuPrefix = Constants.ATTR_WSU;
98 writer.setPrefix(wsuPrefix, Constants.URI_WSU_NS);
99 }
100
101 writer.writeStartElement(wspPrefix, Constants.ELEM_POLICY,
102 Constants.URI_POLICY_NS);
103
104 QName key;
105
106 String prefix = null;
107 String namespaceURI = null;
108 String localName = null;
109
110 HashMap prefix2ns = new HashMap();
111
112 for (Iterator iterator = getAttributes().keySet().iterator(); iterator
113 .hasNext();) {
114
115 key = (QName) iterator.next();
116 localName = key.getLocalPart();
117
118 namespaceURI = key.getNamespaceURI();
119 namespaceURI = (namespaceURI == null || namespaceURI.length() == 0) ? null : namespaceURI;
120
121 if (namespaceURI != null) {
122
123 String writerPrefix = writer.getPrefix(namespaceURI);
124 writerPrefix = (writerPrefix == null || writerPrefix.length() == 0) ? null : writerPrefix;
125
126 if (writerPrefix == null) {
127 prefix = key.getPrefix();
128 prefix = (prefix == null || prefix.length() == 0) ? null : prefix;
129
130 } else {
131 prefix = writerPrefix;
132 }
133
134 if (prefix != null) {
135 writer.writeAttribute(prefix, namespaceURI, localName, getAttribute(key));
136 prefix2ns.put(prefix, key.getNamespaceURI());
137
138 } else {
139 writer.writeAttribute(namespaceURI, localName, getAttribute(key));
140 }
141
142 } else {
143 writer.writeAttribute(localName, getAttribute(key));
144 }
145
146
147 }
148
149
150 writer.writeNamespace(wspPrefix, Constants.URI_POLICY_NS);
151
152 String prefiX;
153
154 for (Iterator iterator = prefix2ns.keySet().iterator(); iterator
155 .hasNext();) {
156 prefiX = (String) iterator.next();
157 writer.writeNamespace(prefiX, (String) prefix2ns.get(prefiX));
158 }
159
160 PolicyComponent policyComponent;
161
162 for (Iterator iterator = getPolicyComponents().iterator(); iterator
163 .hasNext();) {
164 policyComponent = (PolicyComponent) iterator.next();
165 policyComponent.serialize(writer);
166 }
167
168 writer.writeEndElement();
169
170 }
171
172
173
174
175 public short getType() {
176 return Constants.TYPE_POLICY;
177 }
178
179
180
181
182
183
184
185
186
187
188 public Iterator getAlternatives() {
189 return new PolicyIterator(this);
190 }
191
192 private class PolicyIterator implements Iterator {
193 Iterator alternatives = null;
194
195 public PolicyIterator(Policy policy) {
196 policy = (Policy) policy.normalize(false);
197 ExactlyOne exactlyOne = (ExactlyOne) policy
198 .getFirstPolicyComponent();
199 alternatives = exactlyOne.getPolicyComponents().iterator();
200 }
201
202 public boolean hasNext() {
203 return alternatives.hasNext();
204 }
205
206 public Object next() {
207 return ((All) alternatives.next()).getPolicyComponents();
208 }
209
210 public void remove() {
211 throw new UnsupportedOperationException(
212 "policyAlternative.remove() is not supported");
213 }
214 }
215
216
217
218
219
220
221
222
223
224 public void addAttribute(QName name, String value) {
225 attributes.put(name, value);
226 }
227
228
229
230
231
232
233
234
235
236 public String getAttribute(QName name) {
237 return (String) attributes.get(name);
238 }
239
240
241
242
243
244
245 public Map getAttributes() {
246 return attributes;
247 }
248
249
250
251
252
253
254
255 public void setName(String name) {
256 addAttribute(new QName("", Constants.ATTR_NAME), name);
257 }
258
259
260
261
262
263
264 public String getName() {
265 return getAttribute(new QName("", Constants.ATTR_NAME));
266 }
267
268
269
270
271
272
273
274 public void setId(String id) {
275 addAttribute(new QName(Constants.URI_WSU_NS, Constants.ATTR_ID), id);
276 }
277
278
279
280
281
282
283 public String getId() {
284 return getAttribute(new QName(Constants.URI_WSU_NS, Constants.ATTR_ID));
285 }
286 }