Coverage Report - org.simpleframework.xml.transform.StringArrayTransform
 
Classes in this File Line Coverage Branch Coverage Complexity
StringArrayTransform
100%
24/24
90%
9/10
1.833
 
 1  
 /*
 2  
  * StringArrayTransform.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.Transform;
 22  
 import java.util.regex.Pattern;
 23  
 
 24  
 /**
 25  
  * The <code>StringArrayTransform</code>  is used to transform string
 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 String[] array;
 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  62
 class StringArrayTransform implements Transform<String[]> {
 46  
 
 47  
    /**
 48  
     * Represents the pattern used to split the string values.
 49  
     */
 50  
    private final Pattern pattern;        
 51  
 
 52  
    /**
 53  
     * This is the token used to split the string into an array.
 54  
     */
 55  
    private final String token;        
 56  
 
 57  
    /**
 58  
     * Constructor for the <code>StringArrayTransform</code> object.
 59  
     * This will create a transform that will split an array using a
 60  
     * comma as the delimeter. In order to perform the split in a
 61  
     * reasonably performant manner the pattern used is compiled.
 62  
     */
 63  
    public StringArrayTransform() {
 64  84
       this(",");           
 65  84
    }        
 66  
   
 67  
    /**
 68  
     * Constructor for the <code>StringArrayTransform</code> object.
 69  
     * This will create a transform that will split an array using a
 70  
     * specified regular expression pattern. To keep the performance
 71  
     * of the transform reasonable the pattern used is compiled.
 72  
     * 
 73  
     * @param token the pattern used to split the string values
 74  
     */
 75  84
    public StringArrayTransform(String token) {
 76  84
       this.pattern = Pattern.compile(token);           
 77  84
       this.token = token;           
 78  84
    }
 79  
    
 80  
    /**
 81  
     * This method is used to convert the string value given to an
 82  
     * appropriate representation. This is used when an object is
 83  
     * being deserialized from the XML document and the value for
 84  
     * the string representation is required.
 85  
     * 
 86  
     * @param value this is the string representation of the value
 87  
     * 
 88  
     * @return this returns an appropriate instanced to be used
 89  
     */
 90  
    public String[] read(String value) {
 91  64
       return read(value, token);           
 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 value this is the string representation of the value
 101  
     * @param token this is the token used to split the string
 102  
     * 
 103  
     * @return this returns an appropriate instanced to be used
 104  
     */
 105  
    private String[] read(String value, String token) {
 106  64
       String[] list = pattern.split(value);
 107  
 
 108  327
       for(int i = 0; i < list.length; i++) {
 109  263
          String text = list[i];
 110  
 
 111  263
          if(text != null) {              
 112  263
             list[i] = text.trim();
 113  
          }
 114  
       }
 115  64
       return list;
 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 list 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(String[] list) {
 129  104
       return write(list, token);
 130  
    }
 131  
    
 132  
    /**
 133  
     * This method is used to convert the provided value into an XML
 134  
     * usable format. This is used in the serialization process when
 135  
     * there is a need to convert a field value in to a string so 
 136  
     * that that value can be written as a valid XML entity.
 137  
     * 
 138  
     * @param list this is the value to be converted to a string
 139  
     * @param token this is the token used to join the strings
 140  
     * 
 141  
     * @return this is the string representation of the given value
 142  
     */
 143  
    private String write(String[] list, String token) {                 
 144  104
       StringBuilder text = new StringBuilder();           
 145  
 
 146  513
       for(int i = 0; i < list.length; i++) {
 147  409
          String item = list[i];
 148  
 
 149  409
          if(item != null) { 
 150  408
             if(text.length() > 0) {
 151  304
                text.append(token);
 152  304
                text.append(' ');
 153  
             }
 154  408
             text.append(item);
 155  
          }
 156  
       }   
 157  104
       return text.toString();
 158  
    }
 159  
 }