1 5 package org.exoplatform.services.grammar.wiki.impl; 6 7 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 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 [] getHandleableTokenType() { 41 String [] 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 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 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 |