Coverage Report - org.simpleframework.xml.transform.PackageMatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
PackageMatcher
78%
57/73
75%
47/62
7.778
 
 1  
 /*
 2  
  * PackageMatcher.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.GregorianCalendar;
 22  
 import java.math.BigDecimal;
 23  
 import java.math.BigInteger;
 24  
 import java.util.TimeZone;
 25  
 import java.util.Locale;
 26  
 import java.sql.Timestamp;
 27  
 import java.util.Currency;
 28  
 import java.util.Date;
 29  
 import java.sql.Time;
 30  
 import java.io.File;
 31  
 import java.net.URL;
 32  
 
 33  
 /**
 34  
  * The <code>PackageMatcher</code> object is used to match the stock
 35  
  * transforms to Java packages. This is used to match useful types 
 36  
  * from the <code>java.lang</code> and <code>java.util</code> packages
 37  
  * as well as other Java packages. This matcher groups types by their
 38  
  * package names and attempts to search the stock transforms for a
 39  
  * suitable match. If no match can be found this throws an exception.
 40  
  *  
 41  
  * @author Niall Gallagher
 42  
  *
 43  
  * @see org.simpleframework.xml.transform.DefaultMatcher
 44  
  */
 45  
 class PackageMatcher implements Matcher {
 46  
    
 47  
    /**
 48  
     * Constructor for the <code>PackageMatcher</code> object. The
 49  
     * package matcher is used to resolve a transform instance to
 50  
     * convert object types to an from strings. If a match cannot
 51  
     * be found with this matcher then an exception is thrown.
 52  
     */
 53  
    public PackageMatcher() {
 54  985
       super();
 55  985
    }
 56  
    
 57  
    /**
 58  
     * This method attempts to perform a resolution of the transform
 59  
     * based on its package prefix. This allows this matcher to create
 60  
     * a logical group of transforms within a single method based on
 61  
     * the types package prefix. If no transform can be found then
 62  
     * this will throw an exception.
 63  
     * 
 64  
     * @param type this is the type to resolve a transform for
 65  
     * 
 66  
     * @return the transform that is used to transform that type
 67  
     */
 68  
    public Transform match(Class type) throws Exception {  
 69  3740
       String name = type.getName();
 70  
       
 71  3740
       if(name.startsWith("java.lang")) {
 72  1209
          return matchLanguage(type);
 73  
       }
 74  2531
       if(name.startsWith("java.util")) {
 75  708
          return matchUtility(type);         
 76  
       }
 77  1823
       if(name.startsWith("java.net")) {
 78  0
          return matchURL(type);         
 79  
       }
 80  1823
       if(name.startsWith("java.io")) {
 81  3
          return matchFile(type);         
 82  
       }
 83  1820
       if(name.startsWith("java.sql")) {
 84  6
          return matchSQL(type);   
 85  
       }     
 86  1814
       if(name.startsWith("java.math")) {
 87  39
          return matchMath(type);
 88  
       }
 89  1775
       return matchEnum(type);
 90  
    }
 91  
    
 92  
    /**
 93  
     * This is used to resolve <code>Transform</code> implementations
 94  
     * that are <code>Enum</code> implementations. If the type is not
 95  
     * an enumeration then this will return null.
 96  
     * 
 97  
     * @param type this is the type to resolve a stock transform for
 98  
     * 
 99  
     * @return this will return a transform for the specified type
 100  
     */
 101  
    private Transform matchEnum(Class type) {
 102  1775
       Class parent = type.getSuperclass();
 103  
       
 104  1775
       if(parent != null) {
 105  1761
          if(parent.isEnum()) {
 106  1
             return new EnumTransform(type);
 107  
          }
 108  1760
          if(type.isEnum()) {
 109  85
             return new EnumTransform(type);
 110  
          }
 111  
       }
 112  1689
       return null;
 113  
    }
 114  
    
 115  
    /**
 116  
     * This is used to resolve <code>Transform</code> implementations
 117  
     * that relate to the <code>java.lang</code> package. If the type
 118  
     * does not resolve to a valid transform then this method will 
 119  
     * throw an exception to indicate that no stock transform exists
 120  
     * for the specified type.
 121  
     * 
 122  
     * @param type this is the type to resolve a stock transform for
 123  
     * 
 124  
     * @return this will return a transform for the specified type
 125  
     */
 126  
    private Transform matchLanguage(Class type) throws Exception {      
 127  1209
       if(type == Boolean.class) {
 128  65
          return new BooleanTransform();
 129  
       }
 130  1144
       if(type == Integer.class) {
 131  217
          return new IntegerTransform();
 132  
       }
 133  927
       if(type == Long.class) {
 134  38
          return new LongTransform();
 135  
       }
 136  889
       if(type == Double.class) {
 137  85
          return new DoubleTransform();
 138  
       }
 139  804
       if(type == Float.class) {
 140  31
          return new FloatTransform();
 141  
       }
 142  773
       if(type == Short.class) {
 143  21
          return new ShortTransform();
 144  
       }
 145  752
       if(type == Byte.class) {
 146  20
          return new ByteTransform();
 147  
       }
 148  732
       if(type == Character.class) {
 149  9
          return new CharacterTransform();
 150  
       }
 151  723
       if(type == String.class) {
 152  688
          return new StringTransform();
 153  
       }      
 154  35
       if(type == Class.class) {
 155  0
          return new ClassTransform();
 156  
       }
 157  35
       return null;
 158  
    }
 159  
    
 160  
    /**
 161  
     * This is used to resolve <code>Transform</code> implementations
 162  
     * that relate to the <code>java.math</code> package. If the type
 163  
     * does not resolve to a valid transform then this method will 
 164  
     * throw an exception to indicate that no stock transform exists
 165  
     * for the specified type.
 166  
     * 
 167  
     * @param type this is the type to resolve a stock transform for
 168  
     * 
 169  
     * @return this will return a transform for the specified type
 170  
     */ 
 171  
    private Transform matchMath(Class type) throws Exception {
 172  39
       if(type == BigDecimal.class) {
 173  39
          return new BigDecimalTransform();
 174  
       }
 175  0
       if(type == BigInteger.class) {
 176  0
          return new BigIntegerTransform();
 177  
       }      
 178  0
       return null;
 179  
    }
 180  
    
 181  
    /**
 182  
     * This is used to resolve <code>Transform</code> implementations
 183  
     * that relate to the <code>java.util</code> package. If the type
 184  
     * does not resolve to a valid transform then this method will 
 185  
     * throw an exception to indicate that no stock transform exists
 186  
     * for the specified type.
 187  
     * 
 188  
     * @param type this is the type to resolve a stock transform for
 189  
     * 
 190  
     * @return this will return a transform for the specified type
 191  
     */
 192  
    private Transform matchUtility(Class type) throws Exception {
 193  708
       if(type == Date.class) {
 194  16
          return new DateTransform(type);
 195  
       }
 196  692
       if(type == Locale.class) {
 197  5
          return new LocaleTransform();
 198  
       }
 199  687
       if(type == Currency.class) {
 200  0
          return new CurrencyTransform();
 201  
       }
 202  687
       if(type == GregorianCalendar.class) {
 203  3
          return new GregorianCalendarTransform();
 204  
       }
 205  684
       if(type == TimeZone.class) {
 206  0
          return new TimeZoneTransform();
 207  
       }      
 208  684
       return null;
 209  
    }  
 210  
    
 211  
    /**
 212  
     * This is used to resolve <code>Transform</code> implementations
 213  
     * that relate to the <code>java.sql</code> package. If the type
 214  
     * does not resolve to a valid transform then this method will 
 215  
     * throw an exception to indicate that no stock transform exists
 216  
     * for the specified type.
 217  
     * 
 218  
     * @param type this is the type to resolve a stock transform for
 219  
     * 
 220  
     * @return this will return a transform for the specified type
 221  
     */
 222  
    private Transform matchSQL(Class type) throws Exception {
 223  6
       if(type == Time.class) {
 224  6
          return new DateTransform(type);
 225  
       }
 226  0
       if(type == java.sql.Date.class) {
 227  0
          return new DateTransform(type);
 228  
       }
 229  0
       if(type == Timestamp.class) {
 230  0
          return new DateTransform(type);
 231  
       }      
 232  0
       return null;
 233  
    }   
 234  
    
 235  
    /**
 236  
     * This is used to resolve <code>Transform</code> implementations
 237  
     * that relate to the <code>java.io</code> package. If the type
 238  
     * does not resolve to a valid transform then this method will 
 239  
     * throw an exception to indicate that no stock transform exists
 240  
     * for the specified type.
 241  
     * 
 242  
     * @param type this is the type to resolve a stock transform for
 243  
     * 
 244  
     * @return this will return a transform for the specified type
 245  
     */
 246  
    private Transform matchFile(Class type) throws Exception {
 247  3
       if(type == File.class) {
 248  3
          return new FileTransform();
 249  
       }       
 250  0
       return null;
 251  
    }   
 252  
    
 253  
    /**
 254  
     * This is used to resolve <code>Transform</code> implementations
 255  
     * that relate to the <code>java.net</code> package. If the type
 256  
     * does not resolve to a valid transform then this method will 
 257  
     * throw an exception to indicate that no stock transform exists
 258  
     * for the specified type.
 259  
     * 
 260  
     * @param type this is the type to resolve a stock transform for
 261  
     * 
 262  
     * @return this will return a transform for the specified type
 263  
     */
 264  
    private Transform matchURL(Class type) throws Exception {
 265  0
       if(type == URL.class) {
 266  0
          return new URLTransform();
 267  
       }      
 268  0
       return null;
 269  
    }
 270  
 }