Author: wglass Date: Fri Oct 14 23:17:50 2005 New Revision: 321296 URL: http://svn.apache.org/viewcvs?rev=321296&view=rev Log: ParseErrorException now reports template name. Thanks Malcolm Edgar! VELOCITY-373.
Added: jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/ParseExceptionTestCase.java (with props) jakarta/velocity/core/trunk/test/parseexception/ jakarta/velocity/core/trunk/test/parseexception/badtemplate.vm Modified: jakarta/velocity/core/trunk/src/java/org/apache/velocity/Template.java jakarta/velocity/core/trunk/src/java/org/apache/velocity/app/Velocity.java jakarta/velocity/core/trunk/src/java/org/apache/velocity/app/VelocityEngine.java jakarta/velocity/core/trunk/src/java/org/apache/velocity/exception/ParseErrorException.java jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/ParseException.java jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/Parser.java jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/Parser.jj jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/Parser.jjt jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/ParserTokenManager.java (props changed) Modified: jakarta/velocity/core/trunk/src/java/org/apache/velocity/Template.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/Template.java?rev=321296&r1=321295&r2=321296&view=diff ============================================================================== --- jakarta/velocity/core/trunk/src/java/org/apache/velocity/Template.java (original) +++ jakarta/velocity/core/trunk/src/java/org/apache/velocity/Template.java Fri Oct 14 23:17:50 2005 @@ -132,9 +132,8 @@ /* * remember the error and convert */ - - errorCondition = new ParseErrorException( pex.getMessage() ); - throw errorCondition; + errorCondition = new ParseErrorException( pex ); + throw errorCondition; } catch( Exception e ) { Modified: jakarta/velocity/core/trunk/src/java/org/apache/velocity/app/Velocity.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/app/Velocity.java?rev=321296&r1=321295&r2=321296&view=diff ============================================================================== --- jakarta/velocity/core/trunk/src/java/org/apache/velocity/app/Velocity.java (original) +++ jakarta/velocity/core/trunk/src/java/org/apache/velocity/app/Velocity.java Fri Oct 14 23:17:50 2005 @@ -258,7 +258,7 @@ } catch ( ParseException pex ) { - throw new ParseErrorException( pex.getMessage() ); + throw new ParseErrorException( pex ); } /* Modified: jakarta/velocity/core/trunk/src/java/org/apache/velocity/app/VelocityEngine.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/app/VelocityEngine.java?rev=321296&r1=321295&r2=321296&view=diff ============================================================================== --- jakarta/velocity/core/trunk/src/java/org/apache/velocity/app/VelocityEngine.java (original) +++ jakarta/velocity/core/trunk/src/java/org/apache/velocity/app/VelocityEngine.java Fri Oct 14 23:17:50 2005 @@ -292,7 +292,7 @@ } catch (ParseException pex) { - throw new ParseErrorException( pex.getMessage() ); + throw new ParseErrorException( pex ); } /* Modified: jakarta/velocity/core/trunk/src/java/org/apache/velocity/exception/ParseErrorException.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/exception/ParseErrorException.java?rev=321296&r1=321295&r2=321296&view=diff ============================================================================== --- jakarta/velocity/core/trunk/src/java/org/apache/velocity/exception/ParseErrorException.java (original) +++ jakarta/velocity/core/trunk/src/java/org/apache/velocity/exception/ParseErrorException.java Fri Oct 14 23:17:50 2005 @@ -16,6 +16,8 @@ * limitations under the License. */ +import org.apache.velocity.runtime.parser.ParseException; + /** * Application-level exception thrown when a resource of any type * has a syntax or other error which prevents it from being parsed. @@ -34,8 +36,74 @@ */ private static final long serialVersionUID = -6665197935086306474L; - public ParseErrorException(String exceptionMessage ) + /** + * 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; + + /** + * The name of the template containing the error, or null if not defined. + */ + private String templateName; + + /** + * Create a ParseErrorException with the given message. + * + * @param exceptionMessage the error exception message + */ + public ParseErrorException(String exceptionMessage) + { + super(exceptionMessage); + } + + /** + * Create a ParseErrorException with the given ParseException. + * + * @param pex the parsing exception + */ + public ParseErrorException(ParseException pex) + { + super(pex.getMessage()); + + columnNumber = pex.columnNumber; + lineNumber = pex.lineNumber; + templateName = pex.templateName; + } + + /** + * 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 + */ + 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 + */ + 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 + */ + public String getTemplateName() { - super(exceptionMessage); - } + return templateName; + } } Modified: jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/ParseException.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/ParseException.java?rev=321296&r1=321295&r2=321296&view=diff ============================================================================== --- jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/ParseException.java (original) +++ jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/ParseException.java Fri Oct 14 23:17:50 2005 @@ -10,7 +10,26 @@ * You can modify this class to customize your error reporting * mechanisms so long as you retain the public fields. */ -public class ParseException extends Exception { +public class ParseException extends Exception +{ + + /** + * This constructor is used to add a template name + * to info cribbed from a ParseException generated in the parser. + */ + public ParseException(Token currentTokenVal, + int[][] expectedTokenSequencesVal, + String[] tokenImageVal, + String templateNameVal + ) + { + super(""); + specialConstructor = true; + currentToken = currentTokenVal; + expectedTokenSequences = expectedTokenSequencesVal; + tokenImage = tokenImageVal; + templateName = templateNameVal; + } /** * This constructor is used by the method "generateParseException" @@ -46,12 +65,14 @@ * these constructors. */ - public ParseException() { + public ParseException() + { super(); specialConstructor = false; } - public ParseException(String message) { + public ParseException(String message) + { super(message); specialConstructor = false; } @@ -83,6 +104,24 @@ * defined in the generated ...Constants interface. */ public String[] tokenImage; + + /** + * This is the name of the template which contains the parsing error, or + * null if not defined. + */ + public String templateName; + + /** + * This is the column number of the error in the template, or -1 if not + * defined. + */ + public int columnNumber = -1; + + /** + * This is the line number of the error in the template, or -1 if not + * defined. + */ + public int lineNumber = -1; /** * This method has the standard behavior when this object has been @@ -94,45 +133,70 @@ * of the final stack trace, and hence the correct error message * gets displayed. */ - public String getMessage() { - if (!specialConstructor) { + public String getMessage() + { + if (!specialConstructor) + { return super.getMessage(); } String expected = ""; int maxSize = 0; - for (int i = 0; i < expectedTokenSequences.length; i++) { - if (maxSize < expectedTokenSequences[i].length) { - maxSize = expectedTokenSequences[i].length; - } - for (int j = 0; j < expectedTokenSequences[i].length; j++) { - expected += tokenImage[expectedTokenSequences[i][j]] + " "; - } - if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { - expected += "..."; - } - expected += eol + " "; + for (int i = 0; i < expectedTokenSequences.length; i++) + { + if (maxSize < expectedTokenSequences[i].length) + { + maxSize = expectedTokenSequences[i].length; + } + for (int j = 0; j < expectedTokenSequences[i].length; j++) + { + expected += tokenImage[expectedTokenSequences[i][j]] + " "; + } + if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) + { + expected += "..."; + } + expected += eol + " "; } + String retval = "Encountered \""; Token tok = currentToken.next; - for (int i = 0; i < maxSize; i++) { - if (i != 0) retval += " "; - if (tok.kind == 0) { - retval += tokenImage[0]; - break; - } - retval += add_escapes(tok.image); - tok = tok.next; - } - retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; - retval += "." + eol; - if (expectedTokenSequences.length == 1) { - retval += "Was expecting:" + eol + " "; - } else { - retval += "Was expecting one of:" + eol + " "; + for (int i = 0; i < maxSize; i++) + { + if (i != 0) + { + retval += " "; + } + if (tok.kind == 0) + { + retval += tokenImage[0]; + break; + } + retval += add_escapes(tok.image); + tok = tok.next; + } + + lineNumber = currentToken.next.beginLine; + columnNumber = currentToken.next.beginColumn; + retval += "\" at line " + lineNumber + " column " + columnNumber; + if (templateName != null) + { + retval += " of " + templateName + eol; + } + else + { + retval += "." + eol; + } + if (expectedTokenSequences.length == 1) + { + retval += "Was expecting:" + eol + " "; + } + else + { + retval += "Was expecting one of:" + eol + " "; } retval += expected; return retval; - } + } /** * The end of line string for this machine. @@ -177,10 +241,13 @@ retval.append("\\\\"); continue; default: - if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { + 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 { + } + else + { retval.append(ch); } continue; Modified: jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/Parser.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/Parser.java?rev=321296&r1=321295&r2=321296&view=diff ============================================================================== --- jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/Parser.java (original) +++ jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/Parser.java Fri Oct 14 23:17:50 2005 @@ -116,8 +116,8 @@ { rsvc.getLog().error("Parser Exception: " + templateName, pe); throw new ParseException (pe.currentToken, - pe.expectedTokenSequences, pe.tokenImage); - } + pe.expectedTokenSequences, pe.tokenImage, currentTemplateName); + } catch (TokenMgrError tme) { throw new ParseException("Lexical error: " + tme.toString()); Modified: jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/Parser.jj URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/Parser.jj?rev=321296&r1=321295&r2=321296&view=diff ============================================================================== --- jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/Parser.jj (original) +++ jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/Parser.jj Fri Oct 14 23:17:50 2005 @@ -1,4 +1,4 @@ -/[EMAIL PROTECTED](jjtree) Generated By:JJTree: Do not edit this line. C:/java/apache/velocity/src/java/org/apache/velocity/runtime/parser\Parser.jj */ +/[EMAIL PROTECTED](jjtree) Generated By:JJTree: Do not edit this line. C:/Documents and Settings/wglass/My Documents/GAWE/velocity/trunk/src/java/org/apache/velocity/runtime/parser\Parser.jj */ /[EMAIL PROTECTED]//* * Copyright 2000-2004 The Apache Software Foundation. * @@ -171,8 +171,8 @@ { rsvc.getLog().error("Parser Exception: " + templateName, pe); throw new ParseException (pe.currentToken, - pe.expectedTokenSequences, pe.tokenImage); - } + pe.expectedTokenSequences, pe.tokenImage, currentTemplateName); + } catch (TokenMgrError tme) { throw new ParseException("Lexical error: " + tme.toString()); Modified: jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/Parser.jjt URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/Parser.jjt?rev=321296&r1=321295&r2=321296&view=diff ============================================================================== --- jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/Parser.jjt (original) +++ jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/Parser.jjt Fri Oct 14 23:17:50 2005 @@ -192,8 +192,8 @@ { rsvc.getLog().error("Parser Exception: " + templateName, pe); throw new ParseException (pe.currentToken, - pe.expectedTokenSequences, pe.tokenImage); - } + pe.expectedTokenSequences, pe.tokenImage, currentTemplateName); + } catch (TokenMgrError tme) { throw new ParseException("Lexical error: " + tme.toString()); Propchange: jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/ParserTokenManager.java ------------------------------------------------------------------------------ eol-style = native Added: jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/ParseExceptionTestCase.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/ParseExceptionTestCase.java?rev=321296&view=auto ============================================================================== --- jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/ParseExceptionTestCase.java (added) +++ jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/ParseExceptionTestCase.java Fri Oct 14 23:17:50 2005 @@ -0,0 +1,140 @@ +package org.apache.velocity.test; + +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * 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. + */ + +import java.io.StringWriter; +import java.io.Writer; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.apache.velocity.Template; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.VelocityEngine; +import org.apache.velocity.exception.ParseErrorException; + +/** + * Test parser exception is generated with appropriate info. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Will Glass-Husain</a> + * @version $Id$ + */ +public class ParseExceptionTestCase extends BaseTestCase +{ + /** + * Path for templates. This property will override the + * value in the default velocity properties file. + */ + private final static String FILE_RESOURCE_LOADER_PATH = "test/parseexception"; + + + /** + * Default constructor. + */ + public ParseExceptionTestCase(String name) + { + super(name); + } + + public void setUp() + throws Exception + { + + } + + public static Test suite () + { + return new TestSuite(ParseExceptionTestCase.class); + } + + /** + * Tests that parseException has useful info when called by template.marge() + * @throws Exception + */ + public void testParseExceptionFromTemplate () + throws Exception + { + + VelocityEngine ve = new VelocityEngine(); + + ve.setProperty("file.resource.loader.cache", "true"); + ve.setProperty("file.resource.loader.path", FILE_RESOURCE_LOADER_PATH); + ve.init(); + + + Writer writer = new StringWriter(); + + VelocityContext context = new VelocityContext(); + + try + { + Template template = ve.getTemplate("badtemplate.vm"); + template.merge(context, writer); + fail("Should have thown a ParseErrorException"); + } + catch (ParseErrorException e) + { + assertEquals("badtemplate.vm",e.getTemplateName()); + assertEquals(5,e.getLineNumber()); + assertEquals(9,e.getColumnNumber()); + } + finally + { + if (writer != null) + { + writer.close(); + } + } + } + + /** + * Tests that parseException has useful info when thrown in VelocityEngine.evaluate() + * @throws Exception + */ + public void testParseExceptionFromEval () + throws Exception + { + + VelocityEngine ve = new VelocityEngine(); + ve.init(); + + VelocityContext context = new VelocityContext(); + + Writer writer = new StringWriter(); + + try + { + ve.evaluate(context,writer,"test"," #set($abc) "); + fail("Should have thown a ParseErrorException"); + } + catch (ParseErrorException e) + { + assertEquals("test",e.getTemplateName()); + assertEquals(1,e.getLineNumber()); + assertEquals(13,e.getColumnNumber()); + } + finally + { + if (writer != null) + { + writer.close(); + } + } + } + + +} Propchange: jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/ParseExceptionTestCase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/ParseExceptionTestCase.java ------------------------------------------------------------------------------ svn:keywords = Id Author Date Revision Added: jakarta/velocity/core/trunk/test/parseexception/badtemplate.vm URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/test/parseexception/badtemplate.vm?rev=321296&view=auto ============================================================================== --- jakarta/velocity/core/trunk/test/parseexception/badtemplate.vm (added) +++ jakarta/velocity/core/trunk/test/parseexception/badtemplate.vm Fri Oct 14 23:17:50 2005 @@ -0,0 +1,7 @@ +ok +ok +ok +ok + #set($s) +ok +ok \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]