Re: JAKARTA COMMONS DIGESTER RULES XML PROBLEM

10 messages Options
Embed this post
Permalink
Kapil Pruthi

Re: JAKARTA COMMONS DIGESTER RULES XML PROBLEM

Reply Threaded More More options
Print post
Permalink
Hi Guys,

>
> I have gone through Jakarta digester project and found it too good but
> there is small info i need for sure to start using this.
>
> How do i create XML rules? Don't tell me manually as my data xml is huge.
> Also i want to use digester for many different cases so obviously i don't
> prefer to write digester-rules.xml for every case manually.
>
> but i want this file to be created somehow...how ???
> my data xml and java objects are identical in terms of
> xml tag name <-> Java bean  (one to one mapping)
>
> so basically i want to avoid specifying any rules (xml or programmatic) ,
> how do i do that?
> Ok lets say i am suppose to use xml rule, then how do i create this huge
> digester-rules.xml ? any tool to do that ?
>
> Any other way to do it >?
> I have data xml's and corresponding java objects (java field names word to
> word same as xml tag name)...
>
>
>
simon.kitching@chello.at

Re: JAKARTA COMMONS DIGESTER RULES XML PROBLEM

Reply Threaded More More options
Print post
Permalink
---- Kapil Pruthi <[hidden email]> schrieb:

> Hi Guys,
>
> >
> > I have gone through Jakarta digester project and found it too good but
> > there is small info i need for sure to start using this.
> >
> > How do i create XML rules? Don't tell me manually as my data xml is huge.
> > Also i want to use digester for many different cases so obviously i don't
> > prefer to write digester-rules.xml for every case manually.
> >
> > but i want this file to be created somehow...how ???
> > my data xml and java objects are identical in terms of
> > xml tag name <-> Java bean  (one to one mapping)
> >
> > so basically i want to avoid specifying any rules (xml or programmatic) ,
> > how do i do that?
> > Ok lets say i am suppose to use xml rule, then how do i create this huge
> > digester-rules.xml ? any tool to do that ?
> >
> > Any other way to do it >?
> > I have data xml's and corresponding java objects (java field names word to
> > word same as xml tag name)...

Possibly in your case you could define a FactoryCreateRule that is mapped using the pattern "*", and the corresponding ObjectFactory would just use the tagname as the name of the class to instantiate. A SetPropertiesRule mapped using the pattern "*" would also handle all the xml attributes. Whether adding child nodes to the parent can be done with a wildcard rule depends upon your format; you haven't given an example. but if it can, then that means you could configure digester with just three rules.

However digester was never really designed for processing input files which have a complete 1:1 mapping to java beans. The strongest feature of Digester is the huge flexibility it offers when mapping xml to java - which you obviously do not need. And digester is not terribly fast as it uses a lot of reflection in order to be so flexible.

As your code is so regular, it might be better to just write your own sax-handler to create and build the appropriate objects. You could possibly use the existing Digester code as inspiration, but a custom solution would likely be 10 times faster and 10 times smaller as you do not need a lot of the flexibility that Digester offers.

NB: personally I dislike the xml-rules module. Using the java API to define rules is much easier.

Regards,

Simon

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

Björn Thalheim-2

Re: JAKARTA COMMONS DIGESTER RULES XML PROBLEM

Reply Threaded More More options
Print post
Permalink
Hi,

Simon Kitching wrote:
> However digester was never really designed for processing input files
> which have a complete 1:1 mapping to java beans. The strongest
> feature of Digester is the huge flexibility it offers when mapping
> xml to java - which you obviously do not need.

Maybe Apache Commons Betwixt is what you need ... AFAIK it works quite
well especially if you use a 1:1 mapping (in any other case one has to
specify the mapping in .betwixt files).

Regards,

Björn


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

Kapil Pruthi

Re: JAKARTA COMMONS DIGESTER RULES XML PROBLEM

Reply Threaded More More options
Print post
Permalink
Can betwixt be used to transform xml to java? on overview page i read that
it can only do java to xml

