001: /* Copyright 2004 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.groups.pags;
007:
008: import java.lang.reflect.Constructor;
009: import java.util.HashMap;
010: import java.util.Map;
011:
012: import org.jasig.portal.groups.pags.PersonAttributesGroupStore.GroupDefinition;
013: import org.jasig.portal.groups.pags.PersonAttributesGroupStore.TestGroup;
014: import org.jasig.portal.utils.ResourceLoader;
015: import org.w3c.dom.Document;
016: import org.w3c.dom.Element;
017: import org.w3c.dom.Node;
018: import org.w3c.dom.NodeList;
019: import org.w3c.dom.Text;
020:
021: /**
022: * Read in the configuration for the Person Attributes group store using
023: * an XML file. See the example file in /properties/groups/PAGSGroupStoreConfig.xml
024: * and the DTD in /webpages/dtd/PAGSGroupStore.dtd for information on the
025: * file format.
026: *
027: * @author Al Wold
028: * @version $Revision: 36683 $
029: */
030: public class XMLPersonAttributesConfiguration implements
031: IPersonAttributesConfiguration {
032:
033: public Map getConfig() {
034: Map groupDefinitions;
035: Document config = null;
036: try {
037: config = ResourceLoader.getResourceAsDocument(this
038: .getClass(),
039: "/properties/groups/PAGSGroupStoreConfig.xml");
040: } catch (Exception rme) {
041: throw new RuntimeException(
042: "PersonAttributesGroupStore: Unable to find configuration document");
043: }
044: groupDefinitions = new HashMap();
045: config.normalize();
046: Element groupStoreElement = config.getDocumentElement();
047: NodeList groupElements = groupStoreElement.getChildNodes();
048: for (int i = 0; i < groupElements.getLength(); i++) {
049: if (groupElements.item(i) instanceof Element) {
050: GroupDefinition groupDef = initGroupDef((Element) groupElements
051: .item(i));
052: groupDefinitions.put(groupDef.getKey(), groupDef);
053: }
054: }
055: return groupDefinitions;
056: }
057:
058: private GroupDefinition initGroupDef(Element groupElement) {
059: GroupDefinition groupDef = new GroupDefinition();
060: NodeList children = groupElement.getChildNodes();
061: for (int i = 0; i < children.getLength(); i++) {
062: if (children.item(i) instanceof Element) {
063: Element element = (Element) children.item(i);
064: String tagName = element.getTagName();
065: element.normalize();
066: String text = null;
067: if (element.getFirstChild() instanceof Text) {
068: text = ((Text) element.getFirstChild()).getData()
069: .trim();
070: }
071: if (tagName.equals("group-key")) {
072: groupDef.setKey(text);
073: } else if (tagName.equals("group-name")) {
074: groupDef.setName(text);
075: } else if (tagName.equals("group-description")) {
076: groupDef.setDescription(text);
077: } else if (tagName.equals("selection-test")) {
078: NodeList testGroups = element.getChildNodes();
079: for (int j = 0; j < testGroups.getLength(); j++) {
080: Node testGroup = testGroups.item(j);
081: if (testGroup instanceof Element
082: && ((Element) testGroup).getTagName()
083: .equals("test-group")) {
084: TestGroup tg = new TestGroup();
085: NodeList tests = testGroup.getChildNodes();
086: for (int k = 0; k < tests.getLength(); k++) {
087: Node test = tests.item(k);
088: if (test instanceof Element
089: && ((Element) test)
090: .getTagName().equals(
091: "test")) {
092: String attribute = null;
093: String tester = null;
094: String value = null;
095: NodeList parameters = test
096: .getChildNodes();
097: for (int l = 0; l < parameters
098: .getLength(); l++) {
099: Node parameter = parameters
100: .item(l);
101: text = null;
102: String nodeName = parameter
103: .getNodeName();
104: if (parameter.getFirstChild() != null
105: && parameter
106: .getFirstChild() instanceof Text) {
107: text = ((Text) parameter
108: .getFirstChild())
109: .getData();
110: }
111: if (nodeName
112: .equals("attribute-name")) {
113: attribute = text;
114: } else if (nodeName
115: .equals("tester-class")) {
116: tester = text;
117: } else if (nodeName
118: .equals("test-value")) {
119: value = text;
120: }
121: }
122: IPersonTester testerInst = initializeTester(
123: tester, attribute, value);
124: tg.addTest(testerInst);
125: }
126: groupDef.addTestGroup(tg);
127: }
128: }
129: }
130: } else if (tagName.equals("members")) {
131: addMemberKeys(groupDef, element);
132: }
133: }
134: }
135: return groupDef;
136: }
137:
138: private void addMemberKeys(GroupDefinition groupDef, Element members) {
139: NodeList children = members.getChildNodes();
140: for (int i = 0; i < children.getLength(); i++) {
141: Node node = children.item(i);
142: if (node instanceof Element
143: && node.getNodeName().equals("member-key")) {
144: Element member = (Element) node;
145: member.normalize();
146: if (member.getFirstChild() instanceof Text) {
147: groupDef.addMember(((Text) member.getFirstChild())
148: .getData());
149: }
150: }
151: }
152: }
153:
154: private IPersonTester initializeTester(String tester,
155: String attribute, String value) {
156: try {
157: Class testerClass = Class.forName(tester);
158: Constructor c = testerClass.getConstructor(new Class[] {
159: String.class, String.class });
160: Object o = c.newInstance(new Object[] { attribute, value });
161: return (IPersonTester) o;
162: } catch (Exception e) {
163: e.printStackTrace();
164: return null;
165: }
166: }
167: }
|