View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * XmlPrimitiveAssertion wraps an OMElement s.t. any unkown elements can be
34   * treated an assertions if there is no AssertionBuilder that can build an
35   * assertion from that OMElement.
36   * 
37   */
38  public class XmlPrimtiveAssertion implements Assertion {
39  
40      OMElement element;
41  
42      boolean isOptional;
43  
44      /**
45       * Constructs a XmlPrimitiveAssertion from an OMElement.
46       * 
47       * @param element
48       *            the OMElement from which the XmlAssertion is constructed
49       */
50      public XmlPrimtiveAssertion(OMElement element) {
51          setValue(element);
52          setOptionality(element);
53      }
54  
55      /**
56       * Returns the QName of the wrapped OMElement.
57       */
58      public QName getName() {
59          return (element != null) ? element.getQName() : null;
60      }
61  
62      /**
63       * Sets the wrapped OMElement.
64       * 
65       * @param element
66       *            the OMElement to be set as wrapped
67       */
68      public void setValue(OMElement element) {
69          this.element = element;
70      }
71  
72      /**
73       * Returns the wrapped OMElement.
74       * 
75       * @return the wrapped OMElement
76       */
77      public OMElement getValue() {
78          return element;
79      }
80  
81      /**
82       * Returns <tt>true</tt> if the wrapped element that assumed to be an
83       * assertion, is optional.
84       */
85      public boolean isOptional() {
86          return isOptional;
87      }
88  
89      /**
90       * Returns the partial normalized version of the wrapped OMElement, that is
91       * assumed to be an assertion.
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      * Throws an UnsupportedOperationException since an assertion of an unknown
117      * element can't be fully normalized due to it's unkonwn composite.
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      * Returns Constants.TYPE_ASSERTION
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 }