01: /*
02: * $Id: XStreamFactory.java 11236 2008-03-06 23:48:23Z tcarlson $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
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.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: * Initializes the XStream utility for converting Objects to XML and XML to Objects.
27: */
28: // @Immutable
29: public class XStreamFactory {
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: private final XStream xstream;
37:
38: public XStreamFactory() throws ClassNotFoundException,
39: InstantiationException, IllegalAccessException {
40: this (XSTREAM_XPP_DRIVER, null, null);
41: }
42:
43: public XStreamFactory(String driverClassName, Map aliases,
44: List converters) throws ClassNotFoundException,
45: InstantiationException, IllegalAccessException {
46: Class driverClass = ClassUtils.loadClass(driverClassName, this
47: .getClass());
48: xstream = new XStream((HierarchicalStreamDriver) driverClass
49: .newInstance());
50:
51: // We must always register this converter as the Mule Message uses
52: // ConcurrentHashMaps, but XStream currently does not support them out of the
53: // box.
54: xstream.registerConverter(
55: new XStreamFactory.ConcurrentHashMapConverter(xstream
56: .getMapper()), -1);
57:
58: if (aliases != null) {
59: for (Iterator iterator = aliases.entrySet().iterator(); iterator
60: .hasNext();) {
61: Map.Entry entry = (Map.Entry) iterator.next();
62: Class aliasClass = ClassUtils.loadClass(entry
63: .getValue().toString(), getClass());
64: xstream.alias(entry.getKey().toString(), aliasClass);
65: }
66: }
67:
68: if (converters != null) {
69: for (Iterator iterator = converters.iterator(); iterator
70: .hasNext();) {
71: Class converterClazz = ClassUtils.loadClass(iterator
72: .next().toString(), getClass());
73: xstream.registerConverter((Converter) converterClazz
74: .newInstance());
75: }
76: }
77: }
78:
79: public final XStream getInstance() {
80: return xstream;
81: }
82:
83: private class ConcurrentHashMapConverter extends MapConverter {
84: public ConcurrentHashMapConverter(Mapper mapper)
85: throws ClassNotFoundException {
86: super (mapper);
87: }
88:
89: public boolean canConvert(Class aClass) {
90: String className = aClass.getName();
91: return className
92: .equals("java.util.concurrent.ConcurrentHashMap")
93: || className
94: .equals("edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap");
95: }
96: }
97:
98: }
|