1 23 24 package org.apache.slide.projector.processor.core; 25 26 import java.util.Map ; 27 28 import org.apache.slide.projector.ContentType; 29 import org.apache.slide.projector.Context; 30 import org.apache.slide.projector.Processor; 31 import org.apache.slide.projector.Result; 32 import org.apache.slide.projector.descriptor.MapValueDescriptor; 33 import org.apache.slide.projector.descriptor.ParameterDescriptor; 34 import org.apache.slide.projector.descriptor.ResultDescriptor; 35 import org.apache.slide.projector.descriptor.ResultEntryDescriptor; 36 import org.apache.slide.projector.descriptor.StateDescriptor; 37 import org.apache.slide.projector.descriptor.StringValueDescriptor; 38 import org.apache.slide.projector.i18n.DefaultMessage; 39 import org.apache.slide.projector.i18n.ParameterMessage; 40 import org.apache.slide.projector.processor.SimpleProcessor; 41 import org.apache.slide.projector.value.MapValue; 42 import org.apache.slide.projector.value.StringValue; 43 import org.apache.slide.projector.value.Value; 44 45 49 public class MapEntry implements Processor { 50 private final static String MAP = "map"; 51 private final static String KEY = "key"; 52 53 private final static String KEY_NOT_FOUND = "keyNotFound"; 54 55 private final static ParameterDescriptor[] parameterDescriptors = new ParameterDescriptor[] { 56 new ParameterDescriptor(MAP, new ParameterMessage("mapEntry/map"), MapValueDescriptor.ANY), 57 new ParameterDescriptor(KEY, new ParameterMessage("mapEntry/key"), new StringValueDescriptor()) 58 }; 59 60 private final static ResultDescriptor resultDescriptor = new ResultDescriptor( 61 new StateDescriptor[] { 62 StateDescriptor.OK_DESCRIPTOR, 63 new StateDescriptor(KEY_NOT_FOUND, new DefaultMessage("mapEntry/state/keyNotFound"))}, 64 new ResultEntryDescriptor[] { 65 new ResultEntryDescriptor(SimpleProcessor.OUTPUT, new DefaultMessage("mapEntry/output"), ContentType.DYNAMIC, false) 66 }); 67 68 public Result process(Map parameter, Context context) throws Exception { 69 Map map = ((MapValue)parameter.get(MAP)).getMap(); 70 String key = ((StringValue)parameter.get(KEY)).toString(); 71 Value entry = (Value)map.get(key); 72 if ( entry == null ) return new Result(KEY_NOT_FOUND); 73 return new Result(StateDescriptor.OK, SimpleProcessor.OUTPUT, entry); 74 } 75 76 public ParameterDescriptor[] getParameterDescriptors() { 77 return parameterDescriptors; 78 } 79 80 public ResultDescriptor getResultDescriptor() { 81 return resultDescriptor; 82 } 83 } 84 | Popular Tags |