Clover coverage report - Code Coverage for hivemind release 1.0-rc-1
Coverage timestamp: Wed Aug 25 2004 13:06:02 EDT
file stats: LOC: 92   Methods: 3
NCLOC: 51   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.Location;
 23   
 import org.apache.hivemind.internal.Module;
 24   
 import org.apache.hivemind.schema.Translator;
 25   
 
 26   
 /**
 27   
  * A "smart" translator that attempts to automatically convert from string types
 28   
  * to object or wrapper types, using {@link java.beans.PropertyEditor}s.
 29   
  *
 30   
  * @author Howard Lewis Ship
 31   
  */
 32   
 public class SmartTranslator implements Translator
 33   
 {
 34   
     private String _default;
 35   
 
 36  105
     public SmartTranslator()
 37   
     {
 38   
     }
 39   
 
 40   
     /**
 41   
      * Initializers:
 42   
      * <ul>
 43   
      * <li>default: default value for empty input
 44   
      * </ul>
 45   
      */
 46  1
     public SmartTranslator(String initializer)
 47   
     {
 48  1
         Map m = RuleUtils.convertInitializer(initializer);
 49   
 
 50  1
         _default = (String) m.get("default");
 51   
     }
 52   
 
 53  2763
     public Object translate(
 54   
         Module contributingModule,
 55   
         Class propertyType,
 56   
         String inputValue,
 57   
         Location location)
 58   
     {
 59  2763
         if (inputValue == null)
 60   
         {
 61  3
             if (_default == null)
 62  2
                 return null;
 63   
 
 64  1
             inputValue = _default;
 65   
         }
 66   
 
 67  2761
         try
 68   
         {
 69  2761
             PropertyEditor e = PropertyEditorManager.findEditor(propertyType);
 70   
 
 71  2761
             if (e == null)
 72  1
                 throw new ApplicationRuntimeException(
 73   
                     RulesMessages.noPropertyEditor(propertyType),
 74   
                     location,
 75   
                     null);
 76   
 
 77  2760
             e.setAsText(inputValue);
 78   
 
 79  2760
             return e.getValue();
 80   
         }
 81   
         catch (Exception ex)
 82   
         {
 83  1
             throw new ApplicationRuntimeException(
 84   
                 RulesMessages.smartTranslatorError(inputValue, propertyType, ex),
 85   
                 location,
 86   
                 ex);
 87   
 
 88   
         }
 89   
     }
 90   
 
 91   
 }
 92