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: */
021: package net.sf.hajdbc.util;
022:
023: import org.jibx.runtime.IAliasable;
024: import org.jibx.runtime.IMarshaller;
025: import org.jibx.runtime.IMarshallingContext;
026: import org.jibx.runtime.IUnmarshaller;
027: import org.jibx.runtime.IUnmarshallingContext;
028: import org.jibx.runtime.JiBXException;
029: import org.jibx.runtime.impl.MarshallingContext;
030: import org.jibx.runtime.impl.UnmarshallingContext;
031:
032: /**
033: * @author Paul Ferraro
034: *
035: */
036: public abstract class AbstractMapper<T> implements IMarshaller,
037: IUnmarshaller, IAliasable {
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: this .targetClass = targetClass;
048: }
049:
050: /**
051: * Constructs a new PropertiesMapper.
052: * @param uri
053: * @param index
054: * @param name
055: */
056: protected AbstractMapper(Class<T> targetClass, String uri,
057: int index, String name) {
058: this (targetClass);
059:
060: this .uri = uri;
061: this .index = index;
062: this .name = name;
063: }
064:
065: /**
066: * @see org.jibx.runtime.IMarshaller#isExtension(int)
067: */
068: @Override
069: public boolean isExtension(int arg0) {
070: return false;
071: }
072:
073: /**
074: * @see org.jibx.runtime.IMarshaller#marshal(java.lang.Object, org.jibx.runtime.IMarshallingContext)
075: */
076: @Override
077: public void marshal(Object object, IMarshallingContext context)
078: throws JiBXException {
079: this .marshal(this .targetClass.cast(object),
080: (MarshallingContext) context);
081: }
082:
083: protected abstract void marshal(T object, MarshallingContext context)
084: throws JiBXException;
085:
086: /**
087: * @see org.jibx.runtime.IUnmarshaller#isPresent(org.jibx.runtime.IUnmarshallingContext)
088: */
089: @Override
090: public boolean isPresent(IUnmarshallingContext context)
091: throws JiBXException {
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)
100: throws JiBXException {
101: return this .unmarshal(this .targetClass.cast(object),
102: (UnmarshallingContext) context);
103: }
104:
105: protected abstract T unmarshal(T object,
106: UnmarshallingContext context) throws JiBXException;
107: }
|