KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > identity > xml > IdentityXmlParser


1 package org.jbpm.identity.xml;
2
3 import java.io.InputStream JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import org.dom4j.Document;
11 import org.dom4j.DocumentException;
12 import org.dom4j.Element;
13 import org.dom4j.io.SAXReader;
14 import org.jbpm.identity.Entity;
15 import org.jbpm.identity.Group;
16 import org.jbpm.identity.Membership;
17 import org.jbpm.identity.User;
18 import org.jbpm.instantiation.ClassLoaderUtil;
19
20 public class IdentityXmlParser {
21   
22   List JavaDoc entities = new ArrayList JavaDoc();
23   Map JavaDoc users = new HashMap JavaDoc();
24   Map JavaDoc groups = new HashMap JavaDoc();
25
26   public static Entity[] parseEntitiesResource(String JavaDoc resource) {
27     return new IdentityXmlParser().parse(resource);
28   }
29
30   public static Entity[] parseEntitiesResource(InputStream JavaDoc inputStream) {
31     return new IdentityXmlParser().parse(inputStream);
32   }
33
34   public Entity[] parse(String JavaDoc resource) {
35     return parse(ClassLoaderUtil.getStream(resource));
36   }
37
38   public Entity[] parse(InputStream JavaDoc inputStream) {
39     Document document;
40     try {
41       document = new SAXReader().read(inputStream);
42     } catch (DocumentException e) {
43       throw new RuntimeException JavaDoc("couldn't parse identities from stream '"+inputStream+"'");
44     }
45     Element identitiesRootElement = document.getRootElement();
46     
47     readUsers(identitiesRootElement.elements("user"));
48     readGroups(identitiesRootElement.elements("group"));
49     readParentGroups(identitiesRootElement.elements("group"));
50     readMemberships(identitiesRootElement.elements("membership"));
51
52     return (Entity[]) entities.toArray(new Entity[entities.size()]);
53   }
54
55   private void readUsers(List JavaDoc userElements) {
56     Iterator JavaDoc iter = userElements.iterator();
57     while (iter.hasNext()) {
58       Element userElement = (Element) iter.next();
59       String JavaDoc name = userElement.attributeValue("name");
60       String JavaDoc email = userElement.attributeValue("email");
61       String JavaDoc password = userElement.attributeValue("password");
62       
63       User user = new User(name);
64       user.setEmail(email);
65       user.setPassword(password);
66       users.put(name, user);
67       entities.add(user);
68     }
69   }
70
71   private void readGroups(List JavaDoc groupElements) {
72     Iterator JavaDoc iter = groupElements.iterator();
73     while (iter.hasNext()) {
74       Element groupElement = (Element) iter.next();
75       String JavaDoc name = groupElement.attributeValue("name");
76       String JavaDoc type = groupElement.attributeValue("type");
77       
78       Group group = new Group(name);
79       group.setType(type);
80       entities.add(group);
81       groups.put(name, group);
82     }
83   }
84
85   private void readParentGroups(List JavaDoc groupElements) {
86     Iterator JavaDoc iter = groupElements.iterator();
87     while (iter.hasNext()) {
88       Element groupElement = (Element) iter.next();
89       String JavaDoc childName = groupElement.attributeValue("name");
90       String JavaDoc parentName = groupElement.attributeValue("parent");
91       
92       if (parentName!=null) {
93         Group parent = (Group) groups.get(parentName);
94         if (parent==null) throw new RuntimeException JavaDoc("unexisting parent group '"+parentName+"'");
95         Group child = (Group) groups.get(childName);
96         
97         parent.addChild(child);
98       }
99     }
100   }
101
102   private void readMemberships(List JavaDoc membershipElements) {
103     Iterator JavaDoc iter = membershipElements.iterator();
104     while (iter.hasNext()) {
105       Element membershipElement = (Element) iter.next();
106       String JavaDoc role = membershipElement.attributeValue("role");
107       String JavaDoc userName = membershipElement.attributeValue("user");
108       User user = (User) users.get(userName);
109       if (user==null) throw new RuntimeException JavaDoc("unexisting membership user '"+userName+"'");
110       String JavaDoc groupName = membershipElement.attributeValue("group");
111       Group group = (Group) groups.get(groupName);
112       if (group==null) throw new RuntimeException JavaDoc("unexisting membership group '"+groupName+"'");
113       
114       Membership membership = new Membership();
115       membership.setRole(role);
116       group.addMembership(membership);
117       user.addMembership(membership);
118       
119       entities.add(membership);
120     }
121   }
122 }
123
Popular Tags