[Digester] Initializing statics

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[Digester] Initializing statics

Grant Ingersoll
Hi,

I have a class that has a static initializer the loads a rather large
file (this is in a library class) and I would like to initialize it with
the file via Digester.  Right now, I create a dummy object of that class
and then apply a CallMethodRule invoking the init method with the
attribute containing the file name.  While this works, seems like a
waste to construct the dummy object.  Is there a preferred way to pass
configurable arguments to a static method on a class?

Thanks,
Grant

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Digester] Initializing statics

Simon Kitching
On Thu, 2005-05-19 at 14:22 -0400, Grant Ingersoll wrote:

> Hi,
>
> I have a class that has a static initializer the loads a rather large
> file (this is in a library class) and I would like to initialize it with
> the file via Digester.  Right now, I create a dummy object of that class
> and then apply a CallMethodRule invoking the init method with the
> attribute containing the file name.  While this works, seems like a
> waste to construct the dummy object.  Is there a preferred way to pass
> configurable arguments to a static method on a class?
>

Sorry, Grant, but I don't understand. Perhaps you could provide some
code demonstrating what you're trying to achieve?

Regards,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Digester] Initializing statics

Grant Ingersoll
In reply to this post by Grant Ingersoll
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'/>

Thanks,
Grant


>>> [hidden email] 5/19/2005 7:35:24 PM >>>
On Thu, 2005-05-19 at 14:22 -0400, Grant Ingersoll wrote:
> Hi,
>
> I have a class that has a static initializer the loads a rather
large
> file (this is in a library class) and I would like to initialize it
with
> the file via Digester.  Right now, I create a dummy object of that
class
> and then apply a CallMethodRule invoking the init method with the
> attribute containing the file name.  While this works, seems like a
> waste to construct the dummy object.  Is there a preferred way to
pass
> configurable arguments to a static method on a class?
>

Sorry, Grant, but I don't understand. Perhaps you could provide some
code demonstrating what you're trying to achieve?

Regards,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Digester] Initializing statics

Mattias Jiderhamn-2
At 2005-05-20 14:04, you 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'/>
>
>Thanks,
>Grant

It was a while since I worked with Digester, so there might be a more
straightforward way to call static methods which I have forgotten, but
otherwise couldn't you use a factory-create-rule which could then access
the file name through it's parameter
   public Object createObject(Attributes attributes)
?



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Digester] Initializing statics

Simon Kitching
In reply to this post by Grant Ingersoll
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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Digester] Initializing statics

Simon Kitching
In reply to this post by Mattias Jiderhamn-2
On Fri, 2005-05-20 at 14:22 +0200, Mattias J wrote:
> It was a while since I worked with Digester, so there might be a more
> straightforward way to call static methods which I have forgotten, but
> otherwise couldn't you use a factory-create-rule which could then access
> the file name through it's parameter
>    public Object createObject(Attributes attributes)

Unfortunately, FactoryCreateRule expects an object to be returned which
it then pushes on the stack. But in Magnus' case, there isn't any
suitable object to return as far as I can see.

I don't know what would happen if null was returned...maybe it would
work (null would be pushed onto the digester stack, but as long as
nothing tried to access it things would be ok).

I think a custom Rule class is a tidier solution though..

Regards,

Simon



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Digester] Initializing statics

Mattias Jiderhamn-2
At 2005-05-20 23:58, Simon Kitching wrote:

>On Fri, 2005-05-20 at 14:22 +0200, Mattias J wrote:
> > It was a while since I worked with Digester, so there might be a more
> > straightforward way to call static methods which I have forgotten, but
> > otherwise couldn't you use a factory-create-rule which could then access
> > the file name through it's parameter
> >    public Object createObject(Attributes attributes)
>
>Unfortunately, FactoryCreateRule expects an object to be returned which
>it then pushes on the stack. But in Magnus' case, there isn't any
>suitable object to return as far as I can see.

My idea was that he'd just return the string "Foo" or the filename or
something. But I forgot this was the first element in the file, which would
cause problems as to what would be returned by Digester. (One could
theoretically create a List and place the "Foo" string and what else is
created by the digester in the list, and just ignore the initial "Foo"
element, but...)

>I think a custom Rule class is a tidier solution though..

... I agree, but I couldn't remember the XML syntax for that either.
Looking through some old files I see I have once tried creating a set of
rules using org.apache.commons.digester.xmlrules.DigesterRulesSource and
the <include class="..." /> tag.

   /Mattias Jiderhamn


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]