001: /*
002: * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.jaxb.skeleton;
031:
032: import com.caucho.jaxb.JAXBContextImpl;
033: import com.caucho.jaxb.accessor.Accessor;
034: import com.caucho.jaxb.accessor.JAXBElementAccessor;
035: import com.caucho.jaxb.mapping.Namer;
036: import com.caucho.jaxb.mapping.XmlMapping;
037: import com.caucho.jaxb.mapping.XmlValueMapping;
038: import com.caucho.jaxb.mapping.JAXBElementMapping;
039: import com.caucho.util.L10N;
040:
041: import javax.xml.bind.annotation.*;
042: import javax.xml.bind.JAXBElement;
043: import javax.xml.bind.JAXBException;
044: import javax.xml.bind.Marshaller;
045: import javax.xml.bind.Unmarshaller;
046: import javax.xml.namespace.QName;
047: import javax.xml.stream.XMLStreamException;
048: import javax.xml.stream.XMLStreamReader;
049: import javax.xml.stream.XMLStreamWriter;
050:
051: import java.io.IOException;
052:
053: import java.lang.annotation.*;
054: import java.lang.reflect.*;
055:
056: import java.util.ArrayList;
057: import java.util.LinkedHashMap;
058: import java.util.logging.Logger;
059:
060: /**
061: *
062: * Wrapper skeleton when a JAXBElement<X> is specified but no explicit
063: * JAXBElementSkeleton<X> is registered. This class is intended to be
064: * instantiated once per JAXBContext.
065: *
066: **/
067: public class DynamicJAXBElementSkeleton extends ClassSkeleton<Object> {
068: // We must extend ClassSkeleton<Object> rather than ClassSkeleton
069: // or else javac complains about overridden methods.
070: private static final Logger log = Logger
071: .getLogger(DynamicJAXBElementSkeleton.class.getName());
072: private static final L10N L = new L10N(
073: DynamicJAXBElementSkeleton.class);
074: private static final Object[] SINGLE_NULL_ARG = new Object[] { null };
075:
076: private final JAXBElementAccessor _accessor;
077:
078: public DynamicJAXBElementSkeleton(JAXBContextImpl context)
079: throws JAXBException {
080: super (context);
081:
082: _value = new JAXBElementMapping(context);
083: _accessor = (JAXBElementAccessor) _value.getAccessor();
084: }
085:
086: public void write(Marshaller m, XMLStreamWriter out, Object obj,
087: Namer namer, ArrayList<XmlMapping> attributes)
088: throws IOException, XMLStreamException, JAXBException {
089: if (!(obj instanceof JAXBElement))
090: throw new IllegalArgumentException(L
091: .l("Object must be a JAXBElement"));
092:
093: JAXBElement element = (JAXBElement) obj;
094:
095: _accessor.setType(element.getDeclaredType());
096:
097: JAXBElementMapping mapping = (JAXBElementMapping) _value;
098: mapping.setQName(element.getName());
099: mapping.setProperty(_context.createProperty(element
100: .getDeclaredType()));
101:
102: super .write(m, out, obj, namer, attributes);
103: }
104:
105: public Object newInstance() throws JAXBException {
106: // This skeleton should not be used for reading
107: throw new IllegalStateException();
108: }
109:
110: public String toString() {
111: return "DynamicJAXBElementSkeleton[]";
112: }
113: }
|