Coverage Report - org.simpleframework.xml.transform.CharacterArrayTransform
 
Classes in this File Line Coverage Branch Coverage Complexity
CharacterArrayTransform
100%
23/23
90%
9/10
2.4
 
 1  
 /*
 2  
  * CharacterArrayTransform.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  
 import java.lang.reflect.Array;
 22  
 
 23  
 /**
 24  
  * The <code>CharacterArrayTransform</code> is used to transform text
 25  
  * values to and from string representations, which will be inserted
 26  
  * in the generated XML document as the value place holder. The
 27  
  * value must be readable and writable in the same format. Fields
 28  
  * and methods annotated with the XML attribute annotation will use
 29  
  * this to persist and retrieve the value to and from the XML source.
 30  
  * <pre>
 31  
  * 
 32  
  *    &#64;Attribute
 33  
  *    private char[] text;
 34  
  *    
 35  
  * </pre>
 36  
  * As well as the XML attribute values using transforms, fields and
 37  
  * methods annotated with the XML element annotation will use this.
 38  
  * Aside from the obvious difference, the element annotation has an
 39  
  * advantage over the attribute annotation in that it can maintain
 40  
  * any references using the <code>CycleStrategy</code> object. 
 41  
  * 
 42  
  * @author Niall Gallagher
 43  
  */
 44  
 class CharacterArrayTransform implements Transform {     
 45  
 
 46  
    /**
 47  
     * This is the entry type for the primitive array to be created.
 48  
     */
 49  
    private final Class entry;
 50  
 
 51  
    /**
 52  
     * Constructor for the <code>PrimitiveArrayTransform</code> object.
 53  
     * This is used to create a transform that will create primitive
 54  
     * arrays and populate the values of the array with values from a
 55  
     * comma separated list of individual values for the entry type.
 56  
     * 
 57  
     * @param entry this is the entry component type for the array
 58  
     */
 59  5
    public CharacterArrayTransform(Class entry) {
 60  5
       this.entry = entry;
 61  5
    }       
 62  
    
 63  
    /**
 64  
     * This method is used to convert the string value given to an
 65  
     * appropriate representation. This is used when an object is
 66  
     * being deserialized from the XML document and the value for
 67  
     * the string representation is required.
 68  
     * 
 69  
     * @param value this is the string representation of the value
 70  
     * 
 71  
     * @return this returns an appropriate instanced to be used
 72  
     */
 73  
    public Object read(String value) throws Exception {
 74  2
       char[] list = value.toCharArray();      
 75  2
       int length = list.length;
 76  
 
 77  2
       if(entry == char.class) {
 78  1
          return list;
 79  
       }
 80  1
       return read(list, length);
 81  
    }
 82  
    
 83  
    /**
 84  
     * This method is used to convert the string value given to an
 85  
     * appropriate representation. This is used when an object is
 86  
     * being deserialized from the XML document and the value for
 87  
     * the string representation is required.
 88  
     * 
 89  
     * @param list this is the string representation of the value
 90  
     * @param length this is the number of string values to use
 91  
     * 
 92  
     * @return this returns an appropriate instanced to be used
 93  
     */
 94  
    private Object read(char[] list, int length) throws Exception {
 95  1
       Object array = Array.newInstance(entry, length);
 96  
 
 97  12
       for(int i = 0; i < length; i++) {
 98  11
          Array.set(array, i, list[i]);                
 99  
       }
 100  1
       return array;
 101  
    }
 102  
    
 103  
    /**
 104  
     * This method is used to convert the provided value into an XML
 105  
     * usable format. This is used in the serialization process when
 106  
     * there is a need to convert a field value in to a string so 
 107  
     * that that value can be written as a valid XML entity.
 108  
     * 
 109  
     * @param value this is the value to be converted to a string
 110  
     * 
 111  
     * @return this is the string representation of the given value
 112  
     */
 113  
    public String write(Object value) throws Exception {
 114  2
       int length = Array.getLength(value);
 115  
 
 116  2
       if(entry == char.class) {
 117  1
          char[] array = (char[])value;
 118  1
          return new String(array);
 119  
       }
 120  1
       return write(value, length);      
 121  
    }
 122  
    
 123  
    /**
 124  
     * This method is used to convert the provided value into an XML
 125  
     * usable format. This is used in the serialization process when
 126  
     * there is a need to convert a field value in to a string so 
 127  
     * that that value can be written as a valid XML entity.
 128  
     * 
 129  
     * @param value this is the value to be converted to a string
 130  
     * 
 131  
     * @return this is the string representation of the given value
 132  
     */
 133  
    private String write(Object value, int length) throws Exception {
 134  1
       StringBuilder text = new StringBuilder(length);
 135  
 
 136  12
       for(int i = 0; i < length; i++) {
 137  11
          Object entry = Array.get(value, i);         
 138  
 
 139  11
          if(entry != null) {
 140  11
             text.append(entry);                             
 141  
          }         
 142  
       }      
 143  1
       return text.toString();
 144  
    }
 145  
 }