01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: Xml2MemoryUsers.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.authentication.credentialsmanagers;
09:
10: import com.uwyn.rife.xml.Xml2Data;
11: import com.uwyn.rife.xml.exceptions.XmlErrorException;
12: import java.util.LinkedHashMap;
13: import org.xml.sax.Attributes;
14:
15: public class Xml2MemoryUsers extends Xml2Data {
16: private LinkedHashMap<String, RoleUserAttributes> mUsers = null;
17: private StringBuilder mCharacterData = null;
18: private RoleUserAttributes mCurrentAttributes = null;
19:
20: public LinkedHashMap<String, RoleUserAttributes> getUsers() {
21: return mUsers;
22: }
23:
24: protected void clear() {
25: mUsers = new LinkedHashMap<String, RoleUserAttributes>();
26: }
27:
28: public void startDocument() {
29: clear();
30: }
31:
32: public void endDocument() {
33: }
34:
35: public void startElement(String namespaceURI, String localName,
36: String qName, Attributes atts) {
37: if (qName.equals("credentials")) {
38: // do nothing
39: } else if (qName.equals("user")) {
40: String login = atts.getValue("login");
41: String userid = atts.getValue("userid");
42:
43: mCurrentAttributes = new RoleUserAttributes();
44: if (userid != null && userid.length() > 0) {
45: try {
46: mCurrentAttributes.setUserId(Integer
47: .parseInt(userid));
48: } catch (NumberFormatException e) {
49: throw new XmlErrorException("Invalid userid '"
50: + userid + "'", e);
51: }
52: }
53:
54: mUsers.put(login, mCurrentAttributes);
55: } else if (qName.equals("password")) {
56: mCharacterData = new StringBuilder();
57: } else if (qName.equals("role")) {
58: mCharacterData = new StringBuilder();
59: } else {
60: throw new XmlErrorException("Unsupport element name '"
61: + qName + "'.");
62: }
63: }
64:
65: public void endElement(String namespaceURI, String localName,
66: String qName) {
67: if (qName.equals("password")) {
68: String password = mCharacterData.toString();
69: mCurrentAttributes.setPassword(password);
70: mCharacterData = new StringBuilder();
71: } else if (qName.equals("role")) {
72: String role = mCharacterData.toString();
73: mCurrentAttributes.addRole(role);
74: mCharacterData = new StringBuilder();
75: }
76: }
77:
78: public void characters(char[] ch, int start, int length) {
79: if (length > 0) {
80: mCharacterData
81: .append(String.copyValueOf(ch, start, length));
82: }
83: }
84: }
|