001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (c) 2004-2007 Paul Ferraro
004 * 
005 * This library is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Lesser General Public License as published by the 
007 * Free Software Foundation; either version 2.1 of the License, or (at your 
008 * option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful, but WITHOUT
011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this library; if not, write to the Free Software Foundation, 
017 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018 * 
019 * Contact: ferraro@users.sourceforge.net
020 */
021package net.sf.hajdbc.util;
022
023import org.jibx.runtime.IAliasable;
024import org.jibx.runtime.IMarshaller;
025import org.jibx.runtime.IMarshallingContext;
026import org.jibx.runtime.IUnmarshaller;
027import org.jibx.runtime.IUnmarshallingContext;
028import org.jibx.runtime.JiBXException;
029import org.jibx.runtime.impl.MarshallingContext;
030import org.jibx.runtime.impl.UnmarshallingContext;
031
032/**
033 * @author Paul Ferraro
034 *
035 */
036public abstract class AbstractMapper<T> implements IMarshaller, IUnmarshaller, IAliasable
037{
038        protected String uri;
039        protected String name;
040        protected int index;
041        private Class<T> targetClass;
042        
043        /**
044         * Constructs a new PropertiesMapper.
045         */
046        protected AbstractMapper(Class<T> targetClass)
047        {
048                this.targetClass = targetClass;
049        }
050        
051        /**
052         * Constructs a new PropertiesMapper.
053         * @param uri
054         * @param index 
055         * @param name
056         */
057        protected AbstractMapper(Class<T> targetClass, String uri, int index, String name)
058        {
059                this(targetClass);
060                
061                this.uri = uri;
062                this.index = index;
063                this.name = name;
064        }
065        
066        /**
067         * @see org.jibx.runtime.IMarshaller#isExtension(java.lang.String)
068         */
069        @Override
070        public boolean isExtension(String arg0)
071        {
072                return false;
073        }
074
075        /**
076         * @see org.jibx.runtime.IMarshaller#marshal(java.lang.Object, org.jibx.runtime.IMarshallingContext)
077         */
078        @Override
079        public void marshal(Object object, IMarshallingContext context) throws JiBXException
080        {
081                this.marshal(this.targetClass.cast(object), (MarshallingContext) context);
082        }
083
084        protected abstract void marshal(T object, MarshallingContext context) throws JiBXException;
085        
086        /**
087         * @see org.jibx.runtime.IUnmarshaller#isPresent(org.jibx.runtime.IUnmarshallingContext)
088         */
089        @Override
090        public boolean isPresent(IUnmarshallingContext context) throws JiBXException
091        {
092                return context.isAt(this.uri, this.name);
093        }
094
095        /**
096         * @see org.jibx.runtime.IUnmarshaller#unmarshal(java.lang.Object, org.jibx.runtime.IUnmarshallingContext)
097         */
098        @Override
099        public Object unmarshal(Object object, IUnmarshallingContext context) throws JiBXException
100        {
101                return this.unmarshal(this.targetClass.cast(object), (UnmarshallingContext) context);
102        }
103
104        protected abstract T unmarshal(T object, UnmarshallingContext context) throws JiBXException;
105}