Clover coverage report - Code Coverage for hivemind release 1.0-beta-2
Coverage timestamp: Sun Aug 1 2004 14:03:45 EDT
file stats: LOC: 93   Methods: 3
NCLOC: 52   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
SmartTranslator.java 100% 100% 100% 100%
coverage
 1   
 //  Copyright 2004 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.hivemind.schema.rules;
 16   
 
 17   
 import java.beans.PropertyEditor;
 18   
 import java.beans.PropertyEditorManager;
 19   
 import java.util.Map;
 20   
 
 21   
 import org.apache.hivemind.ApplicationRuntimeException;
 22   
 import org.apache.hivemind.HiveMind;
 23   
 import org.apache.hivemind.Location;
 24   
 import org.apache.hivemind.internal.Module;
 25   
 import org.apache.hivemind.schema.Translator;
 26   
 
 27   
 /**
 28   
  * A "smart" translator that attempts to automatically convert from string types
 29   
  * to object or wrapper types, using {@link java.beans.PropertyEditor}s.
 30   
  *
 31   
  * @author Howard Lewis Ship
 32   
  */
 33   
 public class SmartTranslator implements Translator
 34   
 {
 35   
     private String _default;
 36   
 
 37  91
     public SmartTranslator()
 38   
     {
 39   
     }
 40   
 
 41   
     /**
 42   
      * Initializers:
 43   
      * <ul>
 44   
      * <li>default: default value for empty input
 45   
      * </ul>
 46   
      */
 47  1
     public SmartTranslator(String initializer)
 48   
     {
 49  1
         Map m = RuleUtils.convertInitializer(initializer);
 50   
 
 51  1
         _default = (String) m.get("default");
 52   
     }
 53   
 
 54  1822
     public Object translate(
 55   
         Module contributingModule,
 56   
         Class propertyType,
 57   
         String inputValue,
 58   
         Location location)
 59   
     {
 60  1822
         if (HiveMind.isBlank(inputValue))
 61   
         {
 62  3
             if (_default == null)
 63  2
                 return null;
 64   
 
 65  1
             inputValue = _default;
 66   
         }
 67   
 
 68  1820
         try
 69   
         {
 70  1820
             PropertyEditor e = PropertyEditorManager.findEditor(propertyType);
 71   
 
 72  1820
             if (e == null)
 73  1
                 throw new ApplicationRuntimeException(
 74   
                     RulesMessages.noPropertyEditor(propertyType),
 75   
                     location,
 76   
                     null);
 77   
 
 78  1819
             e.setAsText(inputValue);
 79   
 
 80  1819
             return e.getValue();
 81   
         }
 82   
         catch (Exception ex)
 83   
         {
 84  1
             throw new ApplicationRuntimeException(
 85   
                 RulesMessages.smartTranslatorError(inputValue, propertyType, ex),
 86   
                 location,
 87   
                 ex);
 88   
 
 89   
         }
 90   
     }
 91   
 
 92   
 }
 93