diff options
10 files changed, 148 insertions, 93 deletions
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/ParserConfiguration.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/ParserConfiguration.java new file mode 100644 index 00000000..6ec74b4a --- /dev/null +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/ParserConfiguration.java @@ -0,0 +1,116 @@ +package org.apache.velocity.runtime; + +/* + * 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. + */ + +/** + * Class gathering configured replacement characters for a specific parser class. + * @since 2.2 + */ + +public class ParserConfiguration +{ + /** + * Configured replacement character for '$' + */ + private char dollar = '$'; + + /** + * Configured replacement character for '#' + */ + private char hash = '#'; + + /** + * Configured replacement character for '@' + */ + private char at = '@'; + + /** + * Configured replacement character for '*' + */ + private char asterisk = '*'; + + /** + * Getter for '$' configured replacement character + * @return configured replacement character for '$' + */ + public char getDollarChar() + { + return dollar; + } + + /** + * Setter for '$' configured replacement character + */ + void setDollarChar(char dollar) + { + this.dollar = dollar; + } + + /** + * Getter for '#' configured replacement character + * @return configured replacement character for '#' + */ + public char getHashChar() + { + return hash; + } + + /** + * Setter for '#' configured replacement character + */ + void setHashChar(char hash) + { + this.hash = hash; + } + + /** + * Getter for '@' configured replacement character + * @return configured replacement character for '@' + */ + public char getAtChar() + { + return at; + } + + /** + * Setter for '@' configured replacement character + */ + void setAtChar(char at) + { + this.at = at; + } + + /** + * Getter for '*' configured replacement character + * @return configured replacement character for '*' + */ + public char getAsteriskChar() + { + return asterisk; + } + + /** + * Setter for '*' configured replacement character + */ + void setAsteriskChar(char asterisk) + { + this.asterisk = asterisk; + } +} diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java index b3e0923e..6971fbbb 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java @@ -40,7 +40,6 @@ import org.apache.velocity.runtime.directive.StopCommand; import org.apache.velocity.runtime.parser.LogContext; import org.apache.velocity.runtime.parser.ParseException; import org.apache.velocity.runtime.parser.Parser; -import org.apache.velocity.runtime.parser.StandardParser; import org.apache.velocity.runtime.parser.node.Node; import org.apache.velocity.runtime.parser.node.SimpleNode; import org.apache.velocity.runtime.resource.ContentResource; @@ -236,29 +235,10 @@ public class RuntimeInstance implements RuntimeConstants, RuntimeServices private Constructor parserConstructor; /** - * Configured '$' character + * Configured replacement characters in parser grammar * @since 2.2 */ - private char dollar = '$'; - - /** - * Configured '#' character - * @since 2.2 - */ - private char hash = '$'; - - /** - * Configured '@' character - * @since 2.2 - */ - private char at = '@'; - - /** - * Configured '*' character - * @since 2.2 - */ - private char asterisk = '*'; - + private ParserConfiguration parserConfiguration; /** * Creates a new RuntimeInstance object. @@ -355,10 +335,7 @@ public class RuntimeInstance implements RuntimeConstants, RuntimeServices this.runtimeDirectivesShared = null; this.uberSpect = null; this.stringInterning = false; - this.dollar = '$'; - this.hash = '#'; - this.at = '@'; - this.asterisk = '*'; + this.parserConfiguration = new ParserConfiguration(); /* * create a VM factory, introspector, and application attributes @@ -1271,10 +1248,11 @@ public class RuntimeInstance implements RuntimeConstants, RuntimeServices * test parser creation and use generated parser to fill up customized characters */ Parser parser = parserPool.get(); - dollar = parser.dollar(); - hash = parser.hash(); - at = parser.at(); - asterisk = parser.asterisk(); + parserConfiguration = new ParserConfiguration(); + parserConfiguration.setDollarChar(parser.dollar()); + parserConfiguration.setHashChar(parser.hash()); + parserConfiguration.setAtChar(parser.at()); + parserConfiguration.setAsteriskChar(parser.asterisk()); parserPool.put(parser); } else @@ -1622,7 +1600,7 @@ public class RuntimeInstance implements RuntimeConstants, RuntimeServices } /* now just create the VM call, and use evaluate */ - StringBuilder template = new StringBuilder(String.valueOf(hash)); + StringBuilder template = new StringBuilder(String.valueOf(parserConfiguration.getHashChar())); template.append(vmName); template.append("("); for (String param : params) @@ -1998,26 +1976,8 @@ public class RuntimeInstance implements RuntimeConstants, RuntimeServices } @Override - public char dollar() - { - return dollar; - } - - @Override - public char hash() - { - return hash; - } - - @Override - public char at() - { - return at; - } - - @Override - public char asterisk() + public ParserConfiguration getParserConfiguration() { - return asterisk; + return parserConfiguration; } } diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java index 951b15e6..82974078 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeServices.java @@ -491,30 +491,9 @@ public interface RuntimeServices boolean isScopeControlEnabled(String scopeName); /** - * Get the character configured for '$' - * @return configured character for '$', or '$' + * Get the replacement characters configured for this runtime service's parser + * @return configured replacement characters * @since 2.2 */ - char dollar(); - - /** - * Get the character configured for '#' - * @return configured character for '#', or '#' - * @since 2.2 - */ - char hash(); - - /** - * Get the character configured for '@' - * @return configured character for '@', or '@' - * @since 2.2 - */ - char at(); - - /** - * Get the character configured for '*' - * @return configured character for '*', or '*' - * @since 2.2 - */ - char asterisk(); + ParserConfiguration getParserConfiguration(); } diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Macro.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Macro.java index c5673f0f..44d3f08c 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Macro.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/Macro.java @@ -220,7 +220,7 @@ public class Macro extends Directive // trim off the leading $ for the args after the macro name. // saves everyone else from having to do it - if (i > 0 && macroArg.name.startsWith(String.valueOf(rsvc.dollar()))) + if (i > 0 && macroArg.name.startsWith(String.valueOf(rsvc.getParserConfiguration().getDollarChar()))) { macroArg.name = macroArg.name.substring(1); } @@ -273,10 +273,10 @@ public class Macro extends Directive { StringBuilder ret = (buf == null) ? new StringBuilder() : buf; - ret.append(rsvc.hash()).append(macroArgs.get(0).name).append("( "); + ret.append(rsvc.getParserConfiguration().getHashChar()).append(macroArgs.get(0).name).append("( "); for (MacroArg marg : macroArgs) { - ret.append(rsvc.dollar()).append(marg.name); + ret.append(rsvc.getParserConfiguration().getDollarChar()).append(marg.name); if (marg.defaultVal != null) { ret.append("=").append(marg.defaultVal); diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java index da019698..655e9b79 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/directive/RuntimeMacro.java @@ -141,7 +141,7 @@ public class RuntimeMacro extends Directive */ // Tokens can be used here since we are in init() and Tokens have not been dropped yet Token t = node.getLastToken(); - if (t.image.startsWith(")") || t.image.startsWith(rsvc.hash() + "end")) + if (t.image.startsWith(")") || t.image.startsWith(rsvc.getParserConfiguration().getHashChar() + "end")) { strictRef = rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false); } @@ -195,7 +195,7 @@ public class RuntimeMacro extends Directive int pos = -1; while (t != null && t != node.getLastToken()) { - if (pos == -1) pos = t.image.lastIndexOf(rsvc.hash()); + if (pos == -1) pos = t.image.lastIndexOf(rsvc.getParserConfiguration().getHashChar()); if (pos != -1) { buffer.append(t.image.substring(pos)); @@ -210,7 +210,7 @@ public class RuntimeMacro extends Directive if (t != null) { - if (pos == -1) pos = t.image.lastIndexOf(rsvc.hash()); + if (pos == -1) pos = t.image.lastIndexOf(rsvc.getParserConfiguration().getHashChar()); if (pos != -1) { buffer.append(t.image.substring(pos)); diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java index a8bf65e2..82cf3b4e 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java @@ -112,7 +112,7 @@ public class ASTDirective extends SimpleNode */ t = getFirstToken(); int pos = -1; - while (t != null && (pos = t.image.lastIndexOf(rsvc.hash())) == -1) + while (t != null && (pos = t.image.lastIndexOf(rsvc.getParserConfiguration().getHashChar())) == -1) { t = t.next; } @@ -147,7 +147,7 @@ public class ASTDirective extends SimpleNode directive.setLocation(t.beginLine, t.beginColumn, getTemplate()); directive.init(rsvc, context, this); } - else if( directiveName.startsWith(String.valueOf(rsvc.at())) ) + else if( directiveName.startsWith(String.valueOf(rsvc.getParserConfiguration().getAtChar())) ) { if( this.jjtGetNumChildren() > 0 ) { @@ -312,7 +312,7 @@ public class ASTDirective extends SimpleNode { writer.write(prefix); writer.write(morePrefix); - writer.write(rsvc.hash()); + writer.write(rsvc.getParserConfiguration().getHashChar()); writer.write(directiveName); writer.write(postfix); } diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java index 670baa14..b2ac0429 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java @@ -93,7 +93,7 @@ public class ASTIfStatement extends SimpleNode */ Token t = getFirstToken(); int pos = -1; - while (t != null && (pos = t.image.lastIndexOf(rsvc.hash())) == -1) + while (t != null && (pos = t.image.lastIndexOf(rsvc.getParserConfiguration().getHashChar())) == -1) { t = t.next; } diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java index 128cb9fa..0a007e23 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java @@ -322,7 +322,7 @@ public class ASTReference extends SimpleNode (!onlyTestingReference || warnInvalidTestedReferences))) { result = EventHandlerUtil.invalidGetMethod(rsvc, context, - rsvc.dollar() + rootString, null, null, uberInfo); + rsvc.getParserConfiguration().getDollarChar() + rootString, null, null, uberInfo); } return result; @@ -388,7 +388,7 @@ public class ASTReference extends SimpleNode (!onlyTestingReference || warnInvalidTestedReferences || numChildren > 0)) { result = EventHandlerUtil.invalidGetMethod(rsvc, context, - rsvc.dollar() + rootString, previousResult, null, uberInfo); + rsvc.getParserConfiguration().getDollarChar() + rootString, previousResult, null, uberInfo); } } else @@ -402,7 +402,7 @@ public class ASTReference extends SimpleNode (referenceType != QUIET_REFERENCE || warnInvalidQuietReferences) && (!onlyTestingReference || warnInvalidTestedReferences || failedChild < numChildren - 1)) { - StringBuilder name = new StringBuilder(String.valueOf(rsvc.dollar())).append(rootString); + StringBuilder name = new StringBuilder(String.valueOf(rsvc.getParserConfiguration().getDollarChar())).append(rootString); for (int i = 0; i <= failedChild; i++) { Node node = jjtGetChild(i); @@ -953,7 +953,7 @@ public class ASTReference extends SimpleNode int i = 0; int len = t.image.length(); - i = t.image.indexOf(rsvc.dollar()); + i = t.image.indexOf(rsvc.getParserConfiguration().getDollarChar()); if (i == -1) { @@ -1037,7 +1037,7 @@ public class ASTReference extends SimpleNode * last $ */ - int loc1 = t.image.lastIndexOf(rsvc.dollar()); + int loc1 = t.image.lastIndexOf(rsvc.getParserConfiguration().getDollarChar()); /* * if we have extra stuff, loc > 0 diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java index b01a0348..4af186d6 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java @@ -115,7 +115,7 @@ public class ASTSetDirective extends SimpleNode */ Token t = getFirstToken(); int pos = -1; - while (t != null && (pos = t.image.lastIndexOf(rsvc.hash())) == -1) + while (t != null && (pos = t.image.lastIndexOf(rsvc.getParserConfiguration().getHashChar())) == -1) { t = t.next; } diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java index 81c3838e..78a462d8 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java @@ -96,8 +96,8 @@ public class ASTStringLiteral extends SimpleNode interpolate = rsvc.getBoolean( RuntimeConstants.INTERPOLATE_STRINGLITERALS, true) && getFirstToken().image.startsWith("\"") - && ((getFirstToken().image.indexOf(rsvc.dollar()) != -1) || (getFirstToken().image - .indexOf(rsvc.hash()) != -1)); + && ((getFirstToken().image.indexOf(rsvc.getParserConfiguration().getDollarChar()) != -1) || (getFirstToken().image + .indexOf(rsvc.getParserConfiguration().getHashChar()) != -1)); /* * get the contents of the string, minus the '/" at each end |