1
2
3
4
5
6
7
8
9
10
11 package org.mule.transformers.xml;
12
13 import org.mule.util.ClassUtils;
14
15 import com.thoughtworks.xstream.XStream;
16 import com.thoughtworks.xstream.converters.Converter;
17 import com.thoughtworks.xstream.converters.collections.MapConverter;
18 import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
19 import com.thoughtworks.xstream.mapper.Mapper;
20
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24
25
26
27
28
29 public class XStreamFactory
30 {
31 public static final String XSTREAM_DOM_DRIVER = "com.thoughtworks.xstream.io.xml.DomDriver";
32 public static final String XSTREAM_DOM4J_DRIVER = "com.thoughtworks.xstream.io.xml.Dom4JDriver";
33 public static final String XSTREAM_JDOM_DRIVER = "com.thoughtworks.xstream.io.xml.JDomDriver";
34 public static final String XSTREAM_STAX_DRIVER = "com.thoughtworks.xstream.io.xml.StaxDriver";
35 public static final String XSTREAM_XPP_DRIVER = "com.thoughtworks.xstream.io.xml.XppDriver";
36
37 private final XStream xstream;
38
39 public XStreamFactory() throws ClassNotFoundException, InstantiationException, IllegalAccessException
40 {
41 this(XSTREAM_XPP_DRIVER, null, null);
42 }
43
44 public XStreamFactory(String driverClassName, Map aliases, List converters)
45 throws ClassNotFoundException, InstantiationException, IllegalAccessException
46 {
47 Class driverClass = ClassUtils.loadClass(driverClassName, this.getClass());
48 xstream = new XStream((HierarchicalStreamDriver)driverClass.newInstance());
49
50
51
52
53 xstream.registerConverter(new XStreamFactory.ConcurrentHashMapConverter(xstream.getMapper()), -1);
54
55 if (aliases != null)
56 {
57 for (Iterator iterator = aliases.entrySet().iterator(); iterator.hasNext();)
58 {
59 Map.Entry entry = (Map.Entry)iterator.next();
60 Class aliasClass = ClassUtils.loadClass(entry.getValue().toString(), getClass());
61 xstream.alias(entry.getKey().toString(), aliasClass);
62 }
63 }
64
65 if (converters != null)
66 {
67 for (Iterator iterator = converters.iterator(); iterator.hasNext();)
68 {
69 Class converterClazz = ClassUtils.loadClass(iterator.next().toString(), getClass());
70 xstream.registerConverter((Converter)converterClazz.newInstance());
71 }
72 }
73 }
74
75 public final XStream getInstance()
76 {
77 return xstream;
78 }
79
80 private class ConcurrentHashMapConverter extends MapConverter
81 {
82 public ConcurrentHashMapConverter(Mapper mapper) throws ClassNotFoundException
83 {
84 super(mapper);
85 }
86
87 public boolean canConvert(Class aClass)
88 {
89 String className = aClass.getName();
90 return className.equals("java.util.concurrent.ConcurrentHashMap")
91 || className.equals("edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap");
92 }
93 }
94
95 }