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.NodeIterator;
036: import com.caucho.jaxb.accessor.Accessor;
037: import com.caucho.jaxb.property.Property;
038: import com.caucho.jaxb.skeleton.ClassSkeleton;
039: import com.caucho.util.L10N;
040: import com.caucho.xml.stream.StaxUtil;
041:
042: import org.w3c.dom.Node;
043:
044: import static javax.xml.XMLConstants.*;
045:
046: import javax.xml.bind.JAXBElement;
047: import javax.xml.bind.JAXBException;
048: import javax.xml.bind.Marshaller;
049: import javax.xml.bind.Unmarshaller;
050: import javax.xml.bind.UnmarshalException;
051: import javax.xml.bind.ValidationEvent;
052: import javax.xml.bind.ValidationEventHandler;
053: import javax.xml.bind.helpers.ValidationEventImpl;
054: import javax.xml.bind.helpers.ValidationEventLocatorImpl;
055:
056: import javax.xml.bind.annotation.XmlAnyAttribute;
057: import javax.xml.bind.annotation.XmlAnyElement;
058: import javax.xml.bind.annotation.XmlAttribute;
059: import javax.xml.bind.annotation.XmlElement;
060: import javax.xml.bind.annotation.XmlElementRef;
061: import javax.xml.bind.annotation.XmlElements;
062: import javax.xml.bind.annotation.XmlElementWrapper;
063: import javax.xml.bind.annotation.XmlID;
064: import javax.xml.bind.annotation.XmlIDREF;
065: import javax.xml.bind.annotation.XmlList;
066: import javax.xml.bind.annotation.XmlMimeType;
067: import javax.xml.bind.annotation.XmlNsForm;
068: import javax.xml.bind.annotation.XmlSchema;
069: import javax.xml.bind.annotation.XmlSchemaType;
070: import javax.xml.bind.annotation.XmlSchemaTypes;
071: import javax.xml.bind.annotation.XmlType;
072: import javax.xml.bind.annotation.XmlValue;
073:
074: import javax.xml.namespace.QName;
075:
076: import javax.xml.stream.XMLStreamException;
077: import javax.xml.stream.XMLStreamReader;
078: import javax.xml.stream.XMLStreamWriter;
079:
080: import java.io.IOException;
081:
082: import java.util.Collection;
083: import java.util.Map;
084:
085: public class XmlValueMapping extends SingleQNameXmlMapping {
086: private static final L10N L = new L10N(XmlValueMapping.class);
087:
088: // special constructor for JAXBElementMapping
089: protected XmlValueMapping(JAXBContextImpl context) {
090: super (context, null);
091: }
092:
093: public XmlValueMapping(JAXBContextImpl context, Accessor accessor)
094: throws JAXBException {
095: super (context, accessor);
096:
097: // XXX
098: _qname = new QName(accessor.getName());
099:
100: XmlMimeType xmlMimeType = accessor
101: .getAnnotation(XmlMimeType.class);
102: String mimeType = null;
103:
104: if (xmlMimeType != null)
105: mimeType = xmlMimeType.value();
106:
107: _property = _context.createProperty(accessor.getGenericType(),
108: false, mimeType, true, true);
109:
110: if (!_property.isXmlPrimitiveType()
111: && !Collection.class.isAssignableFrom(accessor
112: .getType()))
113: throw new JAXBException(
114: L
115: .l("XmlValue must be either a collection or a simple type"));
116: }
117:
118: public void setQName(QName qname) {
119: _qname = qname;
120: }
121:
122: public void generateSchema(XMLStreamWriter out)
123: throws JAXBException, XMLStreamException {
124: out.writeEmptyElement(XML_SCHEMA_PREFIX, "element",
125: W3C_XML_SCHEMA_NS_URI);
126:
127: XmlElementRef elementRef = _accessor
128: .getAnnotation(XmlElementRef.class);
129:
130: if (_property.getMaxOccurs() != null)
131: out.writeAttribute("maxOccurs", _property.getMaxOccurs());
132:
133: // XXX
134: out.writeAttribute("ref", "XXX");
135: }
136: }
|