1 23 24 package com.rift.coad.lib.security.user.xml; 25 26 import java.util.Map ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.io.BufferedReader ; 31 import java.io.File ; 32 import java.io.FileReader ; 33 import javax.xml.parsers.SAXParserFactory ; 34 import javax.xml.parsers.SAXParser ; 35 import org.xml.sax.InputSource ; 36 import org.xml.sax.helpers.DefaultHandler ; 37 import org.xml.sax.SAXException ; 38 import org.xml.sax.Attributes ; 39 40 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 50 public class XMLUserParser { 51 52 56 public class XMLUserHandler extends DefaultHandler { 57 58 private final static String USERS = "users"; 60 private final static String USER = "user"; 61 private final static String USER_NAME = "name"; 62 private final static String USER_PASSWD = "password"; 63 private final static String PRINCIPAL = "principal"; 64 65 private Map users = null; 67 68 private boolean inUsers = false; 70 private boolean inUser = false; 71 private boolean inPrincipal = false; 72 private String inData = null; 73 private UserData userData = null; 74 75 76 77 82 public XMLUserHandler(Map users) { 83 this.users = users; 84 } 85 86 89 public void startElement(String uri, String localName, String qName, 90 Attributes attributes) throws SAXException { 91 try { 92 if (qName.compareToIgnoreCase(USERS) == 0) { 94 inUsers = true; 95 } else if (inUsers && 96 qName.compareToIgnoreCase(USER) == 0) { 97 String username = (String )attributes.getValue(USER_NAME); 98 String password = (String )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 (); 104 inPrincipal = true; 105 } 106 } catch (Exception ex) { 107 throw new SAXException ("Failed to process the user information :" 108 + ex.getMessage(),ex); 109 } 110 } 111 112 113 116 public void characters(char[] ch, int start, int length) { 117 if (inPrincipal) { 118 inData += new String (ch,start,length); 119 } 120 } 121 122 125 public void endElement(String uri, String localName, String qName) 126 throws SAXException { 127 try { 128 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 ex) { 141 throw new SAXException ("Failed to set the end element : " + 142 ex.getMessage(),ex); 143 } 144 } 145 146 } 147 148 private final static String PASSWD_FILE = "password_file"; 150 151 private Map users = null; 153 154 157 public XMLUserParser(Map 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 parser = SAXParserFactory.newInstance().newSAXParser(); 165 InputSource source = new InputSource ( 166 new FileReader (new File (config.getString(PASSWD_FILE)))); 167 parser.parse(source,handler); 168 } catch (Exception ex) { 169 throw new UserException("Failed to parse the user xml file : " + 170 ex.getMessage(),ex); 171 } 172 } 173 174 175 180 public Map getUsers() { 181 return users; 182 } 183 184 } 185 | Popular Tags |