On 10/31/07, Björn Thalheim <[hidden email]> wrote:

>
> Hi,
>
> Simon Kitching wrote:
> > However digester was never really designed for processing input files
> > which have a complete 1:1 mapping to java beans. The strongest
> > feature of Digester is the huge flexibility it offers when mapping
> > xml to java - which you obviously do not need.
>
> Maybe Apache Commons Betwixt is what you need ... AFAIK it works quite
> well especially if you use a 1:1 mapping (in any other case one has to
> specify the mapping in .betwixt files).
>
> Regards,
>
> Björn
>
>
Björn Thalheim-2

Re: JAKARTA COMMONS DIGESTER RULES XML PROBLEM

Reply Threaded More More options
Print post
Permalink
Hi,

Kapil Pruthi schrieb:
> Can betwixt be used to transform xml to java? on overview page i read that
> it can only do java to xml

In our Project (I personally joined in late) we do it (reading the
contents of XML files into Beans) like this:

<<<<< snip >>>>>
BeanReader reader = new BeanReader();
final IntrospectionConfiguration xmlconf =
        reader.getBindingConfiguration();
xmlconf.set...(...);
final BindingConfiguration bindconf = reader.getBindingConfiguration();
bindconf.set...(...);
reader.setNamespaceAware(true);
// this is the most important step
reader.registerBeanClass(MyClass.class)
...
// here, read in the contents of one XML file to the corresponding Bean.
MyClass mc = (MyClass)reader.parse(InputStream in);
<<<<< snap >>>>>

I hope this is what you need and you have already:
- all XML files, and
- all Bean classes.

If you don't have any Bean class which you can register, then betwixt
can't help you. First, you'd have to create the Bean class files
according to the XML files.

Let us know, if you can work with this, at least I'm curious about it;-)

By the way: betwixt internally uses commons digester.

Regards,

Björn

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

Kapil Pruthi

Re: JAKARTA COMMONS DIGESTER RULES XML PROBLEM

Reply Threaded More More options
Print post
Permalink
excellent, I will try this and let u know.

On 11/1/07, Björn Thalheim <[hidden email]> wrote:

>
> Hi,
>
> Kapil Pruthi schrieb:
> > Can betwixt be used to transform xml to java? on overview page i read
> that
> > it can only do java to xml
>
> In our Project (I personally joined in late) we do it (reading the
> contents of XML files into Beans) like this:
>
> <<<<< snip >>>>>
> BeanReader reader = new BeanReader();
> final IntrospectionConfiguration xmlconf =
>         reader.getBindingConfiguration();
> xmlconf.set...(...);
> final BindingConfiguration bindconf = reader.getBindingConfiguration();
> bindconf.set...(...);
> reader.setNamespaceAware(true);
> // this is the most important step
> reader.registerBeanClass(MyClass.class)
> ...
> // here, read in the contents of one XML file to the corresponding Bean.
> MyClass mc = (MyClass)reader.parse(InputStream in);
> <<<<< snap >>>>>
>
> I hope this is what you need and you have already:
> - all XML files, and
> - all Bean classes.
>
> If you don't have any Bean class which you can register, then betwixt
> can't help you. First, you'd have to create the Bean class files
> according to the XML files.
>
> Let us know, if you can work with this, at least I'm curious about it;-)
>
> By the way: betwixt internally uses commons digester.
>
> Regards,
>
> Björn
>
Watkin, James

Re: JAKARTA COMMONS DIGESTER RULES XML PROBLEM

Reply Threaded More More options
Print post
Permalink
In reply to this post by Kapil Pruthi
Kapil,

Not to take anything away from the excellent Apache Commons projects,
but I recently used XStream on a project with great success. It's very
easy to use for a variety of use-cases.

http://xstream.codehaus.org/

- Jim

Kapil Pruthi wrote:

