KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > security > user > xml > XMLUserParser


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * XMLUserParser.java
20  *
21  * The parser for the XML user file.
22  */

23
24 package com.rift.coad.lib.security.user.xml;
25
26 // java imports
27
import java.util.Map JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.io.BufferedReader JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.FileReader JavaDoc;
33 import javax.xml.parsers.SAXParserFactory JavaDoc;
34 import javax.xml.parsers.SAXParser JavaDoc;
35 import org.xml.sax.InputSource JavaDoc;
36 import org.xml.sax.helpers.DefaultHandler JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
38 import org.xml.sax.Attributes JavaDoc;
39
40 // coadunation imports
41
import com.rift.coad.lib.configuration.Configuration;
42 import com.rift.coad.lib.configuration.ConfigurationFactory;
43 import com.rift.coad.lib.security.user.UserException;
44
45 /**
46  * The parser for the XML user file.
47  *
48  * @author Brett Chaldecott
49  */

50 public class XMLUserParser {
51     
52     /**
53      * The inner class responsible for handling the contents of the Coadunation
54      * xml user document.
55      */

56     public class XMLUserHandler extends DefaultHandler JavaDoc {
57         
58         // class constant static member variables
59
private final static String JavaDoc USERS = "users";
60         private final static String JavaDoc USER = "user";
61         private final static String JavaDoc USER_NAME = "name";
62         private final static String JavaDoc USER_PASSWD = "password";
63         private final static String JavaDoc PRINCIPAL = "principal";
64         
65         // the member variables
66
private Map JavaDoc users = null;
67         
68         // tracking variables
69
private boolean inUsers = false;
70         private boolean inUser = false;
71         private boolean inPrincipal = false;
72         private String JavaDoc inData = null;
73         private UserData userData = null;
74         
75         
76         
77         /**
78          * The constructor of the xml users handler.
79          *
80          * @param users The list of users.
81          */

82         public XMLUserHandler(Map JavaDoc users) {
83             this.users = users;
84         }
85         
86         /**
87          * Parse the starting element
88          */

89         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
90                          Attributes JavaDoc attributes) throws SAXException JavaDoc {
91             try {
92                 // handle a package and retrieve the value information
93
if (qName.compareToIgnoreCase(USERS) == 0) {
94                     inUsers = true;
95                 } else if (inUsers &&
96                         qName.compareToIgnoreCase(USER) == 0) {
97                     String JavaDoc username = (String JavaDoc)attributes.getValue(USER_NAME);
98                     String JavaDoc password = (String JavaDoc)attributes.getValue(USER_PASSWD);
99                     userData = new UserData(username,password);
100                     inUser = true;
101                 } else if (inUsers && inUser &&
102                         qName.compareToIgnoreCase(PRINCIPAL) == 0) {
103                     inData = new String JavaDoc();
104                     inPrincipal = true;
105                 }
106             } catch (Exception JavaDoc ex) {
107                 throw new SAXException JavaDoc("Failed to process the user information :"
108                         + ex.getMessage(),ex);
109             }
110         }
111         
112         
113         /**
114          * Read in the characters
115          */

116         public void characters(char[] ch, int start, int length) {
117             if (inPrincipal) {
118                 inData += new String JavaDoc(ch,start,length);
119             }
120         }
121         
122         /**
123          * Handle the end of an element
124          */

125         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
126                 throws SAXException JavaDoc {
127             try {
128                 // handle a package and retrieve the value information
129
if (qName.compareToIgnoreCase(USERS) == 0) {
130                     inUsers = false;
131                 } else if (inUsers &&
132                         qName.compareToIgnoreCase(USER) == 0) {
133                     users.put(userData.getUsername(),userData);
134                     inUser = false;
135                 } else if (inUsers && inUser &&
136                         qName.compareToIgnoreCase(PRINCIPAL) == 0) {
137                     userData.addPrincipal(inData.trim());
138                     inPrincipal = false;
139                 }
140             } catch (Exception JavaDoc ex) {
141                 throw new SAXException JavaDoc("Failed to set the end element : " +
142                         ex.getMessage(),ex);
143             }
144         }
145         
146     }
147     
148     // class static variabls
149
private final static String JavaDoc PASSWD_FILE = "password_file";
150     
151     // the classes private member variables
152
private Map JavaDoc users = null;
153     
154     /**
155      * Creates a new instance of XMLUserParser
156      */

157     public XMLUserParser(Map JavaDoc users) throws UserException {
158         try {
159             Configuration config = ConfigurationFactory.getInstance().
160                     getConfig(getClass());
161             this.users = users;
162             
163             XMLUserHandler handler = new XMLUserHandler(users);
164             SAXParser JavaDoc parser = SAXParserFactory.newInstance().newSAXParser();
165             InputSource JavaDoc source = new InputSource JavaDoc(
166                     new FileReader JavaDoc(new File JavaDoc(config.getString(PASSWD_FILE))));
167             parser.parse(source,handler);
168         } catch (Exception JavaDoc ex) {
169             throw new UserException("Failed to parse the user xml file : " +
170                     ex.getMessage(),ex);
171         }
172     }
173     
174     
175     /**
176      * This method returns the map containing the users.
177      *
178      * @return The object containing the list of users.
179      */

180     public Map JavaDoc getUsers() {
181         return users;
182     }
183     
184 }
185
Popular Tags