On Fri, 2005-05-20 at 08:04 -0400, Grant Ingersoll wrote:
> I have a static method that needs to load a rather large file once (this
> method is in a third party library, so refactoring is not an option):
> 
> public class Foo
> {
>    /**
>    * Call once before using Foo to load
>    **/
>    public static void init(String filename);
> 
>    ...
> }
> My config xmlrules file looks like:
>  
> <pattern value="example/foo">
>         <!-- static initializer, somewhat-->
>         <object-create-rule classname="Foo"/>
>         <call-method-rule methodname="init"
>                       paramcount="1" />
>         <call-param-rule paramnumber='0' attrname='file'/>
>     </pattern>
> 
> And my configuration file looks like:
> <example>
>      <foo file="path.to.foo.initialization.file"/>
> </example>
> 
> 
> The object-create-rule is superflous to some extent, b/c the init call
> is on a static.  However, from my limited understanding of Digester, I
> need to have an object
> on the stack of type Foo in order to call a method on it (even if it is
> static).
> 
> The above works, it's just that the object created is just thrown away,
> as it is not needed, since the other method that I use on Foo is also
> static.  I was just wondering if there was another way to do this, I can
> envision something like:
> 
> <call-static-method-rule methodname="init" paramcount="1"
> classname="Foo"/>
> <call-param-rule paramnumber='0' attrname='file'/>

Ah. All is clear now, thanks.

There is no such rule built into Digester; as you state the
call-method-rule assumes the target method is on an object on the
digester stack.

However Digester is explicitly designed to allow users to create their
own custom rules. I think that's the best option in your case.

public class FooInitializationRule extends Rule {
   public void begin(String namespace, String name, 
     Attributes attrs) {  

     // here you get the value of the file attribute
     // then call Foo.init(value)
   }
}

 // if you were using the digester API instead of xmlrules,
 // you could then do this...
 digester.addRule("example/foo", new FooInitializationRule());

The fact that you're using that xmlrules stuff makes things more
complicated, though. The xmlrules documentation does mention support for
"programmatically-created rules" which I suppose is the hook to allow
custom rules in xmlrules files. You'll need to figure that out yourself,
though, as I don't use xmlrules.

Regards,

Simon



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to