Coverage Report - org.simpleframework.xml.transform.DefaultMatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultMatcher
100%
15/15
100%
6/6
3
 
 1  
 /*
 2  
  * DefaultMatcher.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>DefaultMatcher</code> is a delegation object that uses
 23  
  * several matcher implementations to correctly resolve both the
 24  
  * stock <code>Transform</code> implementations and implementations
 25  
  * that have been overridden by the user with a custom matcher. This
 26  
  * will perform the resolution of the transform using the specified 
 27  
  * matcher, if this results in no transform then this will look for
 28  
  * a transform within the collection of implementations.
 29  
  * 
 30  
  * @author Niall Gallagher
 31  
  *
 32  
  * @see org.simpleframework.xml.transform.Transformer
 33  
  */
 34  
 class DefaultMatcher implements Matcher {
 35  
    
 36  
    /**
 37  
     * Matcher used to resolve stock transforms for primitive types.
 38  
     */
 39  
    private Matcher primitive;   
 40  
    
 41  
    /**
 42  
     * Matcher used to resolve user specified transform overrides.
 43  
     */
 44  
    private Matcher matcher;
 45  
    
 46  
    /**
 47  
     * Matcher used to resolve all the core Java object transforms.
 48  
     */
 49  
    private Matcher stock;
 50  
    
 51  
    /**
 52  
     * Matcher used to resolve transforms for array type objects.
 53  
     */
 54  
    private Matcher array; 
 55  
    
 56  
    /**
 57  
     * Constructor for the <code>DefaultMatcher</code> object. This
 58  
     * performs resolution of <code>Transform</code> implementations 
 59  
     * using the specified matcher. If that matcher fails to resolve
 60  
     * a suitable transform then the stock implementations are used.
 61  
     * 
 62  
     * @param matcher this is the user specified matcher object
 63  
     */
 64  985
    public DefaultMatcher(Matcher matcher) {
 65  985
       this.primitive = new PrimitiveMatcher();
 66  985
       this.stock = new PackageMatcher();
 67  985
       this.array = new ArrayMatcher(this);
 68  985
       this.matcher = matcher;
 69  985
    }
 70  
    
 71  
    /**
 72  
     * This is used to match a <code>Transform</code> for the given
 73  
     * type. If a transform cannot be resolved this this will throw an
 74  
     * exception to indicate that resolution of a transform failed. A
 75  
     * transform is resolved by first searching for a transform within
 76  
     * the user specified matcher then searching the stock transforms.
 77  
     * 
 78  
     * @param type this is the type to resolve a transform object for
 79  
     * 
 80  
     * @return this returns a transform used to transform the type
 81  
     */
 82  
    public Transform match(Class type) throws Exception {
 83  4288
       Transform value = matcher.match(type);
 84  
       
 85  4288
       if(value != null) {
 86  8
          return value;
 87  
       }
 88  4280
       return matchType(type);
 89  
    }
 90  
    
 91  
    /**
 92  
     * This is used to match a <code>Transform</code> for the given
 93  
     * type. If a transform cannot be resolved this this will throw an
 94  
     * exception to indicate that resolution of a transform failed. A
 95  
     * transform is resolved by first searching for a transform within
 96  
     * the user specified matcher then searching the stock transforms.
 97  
     * 
 98  
     * @param type this is the type to resolve a transform object for
 99  
     * 
 100  
     * @return this returns a transform used to transform the type
 101  
     */
 102  
    private Transform matchType(Class type) throws Exception {
 103  4280
       if(type.isArray()) {
 104  130
          return array.match(type);
 105  
       }
 106  4150
       if(type.isPrimitive()) {
 107  410
          return primitive.match(type);
 108  
       }
 109  3740
       return stock.match(type); 
 110  
    }
 111  
 }
 112