Hi Werner,
Sorry my description wasn't the best, I'll try to explain the problem first
then my solution more clearly.
First off, the xml to be Unmarshalled (See the SOURCE XML FILE code listing
from previous post below).
This xml is what is returned when I execute a request for content, I want to
bind this xml to some Java Objects. I have 3 classes that represent this
xml, GetRecordsResponse, SearchResult and ExtrinsicObject, the
ExtrinsicObject is the most relevant as this is the information I care
about, SearchResult just contains zero or more ExtrinsicObject instances and
GetRecordsResponse contains one SearchResult instance.
I am only interested in a sub-set of the attributes and elements contained
within the xml, therefore I set the Unmarshaller object to ignore extra
elements and attributes. Now I thought this would ensure that any attribute
or element I was not interested in would be ignored but this is not the
case. If I did not bind the attributes to some Java Object attribute, then
there were inconsistent results produced when binding occurred. For
instance, I was getting the first attribute of an xml element bound fine but
the other attributes of the element I was interested in where not being
bound. As an example,
<rim:ExtrinsicObject id="urn:uuid:64c67376-5814-49dd-b73c-1f046536795b"
home="http://motiive.edina.ac.uk:50080/cwwrs/cwwrs.cgi"
objectType="Document"
status="Submitted"
majorVersion="1"
minorVersion="0"
mimeType="text/xml"
isOpaque="false">
In the above xml, I am only interested in attributes, id and objectType.
The binding of the 2 attributes I care about didn't work, I got the id fine
but 'objectType' was not bound to the attribute of my Java Class. I think
the setIgnoreExtraAttributes was NOT handled correctly (where this is not
being handled correctly though I do not know and spent little time
investigating, it could be my code, or my lack of a castor.properties file
or to do with the castor codebase itself).
As a quick fix, I bind every attribute of the element and this worked first
time with the exact same mapping file plus the extra field bindings for the
extra attributes I am handling.
Hope this makes things a little clearer. I have also uploaded a zipped file
with the solution I used, the code listings I supplied before show the
incorrect functioning way and the zipped file shows the correct functioning
way.
MARK
CIAO http://www.nabble.com/file/p11286400/castor.tgz castor.tgz
Werner Guttmann wrote:
>
> Sorry, somehow I find it hard to follow your explanation(s). It looks
> like you have managed to find a solution yourself, but I don't really
> (fully) understand what you are trying to describe as the cause.
>
> Can you please try it again ?
>
> Thanks
> Werner
>
> Mark Small wrote:
>> Hi there,
>>
>> I've figured this out for myself, in case anyone else has the same
>> problem,
>> I give my fix here.
>>
>> For some inexplicable reason, requesting only some of the attributes for
>> an
>> element to be bound causes inconsistent binding i.e.
>> <element id="one" name="element" size="2">
>> ....
>> </element>
>>
>> Requesting only id and name be bound causes one or more to NOT be bound
>> to
>> the corresponding attribute in the Java class. If you change this to
>> bind
>> all (add fields to mapping file and attributes to Java Class), then the
>> same
>> mapping will work fine, this is even though I'd set ignoring attributes
>> to
>> true for hte unmarshaller i.e.
>>
>> unmarshaller.setIgnoreExtraAttributes(true);
>>
>> There may be a better way and if anyone knows of one then please do post
>> a
>> reply.
>>
>> MARK
>>
>> CIAO
>>
>>
>>
>> Mark Small wrote:
>>> Hi there,
>>>
>>> I am having a problem in regards to the binding of a number of values.
>>> I
>>> have a rather complex bit of xml (to me it is anyway), upon
>>> Unmarshalling,
>>> I get no errors but some values are not being bound while others are.
>>>
>>> Output of toString method (after Unmarshalling)
>>> =====================================
>>> Class Name: castor.mapping.namespace.csw.GetRecordsResponse
>>> Attributes:
>>> result: Class Name: castor.mapping.namespace.csw.SearchResult
>>> Attributes:
>>> numberOfRecordsMatched: 0
>>> numberOfRecordsReturned: 0
>>> extrinsic: Class Name:
>>> castor.mapping.namespace.csw.ExtrinsicObjectData
>>> Attributes:
>>> id:
>>> urn:uuid:64c67376-5814-49dd-b73c-1f046536795b
>>> objectType:
>>>
>>> From the output above it can be seen that I have a number of objects
>>> within objects. The GetRecordsResponse class has one attribute
>>> (SearchResult), which itself contains 2 int attributes and an
>>> ExtrinsicObjectData attribute, which is itself a class which contains 2
>>> String attributes. The xml I am trying to Unmarshall (See below for al
>>> code listings), contains a GetRecordsResponse element which contains the
>>> SearchResult element which contains numberOfRecordsMatched,
>>> numberOfRecordsReturned as attribute nodes and ExtrinsicObject as a
>>> child
>>> element. The ExtrinsicObject element contains a number of attributes
>>> and
>>> child elements, most of which I don't care about at the moment.
>>>
>>> At the moment I only care about the 2 int number of records attributes
>>> in
>>> the SearchResult element and the id and objectType attributes of the
>>> ExtrinsicObject element.
>>>
>>> the problem is that the binding appears to be occurring without any
>>> errors
>>> yet still is unable to bind the correct values to the associated fields
>>> in
>>> my Java Classes. The only value that is correctly bound is the id
>>> value,
>>> the zero values for the numberOfRecords... fields are set upon
>>> initialization of the SearchResult object in Java and not bound to any
>>> value in the xml, this is the same for the objectType field in the
>>> ExtrinsicObject object.
>>>
>>> Any help is gratefully appreciated, cheers.
>>>
>>> CODE LISTINGS
>>> ==============
>>>
>>> JAVA CLASSES
>>> =============
>>>
>>> public class GetRecordsResponse {
>>>
>>> private SearchResult result = new SearchResult();
>>>
>>> public SearchResult getResult() {
>>> return this.result;
>>> }
>>>
>>> public void setResult(SearchResult result) {
>>> this.result = result;
>>> }
>>>
>>> public String toString() {
>>> StringBuilder msg = new StringBuilder();
>>> msg.append("\nClass Name: " + getClass().getName() + "\n");
>>> msg.append("\tAttributes: \n");
>>> msg.append("\t\tresult: " + getResult().toString() + "\n");
>>>
>>> return msg.toString();
>>> }
>>> } // End of class GetRecordsResponse.
>>>
>>>
>>> public class SearchResult {
>>>
>>> private int numberOfRecordsMatched = 0;
>>> private int numberOfRecordsReturned = 0;
>>> private ExtrinsicObjectData extrinsic = new ExtrinsicObjectData();
>>>
>>> public ExtrinsicObjectData getExtrinsic() {
>>> return this.extrinsic;
>>> }
>>>
>>> public void setExtrinsic(ExtrinsicObjectData extrinsic) {
>>> this.extrinsic = extrinsic;
>>> }
>>>
>>> public int getNumberOfRecordsMatched() {
>>> return this.numberOfRecordsMatched;
>>> }
>>>
>>> public void setNumberOfRecordsMatched(int numberOfRecordsMatched) {
>>> this.numberOfRecordsMatched = numberOfRecordsMatched;
>>> }
>>>
>>> public int getNumberOfRecordsReturned() {
>>> return this.numberOfRecordsReturned;
>>> }
>>>
>>> public void setNumberOfRecordsReturned(int numberOfRecordsReturned) {
>>> this.numberOfRecordsReturned = numberOfRecordsReturned;
>>> }
>>>
>>> public String toString() {
>>> StringBuilder msg = new StringBuilder();
>>> msg.append("Class Name: " + getClass().getName() + "\n");
>>> msg.append("\t\tAttributes: \n");
>>> msg.append("\t\t\tnumberOfRecordsMatched: " +
>>> getNumberOfRecordsMatched() + "\n");
>>> msg.append("\t\t\tnumberOfRecordsReturned: " +
>>> getNumberOfRecordsReturned() + "\n");
>>> msg.append("\t\t\textrinsic: " + getExtrinsic().toString() + "\n");
>>>
>>> return msg.toString();
>>> }
>>> } // End of class SearchResult.
>>>
>>> public class ExtrinsicObjectData {
>>>
>>> private String id = "";
>>> private String objectType = "";
>>>
>>> public ExtrinsicObjectData() {}
>>>
>>> public ExtrinsicObjectData(String id, String objectType) {
>>> setId(id);
>>> setObjectType(objectType);
>>> }
>>>
>>> public String getId() {
>>> return this.id;
>>> }
>>>
>>> public void setId(String id) {
>>> this.id = id;
>>> }
>>>
>>> public String getObjectType() {
>>> return this.objectType;
>>> }
>>>
>>> public void setObjectType(String objectType) {
>>> this.objectType = objectType;
>>> }
>>>
>>> public String toString() {
>>> StringBuilder msg = new StringBuilder();
>>> msg.append("Class Name: " + getClass().getName() + "\n");
>>> msg.append("Attributes: \n");
>>> msg.append("\t\t\t\tid: " + getId() + "\n");
>>> msg.append("\t\t\t\tobjectType: " + getObjectType() + "\n");
>>>
>>> return msg.toString();
>>> }
>>> } // End of class ExtrinsicObject.
>>>
>>> JUnit Test
>>> ========
>>> import java.io.FileReader;
>>> import java.io.IOException;
>>>
>>> import org.apache.commons.logging.Log;
>>> import org.apache.commons.logging.LogFactory;
>>> import org.exolab.castor.mapping.Mapping;
>>> import org.exolab.castor.mapping.MappingException;
>>> import org.exolab.castor.xml.MarshalException;
>>> import org.exolab.castor.xml.Unmarshaller;
>>> import org.exolab.castor.xml.ValidationException;
>>> import org.junit.Test;
>>>
>>> public class GetRecordsResponseTest {
>>>
>>> private static final Log log =
>>> LogFactory.getLog(GetRecordsResponseTest.class);
>>>
>>> @Test
>>> public void testMapping() {
>>> String filename =
>>> "src/test/resources/GetRecords-extrinsic-response.xml";
>>> String mapfile =
>>> "src/test/resources/GetRecords-extrinsic-response-mapping.xml";
>>>
>>> try {
>>> Mapping mapping = new Mapping();
>>> mapping.loadMapping(mapfile);
>>>
>>> Unmarshaller unmarshaller = new Unmarshaller(mapping);
>>> unmarshaller.setIgnoreExtraElements(true);
>>> unmarshaller.setIgnoreExtraAttributes(true);
>>>
>>> GetRecordsResponse recordsResponse =
>>> (GetRecordsResponse)unmarshaller.unmarshal(new
>>> FileReader(filename));
>>>
>>> log.debug(recordsResponse.toString());
>>> } catch(IOException ex) {
>>> log.error("ERROR: Cannot read file: " + filename + ex);
>>> } catch(MappingException ex) {
>>> log.error("ERROR: Cannot load mapping file: " + filename + ex);
>>> } catch(MarshalException ex) {
>>> log.error("ERROR: Cannot Unmarshall file: " + filename + ex);
>>> } catch(ValidationException ex) {
>>> log.error("ERROR: Cannot validate file: " + filename + ex);
>>> }
>>> }
>>> } // End of class GetRecordsResponseTest.
>>>
>>> MAPPING FILE
>>> ============
>>>
>>> <?xml version="1.0" encoding="UTF-8"?>
>>>
>>> <mapping xmlns="http://castor.exolab.org/"
>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>> xmlns:csw="http://www.opengis.net/cat/csw"
>>> xmlns:rim="urn:oasis:names:tc:ebxml-regrep:rim:xsd:2.5"
>>> xsi:schemaLocation="http://castor.exolab.org/mapping.xsd"
>>>
>>> csw:schemaLocation="http://schemas.cubewerx.com/schemas/csw/2.0.1/CSW-discovery.xsd"
>>>
>>> rim:schemaLocation="http://schemas.cubewerx.com/schemas/ebrim/2.5/schema/rim.xsd">
>>>
>>> <class name="castor.mapping.namespace.csw.GetRecordsResponse">
>>> <map-to xml="GetRecordsResponse" />
>>> <field name="result"
>>> type="castor.mapping.namespace.csw.SearchResult"
>>> direct="false">
>>> <bind-xml name="csw:SearchResults" />
>>> </field>
>>> </class>
>>>
>>> <class name="castor.mapping.namespace.csw.SearchResult">
>>> <map-to xml="SearchResults" />
>>> <field name="numberOfRecordsMatched"
>>> type="integer"
>>> direct="false">
>>> <bind-xml name="numberOfRecordsMatched" node="attribute" />
>>> </field>
>>>
>>> <field name="numberOfRecordsReturned"
>>> type="integer"
>>> direct="false">
>>> <bind-xml name="numberOfRecordsReturned" node="attribute" />
>>> </field>
>>>
>>> <field name="extrinsic"
>>> type="castor.mapping.namespace.csw.ExtrinsicObjectData"
>>> direct="false">
>>> <bind-xml name="rim:ExtrinsicObject" />
>>> </field>
>>> </class>
>>>
>>> <class name="castor.mapping.namespace.csw.ExtrinsicObjectData">
>>> <field name="id" type="string" direct="false">
>>> <bind-xml name="id" node="attribute" />
>>> </field>
>>>
>>> <field name="objectType" type="string" direct="false">
>>> <bind-xml name="objectType" node="attribute" />
>>> </field>
>>> </class>
>>> </mapping>
>>>
>>> SOURCE XML FILE
>>> ================
>>> <csw:GetRecordsResponse
>>> xsi:schemaLocation="http://www.opengis.net/cat/csw
>>>
>>> http://schemas.cubewerx.com/schemas/csw/2.0.1/CSW-discovery.xsd
>>>
>>> urn:oasis:names:tc:ebxml-regrep:rim:xsd:2.5
>>>
>>> http://schemas.cubewerx.com/schemas/ebrim/2.5/schema/rim.xsd
>>> http://www.opengis.net/gml
>>>
>>> http://schemas.cubewerx.com/schemas/gml/3.1.1/base/geometryAggregates.xsd">
>>> <csw:SearchStatus status="complete" timestamp="2007-05-29T09:34:37" />
>>>
>>> <csw:SearchResults
>>> recordSchema="urn:oasis:names:tc:ebxml-regrep:rim:xsd:2.5"
>>> numberOfRecordsMatched="1"
>>> numberOfRecordsReturned="1">
>>> <rim:ExtrinsicObject
>>> id="urn:uuid:64c67376-5814-49dd-b73c-1f046536795b"
>>>
>>> home="http://motiive.edina.ac.uk:50080/cwwrs/cwwrs.cgi"
>>> objectType="Document"
>>> status="Submitted"
>>> majorVersion="1"
>>> minorVersion="0"
>>> mimeType="text/xml"
>>> isOpaque="false">
>>> <rim:Name>
>>> <rim:LocalizedString rim:lang="en-US"
>>> charset="UTF-8"
>>> value="Usage note for WMS cookbook" />
>>> </rim:Name>
>>>
>>> <rim:Description>
>>> <rim:LocalizedString rim:lang="en-US"
>>> charset="UTF-8"
>>> value="notes that relate to WMS" />
>>> </rim:Description>
>>>
>>> <rim:Slot name="RepositoryItem" slotType="string">
>>> <rim:ValueList>
>>> <rim:Value>TRUE</rim:Value>
>>> </rim:ValueList>
>>> </rim:Slot>
>>>
>>> <rim:Slot name="externalIdentifier" slotType="string">
>>> <rim:ValueList>
>>> <rim:Value>
>>> http://motiive.edina.ac.uk:50080/harvest/docs/UsageNote.xml
>>> </rim:Value>
>>> </rim:ValueList>
>>> </rim:Slot>
>>>
>>> <rim:Slot name="externalURI" slotType="string">
>>> <rim:ValueList>
>>> <rim:Value>
>>> http://motiive.edina.ac.uk:50080/harvest/docs/UsageNote.xml
>>> </rim:Value>
>>> </rim:ValueList>
>>> </rim:Slot>
>>>
>>> <rim:Classification
>>> id="urn:uuid:444c959c-ea0c-4853-ba92-323b883da7ae"
>>> home="http://demo.cubewerx.com/noo/cwwrs.cgi"
>>> status="Submitted"
>>>
>>> classificationScheme="urn:esdi:marine:members-taxonomy"
>>>
>>> classifiedObject="urn:uuid:64c67376-5814-49dd-b73c-1f046536795b"
>>>
>>> classificationNode="urn:esdi:marine:members-taxonomy:EDINA" />
>>>
>>> <rim:Classification
>>> id="urn:uuid:f3ed73c9-c530-49d1-9a14-a7fbd97ca310"
>>> home="http://demo.cubewerx.com/noo/cwwrs.cgi"
>>> status="Submitted"
>>>
>>> classificationScheme="urn:esdi:marine:content:type"
>>>
>>> classifiedObject="urn:uuid:64c67376-5814-49dd-b73c-1f046536795b"
>>>
>>> classificationNode="urn:esdi:marine:content:type:document" />
>>> </rim:ExtrinsicObject>
>>> </csw:SearchResults>
>>> </csw:GetRecordsResponse>
>>>
>>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
> http://xircles.codehaus.org/manage_email
>
>
>
--
View this message in context:
http://www.nabble.com/Values-not-being-bound-tf3951323.html#a11286400
Sent from the Castor - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email