aboutsummaryrefslogtreecommitdiff
path: root/velocity-engine-core/src/main/java/org/apache/velocity/exception/ParseErrorException.java
blob: 824675c43d1ffec383d467cccea694e6637695ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package org.apache.velocity.exception;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import org.apache.velocity.runtime.parser.ParseException;
import org.apache.velocity.util.StringUtils;
import org.apache.velocity.util.introspection.Info;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *  Application-level exception thrown when a resource of any type
 *  has a syntax or other error which prevents it from being parsed.
 *  <br>
 *  When this resource is thrown, a best effort will be made to have
 *  useful information in the exception's message.  For complete
 *  information, consult the runtime log.
 *
 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
 * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
 * @version $Id$
 */
public class ParseErrorException extends VelocityException
{
    /**
     * Version Id for serializable
     */
    private static final long serialVersionUID = -6665197935086306472L;

    /**
     * The column number of the parsing error, or -1 if not defined.
     */
    private int columnNumber = -1;

    /**
     * The line number of the parsing error, or -1 if not defined.
     */
    private int lineNumber = -1;

    /**
     * The name of the template containing the error, or null if not defined.
     */
    private String templateName = "*unset*";

    /**
     * If applicable, contains the invalid syntax or reference that triggered this exception
     */
    private String invalidSyntax;

    /**
     * If we modify the message, then we set this
     */
    private String msg = null;

    /**
     * Create a ParseErrorException with the given message.
     *
     * @param exceptionMessage the error exception message
     */
    public ParseErrorException(String exceptionMessage)
    {
          super(exceptionMessage);
    }

    private static final Pattern lexError = Pattern.compile("Lexical error.*TokenMgrError.*line (\\d+),.*column (\\d+)\\.(.*)");

    /**
     * Create a ParseErrorException with the given ParseException.
     *
     * @param pex the parsing exception
     * @param templName
     * @since 1.5
     */
    public ParseErrorException(ParseException pex, String templName)
    {
        super(pex.getMessage());

        if (templName != null) templateName = templName;

        // Don't use a second C'tor, TemplateParseException is a subclass of
        // ParseException...
        if (pex instanceof ExtendedParseException)
        {
            ExtendedParseException xpex = (ExtendedParseException) pex;

            columnNumber = xpex.getColumnNumber();
            lineNumber = xpex.getLineNumber();
            templateName = xpex.getTemplateName();
        }
        else
        {
            // We get here if the the Parser has thrown an exception. Unfortunately,
            // the error message created is hard coded by javacc, so here we alter
            // the error message, so that it is in our standard format.
            Matcher match =  lexError.matcher(pex.getMessage());
            if (match.matches())
            {
               lineNumber = Integer.parseInt(match.group(1));
               columnNumber = Integer.parseInt(match.group(2));
               String restOfMsg = match.group(3);
               msg = "Lexical error, " + restOfMsg + " at "
                 + StringUtils.formatFileString(templateName, lineNumber, columnNumber);
            }

            //  ugly, ugly, ugly...

            if (pex.currentToken != null && pex.currentToken.next != null)
            {
                columnNumber = pex.currentToken.next.beginColumn;
                lineNumber = pex.currentToken.next.beginLine;
            }
        }
    }

    /**
     * Create a ParseErrorException with the given ParseException.
     *
     * @param pex the parsing exception
     * @param templName
     * @since 1.5
     */
    public ParseErrorException(VelocityException pex, String templName)
    {
        super(pex.getMessage());

        if (templName != null) templateName = templName;

        // Don't use a second C'tor, TemplateParseException is a subclass of
        // ParseException...
        if (pex instanceof ExtendedParseException)
        {
            ExtendedParseException xpex = (ExtendedParseException) pex;

            columnNumber = xpex.getColumnNumber();
            lineNumber = xpex.getLineNumber();
            templateName = xpex.getTemplateName();
        }
        else if (pex.getCause() instanceof ParseException)
        {
            ParseException pex2 = (ParseException) pex.getCause();

            if (pex2.currentToken != null && pex2.currentToken.next != null)
            {
                columnNumber = pex2.currentToken.next.beginColumn;
                lineNumber = pex2.currentToken.next.beginLine;
            }
        }
    }


    /**
     * Create a ParseErrorRuntimeException with the given message and info
     *
     * @param exceptionMessage the error exception message
     * @param info an Info object with the current template info
     * @since 1.5
     */
    public ParseErrorException(String exceptionMessage, Info info)
    {
        super(exceptionMessage);
        columnNumber = info.getColumn();
        lineNumber = info.getLine();
        templateName = info.getTemplateName();
    }

    /**
     * Create a ParseErrorRuntimeException with the given message and info
     *
     * @param exceptionMessage the error exception message
     * @param info an Info object with the current template info
     * @param invalidSyntax the invalid syntax or reference triggering this exception
     * @since 1.5
     */
    public ParseErrorException(String exceptionMessage,
            Info info, String invalidSyntax)
    {
        super(exceptionMessage);
        columnNumber = info.getColumn();
        lineNumber = info.getLine();
        templateName = info.getTemplateName();
        this.invalidSyntax = invalidSyntax;
    }


    /**
     * Return the column number of the parsing error, or -1 if not defined.
     *
     * @return column number of the parsing error, or -1 if not defined
     * @since 1.5
     */
    public int getColumnNumber()
    {
        return columnNumber;
    }

    /**
     * Return the line number of the parsing error, or -1 if not defined.
     *
     * @return line number of the parsing error, or -1 if not defined
     * @since 1.5
     */
    public int getLineNumber()
    {
        return lineNumber;
    }

    /**
     * Return the name of the template containing the error, or null if not
     * defined.
     *
     * @return the name of the template containing the parsing error, or null
     *      if not defined
     * @since 1.5
     */
    public String getTemplateName()
    {
        return templateName;
    }

    /**
     * Return the invalid syntax or reference that triggered this error, or null
     * if not defined.
     *
     * @return Return the invalid syntax or reference that triggered this error, or null
     * if not defined
     * @since 1.5
     */
    public String getInvalidSyntax()
    {
        return invalidSyntax;
    }

    /**
     * Return our custom message if we have one, else return the default message
     */
    public String getMessage()
    {
      if (msg != null) return msg;
      return super.getMessage();
    }
}