Hi Vairoj,

From your original description, you should be able to just use the add-method="addEmployee" approach originally suggested by Joshua, without the need to use a post-set method (since your addEmployee method takes care of setting the association link). If you're only unmarshalling documents use the direction="input" attribute on your <binding> element so that only the add-method is needed. If you're also marshalling, you'll need to tell JiBX how to get the items for output; I don't think I've tried this, but you should be able to use field="employees" to handle the output side, while still using add-method="addEmployee" to handle the input. You may need the CVS code (or upcoming RC2) to make this work correctly, though.

 - Dennis

Vairoj A. wrote:

Hi,

Thanks. The solution works correctly and do solve the back-reference problem. However, the solution requires the domain class (in this case, Person) to have direct dependency on JiBX library, which is not allowed in our architecture. Therefore, is there any other alternatives for this problem?

Cheers,
Vairoj


--__--__--

Message: 2
Subject: RE: [jibx-users] Re: Mapping bi-directional association
Date: Fri, 4 Nov 2005 08:04:47 -0600
From: "Davies, Joshua" <[EMAIL PROTECTED]>
To: <[email protected]>
Reply-To: [email protected]

Whoops - guess I should have tried compiling that before I posted it,
eh?  I changed the HashSet to an ArrayList and made some other
modifications to your "Person" class so I could focus on the
back-reference problem; I guess I forgot to unmake them before I copied
the solution into my e-mail.

Anyway, what I was really trying to direct you to the post-set
attribute; that will solve your problem of maintaining a "back
reference" to the person's employer, which I believe was the question in
your original post.  I just ran a new compile using your original Person
class, and a (simpler) collection mapping of:

<collection name=3D"employees" field=3D"employees" usage=3D"optional" =
/>

And got your test.xml to unmarshal correctly using the unmodified
(except for the addition of the postSet handler) Person class.

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Vairoj A.
Sent: Friday, November 04, 2005 2:13 AM
To: [email protected]
Subject: [jibx-users] Re: Mapping bi-directional association

Hi,

I have tried adding 'add-method' attribute to the binding file as you=20
suggested. However, I believe that the add-method specified here is the=20 method for the collection. However, the addEmployee method resides in=20 the Person class, not in the collection - in this case, HashSet class.=20
So there is an error when running the binding compiler as follow:

Error: add-method addEmployee not found in class java.util.HashSet for=20
collection element at (line 43, col 5, in Campaign/jibx-binding.xml)

Regards,

