KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > aciitemeditor > sourceeditor > ACICodeScanner


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20 package org.apache.directory.ldapstudio.aciitemeditor.sourceeditor;
21
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.eclipse.jface.text.rules.IRule;
27 import org.eclipse.jface.text.rules.IToken;
28 import org.eclipse.jface.text.rules.IWhitespaceDetector;
29 import org.eclipse.jface.text.rules.IWordDetector;
30 import org.eclipse.jface.text.rules.RuleBasedScanner;
31 import org.eclipse.jface.text.rules.SingleLineRule;
32 import org.eclipse.jface.text.rules.Token;
33 import org.eclipse.jface.text.rules.WhitespaceRule;
34 import org.eclipse.jface.text.rules.WordRule;
35
36
37 /**
38  * Scanner used to analyse ACI code. Allows syntax coloring.
39  *
40  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
41  * @version $Rev$, $Date$
42  */

43 public class ACICodeScanner extends RuleBasedScanner
44 {
45     /** 'identificationTag' keyword */
46     public static final String JavaDoc identificationTagPartition = "identificationTag"; //$NON-NLS-1$
47

48     /** 'precedence' keyword */
49     public static final String JavaDoc precedencePartition = "precedence"; //$NON-NLS-1$
50

51     /** 'authenticationLevel' keyword */
52     public static final String JavaDoc authenticationLevelPartition = "authenticationLevel"; //$NON-NLS-1$
53

54     /** Keywords for the itemOrUserFirst Section */
55     public static final String JavaDoc[] itemOrUserFirstSectionPartition = new String JavaDoc[]
56         { "itemOrUserFirst", "itemFirst", "userFirst" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
57

58     /** Keywords for 'userFirst' section */
59     public static final String JavaDoc[] userSection = new String JavaDoc[]
60         { "userClasses", "userPermissions" }; //$NON-NLS-1$ //$NON-NLS-2$
61

62     /** Keywords for AciItems values */
63     public static final String JavaDoc[] aciKeywords = new String JavaDoc[]
64         { "protectedItems", //$NON-NLS-1$
65
"itemPermissions", //$NON-NLS-1$
66
"entry", //$NON-NLS-1$
67
"allUserAttributeTypes", //$NON-NLS-1$
68
"attributeType", //$NON-NLS-1$
69
"allAttributeValues", //$NON-NLS-1$
70
"allUserAttributeTypesAndValues", //$NON-NLS-1$
71
"attributeValue", //$NON-NLS-1$
72
"selfValue", //$NON-NLS-1$
73
"rangeOfValues", //$NON-NLS-1$
74
"maxValueCount", //$NON-NLS-1$
75
"maxImmSub", //$NON-NLS-1$
76
"restrictedBy", //$NON-NLS-1$
77
"classes", //$NON-NLS-1$
78
"grantsAndDenials", //$NON-NLS-1$
79
"allUsers", //$NON-NLS-1$
80
"thisEntry", //$NON-NLS-1$
81
"name", //$NON-NLS-1$
82
"userGroup", //$NON-NLS-1$
83
"subtree", //$NON-NLS-1$
84
"type", //$NON-NLS-1$
85
"valuesIn", //$NON-NLS-1$
86
"none", //$NON-NLS-1$
87
"simple", //$NON-NLS-1$
88
"strong" }; //$NON-NLS-1$
89

90     /** Keywords for grant values */
91     public static final String JavaDoc[] aciGrantValues = new String JavaDoc[]
92         { "grantAdd", //$NON-NLS-1$
93
"grantDiscloseOnError", //$NON-NLS-1$
94
"grantRead", //$NON-NLS-1$
95
"grantRemove", //$NON-NLS-1$
96
"grantBrowse", //$NON-NLS-1$
97
"grantExport", //$NON-NLS-1$
98
"grantImport", //$NON-NLS-1$
99
"grantModify", //$NON-NLS-1$
100
"grantRename", //$NON-NLS-1$
101
"grantReturnDN", //$NON-NLS-1$
102
"grantCompare", //$NON-NLS-1$
103
"grantFilterMatch", //$NON-NLS-1$
104
"grantInvoke", }; //$NON-NLS-1$
105

106     /** Keywords for deny values */
107     public static final String JavaDoc[] aciDenyValues = new String JavaDoc[]
108         { "denyAdd", //$NON-NLS-1$
109
"denyDiscloseOnError", //$NON-NLS-1$
110
"denyRead", //$NON-NLS-1$
111
"denyRemove", //$NON-NLS-1$
112
"denyBrowse", //$NON-NLS-1$
113
"denyExport", //$NON-NLS-1$
114
"denyImport", //$NON-NLS-1$
115
"denyModify", //$NON-NLS-1$
116
"denyRename", //$NON-NLS-1$
117
"denyReturnDN", //$NON-NLS-1$
118
"denyCompare", //$NON-NLS-1$
119
"denyFilterMatch", //$NON-NLS-1$
120
"denyInvoke" }; //$NON-NLS-1$
121

122
123     /**
124      * Creates a new instance of AciCodeScanner.
125      *
126      * @param provider
127      * the provider
128      */

129     public ACICodeScanner( ACITextAttributeProvider provider )
130     {
131         List JavaDoc<IRule> rules = new ArrayList JavaDoc<IRule>();
132
133         IToken keyword = new Token( provider.getAttribute( ACITextAttributeProvider.KEYWORD_ATTRIBUTE ) );
134         IToken undefined = new Token( provider.getAttribute( ACITextAttributeProvider.DEFAULT_ATTRIBUTE ) );
135         IToken string = new Token( provider.getAttribute( ACITextAttributeProvider.STRING_ATTRIBUTE ) );
136         IToken grantValue = new Token( provider.getAttribute( ACITextAttributeProvider.GRANT_VALUE ) );
137         IToken denyValue = new Token( provider.getAttribute( ACITextAttributeProvider.DENY_VALUE ) );
138         IToken identification = new Token( provider.getAttribute( ACITextAttributeProvider.IDENTIFICATION_ATTRIBUTE ) );
139         IToken precedence = new Token( provider.getAttribute( ACITextAttributeProvider.PRECEDENCE_ATTRIBUTE ) );
140         IToken authenticationLevel = new Token( provider
141             .getAttribute( ACITextAttributeProvider.AUTHENTICATIONLEVEL_ATTRIBUTE ) );
142         IToken itemOrUserFirst = new Token( provider.getAttribute( ACITextAttributeProvider.ITEMORUSERFIRST_ATTRIBUTE ) );
143         IToken user = new Token( provider.getAttribute( ACITextAttributeProvider.USER_ATTRIBUTE ) );
144
145         // Rules for Strings
146
rules.add( new SingleLineRule( "\"", "\"", string, '\0', true ) ); //$NON-NLS-1$ //$NON-NLS-2$
147
rules.add( new SingleLineRule( "'", "'", string, '\0', true ) ); //$NON-NLS-1$ //$NON-NLS-2$
148
// Generic rule for whitespaces
149
rules.add( new WhitespaceRule( new IWhitespaceDetector()
150         {
151             /**
152              * Indicates if the given character is a whitespace
153              * @param c the character to analyse
154              * @return <code>true</code> if the character is to be considered as a whitespace, <code>false</code> if not.
155              * @see org.eclipse.jface.text.rules.IWhitespaceDetector#isWhitespace(char)
156              */

157             public boolean isWhitespace( char c )
158             {
159                 return Character.isWhitespace( c );
160             }
161         } ) );
162
163         // If the word isn't in the List, returns undefined
164
WordRule wr = new WordRule( new AciWordDetector(), undefined );
165
166         // Adding Keywords
167
for ( int i = 0; i < aciKeywords.length; ++i )
168         {
169             wr.addWord( aciKeywords[i], keyword );
170         }
171
172         // Adding GrantValues
173
for ( int i = 0; i < aciGrantValues.length; ++i )
174         {
175             wr.addWord( aciGrantValues[i], grantValue );
176         }
177
178         // Adding DenyValues
179
for ( int i = 0; i < aciDenyValues.length; ++i )
180         {
181             wr.addWord( aciDenyValues[i], denyValue );
182         }
183
184         // Adding itemOrUserFirstSectionPartition
185
for ( int i = 0; i < itemOrUserFirstSectionPartition.length; ++i )
186         {
187             wr.addWord( itemOrUserFirstSectionPartition[i], itemOrUserFirst );
188         }
189
190         // Adding User
191
for ( int i = 0; i < userSection.length; ++i )
192         {
193             wr.addWord( userSection[i], user );
194         }
195
196         wr.addWord( identificationTagPartition, identification );
197
198         wr.addWord( precedencePartition, precedence );
199
200         wr.addWord( authenticationLevelPartition, authenticationLevel );
201
202         rules.add( wr );
203
204         // Conversion de la List en tableau pour la passer à la méthode setRules
205
IRule[] param = new IRule[rules.size()];
206         rules.toArray( param );
207         setRules( param );
208     }
209
210     /**
211      * This class implements a word detector for ACI Items
212      *
213      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
214      * @version $Rev$, $Date$
215      */

216     static class AciWordDetector implements IWordDetector
217     {
218         /**
219          * {@inheritDoc}
220          */

221         public boolean isWordPart( char c )
222         {
223             return ( Character.isLetterOrDigit( c ) || c == '_' || c == '$' || c == '#' || c == '@' || c == '~'
224                 || c == '.' || c == '?' );
225         }
226
227
228         /**
229          * {@inheritDoc}
230          */

231         public boolean isWordStart( char c )
232         {
233             return ( Character.isLetter( c ) || c == '.' || c == '_' || c == '?' || c == '$' );
234         }
235     }
236 }
237
Popular Tags