Author: byron
Date: Thu Jan 22 05:42:52 2009
New Revision: 736638
URL: http://svn.apache.org/viewvc?rev=736638&view=rev
Log:
Fix lexical parse error messages so that they are in the standard format
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/Template.java
velocity/engine/trunk/src/java/org/apache/velocity/exception/ParseErrorException.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
velocity/engine/trunk/src/test/org/apache/velocity/test/IndexTestCase.java
velocity/engine/trunk/src/test/org/apache/velocity/test/StrictEscapeTestCase.java
velocity/engine/trunk/src/test/org/apache/velocity/test/VelocimacroTestCase.java
Modified: velocity/engine/trunk/src/java/org/apache/velocity/Template.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/Template.java?rev=736638&r1=736637&r2=736638&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/Template.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/Template.java Thu Jan 22
05:42:52 2009
@@ -139,12 +139,12 @@
/*
* remember the error and convert
*/
- errorCondition = new ParseErrorException( pex );
+ errorCondition = new ParseErrorException(pex, name);
throw errorCondition;
}
catch ( TemplateInitException pex )
{
- errorCondition = new ParseErrorException( pex );
+ errorCondition = new ParseErrorException( pex, name);
throw errorCondition;
}
/**
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/exception/ParseErrorException.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/exception/ParseErrorException.java?rev=736638&r1=736637&r2=736638&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/exception/ParseErrorException.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/exception/ParseErrorException.java
Thu Jan 22 05:42:52 2009
@@ -19,6 +19,10 @@
* under the License.
*/
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.velocity.runtime.log.Log;
import org.apache.velocity.runtime.parser.ParseException;
import org.apache.velocity.util.introspection.Info;
@@ -60,6 +64,11 @@
* 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.
@@ -67,19 +76,23 @@
* @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
* @since 1.5
*/
- public ParseErrorException(ParseException pex)
+ 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...
@@ -92,7 +105,20 @@
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 "
+ + Log.formatFileString(templateName, lineNumber,
columnNumber);
+ }
+
// ugly, ugly, ugly...
if (pex.currentToken != null && pex.currentToken.next != null)
@@ -109,9 +135,11 @@
* @param pex the parsing exception
* @since 1.5
*/
- public ParseErrorException(VelocityException pex)
+ 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...
@@ -218,4 +246,12 @@
return invalidSyntax;
}
+ /**
+ * Return our custum message if we have one, else return the default
message
+ */
+ public String getMessage()
+ {
+ if (msg != null) return msg;
+ return super.getMessage();
+ }
}
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java?rev=736638&r1=736637&r2=736638&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java
Thu Jan 22 05:42:52 2009
@@ -1234,11 +1234,11 @@
}
catch (ParseException pex)
{
- throw new ParseErrorException(pex);
+ throw new ParseErrorException(pex, null);
}
catch (TemplateInitException pex)
{
- throw new ParseErrorException(pex);
+ throw new ParseErrorException(pex, null);
}
if (nodeTree == null)
@@ -1288,7 +1288,7 @@
}
catch (TemplateInitException pex)
{
- throw new ParseErrorException(pex);
+ throw new ParseErrorException(pex, null);
}
/**
* pass through application level runtime exceptions
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java?rev=736638&r1=736637&r2=736638&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
Thu Jan 22 05:42:52 2009
@@ -189,9 +189,8 @@
StringBuffer out = new StringBuffer(100)
.append("Max calling depth of ").append(maxCallDepth)
- .append(" was exceeded in Template:").append(templateName)
- .append(" and Macro:").append(macroName)
- .append(" with Call Stack:");
+ .append(" was exceeded in macro '").append(macroName)
+ .append("' with Call Stack:");
for (int i = 0; i < stack.length; i++)
{
if (i != 0)
Modified:
velocity/engine/trunk/src/test/org/apache/velocity/test/IndexTestCase.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/IndexTestCase.java?rev=736638&r1=736637&r2=736638&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/IndexTestCase.java
(original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/IndexTestCase.java
Thu Jan 22 05:42:52 2009
@@ -59,6 +59,8 @@
Boo boo = new Boo();
context.put("boo", boo);
+
+ DEBUG=true;
}
public void testCallingIndex()
@@ -126,8 +128,7 @@
{
assertEvalExceptionAt("$boo['throwex']", 1, 5);
assertEvalExceptionAt("$boo[]", 1, 6);
- // Need to fix parse error reporting
- // assertEvalExceptionAt("$boo[blaa]", 1, 6);
+ assertEvalExceptionAt("$boo[blaa]", 1, 6);
assertEvalExceptionAt("#set($foo[1] = 3)", 1, 10);
assertEvalExceptionAt("$a[500]", 1, 3);
}
Modified:
velocity/engine/trunk/src/test/org/apache/velocity/test/StrictEscapeTestCase.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/StrictEscapeTestCase.java?rev=736638&r1=736637&r2=736638&view=diff
==============================================================================
---
velocity/engine/trunk/src/test/org/apache/velocity/test/StrictEscapeTestCase.java
(original)
+++
velocity/engine/trunk/src/test/org/apache/velocity/test/StrictEscapeTestCase.java
Thu Jan 22 05:42:52 2009
@@ -37,7 +37,6 @@
{
super.setUp();
engine.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT_ESCAPE,
Boolean.TRUE);
- DEBUG=true;
context.put("pow", "bang");
}
Modified:
velocity/engine/trunk/src/test/org/apache/velocity/test/VelocimacroTestCase.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/VelocimacroTestCase.java?rev=736638&r1=736637&r2=736638&view=diff
==============================================================================
---
velocity/engine/trunk/src/test/org/apache/velocity/test/VelocimacroTestCase.java
(original)
+++
velocity/engine/trunk/src/test/org/apache/velocity/test/VelocimacroTestCase.java
Thu Jan 22 05:42:52 2009
@@ -102,8 +102,8 @@
}
catch (MacroOverflowException e)
{
- assertEquals("Max calling depth of 5 was exceeded in
Template:vm_chain2"+
- " and Macro:bar with Call
Stack:bar->bar->bar->bar->bar",
+ assertEquals("Max calling depth of 5 was exceeded in macro 'bar'"+
+ " with Call Stack:bar->bar->bar->bar->bar at
vm_chain2[line 1, column 15]",
e.getMessage());
}
@@ -114,8 +114,8 @@
}
catch (MacroOverflowException e)
{
- assertEquals("Max calling depth of 5 was exceeded in
Template:vm_chain3"+
- " and Macro:inner with Call
Stack:baz->inner->baz->inner->baz",
+ assertEquals("Max calling depth of 5 was exceeded in macro
'inner'"+
+ " with Call Stack:baz->inner->baz->inner->baz at
vm_chain3[line 1, column 64]",
e.getMessage());
}
@@ -126,8 +126,8 @@
}
catch (MacroOverflowException e)
{
- assertEquals("Max calling depth of 5 was exceeded in
Template:vm_chain4"+
- " and Macro:loop with Call
Stack:bad->inside->loop->bad->inside",
+ assertEquals("Max calling depth of 5 was exceeded in macro 'loop'"+
+ " with Call Stack:bad->inside->loop->bad->inside
at vm_chain4[line 1, column 94]",
e.getMessage());
}
}