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 Adam Megacz
028: */
029:
030: package com.caucho.jaxb.property;
031:
032: import java.util.HashMap;
033: import java.util.Map;
034: import javax.xml.bind.JAXBException;
035: import javax.xml.bind.Marshaller;
036: import javax.xml.bind.Unmarshaller;
037: import javax.xml.namespace.QName;
038: import javax.xml.stream.XMLStreamException;
039: import javax.xml.stream.XMLStreamReader;
040: import javax.xml.stream.XMLStreamWriter;
041:
042: import java.io.IOException;
043:
044: import org.w3c.dom.Document;
045: import org.w3c.dom.Node;
046:
047: import com.caucho.jaxb.BinderImpl;
048: import com.caucho.jaxb.JAXBUtil;
049: import com.caucho.jaxb.NodeIterator;
050: import com.caucho.jaxb.mapping.Namer;
051: import com.caucho.xml.stream.StaxUtil;
052:
053: /**
054: * a Map property
055: */
056: public class MapProperty extends Property {
057: private static final QName _keyName = new QName("key");
058: private static final QName _valueName = new QName("value");
059:
060: private static final Namer _keyNamer = new Namer() {
061: public QName getQName(Object obj) {
062: return _keyName;
063: }
064: };
065:
066: private static final Namer _valueNamer = new Namer() {
067: public QName getQName(Object obj) {
068: return _valueName;
069: }
070: };
071:
072: private Class _mapType;
073: private Property _keyProperty;
074: private Property _valueProperty;
075:
076: public MapProperty(Class mapType, Property keyProperty,
077: Property valueProperty) {
078: _mapType = mapType;
079: _keyProperty = keyProperty;
080: _valueProperty = valueProperty;
081: }
082:
083: public Object read(Unmarshaller u, XMLStreamReader in,
084: Object previous) throws IOException, XMLStreamException,
085: JAXBException {
086: in.nextTag();
087:
088: Object obj = previous;
089:
090: if (obj == null) {
091: try {
092: obj = _mapType.newInstance();
093: } catch (IllegalAccessException e) {
094: throw new JAXBException(e);
095: } catch (InstantiationException e) {
096: throw new JAXBException(e);
097: }
098: }
099:
100: Map<Object, Object> map = (Map<Object, Object>) obj;
101:
102: while (in.getEventType() == in.START_ELEMENT
103: && "key".equals(in.getLocalName())) {
104: Object key = _keyProperty.read(u, in, null);
105:
106: if (in.getEventType() != in.START_ELEMENT
107: || !"value".equals(in.getLocalName()))
108: throw new JAXBException(
109: "Key without value while reading map");
110:
111: Object value = _valueProperty.read(u, in, null);
112:
113: map.put(key, value);
114: }
115:
116: in.nextTag();
117:
118: return map;
119: }
120:
121: public Object bindFrom(BinderImpl binder, NodeIterator node,
122: Object previous) throws JAXBException {
123: throw new UnsupportedOperationException();
124: }
125:
126: public void write(Marshaller m, XMLStreamWriter out, Object obj,
127: Namer namer) throws IOException, XMLStreamException,
128: JAXBException {
129: if (obj != null) {
130: Map<Object, Object> map = (Map<Object, Object>) obj;
131: QName qname = namer.getQName(obj);
132:
133: StaxUtil.writeStartElement(out, qname);
134:
135: for (Map.Entry<Object, Object> entry : map.entrySet()) {
136: _keyProperty.write(m, out, entry.getKey(), _keyNamer);
137: _valueProperty.write(m, out, entry.getValue(),
138: _valueNamer);
139: }
140:
141: StaxUtil.writeEndElement(out, qname);
142: }
143: }
144:
145: public Node bindTo(BinderImpl binder, Node node, Object obj,
146: Namer namer) throws JAXBException {
147: throw new UnsupportedOperationException();
148: }
149:
150: public void generateSchema(XMLStreamWriter out)
151: throws XMLStreamException {
152: throw new UnsupportedOperationException();
153: }
154:
155: public QName getSchemaType() {
156: throw new UnsupportedOperationException();
157: }
158:
159: public boolean isXmlPrimitiveType() {
160: return false;
161: }
162: }
|