| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ArrayTransform |
|
| 1.8;1.8 |
| 1 | /* | |
| 2 | * PrimitiveArrayTransform.java May 2007 | |
| 3 | * | |
| 4 | * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net> | |
| 5 | * | |
| 6 | * This library is free software; you can redistribute it and/or | |
| 7 | * modify it under the terms of the GNU Lesser General Public | |
| 8 | * License as published by the Free Software Foundation. | |
| 9 | * | |
| 10 | * This library is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | * GNU Lesser General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU Lesser General | |
| 16 | * Public License along with this library; if not, write to the | |
| 17 | * Free Software Foundation, Inc., 59 Temple Place, Suite 330, | |
| 18 | * Boston, MA 02111-1307 USA | |
| 19 | */ | |
| 20 | ||
| 21 | package org.simpleframework.xml.transform; | |
| 22 | ||
| 23 | import org.simpleframework.xml.transform.StringArrayTransform; | |
| 24 | import java.lang.reflect.Array; | |
| 25 | ||
| 26 | /** | |
| 27 | * The <code>PrimitiveArrayTransform</code> is used to transform | |
| 28 | * arrays to and from string representations, which will be inserted | |
| 29 | * in the generated XML document as the value place holder. The | |
| 30 | * value must be readable and writable in the same format. Fields | |
| 31 | * and methods annotated with the XML attribute annotation will use | |
| 32 | * this to persist and retrieve the value to and from the XML source. | |
| 33 | * <pre> | |
| 34 | * | |
| 35 | * @Attribute | |
| 36 | * private int[] text; | |
| 37 | * | |
| 38 | * </pre> | |
| 39 | * As well as the XML attribute values using transforms, fields and | |
| 40 | * methods annotated with the XML element annotation will use this. | |
| 41 | * Aside from the obvious difference, the element annotation has an | |
| 42 | * advantage over the attribute annotation in that it can maintain | |
| 43 | * any references using the <code>CycleStrategy</code> object. | |
| 44 | * | |
| 45 | * @author Niall Gallagher | |
| 46 | */ | |
| 47 | class ArrayTransform implements Transform { | |
| 48 | ||
| 49 | /** | |
| 50 | * This transform is used to split the comma separated values. | |
| 51 | */ | |
| 52 | private final StringArrayTransform split; | |
| 53 | ||
| 54 | /** | |
| 55 | * This is the transform that performs the individual transform. | |
| 56 | */ | |
| 57 | private final Transform delegate; | |
| 58 | ||
| 59 | /** | |
| 60 | * This is the entry type for the primitive array to be created. | |
| 61 | */ | |
| 62 | private final Class entry; | |
| 63 | ||
| 64 | /** | |
| 65 | * Constructor for the <code>PrimitiveArrayTransform</code> object. | |
| 66 | * This is used to create a transform that will create primitive | |
| 67 | * arrays and populate the values of the array with values from a | |
| 68 | * comma separated list of individula values for the entry type. | |
| 69 | * | |
| 70 | * @param delegate this is used to perform individual transforms | |
| 71 | * @param entry this is the entry component type for the array | |
| 72 | */ | |
| 73 | 19 | public ArrayTransform(Transform delegate, Class entry) { |
| 74 | 19 | this.split = new StringArrayTransform(); |
| 75 | 19 | this.delegate = delegate; |
| 76 | 19 | this.entry = entry; |
| 77 | 19 | } |
| 78 | ||
| 79 | /** | |
| 80 | * This method is used to convert the string value given to an | |
| 81 | * appropriate representation. This is used when an object is | |
| 82 | * being deserialized from the XML document and the value for | |
| 83 | * the string representation is required. | |
| 84 | * | |
| 85 | * @param value this is the string representation of the value | |
| 86 | * | |
| 87 | * @return this returns an appropriate instanced to be used | |
| 88 | */ | |
| 89 | public Object read(String value) throws Exception { | |
| 90 | 27 | String[] list = split.read(value); |
| 91 | 27 | int length = list.length; |
| 92 | ||
| 93 | 27 | return read(list, length); |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * This method is used to convert the string value given to an | |
| 98 | * appropriate representation. This is used when an object is | |
| 99 | * being deserialized from the XML document and the value for | |
| 100 | * the string representation is required. | |
| 101 | * | |
| 102 | * @param list this is the string representation of the value | |
| 103 | * @param length this is the number of string values to use | |
| 104 | * | |
| 105 | * @return this returns an appropriate instanced to be used | |
| 106 | */ | |
| 107 | private Object read(String[] list, int length) throws Exception { | |
| 108 | 27 | Object array = Array.newInstance(entry, length); |
| 109 | ||
| 110 | 163 | for(int i = 0; i < length; i++) { |
| 111 | 136 | Object item = delegate.read(list[i]); |
| 112 | ||
| 113 | 136 | if(item != null) { |
| 114 | 136 | Array.set(array, i, item); |
| 115 | } | |
| 116 | } | |
| 117 | 27 | return array; |
| 118 | } | |
| 119 | ||
| 120 | /** | |
| 121 | * This method is used to convert the provided value into an XML | |
| 122 | * usable format. This is used in the serialization process when | |
| 123 | * there is a need to convert a field value in to a string so | |
| 124 | * that that value can be written as a valid XML entity. | |
| 125 | * | |
| 126 | * @param value this is the value to be converted to a string | |
| 127 | * | |
| 128 | * @return this is the string representation of the given value | |
| 129 | */ | |
| 130 | public String write(Object value) throws Exception { | |
| 131 | 34 | int length = Array.getLength(value); |
| 132 | ||
| 133 | 34 | return write(value, length); |
| 134 | } | |
| 135 | ||
| 136 | /** | |
| 137 | * This method is used to convert the provided value into an XML | |
| 138 | * usable format. This is used in the serialization process when | |
| 139 | * there is a need to convert a field value in to a string so | |
| 140 | * that that value can be written as a valid XML entity. | |
| 141 | * | |
| 142 | * @param value this is the value to be converted to a string | |
| 143 | * | |
| 144 | * @return this is the string representation of the given value | |
| 145 | */ | |
| 146 | private String write(Object value, int length) throws Exception { | |
| 147 | 34 | String[] list = new String[length]; |
| 148 | ||
| 149 | 193 | for(int i = 0; i < length; i++) { |
| 150 | 159 | Object entry = Array.get(value, i); |
| 151 | ||
| 152 | 159 | if(entry != null) { |
| 153 | 159 | list[i] = delegate.write(entry); |
| 154 | } | |
| 155 | } | |
| 156 | 34 | return split.write(list); |
| 157 | } | |
| 158 | } |