Save This Page
Home » sitemesh-2.3 » com.opensymphony.module » sitemesh » html » [javadoc | source]
    1   package com.opensymphony.module.sitemesh.html;
    2   
    3   import com.opensymphony.module.sitemesh.html.tokenizer.TagTokenizer;
    4   import com.opensymphony.module.sitemesh.html.tokenizer.TokenHandler;
    5   import com.opensymphony.module.sitemesh.html.util.CharArray;
    6   
    7   import java.util.LinkedList;
    8   
    9   public class HTMLProcessor {
   10   
   11       private final char[] input;
   12       private final CharArray body;
   13       private final State defaultState = new State();
   14   
   15       private State currentState = defaultState;
   16   
   17       public HTMLProcessor(char[] input, CharArray body) {
   18           this.input = input;
   19           this.body = body;
   20       }
   21   
   22       public State defaultState() {
   23           return defaultState;
   24       }
   25   
   26       public void process() {
   27           TagTokenizer tokenizer = new TagTokenizer(input);
   28           final HTMLProcessorContext context = new HTMLProcessorContext() {
   29               public State currentState() {
   30                   return currentState;
   31               }
   32   
   33               public void changeState(State newState) {
   34                   currentState = newState;
   35               }
   36   
   37               private LinkedList bufferStack = new LinkedList();
   38   
   39               public void pushBuffer(CharArray buffer) {
   40                   bufferStack.add(buffer);
   41               }
   42   
   43               public CharArray currentBuffer() {
   44                   return (CharArray) bufferStack.getLast();
   45               }
   46   
   47               public CharArray popBuffer() {
   48                   return (CharArray) bufferStack.removeLast();
   49               }
   50   
   51               public void mergeBuffer() {
   52                   CharArray top = (CharArray) bufferStack.getLast();
   53                   CharArray nextDown = (CharArray) bufferStack.get(bufferStack.size() - 2);
   54                   nextDown.append(top);
   55               }
   56           };
   57           context.pushBuffer(body);
   58           tokenizer.start(new TokenHandler() {
   59   
   60               public boolean shouldProcessTag(String name) {
   61                   return currentState.shouldProcessTag(name);
   62               }
   63   
   64               public void tag(Tag tag) {
   65                   TagRule tagRule = currentState.getRule(tag.getName());
   66                   tagRule.setContext(context);
   67                   tagRule.process(tag);
   68               }
   69   
   70               public void text(Text text) {
   71                   text.writeTo(context.currentBuffer());
   72               }
   73   
   74               public void warning(String message, int line, int column) {
   75                   // TODO
   76                   // System.out.println(line + "," + column + ": " + message);
   77               }
   78           });
   79           defaultState.endOfState();
   80       }
   81   }

Save This Page
Home » sitemesh-2.3 » com.opensymphony.module » sitemesh » html » [javadoc | source]