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.NodeIterator;
035: import com.caucho.util.L10N;
036: import com.caucho.xml.stream.StaxUtil;
037:
038: import org.w3c.dom.Node;
039:
040: import static javax.xml.XMLConstants.*;
041:
042: import javax.xml.bind.JAXBException;
043: import javax.xml.bind.Marshaller;
044: import javax.xml.bind.Unmarshaller;
045:
046: import javax.xml.bind.annotation.XmlAttribute;
047: import javax.xml.bind.annotation.XmlID;
048: import javax.xml.bind.annotation.XmlList;
049: import javax.xml.bind.annotation.XmlMimeType;
050:
051: import javax.xml.namespace.QName;
052:
053: import javax.xml.stream.XMLStreamException;
054: import javax.xml.stream.XMLStreamReader;
055: import javax.xml.stream.XMLStreamWriter;
056:
057: import java.lang.annotation.Annotation;
058:
059: import java.io.IOException;
060:
061: import java.util.Map;
062:
063: import com.caucho.jaxb.accessor.Accessor;
064:
065: import com.caucho.jaxb.property.CDataProperty;
066: import com.caucho.jaxb.property.QNameProperty;
067:
068: import com.caucho.jaxb.skeleton.ClassSkeleton;
069:
070: public class AttributeMapping extends SingleQNameXmlMapping {
071: private static final L10N L = new L10N(AttributeMapping.class);
072:
073: public AttributeMapping(JAXBContextImpl context, Accessor accessor)
074: throws JAXBException {
075: super (context, accessor);
076:
077: XmlAttribute attribute = accessor
078: .getAnnotation(XmlAttribute.class);
079:
080: String name = accessor.getName();
081: String namespace = null;
082:
083: if (attribute != null) {
084: if (!attribute.name().equals("##default"))
085: name = attribute.name();
086:
087: if (!attribute.namespace().equals("##default"))
088: namespace = attribute.namespace();
089: }
090:
091: if (namespace == null)
092: _qname = new QName(name);
093: else
094: _qname = new QName(namespace, name);
095:
096: XmlMimeType xmlMimeType = accessor
097: .getAnnotation(XmlMimeType.class);
098: String mimeType = null;
099:
100: if (xmlMimeType != null)
101: mimeType = xmlMimeType.value();
102:
103: // XXX XmlList value?
104: _property = _context.createProperty(accessor.getGenericType(),
105: false, mimeType, true);
106:
107: if (!(_property instanceof CDataProperty)
108: && !(_property instanceof QNameProperty))
109: throw new JAXBException(L.l(
110: "Attributes must be simple XML types: {0}",
111: _property));
112: }
113:
114: public void write(Marshaller m, XMLStreamWriter out, Object obj)
115: throws IOException, XMLStreamException, JAXBException {
116: Object value = _accessor.get(obj);
117:
118: if (value != null) {
119: String output = null;
120:
121: if (_property instanceof CDataProperty)
122: output = ((CDataProperty) _property).write(value);
123:
124: else if (_property instanceof QNameProperty)
125: output = StaxUtil.qnameToString(out, (QName) value);
126:
127: else
128: throw new JAXBException(L.l(
129: "Invalid property for attribute: {0}",
130: _property));
131:
132: StaxUtil.writeAttribute(out, _qname, output);
133: }
134: }
135:
136: // input methods
137:
138: public void readAttribute(XMLStreamReader in, int i, Object parent)
139: throws IOException, XMLStreamException, JAXBException {
140: Object value = _property.readAttribute(in, i);
141: _accessor.set(parent, value);
142: }
143:
144: public void read(Unmarshaller u, XMLStreamReader in, Object parent,
145: ClassSkeleton attributed) throws IOException,
146: XMLStreamException, JAXBException {
147: throw new UnsupportedOperationException();
148: }
149:
150: public void read(Unmarshaller u, XMLStreamReader in, Object parent)
151: throws IOException, XMLStreamException, JAXBException {
152: throw new UnsupportedOperationException();
153: }
154:
155: public void bindFrom(BinderImpl binder, NodeIterator node,
156: Object obj) throws IOException, JAXBException {
157: throw new UnsupportedOperationException();
158: }
159:
160: public void generateSchema(XMLStreamWriter out)
161: throws JAXBException, XMLStreamException {
162: out.writeEmptyElement(XML_SCHEMA_PREFIX, "attribute",
163: W3C_XML_SCHEMA_NS_URI);
164:
165: // See http://forums.java.net/jive/thread.jspa?messageID=167171
166: // Primitives are always required
167:
168: XmlAttribute attribute = _accessor
169: .getAnnotation(XmlAttribute.class);
170:
171: if (attribute.required()
172: || (_generateRICompatibleSchema && _accessor.getType()
173: .isPrimitive()))
174: out.writeAttribute("use", "required");
175:
176: XmlID xmlID = _accessor.getAnnotation(XmlID.class);
177:
178: if (xmlID != null)
179: out.writeAttribute("type", "xsd:ID"); // jaxb/22d0
180: else {
181: String type = StaxUtil.qnameToString(out, _property
182: .getSchemaType());
183:
184: out.writeAttribute("type", type);
185: }
186:
187: out.writeAttribute("name", _qname.getLocalPart());
188: }
189: }
|