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