KickJava   Java API By Example, From Geeks To Geeks.

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


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.IRule;
26 import org.eclipse.jface.text.rules.IToken;
27 import org.eclipse.jface.text.rules.Token;
28
29
30 public class LdifValueRule implements IRule
31 {
32
33     private IToken token;
34
35
36     public LdifValueRule( IToken token )
37     {
38         this.token = token;
39     }
40
41
42     public IToken evaluate( ICharacterScanner scanner )
43     {
44
45         if ( matchContent( scanner ) )
46         {
47             return this.token;
48         }
49         else
50         {
51             return Token.UNDEFINED;
52         }
53
54     }
55
56
57     protected boolean matchContent( ICharacterScanner scanner )
58     {
59
60         int count = 0;
61
62         int c = scanner.read();
63         while ( c != ICharacterScanner.EOF )
64         {
65
66             // check for folding
67
if ( c == '\n' || c == '\r' )
68             {
69                 StringBuffer JavaDoc temp = new StringBuffer JavaDoc( 3 );
70                 if ( c == '\r' )
71                 {
72                     c = scanner.read();
73                     if ( c == '\n' )
74                     {
75                         temp.append( c );
76                     }
77                     else
78                     {
79                         scanner.unread();
80                     }
81                 }
82                 else if ( c == '\n' )
83                 {
84                     c = scanner.read();
85                     if ( c == '\r' )
86                     {
87                         temp.append( c );
88                     }
89                     else
90                     {
91                         scanner.unread();
92                     }
93                 }
94
95                 c = scanner.read();
96                 if ( c == ' ' && c != ICharacterScanner.EOF )
97                 {
98                     // space after newline, continue
99
temp.append( c );
100                     count += temp.length();
101                     c = scanner.read();
102                 }
103                 else
104                 {
105                     for ( int i = 0; i < temp.length(); i++ )
106                         scanner.unread();
107                     break;
108                 }
109             }
110             else
111             {
112                 count++;
113                 c = scanner.read();
114             }
115         }
116         scanner.unread();
117
118         return count > 0;
119     }
120
121 }
122
Popular Tags