> Can betwixt be used to transform xml to java? on overview page i read that
> it can only do java to xml
>
> On 10/31/07, Björn Thalheim <[hidden email]> wrote:
>> Hi,
>>
>> Simon Kitching wrote:
>>> However digester was never really designed for processing input files
>>> which have a complete 1:1 mapping to java beans. The strongest
>>> feature of Digester is the huge flexibility it offers when mapping
>>> xml to java - which you obviously do not need.
>> Maybe Apache Commons Betwixt is what you need ... AFAIK it works quite
>> well especially if you use a 1:1 mapping (in any other case one has to
>> specify the mapping in .betwixt files).
>>
>> Regards,
>>
>> Björn
>>
>>
>

--
______________________________
James Watkin
ACIS Software Development
UCLA Anderson School
[hidden email]
Voice: 1-310-825-5030
   Fax: 1-310-825-4835
______________________________

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

Kapil Pruthi

Re: JAKARTA COMMONS DIGESTER RULES XML PROBLEM

Reply Threaded More More options
Print post
Permalink
Thanks, this sounds interesting to me.

straight fromXmL and toXML functions, i will surely try this n betwixt next
week.

On 11/5/07, James Watkin <[hidden email]> wrote:

>
> Kapil,
>
> Not to take anything away from the excellent Apache Commons projects,
> but I recently used XStream on a project with great success. It's very
> easy to use for a variety of use-cases.
>
> http://xstream.codehaus.org/
>
> - Jim
>
> Kapil Pruthi wrote:
> > Can betwixt be used to transform xml to java? on overview page i read
> that
> > it can only do java to xml
> >
> > On 10/31/07, Björn Thalheim <[hidden email]> wrote:
> >> Hi,
> >>
> >> Simon Kitching wrote:
> >>> However digester was never really designed for processing input files
> >>> which have a complete 1:1 mapping to java beans. The strongest
> >>> feature of Digester is the huge flexibility it offers when mapping
> >>> xml to java - which you obviously do not need.
> >> Maybe Apache Commons Betwixt is what you need ... AFAIK it works quite
> >> well especially if you use a 1:1 mapping (in any other case one has to
> >> specify the mapping in .betwixt files).
> >>
> >> Regards,
> >>
> >> Björn
> >>
> >>
> >
>
> --
> ______________________________
> James Watkin
> ACIS Software Development
> UCLA Anderson School
> [hidden email]
> Voice: 1-310-825-5030
>    Fax: 1-310-825-4835
> ______________________________
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
twathelet

JAKARTA COMMONS DIGESTER RULES XML PROBLEM

Reply Threaded More More options
Print post
Permalink
In reply to this post by Kapil Pruthi
High I'm using DIGESTER to parse an xml doc but it seems that I don't understang somethig
My xml file:

<!-- message JMS -->
<message>
        <!-- enveloppe des informations propre  la FDR -->
        <fdr>
                <!-- Numro de FDR (ex: 650451) -->
                <number>690504</number>
                <docType>
                        <!-- Mnmonique du type de document (ex: AM, RR, ...) -->
                        <docTypeMnemo>AM</docTypeMnemo>
                </docType>
        </fdr>
        <version>
                <!-- Indique l'outil utilis pour produire le doc. -->
                <jobProductionTool>SEIAMD</jobProductionTool>
                <!-- Donne toutes les langues cibles de la traduction ou de la mise en forme -->
                <languagesCouples>
                        <languageCouple>
                                <targetLanguage>EN</targetLanguage>
                        </languageCouple>
                        <languageCouple>
                                <targetLanguage>FR</targetLanguage>
                        </languageCouple>
                        <languageCouple>
                                <targetLanguage>DE</targetLanguage>
                        </languageCouple>
                </languagesCouples>
        </version>
</message>

***************************************************************

My xml rules:

