Search code examples
apache-commons-digester

declare plugin with digester xmlrules


I try to write Apache digester parsing rules in xml file (xmlrules). But I don't see something like PluginCreateRule in digester-rules.dtd. The question is: (How) Can I declare and reference plugin in a xml rule file?


Solution

  • I think you're out of luck here.

    As you've seen, there is no plugin-create-rule in the DTD file. Also, Digester uses itself to parse the XML rules, and there is no mention of the plugin-create-rule in the digester configuration itself.

    Looking at that code, it would seem pretty straight forward to add that rule, so I'm not sure if it has been left out just because no-one has gotten around to adding it, or because there are some problems with the implementation which are not immediately obvious.

    I would have a go at a patch to implement this, but there appears to be little activity in Digester development these days...

    Update

    Yep - seems fairly simple to create a basic implementation which supports this:

    • Add a method to binder/PluginCreateRuleBuilder to support creation by class name not just class instance (just copied code from ObjectCreateBuilder).
    • Add a new xmlrules/PluginCreateRule class (again, based on ObjectCreateRule with most of it thrown away).
    • Add the rule to the XmlRulesModule.
    • Add the rule to the DTD.

    Full patch file here.

    Tested using the example from the Digester Plugin doco with the following XML

    <digester-rules>
      <pattern value="pipeline">
        <object-create-rule classname="Pipeline"/>
        <pattern value="source">
          <call-method-rule methodname="setSource" paramcount="1"/>
          <call-param-rule paramnumber="0" attrname="file"/>
        </pattern>
        <pattern value="transform">
          <plugin-create-rule pluginclass="Transform"/>
        </pattern>
        <pattern value="destination">
          <call-method-rule methodname="setDest" paramcount="1"/>
          <call-param-rule paramnumber="0" attrname="file"/>
        </pattern>
      </pattern>
    </digester-rules>
    

    Note that this is only a basic proof-of-concept, but should give you something to work on if you want to go that way. Also, there are a bunch more options for creating plugins which are not covered by this and would be required for a complete solution, so this patch is not (in its current state) suitable for submission back to the Digester project.

    Update 2

    To call this, you need to pass a PluginRules instance to the loader when creating a digester:

    DigesterLoader loader = DigesterLoader.newLoader(new FromXmlRulesModule() {
      ...
    });
    
    Digester digester= loader.newDigester(new PluginRules());
    

    Cheers,