1 10 11 package org.mule.transformers.xml; 12 13 import com.thoughtworks.xstream.XStream; 14 import com.thoughtworks.xstream.converters.Converter; 15 import com.thoughtworks.xstream.converters.collections.MapConverter; 16 import com.thoughtworks.xstream.io.HierarchicalStreamDriver; 17 import com.thoughtworks.xstream.mapper.Mapper; 18 import org.mule.util.ClassUtils; 19 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 27 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 private final XStream xstream; 37 38 public XStreamFactory() throws ClassNotFoundException , InstantiationException , IllegalAccessException 39 { 40 this(XSTREAM_XPP_DRIVER, null, null); 41 } 42 43 public XStreamFactory(String driverClassName, Map aliases, List converters) 44 throws ClassNotFoundException , InstantiationException , IllegalAccessException 45 { 46 Class driverClass = ClassUtils.loadClass(driverClassName, this.getClass()); 47 xstream = new XStream((HierarchicalStreamDriver)driverClass.newInstance()); 48 49 xstream.registerConverter(new XStreamFactory.ConcurrentHashMapConverter(xstream.getClassMapper()), -1); 55 56 if (aliases != null) 57 { 58 for (Iterator iterator = aliases.entrySet().iterator(); iterator.hasNext();) 59 { 60 Map.Entry entry = (Map.Entry )iterator.next(); 61 Class aliasClass = ClassUtils.loadClass(entry.getValue().toString(), getClass()); 62 xstream.alias(entry.getKey().toString(), aliasClass); 63 } 64 } 65 66 if (converters != null) 67 { 68 for (Iterator iterator = converters.iterator(); iterator.hasNext();) 69 { 70 Class converterClazz = ClassUtils.loadClass(iterator.next().toString(), getClass()); 71 xstream.registerConverter((Converter)converterClazz.newInstance()); 72 } 73 } 74 } 75 76 public final XStream getInstance() 77 { 78 return xstream; 79 } 80 81 private class ConcurrentHashMapConverter extends MapConverter 82 { 83 public ConcurrentHashMapConverter(Mapper mapper) throws ClassNotFoundException 84 { 85 super(mapper); 86 } 87 88 public boolean canConvert(Class aClass) 89 { 90 String className = aClass.getName(); 91 return className.equals("java.util.concurrent.ConcurrentHashMap") 92 || className.equals("edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap"); 93 } 94 } 95 96 } 97 | Popular Tags |