KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > UserInfoStreamer


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24 import org.xml.sax.Attributes JavaDoc;
25 import org.apache.cocoon.xml.AttributesImpl;
26 import org.apache.excalibur.xml.sax.XMLizable;
27
28 /**
29  * Utility class to stream information about the current repository user as SAX events.
30  *
31  * At the time of this writing, this was both used in the class {@link PageContext},
32  * and in the document.xml template (which is the start of the document type specific
33  * styling pipelines).
34  */

35 public class UserInfoStreamer implements XMLizable {
36     private static final Attributes JavaDoc 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 JavaDoc contentHandler) throws SAXException JavaDoc {
44         try {
45             streamUserInfo(repository, contentHandler);
46         } catch (RepositoryException e) {
47             throw new SAXException JavaDoc(e);
48         }
49     }
50
51     public static void streamUserInfo(Repository repository, ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc, RepositoryException {
52         contentHandler.startElement("", "user", "user", new org.xml.sax.helpers.AttributesImpl JavaDoc());
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 JavaDoc());
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 JavaDoc attrs = new org.xml.sax.helpers.AttributesImpl JavaDoc();
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 JavaDoc availableRolesAttrs = new org.xml.sax.helpers.AttributesImpl JavaDoc();
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 JavaDoc attrs = new org.xml.sax.helpers.AttributesImpl JavaDoc();
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 JavaDoc name, String JavaDoc value, ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
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