KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > ac > usecase > UsecaseRolesBuilder


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
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  */

17
18 /* $Id: UsecaseRolesBuilder.java 42660 2004-03-08 16:48:21Z gregor $ */
19
20 package org.apache.lenya.cms.ac.usecase;
21
22 import java.io.InputStream JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
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 JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33
34 public class UsecaseRolesBuilder implements InputStreamBuilder {
35
36     protected static final String JavaDoc USECASES_ELEMENT = "usecases";
37     protected static final String JavaDoc USECASE_ELEMENT = "usecase";
38     protected static final String JavaDoc ROLE_ELEMENT = "role";
39     protected static final String JavaDoc ID_ATTRIBUTE = "id";
40
41     /**
42      * @see org.apache.lenya.ac.cache.InputStreamBuilder#build(java.io.InputStream)
43      */

44     public Object JavaDoc build(InputStream JavaDoc stream) throws BuildException {
45
46         UsecaseRoles usecaseRoles = new UsecaseRoles();
47
48         Document JavaDoc document;
49         try {
50             document = DocumentHelper.readDocument(stream);
51         } catch (Exception JavaDoc 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 JavaDoc[] usecaseElements =
63             helper.getChildren(document.getDocumentElement(), USECASE_ELEMENT);
64         for (int i = 0; i < usecaseElements.length; i++) {
65             String JavaDoc usecaseId = usecaseElements[i].getAttribute(ID_ATTRIBUTE);
66             Element JavaDoc[] roleElements = helper.getChildren(usecaseElements[i], ROLE_ELEMENT);
67             Set JavaDoc roleIds = new HashSet JavaDoc();
68             for (int j = 0; j < roleElements.length; j++) {
69                 String JavaDoc roleId = roleElements[j].getAttribute(ID_ATTRIBUTE);
70                 roleIds.add(roleId);
71             }
72             String JavaDoc[] roleIdArray = (String JavaDoc[]) roleIds.toArray(new String JavaDoc[roleIds.size()]);
73             usecaseRoles.setRoles(usecaseId, roleIdArray);
74         }
75         return usecaseRoles;
76     }
77
78 }
79
Popular Tags