Coverage Report - org.simpleframework.xml.core.ScannerFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ScannerFactory
100%
12/12
100%
4/4
2
 
 1  
 /*
 2  
  * ScannerFactory.java July 2006
 3  
  *
 4  
  * Copyright (C) 2006, 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.core;
 20  
 
 21  
 import org.simpleframework.xml.util.Cache;
 22  
 import org.simpleframework.xml.util.ConcurrentCache;
 23  
 
 24  
 /**
 25  
  * The <code>ScannerFactory</code> is used to create scanner objects
 26  
  * that will scan a class for its XML class schema. Caching is done
 27  
  * by this factory so that repeat retrievals of a <code>Scanner</code>
 28  
  * will not require repeat scanning of the class for its XML schema.
 29  
  * 
 30  
  * @author Niall Gallagher
 31  
  * 
 32  
  * @see org.simpleframework.xml.core.Context
 33  
  */
 34  
 class ScannerFactory {
 35  
    
 36  
    /**
 37  
     * This is used to cache all schemas built to represent a class.
 38  
     */
 39  
    private final Cache<Scanner> cache;
 40  
    
 41  
    /**
 42  
     * This is used to determine which objects are primitives.
 43  
     */
 44  
    private final Support support;
 45  
    
 46  
    /**
 47  
     * Constructor for the <code>ScannerFactory</code> object. This is
 48  
     * used to create a factory that will create and cache scanned 
 49  
     * data for a given class. Scanning the class is required to find
 50  
     * the fields and methods that have been annotated.
 51  
     * 
 52  
     * @param support this is used to determine if a type is primitive
 53  
     */
 54  950
    public ScannerFactory(Support support) {
 55  950
       this.cache = new ConcurrentCache<Scanner>();
 56  950
       this.support = support;
 57  950
    }
 58  
    
 59  
    /**
 60  
     * This creates a <code>Scanner</code> object that can be used to
 61  
     * examine the fields within the XML class schema. The scanner
 62  
     * maintains information when a field from within the scanner is
 63  
     * visited, this allows the serialization and deserialization
 64  
     * process to determine if all required XML annotations are used.
 65  
     * 
 66  
     * @param type the schema class the scanner is created for
 67  
     * 
 68  
     * @return a scanner that can maintains information on the type
 69  
     * 
 70  
     * @throws Exception if the class contains an illegal schema 
 71  
     */ 
 72  
    public Scanner getInstance(Class type) throws Exception {
 73  2374169
       Scanner schema = cache.fetch(type);
 74  
       
 75  2374169
       if(schema == null) {
 76  3379
          Detail detail = support.getDetail(type);
 77  
          
 78  3379
          if(support.isPrimitive(type)) {
 79  1012
             schema = new PrimitiveScanner(detail);
 80  
          } else {
 81  2367
             schema = new ObjectScanner(detail, support);
 82  
          }
 83  3349
          cache.cache(type, schema);
 84  
       }
 85  2374139
       return schema;
 86  
    }
 87  
 }