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