1 17 18 19 20 package org.apache.lenya.cms.ac.usecase; 21 22 import java.io.InputStream ; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 26 import org.apache.lenya.ac.AccessController; 27 import org.apache.lenya.ac.cache.BuildException; 28 import org.apache.lenya.ac.cache.InputStreamBuilder; 29 import org.apache.lenya.xml.DocumentHelper; 30 import org.apache.lenya.xml.NamespaceHelper; 31 import org.w3c.dom.Document ; 32 import org.w3c.dom.Element ; 33 34 public class UsecaseRolesBuilder implements InputStreamBuilder { 35 36 protected static final String USECASES_ELEMENT = "usecases"; 37 protected static final String USECASE_ELEMENT = "usecase"; 38 protected static final String ROLE_ELEMENT = "role"; 39 protected static final String ID_ATTRIBUTE = "id"; 40 41 44 public Object build(InputStream stream) throws BuildException { 45 46 UsecaseRoles usecaseRoles = new UsecaseRoles(); 47 48 Document document; 49 try { 50 document = DocumentHelper.readDocument(stream); 51 } catch (Exception e) { 52 throw new BuildException(e); 53 } 54 assert document.getDocumentElement().getLocalName().equals(USECASES_ELEMENT); 55 56 NamespaceHelper helper = 57 new NamespaceHelper( 58 AccessController.NAMESPACE, 59 AccessController.DEFAULT_PREFIX, 60 document); 61 62 Element [] usecaseElements = 63 helper.getChildren(document.getDocumentElement(), USECASE_ELEMENT); 64 for (int i = 0; i < usecaseElements.length; i++) { 65 String usecaseId = usecaseElements[i].getAttribute(ID_ATTRIBUTE); 66 Element [] roleElements = helper.getChildren(usecaseElements[i], ROLE_ELEMENT); 67 Set roleIds = new HashSet (); 68 for (int j = 0; j < roleElements.length; j++) { 69 String roleId = roleElements[j].getAttribute(ID_ATTRIBUTE); 70 roleIds.add(roleId); 71 } 72 String [] roleIdArray = (String []) roleIds.toArray(new String [roleIds.size()]); 73 usecaseRoles.setRoles(usecaseId, roleIdArray); 74 } 75 return usecaseRoles; 76 } 77 78 } 79 | Popular Tags |