<?xml version="1.0" encoding="UTF-8"?>
<digester-rules>
        <pattern value="message">
                <object-create-rule
                        classname="europarl.trad.sild.movearch.model.FdrFile" />
                <call-method-rule pattern="fdr/number" methodname="setFdrNumber"
                        paramcount="0" />
                <call-method-rule pattern="fdr/docType/docTypeMnemo"
                        methodname="setDocType" paramcount="0" />
                <call-method-rule pattern="version/jobProductionTool"
                        methodname="setProductionTool" paramcount="0" />
                <pattern
                        value="message/version/languagesCouples/languageCouple">
                        <object-create-rule
                                classname="europarl.trad.sild.movearch.model.Lang" />
                        <call-method-rule pattern="targetLanguage"
                                methodname="setCode" paramnumber="0" />
                        <call-method-rule pattern="targetLanguage"
                                methodname="setLabel" paramnumber="0" />
                        <set-next-rule methodename="addLang" />
                </pattern>
        </pattern>
</digester-rules>

********************************************************************

My classes are:

/**
 *
 */
package europarl.trad.sild.movearch.model;

import java.util.Vector;

/**
 * @author twathelet
 */
public class FdrFile {

    private String fdrNumber;

    private String docType;

    private String productionTool;

    private Vector coupleLang;

    /**
     * @return the coupleLang
     */
    public Vector getCoupleLang() {
        return coupleLang;
    }

    /**
     * @param coupleLang
     *            the coupleLang to set
     */
    public void setCoupleLang(Vector coupleLang) {
        this.coupleLang = coupleLang;
    }

    /**
     * @return the fdrNumber
     */
    public String getFdrNumber() {
        return fdrNumber;
    }

    /**
     * @param fdrNumber
     *            the fdrNumber to set
     */
    public void setFdrNumber(String fdrNumber) {
        this.fdrNumber = fdrNumber;
    }

    /**
     * @return the docType
     */
    public String getDocType() {
        return docType;
    }

    /**
     * @param docType
     *            the docType to set
     */
    public void setDocType(String docType) {
        this.docType = docType;
    }

    /**
     * @return the productionTool
     */
    public String getProductionTool() {
        return productionTool;
    }

    /**
     * @param productionTool
     *            the productionTool to set
     */
    public void setProductionTool(String productionTool) {
        this.productionTool = productionTool;
    }

    public void addLang(Lang lang) {
        this.coupleLang.addElement(lang);
    }

    /**
     *
     */
    public FdrFile() {
        this.coupleLang = new Vector();
    }

}

****************************************************

/**
 *
 */
package europarl.trad.sild.movearch.model;

/**
 * @author twathelet
 */
public class Lang {
    private String code;

    private String label;

    /**
     * @return the code
     */
    public String getCode() {
        return code;
    }

    /**
     * @param code
     *            the code to set
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * @return the label
     */
    public String getLabel() {
        return label;
    }

    /**
     * @param label
     *            the label to set
     */
    public void setLabel(String label) {
        this.label = label;
    }

    /**
     *
     */
    public Lang() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param code
     * @param label
     */
    public Lang(String code, String label) {
        super();
        this.code = code;
        this.label = label;
    }

    /**
     * @param code
     */
    public Lang(String code) {
        super();
        this.code = code;
    }

   
}


**********************************************************

/**
 *
 */
package europarl.trad.sild.movearch;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import europarl.trad.sild.movearch.model.FdrFile;

/**
 * @author twathelet
 */
public class Executable {

