KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > grammar > wiki > impl > ParsingContext


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.grammar.wiki.impl;
6
7 import org.exoplatform.container.configuration.ServiceConfiguration;
8
9 /**
10  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
11  * @since Dec 17, 2004
12  * @version $Id$
13  */

14 public class ParsingContext {
15   char[] buf_ ;
16   int currentPosition_ ;
17   Token currentToken_ ;
18   private TokenHandlerManager tokenManager_ ;
19   private StringBuffer JavaDoc out_ ;
20   
21   public ParsingContext(ServiceConfiguration sconf) {
22     currentToken_ = new Token(0, 0);
23     tokenManager_ = new TokenHandlerManager(sconf) ;
24   }
25   
26   public StringBuffer JavaDoc getOutputBuffer() { return out_ ; }
27   public void SetOutputBuffer(StringBuffer JavaDoc out) { out_ = out ; }
28   
29   public TokenHandlerManager getTokenHandlerManager() { return tokenManager_; }
30   
31   public void transform(String JavaDoc text) {
32     buf_ = text.toCharArray() ;
33     out_ = new StringBuffer JavaDoc() ;
34     currentToken_.end = -1 ;
35     Token token = nextToken(currentToken_) ;
36     while(token != null) {
37       token = tokenManager_.handleToken(null, token, this) ;
38     }
39   }
40   
41   public Token nextToken(Token previous) {
42     currentPosition_ = previous.end + 1;
43     if(currentPosition_ >= buf_.length) return null ;
44     currentToken_.start = currentPosition_ ;
45     switch(buf_[currentPosition_]) {
46       case '{' : return readCurlyBracePrefixToken() ;
47       case '-' : return readDashPrefixToken() ;
48       case '_' : return readUnderscorePrefixToken() ;
49       case '*' : return readStarPrefixToken() ;
50       case '~' : return readTildePrefixToken() ;
51       case ':' : return readColonPrefixToken();
52       case '[' : return readSquareBracePrefixToken() ;
53       case '\n': case '\r': return readNewLinePrefixToken() ;
54       default : return readDefaultToken() ;
55     }
56   }
57   
58   private Token readDefaultToken() {
59     for(; currentPosition_ < buf_.length; currentPosition_++) {
60       switch(buf_[currentPosition_]) {
61         case '{' : case '-' : case '_' : case '*' : case '~' :
62         case ':' : case '[' : case '\n' : case '\r' :{
63           return createToken(--currentPosition_, Token.DEFAULT_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
64         }
65         default :
66       }
67     }
68     return createToken(--currentPosition_, Token.DEFAULT_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
69   }
70   
71   private Token readCurlyBracePrefixToken() {
72     for(; currentPosition_ < buf_.length; currentPosition_++) {
73       switch(buf_[currentPosition_]) {
74         case '}' : {
75           currentToken_ = createToken(currentPosition_, Token.CURLY_BRACES_TOKEN, Token.BLOCK_TOKEN_GROUP) ;
76           currentToken_.type = getSubstring(currentToken_.start, currentToken_.end) ;
77           return currentToken_ ;
78         }
79         case '\n' : {
80           return createToken(--currentPosition_, Token.DEFAULT_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
81         }
82       }
83     }
84     return createToken(currentPosition_--, Token.DEFAULT_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
85   }
86   
87   private Token readSquareBracePrefixToken() {
88     for(; currentPosition_ < buf_.length; currentPosition_++) {
89       switch(buf_[currentPosition_]) {
90         case ']' : {
91           return createToken(currentPosition_, Token.LINK_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
92         }
93         case '\n' : case '\r' :{
94           return createToken(--currentPosition_, Token.DEFAULT_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
95         }
96       }
97     }
98     return createToken(--currentPosition_, Token.DEFAULT_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
99   }
100   
101   private Token readColonPrefixToken() {
102     for(; currentPosition_ < buf_.length; currentPosition_++) {
103       switch(buf_[currentPosition_]) {
104         case '\n' : case '\r' : case ' ': case '\t': {
105           return createToken(--currentPosition_, Token.SMILEY_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
106         }
107       }
108     }
109     return createToken(--currentPosition_, Token.DEFAULT_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
110   }
111   
112   private Token readDashPrefixToken() {
113     if(currentPosition_ + 1 >= buf_.length) {
114       return createToken(currentPosition_, Token.DEFAULT_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
115     }
116     if(buf_[currentPosition_] == '-' && buf_[currentPosition_ + 1] == '-') {
117       int i = currentPosition_ ;
118       while(i < buf_.length && buf_[i] == '-') i++;
119       i-- ;
120       if(i > currentPosition_ + 2) {
121         currentPosition_ = i ;
122         return createToken(currentPosition_, Token.HORIZONTAL_LINE_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
123       }
124       currentPosition_ = i ;
125       return createToken(currentPosition_ , Token.STRIKE_TOKEN, Token.INLINE_TOKEN_GROUP) ;
126     }
127     return createToken(currentPosition_, Token.DEFAULT_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
128   }
129   
130   private Token readUnderscorePrefixToken() {
131     if(currentPosition_ + 1 >= buf_.length) {
132       return createToken(currentPosition_, Token.DEFAULT_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
133     }
134     if(buf_[currentPosition_] == '_' && buf_[currentPosition_ + 1] == '_') {
135       return createToken(++currentPosition_ , Token.UNDERLINE_TOKEN, Token.INLINE_TOKEN_GROUP) ;
136     }
137     return createToken(currentPosition_, Token.DEFAULT_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
138   }
139   
140   private Token readStarPrefixToken() {
141     if(currentPosition_ + 1 >= buf_.length) {
142       return createToken(currentPosition_, Token.DEFAULT_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
143     }
144     if(buf_[currentPosition_] == '*' && buf_[currentPosition_ + 1] == '*') {
145       return createToken(++currentPosition_ , Token.BOLD_TOKEN, Token.INLINE_TOKEN_GROUP) ;
146     }
147     
148     if(buf_[currentPosition_] == '*') {
149       return createToken(currentPosition_ , Token.BOLD_TOKEN, Token.INLINE_TOKEN_GROUP) ;
150     }
151     return createToken(currentPosition_, Token.DEFAULT_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
152   }
153   
154   private Token readTildePrefixToken() {
155     if(currentPosition_ + 1 >= buf_.length) {
156       return createToken(currentPosition_, Token.DEFAULT_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
157     }
158     if(buf_[currentPosition_] == '~' && buf_[currentPosition_ + 1] == '~') {
159       return createToken(++currentPosition_ , Token.ITALIC_TOKEN, Token.INLINE_TOKEN_GROUP) ;
160     }
161     return createToken(currentPosition_, Token.DEFAULT_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
162   }
163   
164   private Token readNewLinePrefixToken() {
165     int i = currentPosition_ ;
166     int newLineCounter = 0 ;
167     while(i < buf_.length && (buf_[i] == '\n' || buf_[i] == '\r')) {
168       if(buf_[i] == '\n') newLineCounter++ ;
169       i++ ;
170     }
171     if(newLineCounter > 1) {
172       if(i == buf_.length) i-- ;
173       if(buf_[i] != '\n' && buf_[i] != '\r') i-- ;
174       currentPosition_ = i - 1;
175       return createToken(currentPosition_, Token.MULTIPLE_NEW_LINE_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
176     }
177     // escape ' '
178
while(i < buf_.length && (buf_[i] == ' ' || buf_[i] == '\t')) i++ ;
179     if(i + 2 < buf_.length) {
180       if(buf_[i] == '*') {
181         if(buf_[i] == '*' && buf_[i + 1] == '*' && buf_[i + 2] == '*') {
182           currentPosition_ = i + 2 ;
183           return createToken(currentPosition_, Token.LIST_LEVEL_3_TOKEN, Token.BLOCK_TOKEN_GROUP) ;
184         }
185         if(buf_[i] == '*' && buf_[i + 1] == '*') {
186           currentPosition_ = i + 1 ;
187           return createToken(currentPosition_, Token.LIST_LEVEL_2_TOKEN, Token.BLOCK_TOKEN_GROUP) ;
188         }
189         currentPosition_ = i ;
190         return createToken(currentPosition_, Token.LIST_LEVEL_1_TOKEN, Token.BLOCK_TOKEN_GROUP) ;
191       }
192       
193       if(buf_[i] == '1' ) {
194         if(buf_[i] == '1' && buf_[i + 1] == '.' && buf_[i + 2] == '1') {
195           currentPosition_ = i + 2 ;
196           return createToken(currentPosition_, Token.TITLE_1_1_TOKEN, Token.BLOCK_TOKEN_GROUP) ;
197         } else if(buf_[i] == '1' && buf_[i + 1] == '.') {
198           currentPosition_ = i + 1 ;
199           return createToken(currentPosition_, Token.ENUMERATED_TOKEN, Token.BLOCK_TOKEN_GROUP) ;
200         } else if(buf_[i] == '1' && buf_[i + 1] == ' ') {
201           currentPosition_ = i + 1 ;
202           return createToken(currentPosition_, Token.TITLE_1_TOKEN, Token.BLOCK_TOKEN_GROUP) ;
203         }
204       }
205       
206       if(buf_[i] == 'a' && buf_[i + 1] == '.') {
207         currentPosition_ = i + 1 ;
208         return createToken(currentPosition_, Token.ALPHABETICAL_ENUMERATED_TOKEN, Token.BLOCK_TOKEN_GROUP) ;
209       }
210       if(buf_[i] == 'i' && buf_[i + 1] == '.') {
211         currentPosition_ = i + 1 ;
212         return createToken(currentPosition_, Token.ROMAN_ENUMERATED_TOKEN, Token.BLOCK_TOKEN_GROUP) ;
213       }
214     }
215     if(i == buf_.length) i-- ;
216     if(buf_[i] != '\n' && buf_[i] != '\r') i-- ;
217     currentPosition_ = i ;
218     return createToken(currentPosition_, Token.SINGLE_NEW_LINE_TOKEN, Token.SINGLE_TOKEN_GROUP) ;
219   }
220   
221   final Token createToken(int nextEnd, String JavaDoc type, String JavaDoc group) {
222     currentToken_.end = nextEnd ;
223     currentToken_.type = type ;
224     currentToken_.group = group ;
225     return currentToken_ ;
226   }
227   
228   final public String JavaDoc getSubstring(int offset , int end) {
229     return new String JavaDoc(buf_, offset, end - offset + 1) ;
230   }
231   
232   final public char[] getSubBuffer(int offset , int end) {
233     char[] sub = new char[end - offset + 1] ;
234     System.arraycopy(buf_, offset, sub, 0, sub.length);
235     return sub;
236   }
237   
238   final public void out(String JavaDoc s) {
239     out_.append(s) ;
240   }
241   
242   final public void out(Token token) {
243     out_.append(buf_, token.start, token.end - token.start + 1) ;
244   }
245 }
Popular Tags