All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.simpleframework.xml.transform.ArrayMatcher Maven / Gradle / Ivy

Go to download

Simple is a high performance XML serialization and configuration framework for Java

There is a newer version: 2.7.1
Show newest version
/*
 * ArrayMatcher.java May 2007
 *
 * Copyright (C) 2007, Niall Gallagher 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General 
 * Public License along with this library; if not, write to the 
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA  02111-1307  USA
 */

package org.simpleframework.xml.transform;

/**
 * The ArrayMatcher object performs matching of array
 * types to array transforms. This uses the array component type to
 * determine the transform to be used. All array transforms created
 * by this will be ArrayTransform object instances. 
 * These will use a type transform for the array component to add
 * values to the individual array indexes. Also such transforms are
 * typically treated as a comma separated list of individual values.
 * 
 * @author Niall Gallagher
 * 
 * @see org.simpleframework.xml.transform.ArrayTransform
 */
class ArrayMatcher implements Matcher {

   /**
    * This is the primary matcher that can resolve transforms.
    */
   private final Matcher primary;
   
   /**
    * Constructor for the ArrayTransform object. This
    * is used to match array types to their respective transform
    * using the ArrayTransform object. This will use
    * a comma separated list of tokens to populate the array.
    * 
    * @param primary this is the primary matcher to be used 
    */
   public ArrayMatcher(Matcher primary) {
      this.primary = primary;
   }
   
   /**
    * This is used to match a Transform based on the
    * array component type of an object to be transformed. This will
    * attempt to match the transform using the fully qualified class
    * name of the array component type. If a transform can not be
    * found then this method will throw an exception.
    * 
    * @param type this is the array to find the transform for
    * 
    * @throws Exception thrown if a transform can not be matched
    */
   public Transform match(Class type) throws Exception {
      Class entry = type.getComponentType();
      
      if(entry == char.class) {
         return new CharacterArrayTransform(entry);
      } 
      if(entry == Character.class) {
         return new CharacterArrayTransform(entry);
      }
      if(entry == String.class) {
         return new StringArrayTransform();
      }
      return matchArray(entry);
   }
   
   /**
    * This is used to match a Transform based on the
    * array component type of an object to be transformed. This will
    * attempt to match the transform using the fully qualified class
    * name of the array component type. If a transform can not be
    * found then this method will throw an exception.
    * 
    * @param entry this is the array component type to be matched
    * 
    * @throws Exception thrown if a transform can not be matched
    */
   private Transform matchArray(Class entry) throws Exception {            
      Transform transform = primary.match(entry);
     
      if(transform == null) {
         return null;
      }
      return new ArrayTransform(transform, entry);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy