01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18: package de.schlund.pfixcore.auth.conditions;
19:
20: import java.util.ArrayList;
21: import java.util.Iterator;
22: import java.util.List;
23:
24: import org.w3c.dom.Document;
25: import org.w3c.dom.Element;
26:
27: import de.schlund.pfixcore.auth.Authentication;
28: import de.schlund.pfixcore.auth.Condition;
29:
30: /**
31: *
32: * @author mleidig@schlund.de
33: *
34: */
35: public abstract class ConditionGroup implements Condition {
36:
37: protected List<Condition> conditions;
38:
39: public ConditionGroup() {
40: conditions = new ArrayList<Condition>();
41: }
42:
43: public ConditionGroup(Condition... conditions) {
44: this ();
45: for (Condition condition : conditions)
46: this .conditions.add(condition);
47: }
48:
49: public void add(Condition condition) {
50: conditions.add(condition);
51: }
52:
53: public abstract boolean evaluate(Authentication auth);
54:
55: public abstract String getOperatorString();
56:
57: @Override
58: public String toString() {
59: StringBuilder sb = new StringBuilder();
60: sb.append("( ");
61: Iterator<Condition> it = conditions.iterator();
62: while (it.hasNext()) {
63: Condition condition = it.next();
64: sb.append(condition);
65: if (it.hasNext())
66: sb.append(" " + getOperatorString() + " ");
67: }
68: sb.append(" )");
69: return sb.toString();
70: }
71:
72: public Element toXML(Document doc) {
73: Element element = doc.createElement(getClass().getSimpleName()
74: .toLowerCase());
75: for (Condition condition : conditions) {
76: element.appendChild(condition.toXML(doc));
77: }
78: return element;
79: }
80:
81: public static void main(String[] args) {
82: HasRole fooRole = new HasRole("foo");
83: HasRole barRole = new HasRole("bar");
84: HasRole bazRole = new HasRole("baz");
85: Condition subCond1 = new And(barRole, new Not(bazRole));
86: Condition subCond2 = new Not(barRole);
87: Condition cond = new Or(fooRole, subCond1, subCond2);
88: System.out.println(cond);
89: }
90:
91: }
|