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 | 2 | this(XSTREAM_XPP_DRIVER, null, null); |
42 | 2 | } |
43 | |
|
44 | |
public XStreamFactory(String driverClassName, Map aliases, List converters) |
45 | |
throws ClassNotFoundException, InstantiationException, IllegalAccessException |
46 | 38 | { |
47 | 38 | Class driverClass = ClassUtils.loadClass(driverClassName, this.getClass()); |
48 | 36 | xstream = new XStream((HierarchicalStreamDriver)driverClass.newInstance()); |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 36 | xstream.registerConverter(new XStreamFactory.ConcurrentHashMapConverter(xstream.getMapper()), -1); |
54 | |
|
55 | 36 | if (aliases != null) |
56 | |
{ |
57 | 0 | for (Iterator iterator = aliases.entrySet().iterator(); iterator.hasNext();) |
58 | |
{ |
59 | 0 | Map.Entry entry = (Map.Entry)iterator.next(); |
60 | 0 | Class aliasClass = ClassUtils.loadClass(entry.getValue().toString(), getClass()); |
61 | 0 | xstream.alias(entry.getKey().toString(), aliasClass); |
62 | 0 | } |
63 | |
} |
64 | |
|
65 | 36 | if (converters != null) |
66 | |
{ |
67 | 0 | for (Iterator iterator = converters.iterator(); iterator.hasNext();) |
68 | |
{ |
69 | 0 | Class converterClazz = ClassUtils.loadClass(iterator.next().toString(), getClass()); |
70 | 0 | xstream.registerConverter((Converter)converterClazz.newInstance()); |
71 | 0 | } |
72 | |
} |
73 | 36 | } |
74 | |
|
75 | |
public final XStream getInstance() |
76 | |
{ |
77 | 36 | return xstream; |
78 | |
} |
79 | |
|
80 | |
private class ConcurrentHashMapConverter extends MapConverter |
81 | |
{ |
82 | |
public ConcurrentHashMapConverter(Mapper mapper) throws ClassNotFoundException |
83 | 36 | { |
84 | 36 | super(mapper); |
85 | 36 | } |
86 | |
|
87 | |
public boolean canConvert(Class aClass) |
88 | |
{ |
89 | 98 | String className = aClass.getName(); |
90 | 98 | return className.equals("java.util.concurrent.ConcurrentHashMap") |
91 | |
|| className.equals("edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap"); |
92 | |
} |
93 | |
} |
94 | |
|
95 | |
} |