You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by nb...@apache.org on 2004/04/16 22:36:12 UTC
cvs commit: jakarta-velocity-tools/src/java/org/apache/velocity/tools/view ToolboxRuleSet.java ViewToolInfo.java
nbubna 2004/04/16 13:36:12
Modified: src/java/org/apache/velocity/tools/view ToolboxRuleSet.java
ViewToolInfo.java
Added: src/java/org/apache/velocity/tools/view/tools
Configurable.java
Log:
Add support for configuring tools via the toolbox definition
Revision Changes Path
1.1 jakarta-velocity-tools/src/java/org/apache/velocity/tools/view/tools/Configurable.java
Index: Configurable.java
===================================================================
/*
* Copyright 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.
*/
package org.apache.velocity.tools.view.tools;
import java.util.Map;
/**
* Interface for tools that can be passed configuration parameters.
*
* @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
* @version $Id: Configurable.java,v 1.1 2004/04/16 20:36:12 nbubna Exp $
* @since VelocityTools 1.1
*/
public interface Configurable
{
/**
* Configure this tool using the specified parameters.
*
* @param parameters the configuration data for this tool
*/
public void configure(Map parameters);
}
1.4 +22 -1 jakarta-velocity-tools/src/java/org/apache/velocity/tools/view/ToolboxRuleSet.java
Index: ToolboxRuleSet.java
===================================================================
RCS file: /home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/view/ToolboxRuleSet.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ToolboxRuleSet.java 18 Feb 2004 20:08:29 -0000 1.3
+++ ToolboxRuleSet.java 16 Apr 2004 20:36:12 -0000 1.4
@@ -17,9 +17,11 @@
package org.apache.velocity.tools.view;
import org.apache.commons.digester.Digester;
+import org.apache.commons.digester.Rule;
import org.apache.commons.digester.RuleSetBase;
import org.apache.velocity.tools.view.DataInfo;
import org.apache.velocity.tools.view.ViewToolInfo;
+import org.xml.sax.Attributes;
/**
* <p>The set of Digester rules required to parse a toolbox
@@ -59,6 +61,7 @@
digester.addObjectCreate("toolbox/tool", getToolInfoClass());
digester.addBeanPropertySetter("toolbox/tool/key", "key");
digester.addBeanPropertySetter("toolbox/tool/class", "classname");
+ digester.addRule("toolbox/tool/parameter", new ParameterRule());
digester.addSetNext("toolbox/tool", "addTool");
}
@@ -91,6 +94,24 @@
protected Class getDataInfoClass()
{
return DataInfo.class;
+ }
+
+
+ /****************************** Custom Rules *****************************/
+
+ /**
+ *
+ */
+ protected class ParameterRule extends Rule
+ {
+ public void begin(String ns, String ln, Attributes attributes)
+ throws Exception
+ {
+ ViewToolInfo toolinfo = (ViewToolInfo)digester.peek();
+ String name = attributes.getValue("name");
+ String value = attributes.getValue("value");
+ toolinfo.setParameter(name, value);
+ }
}
}
1.9 +54 -8 jakarta-velocity-tools/src/java/org/apache/velocity/tools/view/ViewToolInfo.java
Index: ViewToolInfo.java
===================================================================
RCS file: /home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/view/ViewToolInfo.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- ViewToolInfo.java 12 Mar 2004 20:30:32 -0000 1.8
+++ ViewToolInfo.java 16 Apr 2004 20:36:12 -0000 1.9
@@ -1,5 +1,5 @@
/*
- * Copyright 2003 The Apache Software Foundation.
+ * Copyright 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.
@@ -16,6 +16,9 @@
package org.apache.velocity.tools.view;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.velocity.tools.view.tools.Configurable;
import org.apache.velocity.tools.view.tools.ViewTool;
import org.apache.velocity.app.Velocity;
@@ -34,8 +37,9 @@
private String key;
private Class clazz;
+ private Map parameters;
private boolean initializable = false;
-
+ private boolean configurable = false;
public ViewToolInfo() {}
@@ -75,16 +79,45 @@
* If an instance of the tool cannot be created from
* the classname passed to this method, it will throw an exception.
*
- * @param classname the fully qualified java.lang.Class of the tool
+ * @param classname the fully qualified java.lang.Class name of the tool
*/
public void setClassname(String classname) throws Exception
{
this.clazz = getApplicationClass(classname);
- /* create an instance and see if it is initializable */
- if (clazz.newInstance() instanceof ViewTool)
+ /* create an instance and see if it is a ViewTool or Configurable */
+ Object instance = clazz.newInstance();
+ if (instance instanceof ViewTool)
{
this.initializable = true;
}
+ if (instance instanceof Configurable)
+ {
+ this.configurable = true;
+ }
+ }
+
+ /**
+ * Set parameter map for this tool.
+ *
+ * @since VelocityTools 1.1
+ */
+ public void setParameters(Map parameters)
+ {
+ this.parameters = parameters;
+ }
+
+ /**
+ * Set/add new parameter for this tool.
+ *
+ * @since VelocityTools 1.1
+ */
+ public void setParameter(String name, String value)
+ {
+ if (parameters == null)
+ {
+ parameters = new HashMap();
+ }
+ parameters.put(name, value);
}
@@ -103,6 +136,15 @@
/**
+ * Get parameters for this tool.
+ * @since VelocityTools 1.1
+ */
+ public Map getParameters()
+ {
+ return parameters;
+ }
+
+ /**
* Returns a new instance of the tool. If the tool
* implements {@link ViewTool}, the new instance
* will be initialized using the given data.
@@ -128,8 +170,12 @@
Velocity.error("Exception while instantiating instance of \"" +
getClassname() + "\": " + e);
}
-
- if (initializable) {
+ if (configurable)
+ {
+ ((Configurable)tool).configure(parameters);
+ }
+ if (initializable)
+ {
((ViewTool)tool).init(initData);
}
return tool;
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org