Source Code Cross Referenced for XMLUserParser.java in  » Net » Coadunation_1.0.1 » com » rift » coad » lib » security » user » xml » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Coadunation_1.0.1 » com.rift.coad.lib.security.user.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www_.__j_av___a___2__s__.co_m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.