KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > ldifeditor > editor > text > LdifRecordRule


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
21 package org.apache.directory.ldapstudio.ldifeditor.editor.text;
22
23
24 import org.eclipse.jface.text.rules.ICharacterScanner;
25 import org.eclipse.jface.text.rules.IPredicateRule;
26 import org.eclipse.jface.text.rules.IToken;
27 import org.eclipse.jface.text.rules.Token;
28
29
30 /**
31  * Rule to detect LDIF records. A LDIF record must start with "dn:" at column 0
32  * and end with new line at column 0.
33  *
34  *
35  */

36 public class LdifRecordRule implements IPredicateRule
37 {
38
39     private static char[] DN_SEQUENCE = new char[]
40         { 'd', 'n', ':' };
41
42     private IToken recordToken;
43
44
45     public LdifRecordRule( IToken recordToken )
46     {
47         this.recordToken = recordToken;
48     }
49
50
51     public IToken getSuccessToken()
52     {
53         return this.recordToken;
54     }
55
56
57     /**
58      * Checks for new line "\n", "\r" or "\r\n".
59      *
60      * @param scanner
61      * @return
62      */

63     private int matchNewline( ICharacterScanner scanner )
64     {
65
66         int c = scanner.read();
67
68         if ( c == '\r' )
69         {
70             c = scanner.read();
71             if ( c == '\n' )
72             {
73                 return 2;
74             }
75             else
76             {
77                 scanner.unread();
78                 return 1;
79             }
80         }
81         else if ( c == '\n' )
82         {
83             c = scanner.read();
84             if ( c == '\r' )
85             {
86                 return 2;
87             }
88             else
89             {
90                 scanner.unread();
91                 return 1;
92             }
93         }
94         else
95         {
96             scanner.unread();
97             return 0;
98         }
99     }
100
101
102     /**
103      * Checks for "dn:".
104      *
105      * @param scanner
106      * @return
107      */

108     private int matchDnAndColon( ICharacterScanner scanner )
109     {
110
111         for ( int i = 0; i < DN_SEQUENCE.length; i++ )
112         {
113
114             int c = scanner.read();
115
116             if ( c != DN_SEQUENCE[i] )
117             {
118                 while ( i >= 0 )
119                 {
120                     scanner.unread();
121                     i--;
122                 }
123                 return 0;
124             }
125
126         }
127
128         return DN_SEQUENCE.length;
129     }
130
131
132     private boolean matchEOF( ICharacterScanner scanner )
133     {
134         int c = scanner.read();
135         if ( c == ICharacterScanner.EOF )
136         {
137             return true;
138         }
139         else
140         {
141             scanner.unread();
142             return false;
143         }
144     }
145
146
147     public IToken evaluate( ICharacterScanner scanner, boolean resume )
148     {
149
150         if ( scanner.getColumn() != 0 )
151         {
152             return Token.UNDEFINED;
153         }
154
155         int c;
156
157         do
158         {
159             c = scanner.read();
160
161             if ( c == '\r' || c == '\n' )
162             {
163
164                 // check end of record
165
scanner.unread();
166
167                 if ( this.matchNewline( scanner ) > 0 )
168                 {
169
170                     int nlCount = this.matchNewline( scanner );
171                     if ( nlCount > 0 )
172                     {
173                         int dnCount = this.matchDnAndColon( scanner );
174                         if ( dnCount > 0 )
175                         {
176                             while ( dnCount > 0 )
177                             {
178                                 scanner.unread();
179                                 dnCount--;
180                             }
181                             return this.recordToken;
182                         }
183                         else if ( this.matchEOF( scanner ) )
184                         {
185                             return this.recordToken;
186                         }
187                         else
188                         {
189                             while ( nlCount > 0 )
190                             {
191                                 scanner.unread();
192                                 nlCount--;
193                             }
194                         }
195                     }
196                     else if ( this.matchEOF( scanner ) )
197                     {
198                         return this.recordToken;
199                     }
200                 }
201                 else if ( this.matchEOF( scanner ) )
202                 {
203                     return this.recordToken;
204                 }
205
206             }
207             else if ( c == ICharacterScanner.EOF )
208             {
209                 return this.recordToken;
210             }
211         }
212         while ( true );
213
214     }
215
216
217     public IToken evaluate( ICharacterScanner scanner )
218     {
219         return this.evaluate( scanner, false );
220     }
221
222 }
223
Popular Tags