1 15 package org.apache.hivemind.schema.rules; 16 17 import java.lang.reflect.Field ; 18 import java.util.Map ; 19 20 import org.apache.hivemind.ApplicationRuntimeException; 21 import org.apache.hivemind.HiveMind; 22 import org.apache.hivemind.Location; 23 import org.apache.hivemind.internal.Module; 24 import org.apache.hivemind.schema.Translator; 25 26 34 public class EnumerationTranslator implements Translator 35 { 36 private Map _mappings; 37 private String _className; 38 private Class _class; 39 40 45 public EnumerationTranslator(String initializer) 46 { 47 int commax = initializer.indexOf(','); 48 49 _className = initializer.substring(0, commax); 50 51 _mappings = RuleUtils.convertInitializer(initializer.substring(commax + 1)); 52 } 53 54 private synchronized Class getClass(Module contributingModule) 55 { 56 if (_class == null) 57 _class = contributingModule.resolveType(_className); 58 59 return _class; 60 } 61 62 public Object translate( 63 Module contributingModule, 64 Class propertyType, 65 String inputValue, 66 Location location) 67 { 68 if (HiveMind.isBlank(inputValue)) 69 return null; 70 71 Class c = getClass(contributingModule); 72 73 String fieldName = (String ) _mappings.get(inputValue); 74 75 if (fieldName == null) 76 throw new ApplicationRuntimeException( 77 RulesMessages.enumNotRecognized(inputValue), 78 location, 79 null); 80 81 try 82 { 83 Field f = c.getField(fieldName); 84 85 return f.get(null); 86 } 87 catch (Exception ex) 88 { 89 throw new ApplicationRuntimeException( 90 RulesMessages.enumError(c, fieldName, ex), 91 location, 92 ex); 93 } 94 } 95 96 } 97 | Popular Tags |