Coverage Report - org.simpleframework.xml.transform.EnumTransform
 
Classes in this File Line Coverage Branch Coverage Complexity
EnumTransform
100%
6/6
N/A
1
 
 1  
 /*
 2  
  * EnumTransform.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>EnumTransform</code> represents a transform that is 
 23  
  * used to transform enumerations to strings and back again. This
 24  
  * is used when enumerations are used in comma separated arrays.
 25  
  * This may be created multiple times for different types.
 26  
  * 
 27  
  * @author Niall Gallagher
 28  
  */
 29  551
 class EnumTransform implements Transform<Enum> {
 30  
    
 31  
    /**
 32  
     * This is the specific enumeration that this transforms.
 33  
     */
 34  
    private final Class type;
 35  
    
 36  
    /**
 37  
     * Constructor for the <code>EnumTransform</code> object. This
 38  
     * is used to create enumerations from strings and convert them
 39  
     * back again. This allows enumerations to be used in arrays.
 40  
     * 
 41  
     * @param type this is the enumeration type to be transformed
 42  
     */
 43  86
    public EnumTransform(Class type) {
 44  86
       this.type = type;
 45  86
    }
 46  
 
 47  
    /**
 48  
     * This method is used to convert the string value given to an
 49  
     * appropriate representation. This is used when an object is
 50  
     * being deserialized from the XML document and the value for
 51  
     * the string representation is required.
 52  
     * 
 53  
     * @param value this is the string representation of the value
 54  
     * 
 55  
     * @return this returns an appropriate instanced to be used
 56  
     */
 57  
    public Enum read(String value) throws Exception {
 58  184
       return Enum.valueOf(type, value);
 59  
    }
 60  
 
 61  
    /**
 62  
     * This method is used to convert the provided value into an XML
 63  
     * usable format. This is used in the serialization process when
 64  
     * there is a need to convert a field value in to a string so 
 65  
     * that that value can be written as a valid XML entity.
 66  
     * 
 67  
     * @param value this is the value to be converted to a string
 68  
     * 
 69  
     * @return this is the string representation of the given value
 70  
     */
 71  
    public String write(Enum value) throws Exception {
 72  367
       return value.name();
 73  
    }
 74  
 }