001 /*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership. The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied. See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 *
020 */
021 package org.apache.qpid.management.wsdm.muse.serializer;
022
023 import java.util.HashMap;
024 import java.util.Map;
025 import java.util.Map.Entry;
026
027 import javax.xml.XMLConstants;
028 import javax.xml.namespace.QName;
029
030 import org.apache.muse.core.serializer.Serializer;
031 import org.apache.muse.core.serializer.SerializerRegistry;
032 import org.apache.muse.util.xml.XmlUtils;
033 import org.apache.muse.ws.addressing.soap.SoapFault;
034 import org.apache.qpid.management.Names;
035 import org.w3c.dom.Element;
036
037 /**
038 * Implementation of Muse Serializer for Map type.
039 *
040 * <amspam>
041 * <entry>
042 * <key>
043 * <value>
044 * </entry>
045 * </amsasm>
046 *
047 * @author Andrea Gazzarini
048 */
049 public class MapSerializer implements Serializer
050 {
051
052 ByteArraySerializer _byteArraySerializer = new ByteArraySerializer();
053 Serializer _objectSerializer = SerializerRegistry.getInstance().getSerializer(Object.class);
054 Serializer _stringSerializer = SerializerRegistry.getInstance().getSerializer(String.class);
055
056 /**
057 * Return a map representation of the given xml element.
058 *
059 * @param xml the element to unmarshal.
060 * @throws SoapFault when the unmarshalling fails.
061 */
062 public Object fromXML(Element xml) throws SoapFault
063 {
064 Map<Object,Object> result = new HashMap<Object,Object>();
065 Element[] children = XmlUtils.getAllElements(xml);
066 Serializer objectDeserializer = SerializerRegistry.getInstance().getSerializer(Object.class);
067
068 for (Element entry : children)
069 {
070 Element[] keysAndValues = XmlUtils.getAllElements(entry);
071 Object key = null;
072 Object value = null;
073 for (Element element : keysAndValues)
074 {
075 if (Names.KEY.equals(element.getLocalName()))
076 {
077 key = _stringSerializer.fromXML(element);
078 } else if (Names.VALUE.equals(element.getLocalName()))
079 {
080 value = objectDeserializer.fromXML(element);
081 }
082 }
083 result.put(key, value);
084 }
085 return result;
086 }
087
088 /**
089 * Returns the java type associated to this class.
090 *
091 * @return the java type associated to this class.
092 */
093 public Class<?> getSerializableType()
094 {
095 return Map.class;
096 }
097
098 /**
099 * Return an xml representation of the given Map with the given name.
100 *
101 * @param object the Map to marshal.
102 * @param qname the qualified (xml) name of the object to use in xml representation.
103 * @return the xml representation of the given Map.
104 * @throws SoapFault when the marshalling fails.
105 */
106 public Element toXML(Object obj, QName qname) throws SoapFault
107 {
108
109 Map<?, ?> data = (Map<?, ?>) obj;
110
111 QName entryQName = new QName(qname.getNamespaceURI(),Names.ENTRY,qname.getPrefix());
112 QName keyQName = new QName(qname.getNamespaceURI(),Names.KEY,qname.getPrefix());
113 QName valueQName = new QName(qname.getNamespaceURI(),Names.VALUE,qname.getPrefix());
114
115 Element root = XmlUtils.createElement(qname);
116 root.setAttribute("xmlns:xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
117 for (Entry<?, ?> mapEntry: data.entrySet())
118 {
119 Element entry = XmlUtils.createElement(entryQName);
120 entry.appendChild(_stringSerializer.toXML(mapEntry.getKey(), keyQName));
121 if (mapEntry.getValue().getClass() == byte[].class) {
122 entry.appendChild(_byteArraySerializer.toXML(mapEntry.getValue(), valueQName));
123 } else {
124 entry.appendChild(_objectSerializer.toXML(mapEntry.getValue(), valueQName));
125 }
126 root.appendChild(entry);
127 }
128 return root;
129 }
130 }
|