001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * XMLUserParser.java
020: *
021: * The parser for the XML user file.
022: */
023:
024: package com.rift.coad.lib.security.user.xml;
025:
026: // java imports
027: import java.util.Map;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.io.BufferedReader;
031: import java.io.File;
032: import java.io.FileReader;
033: import javax.xml.parsers.SAXParserFactory;
034: import javax.xml.parsers.SAXParser;
035: import org.xml.sax.InputSource;
036: import org.xml.sax.helpers.DefaultHandler;
037: import org.xml.sax.SAXException;
038: import org.xml.sax.Attributes;
039:
040: // coadunation imports
041: import com.rift.coad.lib.configuration.Configuration;
042: import com.rift.coad.lib.configuration.ConfigurationFactory;
043: import com.rift.coad.lib.security.user.UserException;
044:
045: /**
046: * The parser for the XML user file.
047: *
048: * @author Brett Chaldecott
049: */
050: public class XMLUserParser {
051:
052: /**
053: * The inner class responsible for handling the contents of the Coadunation
054: * xml user document.
055: */
056: public class XMLUserHandler extends DefaultHandler {
057:
058: // class constant static member variables
059: private final static String USERS = "users";
060: private final static String USER = "user";
061: private final static String USER_NAME = "name";
062: private final static String USER_PASSWD = "password";
063: private final static String PRINCIPAL = "principal";
064:
065: // the member variables
066: private Map users = null;
067:
068: // tracking variables
069: private boolean inUsers = false;
070: private boolean inUser = false;
071: private boolean inPrincipal = false;
072: private String inData = null;
073: private UserData userData = null;
074:
075: /**
076: * The constructor of the xml users handler.
077: *
078: * @param users The list of users.
079: */
080: public XMLUserHandler(Map users) {
081: this .users = users;
082: }
083:
084: /**
085: * Parse the starting element
086: */
087: public void startElement(String uri, String localName,
088: String qName, Attributes attributes)
089: throws SAXException {
090: try {
091: // handle a package and retrieve the value information
092: if (qName.compareToIgnoreCase(USERS) == 0) {
093: inUsers = true;
094: } else if (inUsers
095: && qName.compareToIgnoreCase(USER) == 0) {
096: String username = (String) attributes
097: .getValue(USER_NAME);
098: String password = (String) attributes
099: .getValue(USER_PASSWD);
100: userData = new UserData(username, password);
101: inUser = true;
102: } else if (inUsers && inUser
103: && qName.compareToIgnoreCase(PRINCIPAL) == 0) {
104: inData = new String();
105: inPrincipal = true;
106: }
107: } catch (Exception ex) {
108: throw new SAXException(
109: "Failed to process the user information :"
110: + ex.getMessage(), ex);
111: }
112: }
113:
114: /**
115: * Read in the characters
116: */
117: public void characters(char[] ch, int start, int length) {
118: if (inPrincipal) {
119: inData += new String(ch, start, length);
120: }
121: }
122:
123: /**
124: * Handle the end of an element
125: */
126: public void endElement(String uri, String localName,
127: String qName) throws SAXException {
128: try {
129: // handle a package and retrieve the value information
130: if (qName.compareToIgnoreCase(USERS) == 0) {
131: inUsers = false;
132: } else if (inUsers
133: && qName.compareToIgnoreCase(USER) == 0) {
134: users.put(userData.getUsername(), userData);
135: inUser = false;
136: } else if (inUsers && inUser
137: && qName.compareToIgnoreCase(PRINCIPAL) == 0) {
138: userData.addPrincipal(inData.trim());
139: inPrincipal = false;
140: }
141: } catch (Exception ex) {
142: throw new SAXException(
143: "Failed to set the end element : "
144: + ex.getMessage(), ex);
145: }
146: }
147:
148: }
149:
150: // class static variabls
151: private final static String PASSWD_FILE = "password_file";
152:
153: // the classes private member variables
154: private Map users = null;
155:
156: /**
157: * Creates a new instance of XMLUserParser
158: */
159: public XMLUserParser(Map users) throws UserException {
160: try {
161: Configuration config = ConfigurationFactory.getInstance()
162: .getConfig(getClass());
163: this .users = users;
164:
165: XMLUserHandler handler = new XMLUserHandler(users);
166: SAXParser parser = SAXParserFactory.newInstance()
167: .newSAXParser();
168: InputSource source = new InputSource(new FileReader(
169: new File(config.getString(PASSWD_FILE))));
170: parser.parse(source, handler);
171: } catch (Exception ex) {
172: throw new UserException(
173: "Failed to parse the user xml file : "
174: + ex.getMessage(), ex);
175: }
176: }
177:
178: /**
179: * This method returns the map containing the users.
180: *
181: * @return The object containing the list of users.
182: */
183: public Map getUsers() {
184: return users;
185: }
186:
187: }
|