KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > view > views > SchemaCodeScanner


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.schemas.view.views;
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 Schema 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 SchemaCodeScanner extends RuleBasedScanner
44 {
45     String JavaDoc attributype = "attributetype"; //$NON-NLS-1$
46

47     String JavaDoc objectclass = "objectclass"; //$NON-NLS-1$
48

49     String JavaDoc[] keywords = new String JavaDoc[]
50         { "NAME", "DESC", "OBSOLETE", "SUP", "EQUALITY", "ORDERING", "MUST", "MAY", "STRUCTURAL", "SUBSTR", "SYNTAX", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$
51
"SINGLE-VALUE", "COLLECTIVE", "NO-USER-MODIFICATION", "USAGE", "userApplications", "directoryOperation", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
52
"distributedOperation", "dSAOperation", "ABSTRACT", "STRUCTURAL", "AUXILIARY", "MUST", "MAY" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
53

54
55     public SchemaCodeScanner( SchemaTextAttributeProvider provider )
56     {
57         List JavaDoc<IRule> rules = new ArrayList JavaDoc<IRule>();
58
59         IToken keyword = new Token( provider.getAttribute( SchemaTextAttributeProvider.KEYWORD_ATTRIBUTE ) );
60         IToken string = new Token( provider.getAttribute( SchemaTextAttributeProvider.STRING_ATTRIBUTE ) );
61         IToken undefined = new Token( provider.getAttribute( SchemaTextAttributeProvider.DEFAULT_ATTRIBUTE ) );
62         IToken ATToken = new Token( provider.getAttribute( SchemaTextAttributeProvider.ATTRIBUTETYPE_ATTRIBUTE ) );
63         IToken OCToken = new Token( provider.getAttribute( SchemaTextAttributeProvider.OBJECTCLASS_ATTRIBUTE ) );
64         IToken oid = new Token( provider.getAttribute( SchemaTextAttributeProvider.OID_ATTRIBUTE ) );
65
66         // Rules for Strings
67
rules.add( new SingleLineRule( "\"", "\"", string, '\0', true ) ); //$NON-NLS-1$ //$NON-NLS-2$
68
rules.add( new SingleLineRule( "'", "'", string, '\0', true ) ); //$NON-NLS-1$ //$NON-NLS-2$
69
// Generic rule for whitespaces
70
rules.add( new WhitespaceRule( new IWhitespaceDetector()
71         {
72             /**
73              * Indicates if the given character is a whitespace
74              * @param c the character to analyse
75              * @return <code>true</code> if the character is to be considered as a whitespace, <code>false</code> if not.
76              * @see org.eclipse.jface.text.rules.IWhitespaceDetector#isWhitespace(char)
77              */

78             public boolean isWhitespace( char c )
79             {
80                 return Character.isWhitespace( c );
81             }
82         } ) );
83
84         WordRule wrOID = new WordRule( new SchemaOIDDetector(), oid );
85
86         rules.add( wrOID );
87
88         // If the word isn't in the List, returns undefined
89
WordRule wr = new WordRule( new SchemaWordDetector(), undefined );
90
91         // 'attributetype' rule
92
wr.addWord( attributype, ATToken );
93
94         // 'objectclass' rule
95
wr.addWord( objectclass, OCToken );
96
97         // Adding Keywords
98
for ( String JavaDoc kw : keywords )
99         {
100             wr.addWord( kw, keyword );
101         }
102
103         rules.add( wr );
104
105         IRule[] param = new IRule[rules.size()];
106         rules.toArray( param );
107         setRules( param );
108     }
109
110     /**
111      * This class implements a word detector for Schema
112      *
113      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
114      * @version $Rev$, $Date$
115      */

116     static class SchemaWordDetector implements IWordDetector
117     {
118         /* (non-Javadoc)
119          * @see org.eclipse.jface.text.rules.IWordDetector#isWordPart(char)
120          */

121         public boolean isWordPart( char c )
122         {
123             return ( Character.isLetterOrDigit( c ) || c == '_' || c == '-' || c == '$' || c == '#' || c == '@'
124                 || c == '~' || c == '.' || c == '?' );
125         }
126
127
128         /* (non-Javadoc)
129          * @see org.eclipse.jface.text.rules.IWordDetector#isWordStart(char)
130          */

131         public boolean isWordStart( char c )
132         {
133             return ( Character.isLetter( c ) || c == '.' || c == '_' || c == '?' || c == '$' );
134         }
135     }
136
137     /**
138      * This class implements an OID detector for Schema
139      *
140      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
141      * @version $Rev$, $Date$
142      */

143     static class SchemaOIDDetector implements IWordDetector
144     {
145         /* (non-Javadoc)
146          * @see org.eclipse.jface.text.rules.IWordDetector#isWordPart(char)
147          */

148         public boolean isWordPart( char c )
149         {
150             return ( Character.isDigit( c ) || c == '.' );
151         }
152
153
154         /* (non-Javadoc)
155          * @see org.eclipse.jface.text.rules.IWordDetector#isWordStart(char)
156          */

157         public boolean isWordStart( char c )
158         {
159             return ( Character.isDigit( c ) );
160         }
161     }
162 }
163
Popular Tags