[xstream-user] Abstract class and field prefix problem

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[xstream-user] Abstract class and field prefix problem

Phil Hayes

Hi All,

I am having a problem reading an XML file into a class that extends an abstract class
I am trying to add a prefix to the XML element names to match the class properties, for which I use a FieldPrefixStrippingMapper, code is attached.
I find that I can only read the data into the POJO quite OK.
But when I try to extend a class, I can not seem to populate any base class properties unless I specify the XML element name exactly as the base class property name.
What do I need to do to allow the prefix to be added to the base class property name before the code is unmarshalled ?
Thank-you very much for your time and assistance and a great product.

Thanks,
Phil

=== this is the code to read the seralized file =====

XStream xs = new XStream(new Sun14ReflectionProvider()) {
            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new FieldPrefixStrippingMapper(next);
            }
};

xs.alias("Config",cmmsCheckConfig.class);
xs.alias("Table",cmmsCheckDatabase.class);
xs.alias("File",cmmsCheckFiles.class);
xs.alias("Host",cmmsCheckHost.class);

cmmsCheckConfig cc = (cmmsCheckConfig)xs.fromXML(new java.io.FileReader("alerting_config.xml"));


========= here is the the code for the FieldPrefixStrippingMapper class =================

package au.com.cmms.Util;

import com.thoughtworks.xstream.alias.ClassMapper;
import com.thoughtworks.xstream.mapper.MapperWrapper;

    /**
     * A sample mapper strips the underscore prefix of fieldnames in the XML
     */
    public class FieldPrefixStrippingMapper extends MapperWrapper {
        public FieldPrefixStrippingMapper(ClassMapper wrapped) {
            super(wrapped);
        }

        public String serializedMember(Class type, String memberName) {
            if (memberName.startsWith("its")) {
                // _blah -> blah
                memberName = memberName.substring(3); // chop off leading char (the underscore)
            }
            return super.serializedMember(type, memberName);
        }

        public String realMember(Class type, String serialized) {
            String fieldName = super.realMember(type, serialized);
            // Not very efficient or elegant, but enough to get the point across.
            // Luckily the CachingMapper will ensure this is only ever called once per field per class.
            try {
                type.getDeclaredField("its" + fieldName);
                return "its" + fieldName;
            } catch (NoSuchFieldException e) {
                return fieldName;
            }
        }

}
------------------------------------------
This e-mail is confidential.  If you are not the intended recipient, any use, disclosure or copying of this document is unauthorised and prohibited.  If you have received this document in error, please delete the email and notify me by return email or by phoning the NEMMCO Helpdesk on 1300 300 295.
Reply | Threaded
Open this post in threaded view
|

[xstream-user] Re: Abstract class and field prefix problem

Mauro Talevi
Phil Hayes wrote:

>
> Hi All,
>
> I am having a problem reading an XML file into a class that extends an
> abstract class
> I am trying to add a prefix to the XML element names to match the class
> properties, for which I use a FieldPrefixStrippingMapper, code is attached.
> I find that I can only read the data into the POJO quite OK.
> But when I try to extend a class, I can not seem to populate any base
> class properties unless I specify the XML element name exactly as the
> base class property name.
> What do I need to do to allow the prefix to be added to the base class
> property name before the code is unmarshalled ?
> Thank-you very much for your time and assistance and a great product.
>
> Thanks,
> Phil

Hi,

would it be possible for you to reproduce this problem in form of a
unit test and submit as a patch?

Submitting unit test cases saves an enormous amount of time
and typically makes it very clear what the expected and acutal results
are.  It would be really appreciated!

Cheers