Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package freemarker.core;
 
 import java.util.Set;
 
Parsing-time exception in a template (as opposed to a runtime exception, a freemarker.template.TemplateException). This usually signals syntactical/lexical errors. Note that on JavaCC-level lexical errors throw TokenMgrError instead of this, however with the API-s that most users use those will be wrapped into ParseException-s. This is a modified version of file generated by JavaCC from FTL.jj. You can modify this class to customize the error reporting mechanisms so long as the public interface remains compatible with the original.

See also:
TokenMgrError
 
 public class ParseException extends IOException implements FMParserConstants {

    
This is the last token that has been consumed successfully. If this object has been created due to a parse error, the token following this token will (therefore) be the first error token.
 
     public Token currentToken;
 
     private static volatile Boolean jbossToolsMode;
 
     private boolean messageAndDescriptionRendered;
     private String message;
     private String description
 
     public int columnNumberlineNumber;
     public int endColumnNumberendLineNumber;

    
Each entry in this array is an array of integers. Each array of integers represents a sequence of tokens (by their ordinal values) that is expected at this point of the parse.
 
     public int[][] expectedTokenSequences;

    
This is a reference to the "tokenImage" array of the generated parser within which the parse error occurred. This array is defined in the generated ...Constants interface.
 
     public String[] tokenImage;

    
The end of line string for this machine.
 
     protected String eol = SecurityUtilities.getSystemProperty("line.separator""\n");

    

Deprecated:
Will be remove without replacement in 2.4.
 
     protected boolean specialConstructor;  
 
     private String templateName;

    
This constructor is used by the method "generateParseException" in the generated parser. Calling this constructor generates a new object of this type with the fields "currentToken", "expectedTokenSequences", and "tokenImage" set. This constructor calls its super class with the empty string to force the "toString" method of parent class "Throwable" to print the error message in the form: ParseException: <result of getMessage>
 
     public ParseException(Token currentTokenVal,
             int[][] expectedTokenSequencesVal,
             String[] tokenImageVal
             )
     {
         super("");
         = currentTokenVal;
         = true;
         = expectedTokenSequencesVal;
         = tokenImageVal;
    }

    
The following constructors are for use by you for whatever purpose you can think of. Constructing the exception in this manner makes the exception behave in the normal way - i.e., as documented in the class "Throwable". The fields "errorToken", "expectedTokenSequences", and "tokenImage" do not contain relevant information. The JavaCC generated code does not use these constructors.

Deprecated:
Use a constructor to which you pass description, template, and positions.
    protected ParseException() {
        super();
    }

    

Deprecated:
Use a constructor to which you can also pass the template, and the end positions.
    public ParseException(String descriptionint lineNumberint columnNumber) {
        this(description, (TemplatenulllineNumbercolumnNumbernull);
    }

    

Since:
2.3.21
    public ParseException(String descriptionTemplate template,
            int lineNumberint columnNumberint endLineNumberint endColumnNumber) {
        this(descriptiontemplatelineNumbercolumnNumberendLineNumberendColumnNumbernull);      
    }

    

Since:
2.3.21
    public ParseException(String descriptionTemplate template,
            int lineNumberint columnNumberint endLineNumberint endColumnNumber,
            Throwable cause) {
        this(description,
                template == null ? null : template.getSourceName(),
                        lineNumbercolumnNumber,
                        endLineNumberendColumnNumber,
                        cause);      
    }
    
    

Deprecated:
Use ParseException(java.lang.String,freemarker.template.Template,int,int,int,int) instead, as IDE-s need the end position of the error too.
Since:
2.3.20
    public ParseException(String descriptionTemplate templateint lineNumberint columnNumber) {
        this(descriptiontemplatelineNumbercolumnNumbernull);      
    }

    

Deprecated:
Use ParseException(java.lang.String,freemarker.template.Template,int,int,int,int,java.lang.Throwable) instead, as IDE-s need the end position of the error too.
Since:
2.3.20
    public ParseException(String descriptionTemplate templateint lineNumberint columnNumberThrowable cause) {
        this(description,
                template == null ? null : template.getSourceName(),
                        lineNumbercolumnNumber,
                        0, 0,
                        cause);      
    }

    

Since:
2.3.20
    public ParseException(String descriptionTemplate templateToken tk) {
        this(descriptiontemplatetknull);
    }

    

Since:
2.3.20
    public ParseException(String descriptionTemplate templateToken tkThrowable cause) {
        this(description,
                template == null ? null : template.getSourceName(),
                        tk.beginLinetk.beginColumn,
                        tk.endLinetk.endColumn,
                        cause);
    }

    

Since:
2.3.20
    public ParseException(String descriptionTemplateObject tobj) {
        this(descriptiontobjnull);
    }

    

Since:
2.3.20
    public ParseException(String descriptionTemplateObject tobjThrowable cause) {
        this(description,
                tobj.getTemplate() == null ? null : tobj.getTemplate().getSourceName(),
                        tobj.beginLinetobj.beginColumn,
                        tobj.endLinetobj.endColumn,
                        cause);
    }
    private ParseException(String descriptionString templateName,
            int lineNumberint columnNumber,
            int endLineNumberint endColumnNumber,
            Throwable cause) {
        super(description);  // but we override getMessage, so it will be different
        this. = description
        this. = templateName;
        this. = lineNumber;
        this. = columnNumber;
        this. = endLineNumber;
        this. = endColumnNumber;
    }

    
Should be used internally only; sets the name of the template that contains the error. This is needed as the constructor that JavaCC automatically calls doesn't pass in the template, so we set it somewhere later in an exception handler.
    public void setTemplateName(String templateName) {
        this. = templateName;
        synchronized (this) {
             = false;
             = null;
        }
    }

    
Returns the error location plus the error description.

    public String getMessage() {
        synchronized (this) {
            if (return ;
        }
        synchronized (this) {
            return ;
        }
    }
    private String getDescription() {
        synchronized (this) {
            if (return ;
        }
        synchronized (this) {
            return ;
        }
    }
    
    
Returns the description of the error without error location or source quotations, or null if there's no description available. This is useful in editors (IDE-s) where the error markers and the editor window itself already carry this information, so it's redundant the repeat in the error dialog.
    public String getEditorMessage() {
        return getDescription();
    }

    
Returns the name (template-root relative path) of the template whose parsing was failed. Maybe null if this is a non-stored template.

Since:
2.3.20
    public String getTemplateName() {
        return ;
    }

    
1-based line number of the failing section, or 0 is the information is not available.
    public int getLineNumber() {
        return ;
    }

    
1-based column number of the failing section, or 0 is the information is not available.
    public int getColumnNumber() {
        return ;
    }

    
1-based line number of the last line that contains the failing section, or 0 if the information is not available.

Since:
2.3.21
    public int getEndLineNumber() {
        return ;
    }

    
1-based column number of the last character of the failing section, or 0 if the information is not available. Note that unlike with Java string API-s, this column number is inclusive.

Since:
2.3.21
    public int getEndColumnNumber() {
        return ;
    }
    private void renderMessageAndDescription() {
        String desc = getOrRenderDescription();
        String prefix;
        if (!isInJBossToolsMode()) {
            prefix = "Syntax error "
                    + MessageUtil.formatLocationForSimpleParsingError()
                    + ":\n";  
        } else {
            prefix = "[col. " +  + "] ";
        }
        String msg = prefix + desc;
        desc = msg.substring(prefix.length());  // so we reuse the backing char[]
        synchronized (this) {
             = msg;
             = desc;
             = true;
        }
    }
    private boolean isInJBossToolsMode() {
        if ( == null) {
            try {
                 = Boolean.valueOf(
                        ParseException.class.getClassLoader().toString().indexOf(
                                "[org.jboss.ide.eclipse.freemarker:") != -1);
            } catch (Throwable e) {
                 = .;
            }
        }
        return .booleanValue();
    }

    
Returns the description of the error without the error location, or null if there's no description available.
    private String getOrRenderDescription() {
        synchronized (this) {
            if ( != nullreturn ;  // When we already have it from the constructor
        }
        String tokenErrDesc;
        if ( != null) {
            tokenErrDesc = getCustomTokenErrorDescription();
            if (tokenErrDesc == null) {
                // The default JavaCC message generation stuff follows.
                StringBuffer expected = new StringBuffer();
                int maxSize = 0;
                for (int i = 0; i < .i++) {
                    if (i != 0) {
                        expected.append();
                    }
                    expected.append("    ");
                    if (maxSize < [i].length) {
                        maxSize = [i].length;
                    }
                    for (int j = 0; j < [i].lengthj++) {
                        if (j != 0) expected.append(' ');
                        expected.append([[i][j]]);
                    }
                }
                tokenErrDesc = "Encountered \"";
                Token tok = .;
                for (int i = 0; i < maxSizei++) {
                    if (i != 0) tokenErrDesc += " ";
                    if (tok.kind == 0) {
                        tokenErrDesc += [0];
                        break;
                    }
                    tokenErrDesc += add_escapes(tok.image);
                    tok = tok.next;
                }
                tokenErrDesc += "\", but ";
                if (. == 1) {
                    tokenErrDesc += "was expecting:" + ;
                } else {
                    tokenErrDesc += "was expecting one of:" + ;
                }
                tokenErrDesc += expected;
            }
        } else {
            tokenErrDesc = null;
        }
        return tokenErrDesc;
    }
        final Token nextToken = .;
        final int kind = nextToken.kind;
        if (kind == ) {
            Set/*<String>*/ endNames = new HashSet();
            for (int i = 0; i < .i++) {
                int[] sequence = [i];
                for (int j = 0; j < sequence.lengthj++) {
                    switch (sequence[j]) {
                    case :
                        endNames.add"#foreach");
                        break;
                    case :
                        endNames.add"#list");
                        break;
                    case :
                        endNames.add"#switch");
                        break;
                    case :
                        endNames.add"#if");
                        break;
                    case :
                        endNames.add"#compress");
                        break;
                    case :
                        endNames.add"#macro");
                    case :
                        endNames.add"#function");
                        break;
                    case :
                        endNames.add"#transform");
                        break;
                    case :
                        endNames.add"#escape");
                        break;
                    case :
                        endNames.add"#noescape");
                        break;
                    case :
                        endNames.add"#assign");
                        break;
                    case :
                        endNames.add"#local");
                        break;
                    case :
                        endNames.add"#global");
                        break;
                    case :
                        endNames.add"#attempt");
                        break;
                    case :
                        endNames.add"\"{\"");
                        break;
                    case :
                        endNames.add"\"[\"");
                        break;
                    case :
                        endNames.add"\"(\"");
                        break;
                    case :
                        endNames.add"@...");
                        break;
                    }
                }
            }
            return "Unexpected end of file reached."
                    + (endNames.size() == 0 ? "" : " You have an unclosed " + concatWithOrs(endNames) + ".");
        } else if (kind == ) {
            return "Unexpected directive, \"#else\". "
                    + "Check if you have a valid #if-#elseif-#else or #list-#else structure.";
        } else if (kind ==  || kind == ) {
            return "Unexpected directive, "
                    + StringUtil.jQuote(nextToken)
                    + ". Check if you have a valid #if-#elseif-#else structure.";
        }
        return null;
    }
    private String concatWithOrs(Set/*<String>*/ endNames) {
        StringBuffer sb = new StringBuffer(); 
        for (Iterator/*<String>*/ it = endNames.iterator(); it.hasNext(); ) {
            String endName = (Stringit.next();
            if (sb.length() != 0) {
                sb.append(" or ");
            }
            sb.append(endName);
        }
        return sb.toString();
    }

    
Used to convert raw characters to their escaped version when these raw version cannot be used as part of an ASCII string literal.
    protected String add_escapes(String str) {
        StringBuffer retval = new StringBuffer();
        char ch;
        for (int i = 0; i < str.length(); i++) {
            switch (str.charAt(i))
            {
            case 0 :
                continue;
            case '\b':
                retval.append("\\b");
                continue;
            case '\t':
                retval.append("\\t");
                continue;
            case '\n':
                retval.append("\\n");
                continue;
            case '\f':
                retval.append("\\f");
                continue;
            case '\r':
                retval.append("\\r");
                continue;
            case '\"':
                retval.append("\\\"");
                continue;
            case '\'':
                retval.append("\\\'");
                continue;
            case '\\':
                retval.append("\\\\");
                continue;
            default:
                if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
                    String s = "0000" + Integer.toString(ch, 16);
                    retval.append("\\u" + s.substring(s.length() - 4, s.length()));
                } else {
                    retval.append(ch);
                }
                continue;
            }
        }
        return retval.toString();
    }
New to GrepCode? Check out our FAQ X