You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rd...@apache.org on 2003/11/13 00:21:19 UTC
cvs commit: jakarta-commons/digester/src/test/org/apache/commons/digester/plugins TestAll.java
rdonkin 2003/11/12 15:21:19
Modified: digester/src/java/org/apache/commons/digester/plugins
PluginCreateRule.java
digester/src/test/org/apache/commons/digester/plugins
TestAll.java
Log:
Added support for xml attribute specifying the plugin declaration class. Submitted by Simon Kitching.
Revision Changes Path
1.7 +163 -27 jakarta-commons/digester/src/java/org/apache/commons/digester/plugins/PluginCreateRule.java
Index: PluginCreateRule.java
===================================================================
RCS file: /home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/plugins/PluginCreateRule.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- PluginCreateRule.java 2 Nov 2003 23:26:59 -0000 1.6
+++ PluginCreateRule.java 12 Nov 2003 23:21:18 -0000 1.7
@@ -80,8 +80,31 @@
*/
public class PluginCreateRule extends Rule implements InitializableRule {
- private static final String PLUGIN_CLASS_ATTR = "plugin-class";
- private static final String PLUGIN_ID_ATTR = "plugin-id";
+ // the xml attribute the user uses on an xml element to specify
+ // the plugin's class
+ public static final String GLOBAL_PLUGIN_CLASS_ATTR_NS = null;
+ public static final String GLOBAL_PLUGIN_CLASS_ATTR = "plugin-class";
+
+ // the xml attribute the user uses on an xml element to specify
+ // the plugin's class
+ public static final String GLOBAL_PLUGIN_ID_ATTR_NS = null;
+ public static final String GLOBAL_PLUGIN_ID_ATTR = "plugin-id";
+
+ // see setGlobalPluginClassAttribute
+ private static String globalPluginClassAttrNs_ = GLOBAL_PLUGIN_CLASS_ATTR_NS;
+ private static String globalPluginClassAttr_ = GLOBAL_PLUGIN_CLASS_ATTR;
+
+ // see setGlobalPluginIdAttribute
+ private static String globalPluginIdAttrNs_ = GLOBAL_PLUGIN_ID_ATTR_NS;
+ private static String globalPluginIdAttr_ = GLOBAL_PLUGIN_ID_ATTR;
+
+ // see setPluginClassAttribute
+ private String pluginClassAttrNs_ = globalPluginClassAttrNs_;
+ private String pluginClassAttr_ = globalPluginClassAttr_;
+
+ // see setPluginIdAttribute
+ private String pluginIdAttrNs_ = globalPluginIdAttrNs_;
+ private String pluginIdAttr_ = globalPluginIdAttr_;
/**
* In order to invoke the addRules method on the plugin class correctly,
@@ -114,6 +137,76 @@
*/
private PluginRules localRules_;
+ //-------------------- static methods -----------------------------------
+
+ /**
+ * Sets the xml attribute which the input xml uses to indicate to a
+ * PluginCreateRule which class should be instantiated.
+ * <p>
+ * Example:
+ * <pre>
+ * PluginCreateRule.setGlobalPluginClassAttribute(null, "class");
+ * </pre>
+ * will allow this in the input xml:
+ * <pre>
+ * [root]
+ * [some-plugin class="com.acme.widget"] ......
+ * </pre>
+ *
+ * Note that this changes the default for <i>all</i> PluginCreateRule
+ * instances. To override just specific PluginCreateRule instances (which
+ * may be more friendly in container-based environments), see method
+ * setPluginClassAttribute.
+ *
+ * @param namespaceUri is the namespace uri that the specified attribute
+ * is in. If the attribute is in no namespace, then this should be null.
+ * Note that if a namespace is used, the attrName value should <i>not</i>
+ * contain any kind of namespace-prefix. Note also that if you are using
+ * a non-namespace-aware parser, this parameter <i>must</i> be null.
+ *
+ * @param attrName is the attribute whose value contains the name of the
+ * class to be instantiated.
+ */
+ public static void setGlobalPluginClassAttribute(
+ String namespaceUri, String attrName) {
+ globalPluginClassAttrNs_ = namespaceUri;
+ globalPluginClassAttr_ = attrName;
+ }
+
+ /**
+ * Sets the xml attribute which the input xml uses to indicate to a
+ * PluginCreateRule which plugin declaration is being referenced.
+ * <p>
+ * Example:
+ * <pre>
+ * PluginCreateRule.setGlobalPluginIdAttribute(null, "id");
+ * </pre>
+ * will allow this in the input xml:
+ * <pre>
+ * [root]
+ * [some-plugin id="widget"] ......
+ * </pre>
+ *
+ * Note that this changes the default for <i>all</i> PluginCreateRule
+ * instances. To override just specific PluginCreateRule instances (which
+ * may be more friendly in container-based environments), see method
+ * setPluginIdAttribute.
+ *
+ * @param namespaceUri is the namespace uri that the specified attribute
+ * is in. If the attribute is in no namespace, then this should be null.
+ * Note that if a namespace is used, the attrName value should <i>not</i>
+ * contain any kind of namespace-prefix. Note also that if you are using
+ * a non-namespace-aware parser, this parameter <i>must</i> be null.
+ *
+ * @param attrName is the attribute whose value contains the id of the
+ * plugin declaration to be used when instantiating an object.
+ */
+ public static void setGlobalPluginIdAttribute(
+ String namespaceUri, String attrName) {
+ globalPluginIdAttrNs_ = namespaceUri;
+ globalPluginIdAttr_ = attrName;
+ }
+
//-------------------- constructors -------------------------------------
/**
@@ -179,6 +272,30 @@
}
}
+ /**
+ * Sets the xml attribute which the input xml uses to indicate to a
+ * PluginCreateRule which class should be instantiated.
+ * <p>
+ * See setGlobalPluginClassAttribute for more info.
+ */
+ public void setPluginClassAttribute(
+ String namespaceUri, String attrName) {
+ pluginClassAttrNs_ = namespaceUri;
+ pluginClassAttr_ = attrName;
+ }
+
+ /**
+ * Sets the xml attribute which the input xml uses to indicate to a
+ * PluginCreateRule which plugin declaration is being referenced.
+ * <p>
+ * See setGlobalPluginIdAttribute for more info.
+ */
+ public void setPluginIdAttribute(
+ String namespaceUri, String attrName) {
+ pluginIdAttrNs_ = namespaceUri;
+ pluginIdAttr_ = attrName;
+ }
+
//------------------- methods --------------------------------------------
/**
@@ -261,8 +378,9 @@
try {
defaultPlugin_.init(digester);
- }
- catch(PluginWrappedException pwe) {
+
+ } catch(PluginWrappedException pwe) {
+
throw new PluginConfigurationException(
pwe.getMessage(), pwe.getCause());
}
@@ -334,8 +452,29 @@
+ ", localrules=" + localRules_.toString());
}
- String pluginClassName = attributes.getValue(PLUGIN_CLASS_ATTR);
- String pluginId = attributes.getValue(PLUGIN_ID_ATTR);
+ String pluginClassName;
+ if (pluginClassAttrNs_ == null) {
+ // Yep, this is ugly.
+ //
+ // In a namespace-aware parser, the one-param version will
+ // return attributes with no namespace.
+ //
+ // In a non-namespace-aware parser, the two-param version will
+ // never return any attributes, ever.
+ pluginClassName = attributes.getValue(pluginClassAttr_);
+ } else {
+ pluginClassName =
+ attributes.getValue(pluginClassAttrNs_, pluginClassAttr_);
+ }
+
+ String pluginId;
+ if (pluginIdAttrNs_ == null) {
+ pluginId = attributes.getValue(pluginIdAttr_);
+ }
+ else {
+ pluginId =
+ attributes.getValue(pluginIdAttrNs_, pluginIdAttr_);
+ }
if (pluginClassName != null) {
currDeclaration = pluginManager.getDeclarationByClass(
@@ -345,23 +484,20 @@
currDeclaration = new Declaration(pluginClassName);
try {
currDeclaration.init(digester);
- }
- catch(PluginWrappedException pwe) {
+ } catch(PluginWrappedException pwe) {
throw new PluginInvalidInputException(
pwe.getMessage(), pwe.getCause());
}
pluginManager.addDeclaration(currDeclaration);
}
- }
- else if (pluginId != null) {
+ } else if (pluginId != null) {
currDeclaration = pluginManager.getDeclarationById(pluginId);
if (currDeclaration == null) {
throw new PluginInvalidInputException(
"Plugin id [" + pluginId + "] is not defined.");
}
- }
- else if (defaultPlugin_ != null) {
+ } else if (defaultPlugin_ != null) {
currDeclaration = defaultPlugin_;
}
else {
@@ -372,20 +508,20 @@
// now load up the custom rules into a private Rules instance
digester.setRules(localRules_);
- {
- currDeclaration.configure(digester, pattern_);
- Class pluginClass = currDeclaration.getPluginClass();
-
- Object instance = pluginClass.newInstance();
- getDigester().push(instance);
- if (debug) {
- log.debug(
- "PluginCreateRule.begin" + ": pattern=[" + pattern_ + "]" +
- " match=[" + digester.getMatch() + "]" +
- " pushed instance of plugin [" + pluginClass.getName() + "]");
- }
+ currDeclaration.configure(digester, pattern_);
+
+ Class pluginClass = currDeclaration.getPluginClass();
+
+ Object instance = pluginClass.newInstance();
+ getDigester().push(instance);
+ if (debug) {
+ log.debug(
+ "PluginCreateRule.begin" + ": pattern=[" + pattern_ + "]" +
+ " match=[" + digester.getMatch() + "]" +
+ " pushed instance of plugin [" + pluginClass.getName() + "]");
}
+
digester.setRules(oldRules);
((PluginRules) oldRules).beginPlugin(this);
1.4 +4 -3 jakarta-commons/digester/src/test/org/apache/commons/digester/plugins/TestAll.java
Index: TestAll.java
===================================================================
RCS file: /home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/plugins/TestAll.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestAll.java 9 Oct 2003 21:09:49 -0000 1.3
+++ TestAll.java 12 Nov 2003 23:21:18 -0000 1.4
@@ -99,6 +99,7 @@
suite.addTest(TestLocalRules.suite());
suite.addTest(TestRuleInfo.suite());
suite.addTest(TestRecursion.suite());
+ suite.addTest(TestConfigurablePluginAttributes.suite());
return suite;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org