    /**
     * @param args
     */
    public static void main(String[] args) {
        InputStream message = ClassLoader.getSystemClassLoader().getResourceAsStream(args[0]);
        InputSource configFile = new InputSource(ClassLoader.getSystemClassLoader().getResourceAsStream(args[1]));
        Digester d = DigesterLoader.createDigester(configFile);
        FdrFile fdrFile = new FdrFile();
        try {
            fdrFile = (FdrFile) d.parse(message);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        System.out.println(fdrFile.getFdrNumber());
        System.out.println(fdrFile.getDocType());
        System.out.println(fdrFile.getProductionTool());
        System.out.println(fdrFile.getCoupleLang().size());
    }

}

********************************************************

And when I execute the code I recive that
690504
AM
SEIAMD
0

my question his why the number of language=0???
Thanks


simon.kitching@chello.at

Re: JAKARTA COMMONS DIGESTER RULES XML PROBLEM

Reply Threaded More More options
Print post
Permalink
In reply to this post by Kapil Pruthi
Hi,

It would be appreciated if you could follow these conventions:
(1) Please do not use all-capitals for an email subject. IT IS ANNOYING AND HARD TO READ.
(2) When using the commons list, put the component name at the start of the subject line, eg
    [digester] ...  because this email list is shared by many components.
(3) Please try to choose a better subject for emails. About 50% of all emails about digester could be described by this subject (the others can be described as "rules api problem"). This subject is therefore no use to anyone searching the archives.

I think you've just got a simple typing error. There is no "paramnumber" attribute on call-method-rule. But using paramcount=0 should work (as you do earlier in the file).

This also looks like a spelling mistake (well, in English anyway..):

