Generating custom classes based on attributes from XML Document

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Generating custom classes based on attributes from XML Document

Peter Uhnak
I have a XML like this 

<packagedElement xmi:type="uml:Class" xmi:id="EAID_4A" name="DCEdge" isAbstract="true">
    <!-- some stuff -->
</packagedElement>
<packagedElement xmi:type="uml:Association" xmi:id="EAID_46">
    <!-- some stuff -->
</packagedElement>

and I would like to generate UmlClass and UmlAssociation classes for this.

I could use XMLPluggableElementFactory, however that only allows me to specify the target class only on the element's name, such as

doc := (XMLDOMParser on: someXML)
nodeFactory:
(XMLPluggableElementFactory new
elementClass: GenericElement;
handleElement: 'packagedElement' withClass: UmlPackagedElement)
parseDocument.

However I need better granularity, because I would like to have a different class for 'packagedElement[xmi:type="uml:Class"]' and different class for 'packagedElement[xmi:type="uml:Association"]'.

Of course I could use double visitors (so use the Pluggable stuff to get UmlPackagedElement) and then make another pass on it to get the final classes,

however I would prefer to have to wanted class directly without the need to go through intermediate representation.

Is there a way to do this?

Thanks,
Peter
Reply | Threaded
Open this post in threaded view
|

Re: Generating custom classes based on attributes from XML Document

stepharo
We had great fun with peter because I could understand what I was looking for around my xml importer.
Peter has the same problem so it was fun that we can talk and see the light.

What we learned in the process is that the pluggable behavior is nice but limited
    - type of matching
    - you cannot match on children because you did not created them yet :)

So what we plan to do and started (peter publish your code :)
    - have a naive visitor on XMLNode
    - use it to write a transform visitor that turns such some nodes into special "domain nodes"
    - Define a builder generating visit methods for an exporterVisitor visiting the "domain nodes"
    (this phase can be really automated)
    - The exporterVisitor will let the programmer to define export rules and create Pharo Object and moose objects.

Peter if you find something useful in C# let me know.
Stef
   

Le 18/3/16 15:17, Peter Uhnák a écrit :
I have a XML like this 

<packagedElement xmi:type="uml:Class" xmi:id="EAID_4A" name="DCEdge" isAbstract="true">
    <!-- some stuff -->
</packagedElement>
<packagedElement xmi:type="uml:Association" xmi:id="EAID_46">
    <!-- some stuff -->
</packagedElement>

and I would like to generate UmlClass and UmlAssociation classes for this.

I could use XMLPluggableElementFactory, however that only allows me to specify the target class only on the element's name, such as

doc := (XMLDOMParser on: someXML)
nodeFactory:
(XMLPluggableElementFactory new
elementClass: GenericElement;
handleElement: 'packagedElement' withClass: UmlPackagedElement)
parseDocument.

However I need better granularity, because I would like to have a different class for 'packagedElement[xmi:type="uml:Class"]' and different class for 'packagedElement[xmi:type="uml:Association"]'.

Of course I could use double visitors (so use the Pluggable stuff to get UmlPackagedElement) and then make another pass on it to get the final classes,

however I would prefer to have to wanted class directly without the need to go through intermediate representation.

Is there a way to do this?

Thanks,
Peter

Reply | Threaded
Open this post in threaded view
|

Re: Generating custom classes based on attributes from XML Document

hernanmd
Hi Peter and Stef,

Sounds like you want a XML Digester? This is the one in Java: https://commons.apache.org/proper/commons-digester/
May be you want to check as reference.
Cheers,

Hernán


2016-03-19 9:14 GMT-03:00 stepharo <[hidden email]>:
We had great fun with peter because I could understand what I was looking for around my xml importer.
Peter has the same problem so it was fun that we can talk and see the light.

What we learned in the process is that the pluggable behavior is nice but limited
    - type of matching
    - you cannot match on children because you did not created them yet :)

So what we plan to do and started (peter publish your code :)
    - have a naive visitor on XMLNode
    - use it to write a transform visitor that turns such some nodes into special "domain nodes"
    - Define a builder generating visit methods for an exporterVisitor visiting the "domain nodes"
    (this phase can be really automated)
    - The exporterVisitor will let the programmer to define export rules and create Pharo Object and moose objects.

Peter if you find something useful in C# let me know.
Stef
   

Le 18/3/16 15:17, Peter Uhnák a écrit :
I have a XML like this 

<packagedElement xmi:type="uml:Class" xmi:id="EAID_4A" name="DCEdge" isAbstract="true">
    <!-- some stuff -->
</packagedElement>
<packagedElement xmi:type="uml:Association" xmi:id="EAID_46">
    <!-- some stuff -->
</packagedElement>

and I would like to generate UmlClass and UmlAssociation classes for this.

I could use XMLPluggableElementFactory, however that only allows me to specify the target class only on the element's name, such as

doc := (XMLDOMParser on: someXML)
nodeFactory:
(XMLPluggableElementFactory new
elementClass: GenericElement;
handleElement: 'packagedElement' withClass: UmlPackagedElement)
parseDocument.

However I need better granularity, because I would like to have a different class for 'packagedElement[xmi:type="uml:Class"]' and different class for 'packagedElement[xmi:type="uml:Association"]'.

Of course I could use double visitors (so use the Pluggable stuff to get UmlPackagedElement) and then make another pass on it to get the final classes,

however I would prefer to have to wanted class directly without the need to go through intermediate representation.

Is there a way to do this?

Thanks,
Peter


Reply | Threaded
Open this post in threaded view
|

Re: Generating custom classes based on attributes from XML Document

monty-3
In reply to this post by Peter Uhnak
You now can match on attributes too. The attributes: arguments can be any attribute specification object or just an ordinary dict or other association collection (nil values mean a key just must be present with any value).
 
And there is visitor pattern support so you don't have to define polymorphic extension methods on every node class or use a bunch of isWhatever testing messages when tree traversing.

> Sent: Friday, March 18, 2016 at 10:17 AM
> From: "Peter Uhnák" <[hidden email]>
> To: "Pharo Users List" <[hidden email]>
> Subject: [Pharo-users] Generating custom classes based on attributes from XML Document
>
> I have a XML like this
>  
>
> <packagedElement xmi:type="uml:Class" xmi:id="EAID_4A" name="DCEdge" isAbstract="true">
>     <!-- some stuff -->
> </packagedElement>
> <packagedElement xmi:type="uml:Association" xmi:id="EAID_46">
>     <!-- some stuff -->
> </packagedElement>
>  
> and I would like to generate UmlClass and UmlAssociation classes for this.
>  
> I could use XMLPluggableElementFactory, however that only allows me to specify the target class only on the element's name, such as
>  
>
> doc := (XMLDOMParser on: someXML)
> nodeFactory:
> (XMLPluggableElementFactory new
> elementClass: GenericElement;
> handleElement: 'packagedElement' withClass: UmlPackagedElement)
> parseDocument.
>  
> However I need better granularity, because I would like to have a different class for 'packagedElement[xmi:type="uml:Class"]' and different class for 'packagedElement[xmi:type="uml:Association"]'.
>  
> Of course I could use double visitors (so use the Pluggable stuff to get UmlPackagedElement) and then make another pass on it to get the final classes,
>  
> however I would prefer to have to wanted class directly without the need to go through intermediate representation.
>  
> Is there a way to do this?
>  
> Thanks,
> Peter
>