KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > auth > acl > AccessControlParser


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.filesys.server.auth.acl;
18
19 import org.alfresco.config.ConfigElement;
20
21 /**
22  * Access Control Parser Class
23  * <p>
24  * Creates an AccessControl instance by parsing a set of name/value parameters.
25  */

26 public abstract class AccessControlParser
27 {
28
29     // Constants
30
//
31
// Standard parameter names
32

33     public final static String JavaDoc ParameterAccess = "access";
34
35     // Access control type names
36

37     private final static String JavaDoc[] _accessTypes = { "None", "Read", "Write" };
38
39     /**
40      * Return the access control type name that uniquely identifies this type of access control.
41      *
42      * @return String
43      */

44     public abstract String JavaDoc getType();
45
46     /**
47      * Create an AccessControl instance by parsing the set of name/value parameters
48      *
49      * @param params ConfigElement
50      * @return AccessControl
51      * @exception ACLParseException
52      */

53     public abstract AccessControl createAccessControl(ConfigElement params) throws ACLParseException;
54
55     /**
56      * Find the access parameter and parse the value
57      *
58      * @param params ConfigElement
59      * @return int
60      * @exception ACLParseException
61      */

62     protected final int parseAccessType(ConfigElement params) throws ACLParseException
63     {
64
65         // Check if the parameter list is valid
66

67         if (params == null)
68             throw new ACLParseException("Empty parameter list");
69
70         // Find the access type parameter
71

72         String JavaDoc accessType = params.getAttribute(ParameterAccess);
73
74         if (accessType == null || accessType.length() == 0)
75             throw new ACLParseException("Required parameter 'access' missing");
76
77         // Parse the access type value
78

79         return parseAccessTypeString(accessType);
80     }
81
82     /**
83      * Parse the access level type and validate
84      *
85      * @param accessType String
86      * @return int
87      * @exception ACLParseException
88      */

89     public static final int parseAccessTypeString(String JavaDoc accessType) throws ACLParseException
90     {
91
92         // Check if the access type is valid
93

94         if (accessType == null || accessType.length() == 0)
95             throw new ACLParseException("Empty access type string");
96
97         // Parse the access type value
98

99         int access = -1;
100
101         for (int i = 0; i < _accessTypes.length; i++)
102         {
103
104             // Check if the access type matches the current type
105

106             if (accessType.equalsIgnoreCase(_accessTypes[i]))
107                 access = i;
108         }
109
110         // Check if we found a valid access type
111

112         if (access == -1)
113             throw new ACLParseException("Invalid access type, " + accessType);
114
115         // Return the access type
116

117         return access;
118     }
119
120     /**
121      * Return the parser details as a string
122      *
123      * @return String
124      */

125     public String JavaDoc toString()
126     {
127         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
128
129         str.append("[");
130         str.append(getType());
131         str.append("]");
132
133         return str.toString();
134     }
135 }
136
Popular Tags