KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > security > permissions > impl > model > AbstractPermission


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.security.permissions.impl.model;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.alfresco.repo.security.permissions.impl.AbstractPermissionReference;
25 import org.alfresco.repo.security.permissions.impl.RequiredPermission;
26 import org.alfresco.service.namespace.NamespacePrefixResolver;
27 import org.alfresco.service.namespace.QName;
28 import org.dom4j.Attribute;
29 import org.dom4j.Element;
30
31 /**
32  * Support to read and store common properties for permissions
33  *
34  * @author andyh
35  */

36 public abstract class AbstractPermission extends AbstractPermissionReference implements XMLModelInitialisable
37 {
38     /* XML Constants */
39     
40     private static final String JavaDoc NAME = "name";
41     
42     private static final String JavaDoc REQUIRED_PERMISSION = "requiredPermission";
43     
44     private static final String JavaDoc RP_NAME = "name";
45
46     private static final String JavaDoc RP_TYPE = "type";
47     
48     private static final String JavaDoc RP_ON = "on";
49
50     private static final String JavaDoc RP_IMPLIES = "implies";
51     
52     private static final String JavaDoc NODE_ENTRY = "node";
53
54     private static final String JavaDoc PARENT_ENTRY = "parent";
55     
56     private static final String JavaDoc CHILDREN_ENTRY = "children";
57     
58     /* Instance variables */
59     
60     private String JavaDoc name;
61
62     private QName typeQName;
63
64     private Set JavaDoc<RequiredPermission> requiredPermissions = new HashSet JavaDoc<RequiredPermission>();
65     
66     public AbstractPermission(QName typeQName)
67     {
68         super();
69         this.typeQName = typeQName;
70     }
71
72     public void initialise(Element element, NamespacePrefixResolver nspr, PermissionModel permissionModel)
73     {
74         name = element.attributeValue(NAME);
75         
76         for (Iterator JavaDoc rpit = element.elementIterator(REQUIRED_PERMISSION); rpit.hasNext(); /**/)
77         {
78             QName qName;
79             Element requiredPermissionElement = (Element) rpit.next();
80             Attribute typeAttribute = requiredPermissionElement.attribute(RP_TYPE);
81             if (typeAttribute != null)
82             {
83                 qName = QName.createQName(typeAttribute.getStringValue(), nspr);
84             }
85             else
86             {
87                 qName = typeQName;
88             }
89
90             String JavaDoc requiredName = requiredPermissionElement.attributeValue(RP_NAME);
91
92             RequiredPermission.On on;
93             String JavaDoc onString = requiredPermissionElement.attributeValue(RP_ON);
94             if (onString.equalsIgnoreCase(NODE_ENTRY))
95             {
96                 on = RequiredPermission.On.NODE;
97             }
98             else if (onString.equalsIgnoreCase(PARENT_ENTRY))
99             {
100                 on = RequiredPermission.On.PARENT;
101             }
102             else if (onString.equalsIgnoreCase(CHILDREN_ENTRY))
103             {
104                 on = RequiredPermission.On.CHILDREN;
105             }
106             else
107             {
108                 throw new PermissionModelException("Required permission must specify parent or node for the on attribute.");
109             }
110             
111             boolean implies = false;
112             Attribute impliesAttribute = requiredPermissionElement.attribute(RP_IMPLIES);
113             if (impliesAttribute != null)
114             {
115                 implies = Boolean.parseBoolean(impliesAttribute.getStringValue());
116             }
117             
118             RequiredPermission rq = new RequiredPermission(qName, requiredName, on, implies);
119             
120             requiredPermissions.add(rq);
121         }
122     }
123
124     public final String JavaDoc getName()
125     {
126         return name;
127     }
128
129     public final Set JavaDoc<RequiredPermission> getRequiredPermissions()
130     {
131         return Collections.unmodifiableSet(requiredPermissions);
132     }
133
134     public final QName getTypeQName()
135     {
136         return typeQName;
137     }
138
139     public final QName getQName()
140     {
141         return getTypeQName();
142     }
143 }
144
Popular Tags