Coverage Report - org.mule.transformers.xml.XStreamFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
XStreamFactory
53%
10/19
25%
2/8
1.8
XStreamFactory$ConcurrentHashMapConverter
100%
5/5
75%
3/4
1.8
 
 1  
 /*
 2  
  * $Id: XStreamFactory.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 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.transformers.xml;
 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  2
         this(XSTREAM_XPP_DRIVER, null, null);
 42  2
     }
 43  
 
 44  
     public XStreamFactory(String driverClassName, Map aliases, List converters)
 45  
         throws ClassNotFoundException, InstantiationException, IllegalAccessException
 46  38
     {
 47  38
         Class driverClass = ClassUtils.loadClass(driverClassName, this.getClass());
 48  36
         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  36
         xstream.registerConverter(new XStreamFactory.ConcurrentHashMapConverter(xstream.getMapper()), -1);
 54  
 
 55  36
         if (aliases != null)
 56  
         {
 57  0
             for (Iterator iterator = aliases.entrySet().iterator(); iterator.hasNext();)
 58  
             {
 59  0
                 Map.Entry entry = (Map.Entry)iterator.next();
 60  0
                 Class aliasClass = ClassUtils.loadClass(entry.getValue().toString(), getClass());
 61  0
                 xstream.alias(entry.getKey().toString(), aliasClass);
 62  0
             }
 63  
         }
 64  
 
 65  36
         if (converters != null)
 66  
         {
 67  0
             for (Iterator iterator = converters.iterator(); iterator.hasNext();)
 68  
             {
 69  0
                 Class converterClazz = ClassUtils.loadClass(iterator.next().toString(), getClass());
 70  0
                 xstream.registerConverter((Converter)converterClazz.newInstance());
 71  0
             }
 72  
         }
 73  36
     }
 74  
 
 75  
     public final XStream getInstance()
 76  
     {
 77  36
         return xstream;
 78  
     }
 79  
 
 80  
     private class ConcurrentHashMapConverter extends MapConverter
 81  
     {
 82  
         public ConcurrentHashMapConverter(Mapper mapper) throws ClassNotFoundException
 83  36
         {
 84  36
             super(mapper);
 85  36
         }
 86  
 
 87  
         public boolean canConvert(Class aClass)
 88  
         {
 89  98
             String className = aClass.getName();
 90  98
             return className.equals("java.util.concurrent.ConcurrentHashMap")
 91  
                             || className.equals("edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap");
 92  
         }
 93  
     }
 94  
 
 95  
 }