Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 package org.apache.xmlrpc.serializer;
00020
00021 import java.util.Iterator;
00022 import java.util.Map;
00023
00024 import org.apache.xmlrpc.common.TypeFactory;
00025 import org.apache.xmlrpc.common.XmlRpcStreamConfig;
00026 import org.xml.sax.ContentHandler;
00027 import org.xml.sax.SAXException;
00028
00029
00032 public class MapSerializer extends TypeSerializerImpl {
00035 public static final String STRUCT_TAG = "struct";
00036
00039 public static final String MEMBER_TAG = "member";
00040
00043 public static final String NAME_TAG = "name";
00044
00045 private final XmlRpcStreamConfig config;
00046 private final TypeFactory typeFactory;
00047
00052 public MapSerializer(TypeFactory pTypeFactory, XmlRpcStreamConfig pConfig) {
00053 typeFactory = pTypeFactory;
00054 config = pConfig;
00055 }
00056
00057 protected void writeEntry(ContentHandler pHandler, Object pKey, Object pValue) throws SAXException {
00058 pHandler.startElement("", MEMBER_TAG, MEMBER_TAG, ZERO_ATTRIBUTES);
00059 pHandler.startElement("", NAME_TAG, NAME_TAG, ZERO_ATTRIBUTES);
00060 if (config.isEnabledForExtensions() && !(pKey instanceof String)) {
00061 writeValue(pHandler, pKey);
00062 } else {
00063 String key = pKey.toString();
00064 pHandler.characters(key.toCharArray(), 0, key.length());
00065 }
00066 pHandler.endElement("", NAME_TAG, NAME_TAG);
00067 writeValue(pHandler, pValue);
00068 pHandler.endElement("", MEMBER_TAG, MEMBER_TAG);
00069 }
00070
00071 private void writeValue(ContentHandler pHandler, Object pValue)
00072 throws SAXException {
00073 TypeSerializer ts = typeFactory.getSerializer(config, pValue);
00074 if (ts == null) {
00075 throw new SAXException("Unsupported Java type: " + pValue.getClass().getName());
00076 }
00077 ts.write(pHandler, pValue);
00078 }
00079
00080 protected void writeData(ContentHandler pHandler, Object pData) throws SAXException {
00081 Map map = (Map) pData;
00082 for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
00083 Map.Entry entry = (Map.Entry) iter.next();
00084 writeEntry(pHandler, entry.getKey(), entry.getValue());
00085 }
00086 }
00087
00088 public void write(final ContentHandler pHandler, Object pObject) throws SAXException {
00089 pHandler.startElement("", VALUE_TAG, VALUE_TAG, ZERO_ATTRIBUTES);
00090 pHandler.startElement("", STRUCT_TAG, STRUCT_TAG, ZERO_ATTRIBUTES);
00091 writeData(pHandler, pObject);
00092 pHandler.endElement("", STRUCT_TAG, STRUCT_TAG);
00093 pHandler.endElement("", VALUE_TAG, VALUE_TAG);
00094 }
00095 }