1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.xml.transformer; |
8 | |
|
9 | |
import org.mule.util.ClassUtils; |
10 | |
|
11 | |
import com.thoughtworks.xstream.XStream; |
12 | |
import com.thoughtworks.xstream.converters.Converter; |
13 | |
import com.thoughtworks.xstream.converters.SingleValueConverter; |
14 | |
import com.thoughtworks.xstream.converters.collections.MapConverter; |
15 | |
import com.thoughtworks.xstream.io.HierarchicalStreamDriver; |
16 | |
import com.thoughtworks.xstream.mapper.Mapper; |
17 | |
|
18 | |
import java.util.Map; |
19 | |
import java.util.Set; |
20 | |
|
21 | |
import org.apache.commons.logging.Log; |
22 | |
import org.apache.commons.logging.LogFactory; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public class XStreamFactory |
29 | |
{ |
30 | |
public static final String XSTREAM_DOM_DRIVER = "com.thoughtworks.xstream.io.xml.DomDriver"; |
31 | |
public static final String XSTREAM_DOM4J_DRIVER = "com.thoughtworks.xstream.io.xml.Dom4JDriver"; |
32 | |
public static final String XSTREAM_JDOM_DRIVER = "com.thoughtworks.xstream.io.xml.JDomDriver"; |
33 | |
public static final String XSTREAM_STAX_DRIVER = "com.thoughtworks.xstream.io.xml.StaxDriver"; |
34 | |
public static final String XSTREAM_XPP_DRIVER = "com.thoughtworks.xstream.io.xml.XppDriver"; |
35 | |
|
36 | 0 | private static final Log logger = LogFactory.getLog(XStreamFactory.class); |
37 | |
|
38 | |
private final XStream xstream; |
39 | |
|
40 | |
public XStreamFactory() throws ClassNotFoundException, InstantiationException, IllegalAccessException |
41 | |
{ |
42 | 0 | this(XSTREAM_XPP_DRIVER, null, null); |
43 | 0 | } |
44 | |
|
45 | |
public XStreamFactory(String driverClassName, Map<String, Class<?>> aliases, Set<Class <? extends Converter>> converters) |
46 | |
throws ClassNotFoundException, InstantiationException, IllegalAccessException |
47 | 0 | { |
48 | 0 | Class<?> driverClass = ClassUtils.loadClass(driverClassName, this.getClass()); |
49 | 0 | xstream = new XStream((HierarchicalStreamDriver) driverClass.newInstance()); |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 0 | xstream.registerConverter(new XStreamFactory.ConcurrentHashMapConverter(xstream.getMapper()), -1); |
55 | |
|
56 | 0 | registerAliases(aliases); |
57 | 0 | registerConverters(converters); |
58 | 0 | } |
59 | |
|
60 | |
private void registerAliases(Map<String, Class<?>> aliases) |
61 | |
{ |
62 | 0 | if (aliases != null) |
63 | |
{ |
64 | 0 | for (Map.Entry<String, Class<?>> entry : aliases.entrySet()) |
65 | |
{ |
66 | 0 | xstream.alias(entry.getKey(), entry.getValue()); |
67 | |
} |
68 | |
} |
69 | 0 | } |
70 | |
|
71 | |
private void registerConverters(Set<Class <? extends Converter>> converters) throws InstantiationException, IllegalAccessException |
72 | |
{ |
73 | 0 | if (converters != null) |
74 | |
{ |
75 | 0 | for (Class<?> converter : converters) |
76 | |
{ |
77 | 0 | Object converterInstance = converter.newInstance(); |
78 | 0 | if (converterInstance instanceof Converter) |
79 | |
{ |
80 | 0 | xstream.registerConverter((Converter) converterInstance); |
81 | |
} |
82 | 0 | else if (converterInstance instanceof SingleValueConverter) |
83 | |
{ |
84 | 0 | xstream.registerConverter((SingleValueConverter) converterInstance); |
85 | |
} |
86 | |
else |
87 | |
{ |
88 | 0 | logger.warn("Invalid converter class specified - ignoring: " + converter.getName()); |
89 | |
} |
90 | 0 | } |
91 | |
} |
92 | 0 | } |
93 | |
|
94 | |
public final XStream getInstance() |
95 | |
{ |
96 | 0 | return xstream; |
97 | |
} |
98 | |
|
99 | |
private class ConcurrentHashMapConverter extends MapConverter |
100 | |
{ |
101 | |
public ConcurrentHashMapConverter(Mapper mapper) throws ClassNotFoundException |
102 | 0 | { |
103 | 0 | super(mapper); |
104 | 0 | } |
105 | |
|
106 | |
@Override |
107 | |
@SuppressWarnings("rawtypes") |
108 | |
public boolean canConvert(Class aClass) |
109 | |
{ |
110 | 0 | String className = aClass.getName(); |
111 | 0 | return className.equals("java.util.concurrent.ConcurrentHashMap") |
112 | |
|| className.equals("edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap"); |
113 | |
} |
114 | |
} |
115 | |
} |