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.util;
019:
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.apache.neethi.All;
024: import org.apache.neethi.Assertion;
025: import org.apache.neethi.ExactlyOne;
026: import org.apache.neethi.Policy;
027: import org.apache.neethi.PolicyComponent;
028:
029: public final class PolicyComparator {
030:
031: private PolicyComparator() {
032: }
033:
034: /**
035: * Returns <tt>true</tt> if the two policies have the same semantics
036: *
037: * @param arg1 a Policy
038: * @param arg2 another Policy
039: * @return <tt>true</tt> iff both policies have the same semantics
040: */
041: public static boolean compare(Policy arg1, Policy arg2) {
042: boolean result = compare(arg1.getPolicyComponents(), arg2
043: .getPolicyComponents());
044:
045: return result;
046: }
047:
048: /**
049: * Returns <tt>true</tt> if the two PolicyComponents have the same
050: * semantics.
051: *
052: * @param arg1 a PolicyComponent
053: * @param arg2 another PolicyComponent
054: * @return <tt>true</tt> iff both PolicyComponents have the same semantics
055: */
056: public static boolean compare(PolicyComponent arg1,
057: PolicyComponent arg2) {
058:
059: // support parameterised assertion implementations
060: /*
061: if (!arg1.getClass().equals(arg2.getClass())) {
062: return false;
063: }
064: */
065:
066: if (arg1 instanceof Policy) {
067: return compare((Policy) arg1, (Policy) arg2);
068:
069: } else if (arg1 instanceof All) {
070: return compare((All) arg1, (All) arg2);
071:
072: } else if (arg1 instanceof ExactlyOne) {
073: return compare((ExactlyOne) arg1, (ExactlyOne) arg2);
074:
075: } else if (arg1 instanceof Assertion) {
076: return compare((Assertion) arg1, (Assertion) arg2);
077:
078: } else {
079: // TODO should I throw an exception ..
080: }
081:
082: return false;
083:
084: }
085:
086: public static boolean compare(All arg1, All arg2) {
087: return compare(arg1.getPolicyComponents(), arg2
088: .getPolicyComponents());
089: }
090:
091: public static boolean compare(ExactlyOne arg1, ExactlyOne arg2) {
092: return compare(arg1.getPolicyComponents(), arg2
093: .getPolicyComponents());
094: }
095:
096: public static boolean compare(Assertion arg1, Assertion arg2) {
097: // we have equal in the assertion interface so use it
098: return arg1.equal(arg2);
099: /*
100: if (!(arg1.getName().equals(arg2.getName()))) {
101: return false;
102: }
103: return true;
104: */
105: }
106:
107: private static boolean compare(List arg1, List arg2) {
108: if (arg1.size() != arg2.size()) {
109: return false;
110: }
111:
112: Iterator iterator = arg1.iterator();
113: PolicyComponent assertion1;
114:
115: while (iterator.hasNext()) {
116: assertion1 = (PolicyComponent) iterator.next();
117:
118: Iterator iterator2 = arg2.iterator();
119: boolean match = false;
120: PolicyComponent assertion2;
121:
122: while (iterator2.hasNext()) {
123: assertion2 = (PolicyComponent) iterator2.next();
124: if (compare(assertion1, assertion2)) {
125: match = true;
126: break;
127: }
128: }
129:
130: if (!match) {
131: return false;
132: }
133: }
134: return true;
135: }
136: }
|