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

xgi.ut.dsl.interpreters.ConversionConfigInterpreter.scala Maven / Gradle / Ivy

The newest version!
/*
 *   ScenarLang a domain specific language that attempts to make it even easier
 *   to write unit tests with mockito
 *    
 *   Copyright (C) 2012  X. Gillard 
 *   
 *   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; either
 *   version 2.1 of the License, or (at your option) any later version.
 *   
 *   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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package xgi.ut.dsl.interpreters

import scala.collection.mutable.HashMap

import xgi.ut.dsl.ConversionConfigParsers._

/**
 * This trait handles the logic to instanciate or parse an object 
 */
trait ConversionConfigInterpreter {
  //import java.lang.Class._
  import xgi.ut.dsl.utl.ReflectionUtil._
  /* keeps track of all the aliases */
  val aliases = HashMap.empty[String, String]
  /* keeps track of all the conversion mapping that have been recorded */
  val conversions = HashMap.empty[Class[_], Class[_]]
  
  def addConfig(conf : TypesConfig) = conf match {
    case x:ConversionConfig => addConversion(x)
    case y:AliasConfig => addAlias(y)
    case _ => 
  }
  
  /* adds an entry to the configuration config */
  def addConversion(conf : ConversionConfig) = conversions += forName(conf.typeName) -> forName(conf.parserClass)
  
  /* adds an entry to the alias config */
  def addAlias(conf : AliasConfig) = aliases += conf.aliasType -> conf.typeName
  
  def forName(name:String):Class[_] = {
    val typeName = aliases.getOrElse(name, {name})
    Class.forName(typeName)
  }
  
  /* attempts to convert an object using the conversion table */
  def convert(text:String, clazz:Class[_]):Any = {
    conversions get clazz match {
      case Some(converter) => {
        invokeConversion(text, converter);
      }
      case None => text 
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy