1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.apache.xmlrpc.serializer; |
17 |
|
|
18 |
|
import java.util.Iterator; |
19 |
|
import java.util.Map; |
20 |
|
|
21 |
|
import org.apache.xmlrpc.common.TypeFactory; |
22 |
|
import org.apache.xmlrpc.common.XmlRpcStreamConfig; |
23 |
|
import org.xml.sax.ContentHandler; |
24 |
|
import org.xml.sax.SAXException; |
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
public class MapSerializer extends TypeSerializerImpl { |
30 |
|
private final XmlRpcStreamConfig config; |
31 |
|
private final TypeFactory typeFactory; |
32 |
|
|
33 |
|
|
34 |
|
public static final String STRUCT_TAG = "struct"; |
35 |
|
|
36 |
|
|
37 |
|
public static final String MEMBER_TAG = "member"; |
38 |
|
|
39 |
|
|
40 |
|
public static final String NAME_TAG = "name"; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
33 |
public MapSerializer(TypeFactory pTypeFactory, XmlRpcStreamConfig pConfig) { |
46 |
33 |
typeFactory = pTypeFactory; |
47 |
33 |
config = pConfig; |
48 |
33 |
} |
49 |
|
protected void writeEntry(ContentHandler pHandler, String pKey, Object pValue) throws SAXException { |
50 |
98 |
pHandler.startElement("", MEMBER_TAG, MEMBER_TAG, ZERO_ATTRIBUTES); |
51 |
98 |
pHandler.startElement("", NAME_TAG, NAME_TAG, ZERO_ATTRIBUTES); |
52 |
98 |
pHandler.characters(pKey.toCharArray(), 0, pKey.length()); |
53 |
98 |
pHandler.endElement("", NAME_TAG, NAME_TAG); |
54 |
98 |
TypeSerializer ts = typeFactory.getSerializer(config, pValue); |
55 |
98 |
if (ts == null) { |
56 |
0 |
throw new SAXException("Unsupported Java type: " + pValue.getClass().getName()); |
57 |
|
} |
58 |
98 |
ts.write(pHandler, pValue); |
59 |
98 |
pHandler.endElement("", MEMBER_TAG, MEMBER_TAG); |
60 |
98 |
} |
61 |
|
protected void writeData(ContentHandler pHandler, Object pData) throws SAXException { |
62 |
33 |
Map map = (Map) pData; |
63 |
164 |
for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) { |
64 |
98 |
Map.Entry entry = (Map.Entry) iter.next(); |
65 |
98 |
writeEntry(pHandler, entry.getKey().toString(), entry.getValue()); |
66 |
|
} |
67 |
33 |
} |
68 |
|
public void write(final ContentHandler pHandler, Object pObject) throws SAXException { |
69 |
33 |
pHandler.startElement("", VALUE_TAG, VALUE_TAG, ZERO_ATTRIBUTES); |
70 |
33 |
pHandler.startElement("", STRUCT_TAG, STRUCT_TAG, ZERO_ATTRIBUTES); |
71 |
33 |
writeData(pHandler, pObject); |
72 |
33 |
pHandler.endElement("", STRUCT_TAG, STRUCT_TAG); |
73 |
33 |
pHandler.endElement("", VALUE_TAG, VALUE_TAG); |
74 |
33 |
} |
75 |
|
} |