001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.bind.v2.runtime.reflect;
027:
028: import javax.xml.bind.annotation.adapters.XmlAdapter;
029: import javax.xml.bind.JAXBException;
030:
031: import com.sun.xml.internal.bind.v2.runtime.Coordinator;
032: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
033: import com.sun.xml.internal.bind.api.AccessorException;
034:
035: import org.xml.sax.SAXException;
036:
037: /**
038: * {@link Lister} that adapts individual item types.
039: */
040: final class AdaptedLister<BeanT, PropT, InMemItemT, OnWireItemT, PackT>
041: extends Lister<BeanT, PropT, OnWireItemT, PackT> {
042: private final Lister<BeanT, PropT, InMemItemT, PackT> core;
043: private final Class<? extends XmlAdapter<OnWireItemT, InMemItemT>> adapter;
044:
045: /*package*/AdaptedLister(
046: Lister<BeanT, PropT, InMemItemT, PackT> core,
047: Class<? extends XmlAdapter<OnWireItemT, InMemItemT>> adapter) {
048:
049: this .core = core;
050: this .adapter = adapter;
051: }
052:
053: private XmlAdapter<OnWireItemT, InMemItemT> getAdapter() {
054: return Coordinator._getInstance().getAdapter(adapter);
055: }
056:
057: public ListIterator<OnWireItemT> iterator(PropT prop,
058: XMLSerializer context) {
059: return new ListIteratorImpl(core.iterator(prop, context),
060: context);
061: }
062:
063: public PackT startPacking(BeanT bean,
064: Accessor<BeanT, PropT> accessor) throws AccessorException {
065: return core.startPacking(bean, accessor);
066: }
067:
068: public void addToPack(PackT pack, OnWireItemT item)
069: throws AccessorException {
070: InMemItemT r;
071: try {
072: r = getAdapter().unmarshal(item);
073: } catch (Exception e) {
074: throw new AccessorException(e);
075: }
076: core.addToPack(pack, r);
077: }
078:
079: public void endPacking(PackT pack, BeanT bean,
080: Accessor<BeanT, PropT> accessor) throws AccessorException {
081: core.endPacking(pack, bean, accessor);
082: }
083:
084: public void reset(BeanT bean, Accessor<BeanT, PropT> accessor)
085: throws AccessorException {
086: core.reset(bean, accessor);
087: }
088:
089: private final class ListIteratorImpl implements
090: ListIterator<OnWireItemT> {
091: private final ListIterator<InMemItemT> core;
092: private final XMLSerializer serializer;
093:
094: public ListIteratorImpl(ListIterator<InMemItemT> core,
095: XMLSerializer serializer) {
096: this .core = core;
097: this .serializer = serializer;
098: }
099:
100: public boolean hasNext() {
101: return core.hasNext();
102: }
103:
104: public OnWireItemT next() throws SAXException, JAXBException {
105: InMemItemT next = core.next();
106: try {
107: return getAdapter().marshal(next);
108: } catch (Exception e) {
109: serializer.reportError(null, e);
110: return null; // recover this error by returning null
111: }
112: }
113: }
114: }
|