01: /* Copyright 2005 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.layout.dlm.providers;
07:
08: import org.jasig.portal.layout.dlm.Evaluator;
09: import org.jasig.portal.security.IPerson;
10:
11: /**
12: * @version $Revision: 36698 $ $Date: 2006-09-08 09:34:37 -0700 (Fri, 08 Sep 2006) $
13: * @since uPortal 2.5
14: */
15: public class AttributeEvaluator implements Evaluator {
16: public static final String RCS_ID = "@(#) $Header$";
17:
18: public static final int CONTAINS = 0;
19: public static final int EQUALS = 1;
20: public static final int STARTS_WITH = 2;
21: public static final int ENDS_WITH = 3;
22: public static final int EXISTS = 4;
23:
24: protected int mode = -1;
25: protected String name = null;
26: protected String value = null;
27:
28: public AttributeEvaluator(String name, String mode, String value) {
29: if (mode.equals("equals")) {
30: this .mode = EQUALS;
31: if (value == null)
32: throw new RuntimeException(
33: "Missing value attribute"
34: + ". For mode of 'equals' value must be defined.");
35: } else if (mode.equals("exists"))
36: this .mode = EXISTS;
37: else if (mode.equals("contains")) {
38: this .mode = CONTAINS;
39: if (value == null || value.equals(""))
40: throw new RuntimeException(
41: "Missing or invalid value attribute"
42: + ". For mode of 'contains' value "
43: + "must be defined and not empty");
44: } else if (mode.equals("startsWith")) {
45: this .mode = STARTS_WITH;
46: if (value == null || value.equals(""))
47: throw new RuntimeException(
48: "Missing or invalid value attribute. "
49: + "For mode of 'startsWith' value must "
50: + "be defined and not empty");
51: } else if (mode.equals("endsWith")) {
52: this .mode = ENDS_WITH;
53: if (value == null || value.equals(""))
54: throw new RuntimeException(
55: "Missing or invalid value attribute. "
56: + "For mode of 'endsWith' value must be "
57: + "defined and not empty");
58: } else
59: throw new RuntimeException(
60: "Invalid mode attribute. Expected mode "
61: + "of 'contains', 'equals', 'startsWith', "
62: + "'exists', or 'endsWith'");
63:
64: this .name = name;
65: this .value = value;
66: }
67:
68: public boolean isApplicable(IPerson p) {
69: String attrib = (String) p.getAttribute(name);
70:
71: // for tests other than 'exists' the attribute must be defined
72: if (attrib == null && mode != EXISTS)
73: return false;
74:
75: if (mode == EQUALS)
76: return attrib.equals(value);
77: if (mode == EXISTS)
78: return attrib != null;
79: if (mode == STARTS_WITH)
80: return attrib.startsWith(value);
81: if (mode == ENDS_WITH)
82: return attrib.endsWith(value);
83: if (mode == CONTAINS)
84: return (attrib.indexOf(value) != -1);
85: // will never get here
86: return false;
87: }
88: }
|