Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2009 eXo Platform SAS. This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this software; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
 package org.exoplatform.groovyscript;
 
 import java.io.Reader;
 import java.util.List;
 

Author(s):
Julien Viet
Version:
$Revision$
 
 public class TemplateParser {
 
     private enum Status {
         TEXT,
 
         EXPR,
 
         SCRIPTLET,
 
         START_ANGLE,
 
         SCRIPLET_OR_EXPR,
 
         MAYBE_SCRIPLET_END,
 
         MAYBE_EXPR_END,
 
         MAYBE_GSTRING_EXPR,
 
         GSTRING_CURLY_EXPR,
 
         GSTRING_EXPR,
 
         BACKSLASH
     }
 
     public List<TemplateSectionparse(String s) {
         try {
             return parse(new StringReader(s));
         } catch (IOException e) {
             throw new UndeclaredIOException(e);
         }
     }
 
     public List<TemplateSectionparse(Reader tmpthrows IOException {
         PushbackReader reader = new PushbackReader(tmp);
 
         //
         ArrayList<TemplateSectionsections = new ArrayList<TemplateSection>();
         StringBuilder accumulator = new StringBuilder();
 
         //
         int lineNumber = 1;
         int colNumber = 1;
         Position pos = new Position(1, 1);
         Status status = .;
         int i;
         while ((i = reader.read()) != -1) {
             char c = (chari;
 
             //
             if (c == '\r') {
                 // On Windows, "\r\n" is a new line
                 int j = reader.read();
                 if (j != -1) {
                     char c2 = (charj;
                     if (c2 == '\n') {
                         c = '\n';
                     } else {
                         reader.unread(j);
                     }
                 }
             }
 
             // Update current position
            if (c == '\n') {
                colNumber = 1;
                lineNumber++;
            } else {
                colNumber++;
            }
            //
            switch (status) {
                case :
                    if (c == '<') {
                        status = .;
                    } else if (c == '\\') {
                        status = .;
                    } else if (c == '$') {
                        status = .;
                    } else {
                        accumulator.append(c);
                    }
                    break;
                case :
                    if (c == '%') {
                        status = .;
                    } else {
                        accumulator.append(c);
                    }
                    break;
                case :
                    if (c == '%') {
                        status = .;
                    } else {
                        accumulator.append(c);
                    }
                    break;
                case :
                    if (c == '%') {
                        status = .;
                    } else {
                        status = .;
                        accumulator.append('<').append(c);
                    }
                    break;
                case :
                    if (accumulator.length() > 0) {
                        sections.add(new TemplateSection(.accumulator.toString(), pos));
                        accumulator.setLength(0);
                        pos = new Position(colNumberlineNumber);
                    }
                    if (c == '=') {
                        status = .;
                    } else if (c == '%') {
                        status = .;
                    } else {
                        status = .;
                        accumulator.append(c);
                    }
                    break;
                case :
                    if (c == '>') {
                        sections.add(new TemplateSection(.accumulator.toString(), pos));
                        accumulator.setLength(0);
                        pos = new Position(colNumberlineNumber);
                        //
                        status = .;
                    } else if (c == '%') {
                        accumulator.append('%');
                    } else {
                        status = .;
                        accumulator.append('%').append(c);
                    }
                    break;
                case :
                    if (c == '>') {
                        sections.add(new TemplateSection(.accumulator.toString(), pos));
                        accumulator.setLength(0);
                        pos = new Position(colNumberlineNumber);
                        //
                        status = .;
                    } else if (c == '%') {
                        accumulator.append('%');
                    } else {
                        status = .;
                        accumulator.append('%').append(c);
                    }
                    break;
                case :
                    if (c == '{') {
                        if (accumulator.length() > 0) {
                            sections.add(new TemplateSection(.accumulator.toString(), pos));
                            accumulator.setLength(0);
                            pos = new Position(colNumberlineNumber);
                        }
                        status = .;
                    } else if (Character.isJavaIdentifierStart(c)) {
                        if (accumulator.length() > 0) {
                            sections.add(new TemplateSection(.accumulator.toString(), pos));
                            accumulator.setLength(0);
                            pos = new Position(colNumberlineNumber);
                        }
                        status = .;
                        accumulator.append(c);
                    } else {
                        accumulator.append('$').append(c);
                    }
                    break;
                case :
                    if (c == '}') {
                        sections.add(new TemplateSection(.accumulator.toString(), pos));
                        accumulator.setLength(0);
                        pos = new Position(colNumberlineNumber);
                        //
                        status = .;
                    } else {
                        accumulator.append(c);
                    }
                    break;
                case :
                    if (c == '.' || Character.isJavaIdentifierPart(c)) {
                        accumulator.append(c);
                    } else {
                        sections.add(new TemplateSection(.accumulator.toString(), pos));
                        accumulator.setLength(0);
                        pos = new Position(colNumberlineNumber);
                        //
                        status = .;
                        accumulator.append(c);
                    }
                    break;
                case :
                    accumulator.append('\\');
                    accumulator.append(c);
                    status = .;
                    break;
                default:
                    throw new AssertionError();
            }
        }
        // Last section
        if (accumulator.length() > 0) {
            switch (status) {
                case :
                    sections.add(new TemplateSection(.accumulator.toString(), pos));
                    accumulator.setLength(0);
                    pos = new Position(colNumberlineNumber);
                    break;
                case :
                    sections.add(new TemplateSection(.accumulator.toString(), pos));
                    accumulator.setLength(0);
                    pos = new Position(colNumberlineNumber);
                    break;
                default:
                    throw new AssertionError();
            }
        }
        //
        return Collections.unmodifiableList(sections);
    }
New to GrepCode? Check out our FAQ X