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