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.jaxb.accessor.Accessor;
036: import com.caucho.jaxb.skeleton.ClassSkeleton;
037: import com.caucho.util.L10N;
038: import com.caucho.xml.stream.StaxUtil;
039:
040: import org.w3c.dom.Node;
041:
042: import static javax.xml.XMLConstants.*;
043:
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.bind.UnmarshalException;
049:
050: import javax.xml.bind.annotation.XmlAnyAttribute;
051:
052: import javax.xml.namespace.QName;
053:
054: import javax.xml.stream.XMLStreamException;
055: import javax.xml.stream.XMLStreamReader;
056: import javax.xml.stream.XMLStreamWriter;
057:
058: import java.io.IOException;
059:
060: import java.util.ArrayList;
061: import java.util.HashMap;
062: import java.util.Map;
063: import java.util.Set;
064:
065: public class AnyAttributeMapping extends XmlMapping {
066: private static final L10N L = new L10N(AnyAttributeMapping.class);
067:
068: public AnyAttributeMapping(JAXBContextImpl context,
069: Accessor accessor) throws JAXBException {
070: super (context, accessor);
071:
072: if (!Map.class.isAssignableFrom(accessor.getType()))
073: throw new JAXBException(
074: L
075: .l("Fields or properties annotated with @XmlAnyAttribute must be Maps"));
076: }
077:
078: // output methods
079:
080: public void write(Marshaller m, XMLStreamWriter out, Object obj)
081: throws IOException, XMLStreamException, JAXBException {
082: Map map = (Map) _accessor.get(obj);
083:
084: if (map != null) {
085: for (Map.Entry entry : (Set<Map.Entry>) map.entrySet()) {
086: QName name = (QName) entry.getKey();
087: String value = (String) entry.getValue();
088:
089: StaxUtil.writeAttribute(out, name, value);
090: }
091: }
092: }
093:
094: public void write(Marshaller m, XMLStreamWriter out, Object obj,
095: ArrayList<XmlMapping> attributes) throws IOException,
096: JAXBException, XMLStreamException {
097: throw new UnsupportedOperationException();
098: }
099:
100: // input methods
101:
102: public void readAttribute(XMLStreamReader in, int i, Object parent)
103: throws IOException, XMLStreamException, JAXBException {
104: Map map = (Map) _accessor.get(parent);
105:
106: if (map == null) {
107: map = new HashMap(); // XXX other map types
108: _accessor.set(parent, map);
109: }
110:
111: map.put(in.getAttributeName(i), in.getAttributeValue(i));
112:
113: }
114:
115: public void read(Unmarshaller u, XMLStreamReader in, Object parent,
116: ClassSkeleton attributed) throws IOException,
117: XMLStreamException, JAXBException {
118: throw new UnsupportedOperationException();
119: }
120:
121: public void read(Unmarshaller u, XMLStreamReader in, Object parent)
122: throws IOException, XMLStreamException, JAXBException {
123: throw new UnsupportedOperationException();
124: }
125:
126: public void bindFrom(BinderImpl binder, NodeIterator node,
127: Object obj) throws IOException, JAXBException {
128: throw new UnsupportedOperationException();
129: }
130:
131: public QName getQName(Object obj) throws JAXBException {
132: throw new UnsupportedOperationException();
133: }
134:
135: public void generateSchema(XMLStreamWriter out)
136: throws JAXBException, XMLStreamException {
137: out.writeStartElement(XML_SCHEMA_PREFIX, "anyAttribute",
138: W3C_XML_SCHEMA_NS_URI);
139: out.writeAttribute("namespace", "##other");
140: out.writeAttribute("processContents", "skip");
141: }
142: }
|