001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.ws.policy.builder.primitive;
019:
020: import javax.xml.namespace.QName;
021: import javax.xml.stream.XMLStreamException;
022: import javax.xml.stream.XMLStreamWriter;
023:
024: import org.w3c.dom.Attr;
025: import org.w3c.dom.Element;
026:
027: import org.apache.cxf.ws.policy.PolicyConstants;
028: import org.apache.neethi.All;
029: import org.apache.neethi.Assertion;
030: import org.apache.neethi.Constants;
031: import org.apache.neethi.ExactlyOne;
032: import org.apache.neethi.Policy;
033: import org.apache.neethi.PolicyComponent;
034:
035: /**
036: *
037: */
038: public class PrimitiveAssertion implements Assertion {
039:
040: protected QName name;
041: protected boolean optional;
042:
043: public PrimitiveAssertion() {
044: this ((QName) null);
045: }
046:
047: public PrimitiveAssertion(QName n) {
048: this (n, false);
049: }
050:
051: public PrimitiveAssertion(QName n, boolean o) {
052: name = n;
053: optional = o;
054: }
055:
056: public PrimitiveAssertion(Element element, PolicyConstants constants) {
057: name = new QName(element.getNamespaceURI(), element
058: .getLocalName());
059: Attr attribute = element.getAttributeNodeNS(constants
060: .getNamespace(), constants.getOptionalAttrName());
061: if (attribute != null) {
062: optional = Boolean.valueOf(attribute.getValue());
063: }
064: }
065:
066: public boolean equal(PolicyComponent policyComponent) {
067: if (policyComponent.getType() != Constants.TYPE_ASSERTION) {
068: return false;
069: }
070: return getName()
071: .equals(((Assertion) policyComponent).getName());
072: }
073:
074: public short getType() {
075: return Constants.TYPE_ASSERTION;
076: }
077:
078: public QName getName() {
079: return name;
080: }
081:
082: public void setName(QName n) {
083: name = n;
084: }
085:
086: public boolean isOptional() {
087: return optional;
088: }
089:
090: public void setOptional(boolean o) {
091: optional = o;
092: }
093:
094: public PolicyComponent normalize() {
095: if (isOptional()) {
096: Policy policy = new Policy();
097: ExactlyOne exactlyOne = new ExactlyOne();
098:
099: All all = new All();
100: all.addPolicyComponent(cloneMandatory());
101: exactlyOne.addPolicyComponent(all);
102: exactlyOne.addPolicyComponent(new All());
103: policy.addPolicyComponent(exactlyOne);
104:
105: return policy;
106: }
107:
108: return cloneMandatory();
109: }
110:
111: public void serialize(XMLStreamWriter writer)
112: throws XMLStreamException {
113: }
114:
115: protected Assertion cloneMandatory() {
116: return new PrimitiveAssertion(name, false);
117: }
118: }
|