KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transformers > xml > XStreamFactory


1 /*
2  * $Id: XStreamFactory.java 3937 2006-11-20 16:04:25Z lajos $
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 MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

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 JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * Initializes the XStream utility for converting Objects to XML and XML to Objects.
26  */

27 // @Immutable
28
public class XStreamFactory
29 {
30     public static final String JavaDoc XSTREAM_DOM_DRIVER = "com.thoughtworks.xstream.io.xml.DomDriver";
31     public static final String JavaDoc XSTREAM_DOM4J_DRIVER = "com.thoughtworks.xstream.io.xml.Dom4JDriver";
32     public static final String JavaDoc XSTREAM_JDOM_DRIVER = "com.thoughtworks.xstream.io.xml.JDomDriver";
33     public static final String JavaDoc XSTREAM_STAX_DRIVER = "com.thoughtworks.xstream.io.xml.StaxDriver";
34     public static final String JavaDoc XSTREAM_XPP_DRIVER = "com.thoughtworks.xstream.io.xml.XppDriver";
35
36     private final XStream xstream;
37
38     public XStreamFactory() throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
39     {
40         this(XSTREAM_XPP_DRIVER, null, null);
41     }
42
43     public XStreamFactory(String JavaDoc driverClassName, Map JavaDoc aliases, List JavaDoc converters)
44         throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
45     {
46         Class JavaDoc driverClass = ClassUtils.loadClass(driverClassName, this.getClass());
47         xstream = new XStream((HierarchicalStreamDriver)driverClass.newInstance());
48
49         // We must always register this converter as the Mule Message uses
50
// ConcurrentHashMaps, but XStream currently does not support them out of the
51
// box.
52
// TODO see MULE-1151 on how to upgrade this code to be XStream-1.2
53
// compliant, maybe for Mule 1.4/2.0.
54
xstream.registerConverter(new XStreamFactory.ConcurrentHashMapConverter(xstream.getClassMapper()), -1);
55
56         if (aliases != null)
57         {
58             for (Iterator JavaDoc iterator = aliases.entrySet().iterator(); iterator.hasNext();)
59             {
60                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
61                 Class JavaDoc 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 JavaDoc iterator = converters.iterator(); iterator.hasNext();)
69             {
70                 Class JavaDoc 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 JavaDoc
84         {
85             super(mapper);
86         }
87
88         public boolean canConvert(Class JavaDoc aClass)
89         {
90             String JavaDoc 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