Coverage Report - org.simpleframework.xml.transform.ClassTransform
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassTransform
75%
6/8
50%
1/2
1.25
 
 1  
 /*
 2  
  * ClassTransform.java May 2007
 3  
  *
 4  
  * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 15  
  * implied. See the License for the specific language governing 
 16  
  * permissions and limitations under the License.
 17  
  */
 18  
 
 19  
 package org.simpleframework.xml.transform;
 20  
 
 21  
 /**
 22  
  * The <code>ClassTransform</code> object is used to transform class
 23  
  * values to and from string representations, which will be inserted
 24  
  * in the generated XML document as the value place holder. The
 25  
  * value must be readable and writable in the same format. Fields
 26  
  * and methods annotated with the XML attribute annotation will use
 27  
  * this to persist and retrieve the value to and from the XML source.
 28  
  * <pre>
 29  
  * 
 30  
  *    &#64;Attribute
 31  
  *    private Class target;
 32  
  *    
 33  
  * </pre>
 34  
  * As well as the XML attribute values using transforms, fields and
 35  
  * methods annotated with the XML element annotation will use this.
 36  
  * Aside from the obvious difference, the element annotation has an
 37  
  * advantage over the attribute annotation in that it can maintain
 38  
  * any references using the <code>CycleStrategy</code> object. 
 39  
  * 
 40  
  * @author Ben Wolfe
 41  
  * @author Niall Gallagher
 42  
  */
 43  1
 class ClassTransform implements Transform<Class> {
 44  
   
 45  
    /**
 46  
     * This method is used to convert the string value given to an
 47  
     * appropriate representation. This is used when an object is
 48  
     * being deserialized from the XML document and the value for
 49  
     * the string representation is required.
 50  
     * 
 51  
     * @param target this is the string representation of the class
 52  
     * 
 53  
     * @return this returns an appropriate instanced to be used
 54  
     */
 55  
    public Class read(String target) throws Exception {
 56  1
       ClassLoader loader = getClassLoader();
 57  
       
 58  1
       if(loader == null) {
 59  0
          loader = getCallerClassLoader();
 60  
       }
 61  1
       return loader.loadClass(target);
 62  
    }
 63  
    
 64  
    /**
 65  
     * This method is used to convert the provided value into an XML
 66  
     * usable format. This is used in the serialization process when
 67  
     * there is a need to convert a field value in to a string so 
 68  
     * that that value can be written as a valid XML entity.
 69  
     * 
 70  
     * @param target this is the value to be converted to a string
 71  
     * 
 72  
     * @return this is the string representation of the given value
 73  
     */
 74  
    public String write(Class target) throws Exception {
 75  1
       return target.getName();
 76  
    }
 77  
 
 78  
    /**
 79  
     * This is used to acquire the caller class loader for this object.
 80  
     * Typically this is only used if the thread context class loader
 81  
     * is set to null. This ensures that there is at least some class
 82  
     * loader available to the strategy to load the class.
 83  
     * 
 84  
     * @return this returns the loader that loaded this class     
 85  
     */
 86  
    private ClassLoader getCallerClassLoader() {
 87  0
       return getClass().getClassLoader();
 88  
    }
 89  
    
 90  
    /**
 91  
     * This is used to acquire the thread context class loader. This
 92  
     * is the default class loader used by the cycle strategy. When
 93  
     * using the thread context class loader the caller can switch the
 94  
     * class loader in use, which allows class loading customization.
 95  
     * 
 96  
     * @return this returns the loader used by the calling thread
 97  
     */
 98  
    private static ClassLoader getClassLoader() {
 99  1
       return Thread.currentThread().getContextClassLoader();
 100  
    }
 101  
 }