  <set-next-rule methodename="addLang" />

Everything else looks ok to me..

Regards,

Simon

---- twathelet <[hidden email]> schrieb:

>
> High I'm using DIGESTER to parse an xml doc but it seems that I don't
> understang somethig
> My xml file:
>
> <!-- message JMS -->
> <message>
> <!-- enveloppe des informations propre  la FDR -->
> <fdr>
> <!-- Numro de FDR (ex: 650451) -->
> <number>690504</number>
> <docType>
> <!-- Mnmonique du type de document (ex: AM, RR, ...) -->
> <docTypeMnemo>AM</docTypeMnemo>
> </docType>
> </fdr>
> <version>
> <!-- Indique l'outil utilis pour produire le doc. -->
> <jobProductionTool>SEIAMD</jobProductionTool>
> <!-- Donne toutes les langues cibles de la traduction ou de la mise en
> forme -->
> <languagesCouples>
> <languageCouple>
> <targetLanguage>EN</targetLanguage>
> </languageCouple>
> <languageCouple>
> <targetLanguage>FR</targetLanguage>
> </languageCouple>
> <languageCouple>
> <targetLanguage>DE</targetLanguage>
> </languageCouple>
> </languagesCouples>
> </version>
> </message>
>
> ***************************************************************
>
> My xml rules:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <digester-rules>
> <pattern value="message">
> <object-create-rule
> classname="europarl.trad.sild.movearch.model.FdrFile" />
> <call-method-rule pattern="fdr/number" methodname="setFdrNumber"
> paramcount="0" />
> <call-method-rule pattern="fdr/docType/docTypeMnemo"
> methodname="setDocType" paramcount="0" />
> <call-method-rule pattern="version/jobProductionTool"
> methodname="setProductionTool" paramcount="0" />
> <pattern
> value="message/version/languagesCouples/languageCouple">
> <object-create-rule
> classname="europarl.trad.sild.movearch.model.Lang" />
> <call-method-rule pattern="targetLanguage"
> methodname="setCode" paramnumber="0" />
> <call-method-rule pattern="targetLanguage"
> methodname="setLabel" paramnumber="0" />
> <set-next-rule methodename="addLang" />
> </pattern>
> </pattern>
> </digester-rules>
>
> ********************************************************************
>
> My classes are:
>
> /**
>  *
>  */
> package europarl.trad.sild.movearch.model;
>
> import java.util.Vector;
>
> /**
>  * @author twathelet
>  */
> public class FdrFile {
>
>     private String fdrNumber;
>
>     private String docType;
>
>     private String productionTool;
>
>     private Vector coupleLang;
>
>     /**
>      * @return the coupleLang
>      */
>     public Vector getCoupleLang() {
>         return coupleLang;
>     }
>
>     /**
>      * @param coupleLang
>      *            the coupleLang to set
>      */
>     public void setCoupleLang(Vector coupleLang) {
>         this.coupleLang = coupleLang;
>     }
>
>     /**
>      * @return the fdrNumber
>      */
>     public String getFdrNumber() {
>         return fdrNumber;
>     }
>
>     /**
>      * @param fdrNumber
>      *            the fdrNumber to set
>      */
>     public void setFdrNumber(String fdrNumber) {
>         this.fdrNumber = fdrNumber;
>     }
>
>     /**
>      * @return the docType
>      */
>     public String getDocType() {
>         return docType;
>     }
>
>     /**
>      * @param docType
>      *            the docType to set
>      */
>     public void setDocType(String docType) {
>         this.docType = docType;
>     }
>
>     /**
>      * @return the productionTool
>      */
>     public String getProductionTool() {
>         return productionTool;
>     }
>
>     /**
>      * @param productionTool
>      *            the productionTool to set
>      */
>     public void setProductionTool(String productionTool) {
>         this.productionTool = productionTool;
>     }
>
>     public void addLang(Lang lang) {
>         this.coupleLang.addElement(lang);
>     }
>
>     /**
>      *
>      */
>     public FdrFile() {
>         this.coupleLang = new Vector();
>     }
>
> }
>
> ****************************************************
>
> /**
>  *
>  */
> package europarl.trad.sild.movearch.model;
>
> /**
>  * @author twathelet
>  */
> public class Lang {
>     private String code;
>
>     private String label;
>
>     /**
>      * @return the code
>      */
>     public String getCode() {
>         return code;
>     }
>
>     /**
>      * @param code
>      *            the code to set
>      */
>     public void setCode(String code) {
>         this.code = code;
>     }
>
>     /**
>      * @return the label
>      */
>     public String getLabel() {
>         return label;
>     }
>
>     /**
>      * @param label
>      *            the label to set
>      */
>     public void setLabel(String label) {
>         this.label = label;
>     }
>
>     /**
>      *
>      */
>     public Lang() {
>         // TODO Auto-generated constructor stub
>     }
>
>     /**
>      * @param code
>      * @param label
>      */
>     public Lang(String code, String label) {
>         super();
>         this.code = code;
>         this.label = label;
>     }
>
>     /**
>      * @param code
>      */
>     public Lang(String code) {
>         super();
>         this.code = code;
>     }
>
>    
> }
>
>
> **********************************************************
>
> /**
>  *
>  */
> package europarl.trad.sild.movearch;
>
> import java.io.IOException;
> import java.io.InputStream;
>
> import org.apache.commons.digester.Digester;
> import org.apache.commons.digester.xmlrules.DigesterLoader;
> import org.xml.sax.InputSource;
> import org.xml.sax.SAXException;
>
> import europarl.trad.sild.movearch.model.FdrFile;
>
> /**
>  * @author twathelet
>  */
> public class Executable {
>
>     /**
>      * @param args
>      */
>     public static void main(String[] args) {
>         InputStream message =
> ClassLoader.getSystemClassLoader().getResourceAsStream(args[0]);
>         InputSource configFile = new
> InputSource(ClassLoader.getSystemClassLoader().getResourceAsStream(args[1]));
>         Digester d = DigesterLoader.createDigester(configFile);
>         FdrFile fdrFile = new FdrFile();
>         try {
>             fdrFile = (FdrFile) d.parse(message);
>         } catch (IOException e) {
>             e.printStackTrace();
>         } catch (SAXException e) {
>             e.printStackTrace();
>         }
>         System.out.println(fdrFile.getFdrNumber());
>         System.out.println(fdrFile.getDocType());
>         System.out.println(fdrFile.getProductionTool());
>         System.out.println(fdrFile.getCoupleLang().size());
>     }
>
> }
>
> ********************************************************
>
> And when I execute the code I recive that
> 690504
> AM
> SEIAMD
> 0
>
> my question his why the number of language=0???
> Thanks
>
>
>
> --
> View this message in context: http://www.nabble.com/Re%3A-JAKARTA-COMMONS-DIGESTER-RULES-XML-PROBLEM-tp13500277p14251258.html
> Sent from the Commons - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> 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]