KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
8  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
9  * @since Dec 19, 2004
10  * @version $Id$
11  */

12 public class ListTokenHandler extends TokenHandler {
13   private int currentLevel_ = 0;
14   
15   public Token handleToken(Token parent, Token token, ParsingContext context) {
16     currentLevel_ = 0 ;
17     StringBuffer JavaDoc b = context.getOutputBuffer() ;
18     TokenHandlerManager manager = context.getTokenHandlerManager() ;
19     while(token != null) {
20       if(token.type == Token.LIST_LEVEL_1_TOKEN) {
21         li(b, 1) ;
22         token = context.nextToken(token) ;
23       } else if(token.type == Token.LIST_LEVEL_2_TOKEN) {
24         li(b, 2) ;
25         token = context.nextToken(token) ;
26       } else if(token.type == Token.LIST_LEVEL_3_TOKEN) {
27         li(b, 3) ;
28         token = context.nextToken(token) ;
29       } else if(token.type == Token.MULTIPLE_NEW_LINE_TOKEN) {
30         break ;
31       } else {
32         token = manager.handleToken(null ,token, context) ;
33       }
34     }
35     b.append("</li>");
36     ul(b, 0);
37     return token ;
38   }
39   
40   public String JavaDoc[] getHandleableTokenType() {
41     String JavaDoc[] s = {Token.LIST_LEVEL_1_TOKEN, Token.LIST_LEVEL_2_TOKEN, Token.LIST_LEVEL_3_TOKEN } ;
42     return s;
43   }
44   
45   private void li(StringBuffer JavaDoc b, int level) {
46     if(currentLevel_ > 0) b.append("</li>\n") ;
47     ul(b, level) ;
48     b.append("<li>\n") ;
49   }
50   
51   private void ul(StringBuffer JavaDoc b, int level) {
52     if(currentLevel_ < level) {
53       for(; currentLevel_ < level; currentLevel_++) {
54         b.append("<ul>\n") ;
55       }
56     } else if (currentLevel_ > level) {
57       for(; currentLevel_ > level; currentLevel_--) {
58         b.append("</ul>\n") ;
59       }
60     }
61   }
62 }
63
Popular Tags