Coverage Report - org.simpleframework.xml.transform.ArrayMatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayMatcher
100%
15/15
100%
8/8
3.667
 
 1  
 /*
 2  
  * ArrayMatcher.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  
 /**
 22  
  * The <code>ArrayMatcher</code> object performs matching of array
 23  
  * types to array transforms. This uses the array component type to
 24  
  * determine the transform to be used. All array transforms created
 25  
  * by this will be <code>ArrayTransform</code> object instances. 
 26  
  * These will use a type transform for the array component to add
 27  
  * values to the individual array indexes. Also such transforms are
 28  
  * typically treated as a comma separated list of individual values.
 29  
  * 
 30  
  * @author Niall Gallagher
 31  
  * 
 32  
  * @see org.simpleframework.xml.transform.ArrayTransform
 33  
  */
 34  
 class ArrayMatcher implements Matcher {
 35  
 
 36  
    /**
 37  
     * This is the primary matcher that can resolve transforms.
 38  
     */
 39  
    private final Matcher primary;
 40  
    
 41  
    /**
 42  
     * Constructor for the <code>ArrayTransform</code> object. This
 43  
     * is used to match array types to their respective transform
 44  
     * using the <code>ArrayTransform</code> object. This will use
 45  
     * a comma separated list of tokens to populate the array.
 46  
     * 
 47  
     * @param primary this is the primary matcher to be used 
 48  
     */
 49  985
    public ArrayMatcher(Matcher primary) {
 50  985
       this.primary = primary;
 51  985
    }
 52  
    
 53  
    /**
 54  
     * This is used to match a <code>Transform</code> based on the
 55  
     * array component type of an object to be transformed. This will
 56  
     * attempt to match the transform using the fully qualified class
 57  
     * name of the array component type. If a transform can not be
 58  
     * found then this method will throw an exception.
 59  
     * 
 60  
     * @param type this is the array to find the transform for
 61  
     * 
 62  
     * @throws Exception thrown if a transform can not be matched
 63  
     */
 64  
    public Transform match(Class type) throws Exception {
 65  130
       Class entry = type.getComponentType();
 66  
       
 67  130
       if(entry == char.class) {
 68  1
          return new CharacterArrayTransform(entry);
 69  
       } 
 70  129
       if(entry == Character.class) {
 71  4
          return new CharacterArrayTransform(entry);
 72  
       }
 73  125
       if(entry == String.class) {
 74  19
          return new StringArrayTransform();
 75  
       }
 76  106
       return matchArray(entry);
 77  
    }
 78  
    
 79  
    /**
 80  
     * This is used to match a <code>Transform</code> based on the
 81  
     * array component type of an object to be transformed. This will
 82  
     * attempt to match the transform using the fully qualified class
 83  
     * name of the array component type. If a transform can not be
 84  
     * found then this method will throw an exception.
 85  
     * 
 86  
     * @param entry this is the array component type to be matched
 87  
     * 
 88  
     * @throws Exception thrown if a transform can not be matched
 89  
     */
 90  
    private Transform matchArray(Class entry) throws Exception {            
 91  106
       Transform transform = primary.match(entry);
 92  
      
 93  106
       if(transform == null) {
 94  45
          return null;
 95  
       }
 96  61
       return new ArrayTransform(transform, entry);
 97  
    }
 98  
 }