commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@multitask.com.au
Subject Re: [Digester] code must be in static method?
Date Tue, 23 Dec 2003 01:26:12 GMT
Got a reason why it 'doesn't work'?

I've successfully used Digester from non static methods several times.
--
dIon Gillard, Multitask Consulting
Blog:      http://blogs.codehaus.org/people/dion/



Mike Zatko <zatko440@comcast.net> wrote on 23/12/2003 12:00:54 PM:

> Howdy,
>     I just started looking into the Digester components and I have come 
> across a peculiarity as follows. I began by using a precanned example of 

> using Digester off of an article on IBM's site I think. Basically, all 
> of the Digester code occurrs in the static main method. For my 
> particular app, I didn't want this and wanted to do my Digester code 
> inside an instantiated object. But, it doesn't work. All it was was a 
> simple copy and paste of code from a static main method to inside a 
> method of a class inside the same package as before. Why is this?
> 
> Here is the code. The first code example works. The second one doesn't.
> 
> 
> public class ConfigParser
> {
>     /**
>      * Prints the contact information to standard output.
>      *
>      * @param contact the <code>Contact</code> to print out
>      */
>     public void addDatabase(Database database)
>     {
>         System.out.println("URL: " + database.getUrl());
>         System.out.println("Driver: " + database.getDriver());
>         System.out.println("User Name: " + database.getUserName());
>         System.out.println("Password: " + database.getPassword());
>     }
> 
>     public void addTable(Table table)
>     {
>         System.out.println("Name: " + table.getName());
>         System.out.println("File: " + table.getFile());
>     }
> 
>     /**
>      * Configures Digester rules and actions, parses the XML file 
specified
>      * as the first argument.
>      *
>      * @param args command line arguments
>      */
>     public static void main(String[] args)
>     {
>         try {
>             // instantiate Digester and disable XML validation
>             Digester digester = new Digester();
>             digester.setValidating(false);
> 
>             // instantiate AddressBookParser class
>             digester.addObjectCreate("configuration", 
ConfigParser.class);
>             // instantiate Contact class
>             digester.addObjectCreate("configuration/database", 
> Database.class );
> 
>             // set type property of Contact instance when 'type' 
> attribute is found
>             //digester.addSetProperties("configuration/contact", 
> "type", "type" );
> 
>             // set different properties of Contact instance using 
> specified methods
>             digester.addCallMethod("configuration/database/url", 
> "setUrl", 0);
>             digester.addCallMethod("configuration/database/driver", 
> "setDriver", 0);
>             digester.addCallMethod("configuration/database/user-name", 
> "setUserName", 0);
>             digester.addCallMethod("configuration/database/password", 
> "setPassword", 0);
> 
>             // call 'addContact' method when the next 
> 'address-book/contact' pattern is seen
>             digester.addSetNext("configuration/database", 
"addDatabase");
> 
> 
>             digester.addObjectCreate("configuration/table", Table.class 
);
>             digester.addSetProperties("configuration/table", "name", 
> "name");
>             digester.addCallMethod("configuration/table/backup-file", 
> "setFile", 0);
>             digester.addSetNext("configuration/table", "addTable");
> 
>             // now that rules and actions are configured, start the 
> parsing process
>             ConfigParser abp = (ConfigParser)digester.parse(new 
> File("config-digest.xml"));
> 
> 
>             ConfigParser configParser = new ConfigParser();
>         }
>         catch (Exception e)
>         {
>             e.printStackTrace();
>         }
>     }
> }
> 
> 
> 
> 
> 
> 
> Second one:
> 
> public class ConfigParser
> {
>     public ConfigParser() throws IOException, SAXException
>     {
>         // instantiate Digester and disable XML validation
>         Digester digester = new Digester();
>         digester.setValidating(false);
> 
>         // instantiate AddressBookParser class
>         digester.addObjectCreate("configuration", ConfigParser.class);
>         // instantiate Contact class
>         digester.addObjectCreate("configuration/database", 
Database.class );
> 
>         // set type property of Contact instance when 'type' attribute 
> is found
>         //digester.addSetProperties("configuration/contact", 
> "type", "type" );
> 
>         // set different properties of Contact instance using specified 
> methods
>         digester.addCallMethod("configuration/database/url", "setUrl", 
0);
>         digester.addCallMethod("configuration/database/driver", 
> "setDriver", 0);
>         digester.addCallMethod("configuration/database/user-name", 
> "setUserName", 0);
>         digester.addCallMethod("configuration/database/password", 
> "setPassword", 0);
> 
>         // call 'addContact' method when the next 'address-book/contact' 

> pattern is seen
>         digester.addSetNext("configuration/database", "addDatabase");
> 
> 
>         digester.addObjectCreate("configuration/table", Table.class );
>         digester.addSetProperties("configuration/table", "name", 
"name");
>         digester.addCallMethod("configuration/table/backup-file", 
> "setFile", 0);
>         digester.addSetNext("configuration/table", "addTable");
> 
>         // now that rules and actions are configured, start the parsing 
> process
>         ConfigParser abp = (ConfigParser)digester.parse(new 
> File("config-digest.xml"));
>     }
> 
>     /**
>      * Prints the contact information to standard output.
>      *
>      * @param contact the <code>Contact</code> to print out
>      */
>     public void addDatabase(Database database)
>     {
>         System.out.println("URL: " + database.getUrl());
>         System.out.println("Driver: " + database.getDriver());
>         System.out.println("User Name: " + database.getUserName());
>         System.out.println("Password: " + database.getPassword());
>     }
> 
>     public void addTable(Table table)
>     {
>         System.out.println("Name: " + table.getName());
>         System.out.println("File: " + table.getFile());
>     }
> 
>     /**
>      * Configures Digester rules and actions, parses the XML file 
specified
>      * as the first argument.
>      *
>      * @param args command line arguments
>      */
>     public static void main(String[] args)
>     {
>         try {
>             ConfigParser configParser = new ConfigParser();
>         }
>         catch (Exception e)
>         {
>             e.printStackTrace();
>         }
>     }
> }
> 
> 
> Thanks,
> Mike Zatko
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message