001: /* Copyright 2005 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.layout.dlm.providers;
007:
008: import org.jasig.portal.layout.dlm.Evaluator;
009: import org.jasig.portal.layout.dlm.EvaluatorFactory;
010: import org.jasig.portal.utils.XML;
011: import org.w3c.dom.NamedNodeMap;
012: import org.w3c.dom.Node;
013: import org.w3c.dom.NodeList;
014:
015: /**
016: * Implementation of the Evaluator Factory interface that creates evaluators
017: * of string attributes in implementations of IPerson to determine if a user
018: * gets a layout fragment.
019: *
020: * @author mboyd@sungardsct.com
021: * @version $Revision: 35711 $ $Date: 2005-05-02 22:55:35 -0700 (Mon, 02 May 2005) $
022: * @since uPortal 2.5
023: */
024: public class PersonEvaluatorFactory implements EvaluatorFactory {
025: public static final String RCS_ID = "@(#) $Header$";
026:
027: private static final int OR = 0;
028: private static final int AND = 1;
029: private static final int NOT = 2;
030:
031: public Evaluator getEvaluator(Node audience) {
032: return getGroupEvaluator(OR, audience);
033: }
034:
035: private Evaluator getGroupEvaluator(int type, Node node) {
036: NodeList nodes = node.getChildNodes();
037: Evaluator container = null;
038:
039: if (nodes == null
040: || nodes.getLength() == 0
041: || (container = createGroupEvaluator(type, nodes)) == null) {
042: throw new RuntimeException(
043: "Invalid content. Expected one to many "
044: + "<paren>, <NOT>, or <attribute> in '"
045: + XML.serializeNode(node) + "'");
046: }
047: return container;
048: }
049:
050: private Evaluator createGroupEvaluator(int type, NodeList nodes) {
051: // if only one child skip wrapping in container for AND and OR
052: if (nodes.getLength() == 1 && (type == OR || type == AND))
053: return createEvaluator(nodes.item(0));
054:
055: Paren container = null;
056:
057: if (type == NOT)
058: container = new Paren(Paren.NOT);
059: else if (type == OR)
060: container = new Paren(Paren.OR);
061: else if (type == AND)
062: container = new Paren(Paren.AND);
063:
064: boolean validContentAdded = false;
065:
066: for (int i = 0; i < nodes.getLength(); i++) {
067: if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
068: Evaluator e = createEvaluator(nodes.item(i));
069:
070: if (e != null) {
071: validContentAdded = true;
072: container.addEvaluator(e);
073: }
074: }
075: }
076: if (validContentAdded)
077: return container;
078: return null;
079: }
080:
081: private Evaluator createEvaluator(Node node) {
082: String nodeName = node.getNodeName();
083:
084: if (nodeName.equals("paren"))
085: return createParen(node);
086: else if (nodeName.equals("attribute"))
087: return createAttributeEvaluator(node);
088: throw new RuntimeException("Unrecognized element '" + nodeName
089: + "' in '" + XML.serializeNode(node) + "'");
090: }
091:
092: private Evaluator createParen(Node n) {
093: NamedNodeMap attribs = n.getAttributes();
094: Node opNode = attribs.getNamedItem("mode");
095:
096: if (opNode == null)
097: throw new RuntimeException(
098: "Invalid mode. Expected 'AND','OR', or 'NOT'"
099: + " in '" + XML.serializeNode(n) + "'");
100: else if (opNode.getNodeValue().equals("OR"))
101: return getGroupEvaluator(OR, n);
102: else if (opNode.getNodeValue().equals("NOT"))
103: return getGroupEvaluator(NOT, n);
104: else if (opNode.getNodeValue().equals("AND"))
105: return getGroupEvaluator(AND, n);
106: else
107: throw new RuntimeException(
108: "Invalid mode. Expected 'AND','OR', or 'NOT'"
109: + " in '" + XML.serializeNode(n) + "'");
110: }
111:
112: private Evaluator createAttributeEvaluator(Node n) {
113: NamedNodeMap attribs = n.getAttributes();
114: Node attribNode = attribs.getNamedItem("name");
115:
116: if (attribNode == null || attribNode.getNodeValue().equals(""))
117: throw new RuntimeException(
118: "Missing or empty name attribute in '"
119: + XML.serializeNode(n) + "'");
120: String name = attribNode.getNodeValue();
121: String value = null;
122: attribNode = attribs.getNamedItem("value");
123:
124: if (attribNode != null)
125: value = attribNode.getNodeValue();
126:
127: attribNode = attribs.getNamedItem("mode");
128:
129: if (attribNode == null || attribNode.getNodeValue().equals(""))
130: throw new RuntimeException(
131: "Missing or empty mode attribute in '"
132: + XML.serializeNode(n) + "'");
133: String mode = attribNode.getNodeValue();
134: Evaluator eval = null;
135:
136: try {
137: eval = getAttributeEvaluator(name, mode, value);
138: } catch (Exception e) {
139: throw new RuntimeException(e.getMessage() + " in '"
140: + XML.serializeNode(n));
141: }
142: return eval;
143: }
144:
145: /**
146: * returns an Evaluator unique to the type of attribute being
147: * evaluated. subclasses can override this method to return the
148: * Evaluator that's appropriate to their implementation.
149: *
150: * @param name the attribute's name.
151: * @param mode the attribute's mode. (i.e. 'equals')
152: * @param value the attribute's value.
153: *
154: * @return an Evaluator for evaluating attributes
155: */
156: public Evaluator getAttributeEvaluator(String name, String mode,
157: String value) throws Exception {
158: return new AttributeEvaluator(name, mode, value);
159: }
160: }
|