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.mapping;
031:
032: import com.caucho.jaxb.BinderImpl;
033: import com.caucho.jaxb.JAXBContextImpl;
034: import com.caucho.jaxb.JAXBUtil;
035: import com.caucho.jaxb.property.QNameProperty;
036: import com.caucho.util.L10N;
037: import com.caucho.xml.stream.StaxUtil;
038:
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Node;
041:
042: import static javax.xml.XMLConstants.*;
043: import javax.xml.bind.annotation.*;
044: import javax.xml.bind.JAXBElement;
045: import javax.xml.bind.JAXBException;
046: import javax.xml.bind.Marshaller;
047: import javax.xml.bind.Unmarshaller;
048: import javax.xml.namespace.QName;
049: import javax.xml.stream.events.*;
050: import javax.xml.stream.XMLEventReader;
051: import javax.xml.stream.XMLEventWriter;
052: import javax.xml.stream.XMLOutputFactory;
053: import javax.xml.stream.XMLStreamConstants;
054: import javax.xml.stream.XMLStreamException;
055: import javax.xml.stream.XMLStreamReader;
056: import javax.xml.stream.XMLStreamWriter;
057: import javax.xml.transform.dom.DOMResult;
058:
059: import java.io.IOException;
060:
061: import java.lang.annotation.*;
062: import java.lang.reflect.*;
063:
064: import java.util.HashMap;
065: import java.util.Map;
066: import java.util.logging.Logger;
067:
068: /**
069: *
070: **/
071: public class XmlInstanceWrapper extends XmlMapping {
072: private static final Logger log = Logger
073: .getLogger(XmlInstanceWrapper.class.getName());
074: private static final L10N L = new L10N(XmlInstanceWrapper.class);
075: private static final QName XSI_TYPE_NAME = new QName(
076: W3C_XML_SCHEMA_INSTANCE_NS_URI, "type", "xsi");
077:
078: private static final HashMap<QName, XmlInstanceWrapper> _instances = new HashMap<QName, XmlInstanceWrapper>();
079:
080: private final QName _type;
081:
082: private XmlInstanceWrapper(QName type) {
083: super ();
084:
085: _type = type;
086: }
087:
088: public static XmlInstanceWrapper getInstance(QName type) {
089: XmlInstanceWrapper instance = _instances.get(type);
090:
091: if (instance == null) {
092: instance = new XmlInstanceWrapper(type);
093: _instances.put(type, instance);
094: }
095:
096: return instance;
097: }
098:
099: public void write(Marshaller m, XMLStreamWriter out, Object obj)
100: throws IOException, XMLStreamException, JAXBException {
101: String typeString = StaxUtil.qnameToString(out, _type);
102: StaxUtil.writeAttribute(out, XSI_TYPE_NAME, typeString);
103: }
104:
105: public QName getQName(Object obj) throws JAXBException {
106: return XSI_TYPE_NAME;
107: }
108:
109: public void putQNames(Map<QName, XmlMapping> map)
110: throws JAXBException {
111: throw new UnsupportedOperationException();
112: }
113:
114: public void generateSchema(XMLStreamWriter out)
115: throws JAXBException, XMLStreamException {
116: throw new UnsupportedOperationException();
117: }
118:
119: public String toString() {
120: return "XmlInstanceWrapper[" + _type + "]";
121: }
122: }
|