Vairoj
Take a look at the "post-set" binding attribute
(http://jibx.sourceforge.net/tutorial/binding-extend.html):

Add this to your binding file:

<mapping name=3D3D"person" class=3D3D"com.package.Person" =3D
post-set=3D3D"postSet">
  <value name=3D3D"name" field=3D3D"name" />
  <collection name=3D3D"employees" add-method=3D3D"addEmployee"
item-type=3D3D"Person" usage=3D3D"optional" />
</mapping>

And this to Person:
public void postSet( IUnmarshallingContext ctx )
{
  if ( ctx.getStackDepth( ) > 1 )
  {
    setEmployer( ( Person ) ctx.getStackObject( 1 ) );
  }
}

The trick is to get at the unmarshalling context and go up one level

in
the stack to the parent (but make sure to check the stack depth as

shown
above and make sure that you're dealing with a collection element!).





-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server.
Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users


--__--__--

Message: 3
Date: Fri, 4 Nov 2005 15:27:55 -0700
From: "Pensock, John" <[EMAIL PROTECTED]>
To: <[email protected]>
Subject: [jibx-users] Unmarshalling with default values
Reply-To: [email protected]

This is a multi-part message in MIME format.

------_=_NextPart_001_01C5E18F.00A61BFF
Content-Type: text/plain;
    charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi all,=20

=20

The POJO that I want to unmarshal XML into contains additional fields
that I want to populate with fixed values. When I try something like:=20

=20

    <mapping name=3D"SCANDTN" ordered=3D"false" class=3D"O_x_class"
pre-set=3D"clear_All">
        <value name=3D"version" field=3D"version_num" =
style=3D"attribute"
usage=3D"optional"/>
        <value name=3D"dummy1" field=3D"eyecatcher" usage=3D"optional"
default=3D"XMLReply"/>
            ...
        <value name=3D"MSGID" field=3D"message_id" usage=3D"optional"/>
            ...
    </mapping>

=20


where the 'dummy1' field is one of these fields (and 'MSGID' is a real,
expected XML tag), the field (in this case 'eyecatcher') gets set to
null and not the default that I want of 'XMLReply'.

=20

If I change the 'ordered' field to 'true' in <mapping> then in this case
'eyecatcher' does get set to 'XMLReply' which is what I want.

=20

Is this expected behavior?  Is there a way to have an unordered set of
XML tags and unmarshall them with default values (as I have no control
over order of the XML tags the sender is creating)??

=20

Thanks for any assistance.

=20

My first use of the product and I am liking it a lot.=20

=20


------_=_NextPart_001_01C5E18F.00A61BFF
Content-Type: text/html;
    charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html xmlns:o=3D"urn:schemas-microsoft-com:office:office" =
xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns=3D"http://www.w3.org/TR/REC-html40";>

<head>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Dus-ascii">
<meta name=3DGenerator content=3D"Microsoft Word 11 (filtered medium)">
<style>
<!--
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
    {margin:0in;
    margin-bottom:.0001pt;
    font-size:12.0pt;
    font-family:"Times New Roman";}
a:link, span.MsoHyperlink
    {color:blue;
    text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
    {color:purple;
    text-decoration:underline;}
span.EmailStyle17
    {mso-style-type:personal-compose;
    font-family:Arial;
    color:windowtext;}
@page Section1
    {size:8.5in 11.0in;
    margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
    {page:Section1;}
-->
</style>

</head>

<body lang=3DEN-US link=3Dblue vlink=3Dpurple>

<div class=3DSection1>

<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>Hi all, <o:p></o:p></span></font></p>

<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'>The POJO that I want to unmarshal XML into contains
additional fields that I want to populate with fixed values. When I try
something like: <o:p></o:p></span></font></p>

<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

<p class=3DMsoNormal style=3D'text-autospace:none'><font size=3D2 =
face=3D"Courier New"><span
style=3D'font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;&nbsp; =
&lt;mapping
name=3D&quot;SCANDTN&quot; ordered=3D&quot;false&quot; =
class=3D&quot;O_x_class&quot;
pre-set=3D&quot;clear_All&quot;&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;value =
name=3D&quot;version&quot; field=3D&quot;version_num&quot;
style=3D&quot;attribute&quot; usage=3D&quot;optional&quot;/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;value =
name=3D&quot;dummy1&quot; field=3D&quot;eyecatcher&quot;
usage=3D&quot;optional&quot; default=3D&quot;XMLReply&quot;/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;value =
name=3D&quot;MSGID&quot; field=3D&quot;message_id&quot;
usage=3D&quot;optional&quot;/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
...<br>
&nbsp;&nbsp;&nbsp; &lt;/mapping&gt;<o:p></o:p></span></font></p>

<p class=3DMsoNormal style=3D'text-autospace:none'><font size=3D2 =
face=3D"Courier New"><span
style=3D'font-size:10.0pt;font-family:"Courier =
New"'><o:p>&nbsp;</o:p></span></font></p>

<p class=3DMsoNormal style=3D'text-autospace:none'><font size=3D2 =
face=3D"Courier New"><span
style=3D'font-size:10.0pt;font-family:"Courier New"'><br>
</span></font><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;font-family:
Arial'>where the &#8216;dummy1&#8217; field is one of these fields (and =
&#8216;MSGID&#8217;
is a real, expected XML tag), the field (in this case =
&#8216;eyecatcher&#8217;)
gets set to null and not the default that I want of =
&#8216;XMLReply&#8217;.<o:p></o:p></span></font></p>

<p class=3DMsoNormal style=3D'text-autospace:none'><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'><o:p>&nbsp;</o:p></span></fo=
nt></p>

<p class=3DMsoNormal style=3D'text-autospace:none'><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'>If I change the =
&#8216;ordered&#8217;
field to &#8216;true&#8217; in &lt;mapping&gt; then in this case =
&#8216;eyecatcher&#8217;
does get set to &#8216;XMLReply&#8217; which is what I =
want.<o:p></o:p></span></font></p>

<p class=3DMsoNormal style=3D'text-autospace:none'><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'><o:p>&nbsp;</o:p></span></fo=
nt></p>

<p class=3DMsoNormal style=3D'text-autospace:none'><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'>Is this expected behavior? =
&nbsp;Is there
a way to have an unordered set of XML tags and unmarshall them with =
default
values (as I have no control over order of the XML tags the sender is =
creating)??<o:p></o:p></span></font></p>

<p class=3DMsoNormal style=3D'text-autospace:none'><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'><o:p>&nbsp;</o:p></span></fo=
nt></p>

<p class=3DMsoNormal style=3D'text-autospace:none'><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'>Thanks for any =
assistance.<o:p></o:p></span></font></p>

<p class=3DMsoNormal style=3D'text-autospace:none'><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'><o:p>&nbsp;</o:p></span></fo=
nt></p>

<p class=3DMsoNormal style=3D'text-autospace:none'><font size=3D2 =
face=3DArial><span
style=3D'font-size:10.0pt;font-family:Arial'>My first use of the product =
and I am
liking it a lot. <o:p></o:p></span></font></p>

<p class=3DMsoNormal><font size=3D2 face=3DArial><span =
style=3D'font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

</div>

</body>

</html>

------_=_NextPart_001_01C5E18F.00A61BFF--


--__--__--

Message: 4
Date: Fri, 4 Nov 2005 16:18:56 -0800
From: "Leif Stainsby" <[EMAIL PROTECTED]>
To: "JiBX Users mailing list" <[email protected]>
Subject: [jibx-users] CVS + Wiki access ?
Reply-To: [email protected]

Hi,=20

I can't seem to access the JiBX CVS repository via anonymous login any
longer. It worked fine up until a few days ago.  Anyone else with
similar difficulty?

As well, I'm getting a 404 from the wiki:
http://www.sosnoski.com/jibx-wiki


...Leif


--__--__--

Message: 5
Subject: Re: [jibx-users] Namespaces and IXMLWriter
Date: Fri, 4 Nov 2005 16:46:42 -0800
From: "Leif Stainsby" <[EMAIL PROTECTED]>
To: "JiBX Users mailing list" <[email protected]>
Reply-To: [email protected]

Hi,

The getOutputStream() method only worked for writer's based on
StreamWriterBase -- this did not account for the GenericXMLWriter which,
heh, uses a Writer and thus does not output to a _stream_.  So, here's a
revised take on the idea being able to output XML content directly to a
writer.

  * Created IRawXMLWriter interface to expose direct write methods;
added it to XMLWriterNamespaceBase which also implements IXMLWriter. =20
  * added implementations to GenericXMLWriter and StreamWriterBase. =20

It works for my limited use case - my custom marshaller is able to emit
pre-serialized XML content directly into another documents stream.  It
also shields existing users of the IXMLWriter interface from anything to
do with this change.

Here's a simple example of what it would look like (assume 'xml' has
been previously populated):       =20
   IXMLWriter writer =3D ctx.getXmlWriter();
   IRawXMLWriter rawWriter =3D (IRawXMLWriter)writer;
   rawWriter.writeRawMarkup(xml); =20

Other code snippets below. It ain't pretty, but it can be very useful.
Any questions, queries, or comments?  Is this worthy of inclusion into
JiBX?

...Leif


---------[ IRawXMLWriter ]----------

public interface IRawXMLWriter
{
  /**
   * Write previously serialized XML content directly to output
   * without any escaping.  It is assumed that the character XML
   * content has already been escaped.
   *  =20
   * Output encoding may still occur, depending on the underlying=20
   * implementation (i.e. only if output is stream based).=20
   *
   * @param xml  XML content in java.lang.String form.
   * @throws IOException on error writing to document
   */
  public void writeRawMarkup(String xml) throws IOException;

  /**
   * Write a single character to the output.  Since no escaping=20
   * will be performed, this method should be used with extreme=20
   * care.
   *=20
   * @param ch  A single character to write to the XML document.
   * @throws IOException on error writing to document
   */
  public void writeRawMarkup(char ch) throws IOException;
}

---------[ StreamWriterBase methods from IRawXMLWriter ]----------

    public void writeRawMarkup(String xml) throws IOException {
      // we flush first to be sure no writes are pending from
      // this classes internal buffer...
      flush();
      m_stream.write(xml.getBytes());
    }
    public void writeRawMarkup(char ch) throws IOException {
      // we flush first to be sure no writes are pending from
      // this classes internal buffer...
      flush();
      m_stream.write(ch);
    }

---------[ GenericXMLWriter methods from IRawXMLWriter ]----------

    public void writeRawMarkup(String xml) throws IOException {
      writeMarkup(xml);
    }
    public void writeRawMarkup(char ch) throws IOException {
      writeMarkup(ch);
    }

---------[ end ]----------




--__--__--

_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users


End of jibx-users Digest







-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users



-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to