Author: henning
Date: Sat Oct 29 10:52:49 2005
New Revision: 329456
URL: http://svn.apache.org/viewcvs?rev=329456&view=rev
Log:
- Remove template specific code from ParseException, reverting it to the
auto-generated state.
- Put template specific code into Subclassed Exception, TemplateParseException
- update ParseErrorException to identify a TemplateParseException and pull the
template name and
line and column number from the exception.
- Even a normal ParseException can report line number and column number by
exploiting a public object.
Add code for this to the ParseErrorException
Added:
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/TemplateParseException.java
(with props)
Modified:
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
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=329456&r1=329455&r2=329456&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
Sat Oct 29 10:52:49 2005
@@ -17,6 +17,7 @@
*/
import org.apache.velocity.runtime.parser.ParseException;
+import org.apache.velocity.runtime.parser.TemplateParseException;
/**
* Application-level exception thrown when a resource of any type
@@ -44,12 +45,12 @@
/**
* The line number of the parsing error, or -1 if not defined.
*/
- private int lineNumber;
+ private int lineNumber = -1;
/**
* The name of the template containing the error, or null if not defined.
*/
- private String templateName;
+ private String templateName = "*unset*";
/**
* Create a ParseErrorException with the given message.
@@ -69,10 +70,24 @@
public ParseErrorException(ParseException pex)
{
super(pex.getMessage());
-
- columnNumber = pex.columnNumber;
- lineNumber = pex.lineNumber;
- templateName = pex.templateName;
+
+ // Don't use a second C'tor, TemplateParseException is a subclass of
+ // ParseException...
+ if (pex instanceof TemplateParseException)
+ {
+ TemplateParseException tpex = (TemplateParseException) pex;
+
+ columnNumber = tpex.getColumnNumber();
+ lineNumber = tpex.getLineNumber();
+ templateName = tpex.getTemplateName();
+ }
+ else
+ {
+ // ugly, ugly, ugly...
+ columnNumber = pex.currentToken.next.beginColumn;
+ lineNumber = pex.currentToken.next.beginLine;
+ templateName = "*unset*";
+ }
}
/**
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=329456&r1=329455&r2=329456&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
Sat Oct 29 10:52:49 2005
@@ -1,4 +1,4 @@
-/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 2.1
*/
+/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0
*/
package org.apache.velocity.runtime.parser;
/**
@@ -10,26 +10,7 @@
* You can modify this class to customize your error reporting
* mechanisms so long as you retain the public fields.
*/
-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;
- }
+public class ParseException extends Exception {
/**
* This constructor is used by the method "generateParseException"
@@ -65,14 +46,12 @@
* these constructors.
*/
- public ParseException()
- {
+ public ParseException() {
super();
specialConstructor = false;
}
- public ParseException(String message)
- {
+ public ParseException(String message) {
super(message);
specialConstructor = false;
}
@@ -104,24 +83,6 @@
* 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
@@ -133,70 +94,45 @@
* 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;
- }
-
- 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 + " ";
+ 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 + " ";
}
retval += expected;
return retval;
- }
+ }
/**
* The end of line string for this machine.
@@ -241,13 +177,10 @@
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;
Added:
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/TemplateParseException.java
URL:
http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/TemplateParseException.java?rev=329456&view=auto
==============================================================================
---
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/TemplateParseException.java
(added)
+++
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/TemplateParseException.java
Sat Oct 29 10:52:49 2005
@@ -0,0 +1,205 @@
+package org.apache.velocity.runtime.parser;
+
+/*
+ * Copyright 2000-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.
+ */
+
+/**
+ * This is an extension of the ParseException, which also takes a
+ * template name.
+ *
+ * @see org.apache.velocity.runtime.parser.ParseException
+ *
+ */
+public class TemplateParseException
+ extends ParseException
+{
+ /**
+ * This is the name of the template which contains the parsing error, or
+ * null if not defined.
+ */
+ private final String templateName;
+
+ /**
+ * This constructor is used to add a template name
+ * to info cribbed from a ParseException generated in the parser.
+ */
+ public TemplateParseException(Token currentTokenVal, int [][]
expectedTokenSequencesVal, String [] tokenImageVal,
+ String templateNameVal)
+ {
+ super(currentTokenVal, expectedTokenSequencesVal, tokenImageVal);
+ this.templateName = templateNameVal;
+ }
+
+ /**
+ * 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. The boolean
+ * flag "specialConstructor" is also set to true to indicate that
+ * this constructor was used to create this object.
+ * 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 TemplateParseException(Token currentTokenVal, int [][]
expectedTokenSequencesVal, String [] tokenImageVal)
+ {
+ super(currentTokenVal, expectedTokenSequencesVal, tokenImageVal);
+ templateName = "*unset*";
+ }
+
+ /**
+ * 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.
+ */
+ public TemplateParseException()
+ {
+ super();
+ templateName = "*unset*";
+ }
+
+ /**
+ * Creates a new TemplateParseException object.
+ *
+ * @param message TODO: DOCUMENT ME!
+ */
+ public TemplateParseException(String message)
+ {
+ super(message);
+ templateName = "*unset*";
+ }
+
+ /**
+ * returns the Template name where this exception occured.
+ */
+ public String getTemplateName()
+ {
+ return templateName;
+ }
+
+ /**
+ * returns the line number where this exception occured.
+ */
+ public int getLineNumber()
+ {
+ if ((currentToken != null) && (currentToken.next != null))
+ {
+ return currentToken.next.beginLine;
+ }
+ else
+ {
+ return -1;
+ }
+ }
+
+ /**
+ * returns the column number where this exception occured.
+ */
+ public int getColumnNumber()
+ {
+ if ((currentToken != null) && (currentToken.next != null))
+ {
+ return currentToken.next.beginColumn;
+ }
+ else
+ {
+ return -1;
+ }
+ }
+
+ /**
+ * This method has the standard behavior when this object has been
+ * created using the standard constructors. Otherwise, it uses
+ * "currentToken" and "expectedTokenSequences" to generate a parse
+ * error message and returns it. If this object has been created
+ * due to a parse error, and you do not catch it (it gets thrown
+ * from the parser), then this method is called during the printing
+ * of the final stack trace, and hence the correct error message
+ * gets displayed.
+ */
+ 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 + " ");
+ }
+
+ 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 += (templateName != null) ? (" of " + templateName + eol) :
("." + eol);
+
+ if (expectedTokenSequences.length == 1)
+ {
+ retval += ("Was expecting:" + eol + " ");
+ }
+ else
+ {
+ retval += ("Was expecting one of:" + eol + " ");
+ }
+
+ retval += expected;
+
+ return retval;
+ }
+}
Propchange:
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/TemplateParseException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/TemplateParseException.java
------------------------------------------------------------------------------
svn:keywords = Id Author Date Revision
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]