View Javadoc

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