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.services.persondir.support.legacy;
007:
008: import java.util.ArrayList;
009: import java.util.List;
010: import java.util.StringTokenizer;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014: import org.jasig.portal.utils.XML;
015: import org.springframework.dao.DataIntegrityViolationException;
016: import org.w3c.dom.Document;
017: import org.w3c.dom.Element;
018: import org.w3c.dom.Node;
019: import org.w3c.dom.NodeList;
020:
021: /**
022: * Static function class which parses a Document such as that contained in
023: * a PersonDirs.xml file to obtain a List of PersonDirInfo objects.
024: *
025: * @version $Revision: 35676 $ $Date: 2005-04-26 09:34:30 -0700 (Tue, 26 Apr 2005) $
026: * @since uPortal 2.5
027: */
028: class PersonDirXmlParser {
029:
030: private static final Log log = LogFactory
031: .getLog(PersonDirXmlParser.class);
032:
033: /**
034: * Computes a List of PersonDirInfo objects from a Document.
035: * @param personDirDoc PersonDirInfo document
036: * @return a List of PersonDirInfo objects
037: * @throws IllegalArgumentException if param personDirDoc is null
038: * @throws DataAccessException on failure
039: */
040: static List getPersonDirInfos(Document personDirDoc) {
041: if (personDirDoc == null)
042: throw new IllegalArgumentException(
043: "Cannot get PersonDirInfos from a null document.");
044:
045: List infos = new ArrayList();
046:
047: // Each directory source is a <PersonDirInfo> (and its contents)
048: NodeList list = personDirDoc
049: .getElementsByTagName("PersonDirInfo");
050: for (int i = 0; i < list.getLength(); i++) { // foreach
051: // PersonDirInfo
052: Element dirinfo = (Element) list.item(i);
053:
054: // Java object into which we're trying to parse the PersonDirInfo
055: // element
056: PersonDirInfo pdi = new PersonDirInfo();
057:
058: // foreach tag under the <PersonDirInfo>
059: for (Node param = dirinfo.getFirstChild(); param != null; param = param
060: .getNextSibling()) {
061:
062: if (!(param instanceof Element))
063: continue; // whitespace (typically \n) between tags
064: Element pele = (Element) param;
065: String tagname = pele.getTagName();
066: String value = XML.getElementText(pele);
067:
068: try {
069: // each tagname corresponds to an object data field
070: if (tagname.equals("url")) {
071: pdi.setUrl(value);
072: } else if (tagname.equals("res-ref-name")) {
073: pdi.setResRefName(value);
074: } else if (tagname.equals("ldap-ref-name")) {
075: pdi.setLdapRefName(value);
076: } else if (tagname.equals("logonid")) {
077: pdi.setLogonid(value);
078: } else if (tagname.equals("driver")) {
079: pdi.setDriver(value);
080: } else if (tagname.equals("logonpassword")) {
081: pdi.setLogonpassword(value);
082: } else if (tagname.equals("uidquery")) {
083: pdi.setUidquery(value);
084: } else if (tagname.equals("usercontext")) {
085: pdi.setUsercontext(value);
086: } else if (tagname.equals("timeout")) {
087: pdi.setLdaptimelimit(Integer.parseInt(value));
088: } else if (tagname.equals("attributes")) {
089: NodeList anodes = pele
090: .getElementsByTagName("attribute");
091: int anodecount = anodes.getLength();
092: if (anodecount != 0) {
093: String[] attributenames = new String[anodecount];
094: String[] attributealiases = new String[anodecount];
095: for (int j = 0; j < anodecount; j++) {
096: Element anode = (Element) anodes
097: .item(j);
098: NodeList namenodes = anode
099: .getElementsByTagName("name");
100: String aname = "$$$";
101: if (namenodes.getLength() != 0)
102: aname = XML
103: .getElementText((Element) namenodes
104: .item(0));
105: attributenames[j] = aname;
106: NodeList aliasnodes = anode
107: .getElementsByTagName("alias");
108: if (aliasnodes.getLength() == 0) {
109: attributealiases[j] = aname;
110: } else {
111: attributealiases[j] = XML
112: .getElementText((Element) aliasnodes
113: .item(0));
114: }
115: }
116: pdi.setAttributenames(attributenames);
117: pdi.setAttributealiases(attributealiases);
118:
119: } else {
120: // The <attributes> tag contains a list of names
121: // and optionally aliases each in the form
122: // name[:alias]
123: // The name is an LDAP property or database column
124: // name.
125: // The alias, if it exists, is an eduPerson property
126: // that
127: // corresponds to the previous LDAP or DBMS name.
128: // If no alias is specified, the eduPerson name is
129: // also
130: // the LDAP or DBMS column name.
131: StringTokenizer st = new StringTokenizer(
132: value);
133: int n = st.countTokens();
134: String[] attributenames = new String[n];
135: String[] attributealiases = new String[n];
136: for (int k = 0; k < n; k++) {
137: String tk = st.nextToken();
138: int pos = tk.indexOf(':');
139: if (pos > 0) { // There is an alias
140: attributenames[k] = tk.substring(0,
141: pos);
142: attributealiases[k] = tk
143: .substring(pos + 1);
144:
145: } else { // There is no alias
146: attributenames[k] = tk;
147: attributealiases[k] = tk;
148: }
149: }
150: pdi.setAttributenames(attributenames);
151: pdi.setAttributealiases(attributealiases);
152: }
153: } else {
154: log
155: .warn("PersonDirectory::getParameters(): Unrecognized tag ["
156: + tagname
157: + "] in PersonDirs.xml");
158: }
159: } catch (Throwable t) {
160: throw new DataIntegrityViolationException(
161: "Error processing tag [" + tagname
162: + "] with value [" + value
163: + "] in PersonDirInfo [" + dirinfo
164: + "]", t);
165: }
166:
167: }
168:
169: String validationMessage = pdi.validate();
170: if (validationMessage != null)
171: throw new DataIntegrityViolationException(
172: "Processing PersonDirInfo " + "element ["
173: + dirinfo + "] resulted in an invalid"
174: + " PersonDirInfo object: "
175: + validationMessage);
176: infos.add(pdi); // Add one LDAP or JDBC source to the list
177: }
178: return infos;
179:
180: }
181:
182: private PersonDirXmlParser() {
183: // this is a static function class. It is stateless. It implements no
184: // interface. This method keeps you from instantiating or extending it, since
185: // it is intended to be neither instantiated nor extended.
186: }
187: }
|