Hi all,

Before the actual questions, please excuse my bad english...
I have a class that can have N collections inside of it, and I want all of
them be represented the same way in my messaging model, the key to know
where to put every one of them is the attribute "name" in the XML
"collection" element tag.


As an example:
public class EmailAddress {

...
Collection<Destination> destinations;
Collection<Attachment> attachments;
...

}

<emailAdress>
     <collection name="attachments">
          <attachment>
               <file>URI</file>
          </attachment>
          <attachment>
               <file>URI</file>
          </attachment>
     </collection>
     <collection name="destinations">
          <destination>
               <name>value</name>
               <lastName>anotherValue</lastName>
          </destination>
     </collection>
</emailAdress>


I tried using the standard "collection" element mapping with
"allow-repeats=true" and it fails because the elements of the collections
are of different types, so I wrote a custom Unmarshaller that iterates over
the collection, populates the elements and set the result collection in the
object being unmarshalled by reflection.
As I debug the process it works fine (i can see that both collections are
correctly populated in the object) until the "return" statement of the
"unmarshall" method:

- If it returns null, both collections of the result object (EmailAddress)
are null.
- If it returns the collection being unmarshalled (without recreating it in
each invocation to the "unmarshall" method), both collections are populated
with all of the elements found in the XML (i mean that "attachaments" has
the elements from it and form "destinations", the same occurs with the
other collection).
- If it returns the collection being unmarshalled (recreating it in each
invocation to the "unmarshall" method), both collections are populated with
the last of the collections found in the XML (both with "destinations" in
this case).



Is there a way to change this behavior?, I mean use custom unmarshallers
but instruct Jibx not to populate the top stack object with the result of
the custom umarshaller process?.
Any suggestions on how to handle this kind of situations?.
Here is my custom unmarshaller code:

public Object unmarshal(Object object, IUnmarshallingContext ictx) throws
JiBXException {
UnmarshallingContext context = (UnmarshallingContext) ictx;
if (!context.isAt(elementNamespace, elementName))
context.throwStartTagNameError(elementNamespace, elementName);
 Collection<Object> resultCollection = (Collection<Object>) object;
/* Verify this condition based on the return of this method */
if (resultCollection == null) resultCollection = new ArrayList<Object>();

String attributeName = context.attributeText(elementNamespace, "name",
elementName);
context.parsePastStartTag(elementNamespace, elementName);
context.toTag();

/* Iterar el contexto y determinar en runtime los unmarshallers requeridos
*/
while (!context.isEnd()) {
Object colletionElement = context.unmarshalElement();
resultCollection.add(colletionElement);
context.toTag();
}

String camelCasedFieldName = attributeName.substring(0, 1).toUpperCase() +
attributeName.substring(1);
context.parsePastEndTag(elementNamespace, elementName);

try {
Object containerObject = context.getStackTop();
Method setterMethod = containerObject.getClass().getDeclaredMethod("set" +
camelCasedFieldName, List.class);
setterMethod.invoke(containerObject, resultCollection);
} catch (Exception e) {
e.printStackTrace();
}

return ???;
}

Thanks in advance.



-- 
Ju@N
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
jibx-users mailing list
jibx-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to