| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| LocaleTransform |
|
| 0.0;0 |
| 1 | /* | |
| 2 | * LocaleTransform.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 java.util.regex.Pattern; | |
| 24 | import java.util.Locale; | |
| 25 | ||
| 26 | /** | |
| 27 | * The <code>LocaleTransform</code> is used to transform locale | |
| 28 | * values 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 Locale locale; | |
| 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 | 0 | class LocaleTransform implements Transform<Locale>{ |
| 48 | ||
| 49 | /** | |
| 50 | * This is the pattern used to split the parts of the locale. | |
| 51 | */ | |
| 52 | private final Pattern pattern; | |
| 53 | ||
| 54 | /** | |
| 55 | * Constructor for the <code>LocaleTransform</code> object. This | |
| 56 | * is used to create a transform that will convert locales to and | |
| 57 | * from string representations. The representations use the Java | |
| 58 | * locale representation of language, country, and varient. | |
| 59 | */ | |
| 60 | 1 | public LocaleTransform() { |
| 61 | 1 | this.pattern = Pattern.compile("_"); |
| 62 | 1 | } |
| 63 | ||
| 64 | /** | |
| 65 | * This method is used to convert the string value given to an | |
| 66 | * appropriate representation. This is used when an object is | |
| 67 | * being deserialized from the XML document and the value for | |
| 68 | * the string representation is required. | |
| 69 | * | |
| 70 | * @param locale the string representation of the date value | |
| 71 | * | |
| 72 | * @return this returns an appropriate instanced to be used | |
| 73 | */ | |
| 74 | public Locale read(String locale) throws Exception { | |
| 75 | 2 | String[] list = pattern.split(locale); |
| 76 | ||
| 77 | 2 | if(list.length < 1) { |
| 78 | 0 | throw new InvalidFormatException("Invalid locale %s", locale); |
| 79 | } | |
| 80 | 2 | return read(list); |
| 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 locale the string representation of the date value | |
| 90 | * | |
| 91 | * @return this returns an appropriate instanced to be used | |
| 92 | */ | |
| 93 | private Locale read(String[] locale) throws Exception { | |
| 94 | 2 | String[] list = new String[] {"", "", ""}; |
| 95 | ||
| 96 | 8 | for(int i = 0; i < list.length; i++) { |
| 97 | 6 | if(i < locale.length) { |
| 98 | 3 | list[i] = locale[i]; |
| 99 | } | |
| 100 | } | |
| 101 | 2 | return new Locale(list[0], list[1], list[2]); |
| 102 | } | |
| 103 | ||
| 104 | /** | |
| 105 | * This method is used to convert the provided value into an XML | |
| 106 | * usable format. This is used in the serialization process when | |
| 107 | * there is a need to convert a field value in to a string so | |
| 108 | * that that value can be written as a valid XML entity. | |
| 109 | * | |
| 110 | * @param locale this is the value to be converted to a string | |
| 111 | * | |
| 112 | * @return this is the string representation of the given date | |
| 113 | */ | |
| 114 | public String write(Locale locale) { | |
| 115 | 2 | return locale.toString(); |
| 116 | } | |
| 117 | } |