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;
019:
020: import java.util.Collection;
021: import java.util.List;
022: import java.util.logging.Level;
023: import java.util.logging.Logger;
024:
025: import org.apache.cxf.helpers.CastUtils;
026: import org.apache.cxf.ws.policy.builder.primitive.NestedPrimitiveAssertion;
027: import org.apache.neethi.Assertion;
028: import org.apache.neethi.Constants;
029: import org.apache.neethi.PolicyComponent;
030: import org.apache.neethi.PolicyOperator;
031:
032: /**
033: *
034: */
035: public final class PolicyUtils {
036:
037: private static final String INDENT = " ";
038:
039: private PolicyUtils() {
040: }
041:
042: /**
043: * Determine if a collection of assertions contains a given assertion, using
044: * the equal method from the Assertion interface.
045: *
046: * @param assertions a collection of assertions
047: * @param candidate the assertion to test
048: * @return true iff candidate is equal to one of the assertions in the collection
049: */
050: public static boolean contains(Collection<Assertion> assertions,
051: Assertion candidate) {
052: for (Assertion a : assertions) {
053: if (a.equal(candidate)) {
054: return true;
055: }
056: }
057: return false;
058: }
059:
060: /**
061: * Determine if one collection of assertions contains another collection of assertion, using
062: * the equal method from the Assertion interface.
063: *
064: * @param assertions a collection of assertions
065: * @param candidates the collections of assertion to test
066: * @return true iff each candidate is equal to one of the assertions in the collection
067: */
068: public static boolean contains(Collection<Assertion> assertions,
069: Collection<Assertion> candidates) {
070: if (null == candidates || candidates.isEmpty()) {
071: return true;
072: }
073: for (Assertion c : candidates) {
074: if (!contains(assertions, c)) {
075: return false;
076: }
077: }
078: return true;
079: }
080:
081: public static void logPolicy(Logger log, Level level, String msg,
082: PolicyComponent pc) {
083: if (null == pc) {
084: log.log(level, msg);
085: return;
086: }
087: StringBuffer buf = new StringBuffer();
088: buf.append(msg);
089: nl(buf);
090: printPolicyComponent(pc, buf, 0);
091: log.log(level, buf.toString());
092: }
093:
094: public static void printPolicyComponent(PolicyComponent pc) {
095: StringBuffer buf = new StringBuffer();
096: printPolicyComponent(pc, buf, 0);
097: System.out.println(buf.toString());
098: }
099:
100: public static void printPolicyComponent(PolicyComponent pc,
101: StringBuffer buf, int level) {
102: indent(buf, level);
103: buf.append("type: ");
104: buf.append(typeToString(pc.getType()));
105: if (Constants.TYPE_ASSERTION == pc.getType()) {
106: buf.append(" ");
107: buf.append(((Assertion) pc).getName());
108: if (((Assertion) pc).isOptional()) {
109: buf.append(" (optional)");
110: }
111: buf.append(" (");
112: buf.append((Assertion) pc);
113: buf.append(")");
114: nl(buf);
115: if (pc instanceof NestedPrimitiveAssertion) {
116: PolicyComponent nested = ((NestedPrimitiveAssertion) pc)
117: .getNested();
118: level++;
119: printPolicyComponent(nested, buf, level);
120: level--;
121: }
122: } else {
123: level++;
124: List<PolicyComponent> children = CastUtils.cast(
125: ((PolicyOperator) pc).getPolicyComponents(),
126: PolicyComponent.class);
127: nl(buf);
128: for (PolicyComponent child : children) {
129: printPolicyComponent(child, buf, level);
130: }
131: level--;
132: }
133: }
134:
135: private static void indent(StringBuffer buf, int level) {
136: for (int i = 0; i < level; i++) {
137: buf.append(INDENT);
138: }
139: }
140:
141: private static void nl(StringBuffer buf) {
142: buf.append(System.getProperty("line.separator"));
143: }
144:
145: private static String typeToString(short type) {
146: switch (type) {
147: case Constants.TYPE_ASSERTION:
148: return "Assertion";
149: case Constants.TYPE_ALL:
150: return "All";
151: case Constants.TYPE_EXACTLYONE:
152: return "ExactlyOne";
153: case Constants.TYPE_POLICY:
154: return "Policy";
155: case Constants.TYPE_POLICY_REF:
156: return "PolicyReference";
157: default:
158: break;
159: }
160: return "";
161: }
162:
163: }
|