1 16 package org.outerj.daisy.frontend; 17 18 import org.outerj.daisy.repository.Repository; 19 import org.outerj.daisy.repository.RepositoryException; 20 import org.outerj.daisy.repository.user.UserManager; 21 import org.outerj.daisy.repository.user.User; 22 import org.xml.sax.ContentHandler ; 23 import org.xml.sax.SAXException ; 24 import org.xml.sax.Attributes ; 25 import org.apache.cocoon.xml.AttributesImpl; 26 import org.apache.excalibur.xml.sax.XMLizable; 27 28 35 public class UserInfoStreamer implements XMLizable { 36 private static final Attributes EMPTY_ATTRIBUTES = new AttributesImpl(); 37 private final Repository repository; 38 39 public UserInfoStreamer(Repository repository) { 40 this.repository = repository; 41 } 42 43 public void toSAX(ContentHandler contentHandler) throws SAXException { 44 try { 45 streamUserInfo(repository, contentHandler); 46 } catch (RepositoryException e) { 47 throw new SAXException (e); 48 } 49 } 50 51 public static void streamUserInfo(Repository repository, ContentHandler contentHandler) throws SAXException , RepositoryException { 52 contentHandler.startElement("", "user", "user", new org.xml.sax.helpers.AttributesImpl ()); 53 generateStringElement("name", repository.getUserDisplayName(), contentHandler); 54 generateStringElement("login", repository.getUserLogin(), contentHandler); 55 generateStringElement("id", String.valueOf(repository.getUserId()), contentHandler); 56 contentHandler.startElement("", "activeRoles", "activeRoles", new org.xml.sax.helpers.AttributesImpl ()); 57 long[] activeRoleIds = repository.getActiveRoleIds(); 58 UserManager userManager = repository.getUserManager(); 59 for (int i = 0; i < activeRoleIds.length; i++) { 60 org.xml.sax.helpers.AttributesImpl attrs = new org.xml.sax.helpers.AttributesImpl (); 61 attrs.addAttribute("", "id", "id", "CDATA", String.valueOf(activeRoleIds[i])); 62 attrs.addAttribute("", "name", "name", "CDATA", userManager.getRoleDisplayName(activeRoleIds[i])); 63 contentHandler.startElement("", "role", "role", attrs); 64 contentHandler.endElement("", "role", "role"); 65 } 66 contentHandler.endElement("", "activeRoles", "activeRoles"); 67 68 User user = userManager.getUser(repository.getUserId(), false); 69 generateStringElement("updateableByUser", String.valueOf(user.isUpdateableByUser()), contentHandler); 70 71 org.xml.sax.helpers.AttributesImpl availableRolesAttrs = new org.xml.sax.helpers.AttributesImpl (); 72 if (user.getDefaultRole() != null) 73 availableRolesAttrs.addAttribute("", "default", "default", "CDATA", user.getDefaultRole().getName()); 74 contentHandler.startElement("", "availableRoles", "availableRoles", availableRolesAttrs); 75 long roles[] = repository.getAvailableRoles(); 76 for (int i = 0; i < roles.length; i++) { 77 org.xml.sax.helpers.AttributesImpl attrs = new org.xml.sax.helpers.AttributesImpl (); 78 attrs.addAttribute("", "id", "id", "CDATA", String.valueOf(roles[i])); 79 attrs.addAttribute("", "name", "name", "CDATA", userManager.getRoleDisplayName(roles[i])); 80 contentHandler.startElement("", "role", "role", attrs); 81 contentHandler.endElement("", "role", "role"); 82 } 83 contentHandler.endElement("", "availableRoles", "availableRoles"); 84 85 contentHandler.endElement("", "user", "user"); 86 } 87 88 private static void generateStringElement(String name, String value, ContentHandler contentHandler) throws SAXException { 89 contentHandler.startElement("", name, name, EMPTY_ATTRIBUTES); 90 contentHandler.characters(value.toCharArray(), 0, value.length()); 91 contentHandler.endElement("", name, name); 92 } 93 } 94 | Popular Tags |