1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.neethi.builders.xml;
17
18 import javax.xml.namespace.QName;
19 import javax.xml.stream.XMLStreamException;
20 import javax.xml.stream.XMLStreamWriter;
21
22 import org.apache.axiom.om.OMAttribute;
23 import org.apache.axiom.om.OMElement;
24 import org.apache.neethi.All;
25 import org.apache.neethi.Assertion;
26 import org.apache.neethi.Constants;
27 import org.apache.neethi.ExactlyOne;
28 import org.apache.neethi.Policy;
29 import org.apache.neethi.PolicyComponent;
30 import org.apache.neethi.PolicyRegistry;
31
32
33
34
35
36
37
38 public class XmlPrimtiveAssertion implements Assertion {
39
40 OMElement element;
41
42 boolean isOptional;
43
44
45
46
47
48
49
50 public XmlPrimtiveAssertion(OMElement element) {
51 setValue(element);
52 setOptionality(element);
53 }
54
55
56
57
58 public QName getName() {
59 return (element != null) ? element.getQName() : null;
60 }
61
62
63
64
65
66
67
68 public void setValue(OMElement element) {
69 this.element = element;
70 }
71
72
73
74
75
76
77 public OMElement getValue() {
78 return element;
79 }
80
81
82
83
84
85 public boolean isOptional() {
86 return isOptional;
87 }
88
89
90
91
92
93 public PolicyComponent normalize() {
94 if (isOptional) {
95 Policy policy = new Policy();
96 ExactlyOne exactlyOne = new ExactlyOne();
97
98 All all = new All();
99 OMElement omElement = element.cloneOMElement();
100
101 omElement.removeAttribute(omElement
102 .getAttribute(Constants.Q_ELEM_OPTIONAL_ATTR));
103 all.addPolicyComponent(new XmlPrimtiveAssertion(omElement));
104 exactlyOne.addPolicyComponent(all);
105
106 exactlyOne.addPolicyComponent(new All());
107 policy.addPolicyComponent(exactlyOne);
108
109 return policy;
110 }
111
112 return this;
113 }
114
115
116
117
118
119 public PolicyComponent normalize(boolean isDeep) {
120 throw new UnsupportedOperationException();
121 }
122
123 public void serialize(XMLStreamWriter writer) throws XMLStreamException {
124 if (element != null) {
125 element.serialize(writer);
126
127 } else {
128 throw new RuntimeException("Wrapped Element is not set");
129 }
130 }
131
132
133
134
135 public final short getType() {
136 return Constants.TYPE_ASSERTION;
137 }
138
139 private void setOptionality(OMElement element) {
140 OMAttribute attribute = element
141 .getAttribute(Constants.Q_ELEM_OPTIONAL_ATTR);
142 if (attribute != null) {
143 this.isOptional = (new Boolean(attribute.getAttributeValue())
144 .booleanValue());
145
146 } else {
147 this.isOptional = false;
148 }
149 }
150
151 public boolean equal(PolicyComponent policyComponent) {
152 if (policyComponent.getType() != Constants.TYPE_ASSERTION) {
153 return false;
154 }
155
156 return getName().equals(((Assertion) policyComponent).getName());
157 }
158 }