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 * Policy is a PolicyOperator that requires to statisfy all of its
28 * PolicyComponents. It is always the outermost component of a Policy.
29 *
30 */
31 public class Policy extends All {
32
33 private HashMap attributes = new HashMap();
34
35 /**
36 * Returns a Normalized version of self. If <tt>deep</tt> is set
37 * <tt>false</tt> then the assertions are not normalized and it returns a
38 * partially normalized version of self.
39 *
40 * @param deep
41 * a flag to indicate whether to normalize the assertions
42 * @return a PolicyComponent that is normalized version of self
43 */
44 public PolicyComponent normalize(boolean deep) {
45 return normalize(null, deep);
46 }
47
48 /**
49 * Returns a normalized version of self.If <tt>deep</tt> is set
50 * <tt>false</tt> then the assertions are not normalized and it returns a
51 * partially normalized version of self.
52 *
53 * @param reg
54 * a PolicyRegistry from which the PolicyReferences are resolved
55 * @param deep
56 * a flag to indicate whether to normalize the assertions
57 * @return a normalzied version of self
58 */
59 public PolicyComponent normalize(PolicyRegistry reg, boolean deep) {
60 return normalize(this, reg, deep);
61 }
62
63 /**
64 * Returns a Policy that is the merge of specified Policy and self.
65 *
66 * @param policy
67 * the Policy to be merged with self
68 * @return a Policy that is the merge of the specified Policy and self
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 * Throws an UnSupportedOpertionException. TODO for a next version.
79 */
80 public Policy intersect(Policy policy) {
81 throw new UnsupportedOperationException();
82 }
83
84 /**
85 * Serializes the Policy to a XMLStreamWriter.
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 * Returns Constants.TYPE_POLICY
174 */
175 public short getType() {
176 return Constants.TYPE_POLICY;
177 }
178
179 /**
180 * Returns an Iterator that will return a list of assertions correspond to a
181 * Policy alternative if any. The <tt>iterator.next()</tt> will return a
182 * list of assertions correspond to a Policy alternative if any and
183 * <tt>iterator.hasNext()</tt> will indicates whether there is another
184 * Policy alternative.
185 *
186 * @return
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 * Adds an attribute to self.
218 *
219 * @param name
220 * the name of the attribute
221 * @param value
222 * the value of the attribute
223 */
224 public void addAttribute(QName name, String value) {
225 attributes.put(name, value);
226 }
227
228 /**
229 * Returns the value of the attribute specified by the QName. Returns
230 * <tt>null</tt> if not present.
231 *
232 * @param name
233 * the QName of the attribute
234 * @return the value of the attribute specified by the QName
235 */
236 public String getAttribute(QName name) {
237 return (String) attributes.get(name);
238 }
239
240 /**
241 * Returns a <tt>Map</tt> of all attributes of self.
242 *
243 * @return a Map of all attributes of self
244 */
245 public Map getAttributes() {
246 return attributes;
247 }
248
249 /**
250 * Sets the <tt>Name</tt> attribute of self.
251 *
252 * @param name
253 * the Name attribute of self
254 */
255 public void setName(String name) {
256 addAttribute(new QName("", Constants.ATTR_NAME), name);
257 }
258
259 /**
260 * Returns the <tt>Name</tt> attribute of self.
261 *
262 * @return the Name attribute of self
263 */
264 public String getName() {
265 return getAttribute(new QName("", Constants.ATTR_NAME));
266 }
267
268 /**
269 * Sets the wsu:Id attribute of self.
270 *
271 * @param id
272 * the Id attribute of self
273 */
274 public void setId(String id) {
275 addAttribute(new QName(Constants.URI_WSU_NS, Constants.ATTR_ID), id);
276 }
277
278 /**
279 * Returns the Id attribute of self.
280 *
281 * @return the Id attribute of self
282 */
283 public String getId() {
284 return getAttribute(new QName(Constants.URI_WSU_NS, Constants.ATTR_ID));
285 }
286 }