1 package org.apache.slide.projector.processor; 2 3 import java.util.Locale ; 4 import java.util.Map ; 5 6 import org.apache.slide.projector.ContentType; 7 import org.apache.slide.projector.Context; 8 import org.apache.slide.projector.EnvironmentConsumer; 9 import org.apache.slide.projector.Processor; 10 import org.apache.slide.projector.Result; 11 import org.apache.slide.projector.Store; 12 import org.apache.slide.projector.descriptor.ArrayValueDescriptor; 13 import org.apache.slide.projector.descriptor.LocaleValueDescriptor; 14 import org.apache.slide.projector.descriptor.ParameterDescriptor; 15 import org.apache.slide.projector.descriptor.RequiredEnvironmentDescriptor; 16 import org.apache.slide.projector.descriptor.ResultDescriptor; 17 import org.apache.slide.projector.descriptor.ResultEntryDescriptor; 18 import org.apache.slide.projector.descriptor.StateDescriptor; 19 import org.apache.slide.projector.descriptor.StringValueDescriptor; 20 import org.apache.slide.projector.i18n.DefaultMessage; 21 import org.apache.slide.projector.i18n.LocalizedMessage; 22 import org.apache.slide.projector.i18n.MessageManager; 23 import org.apache.slide.projector.i18n.ParameterMessage; 24 import org.apache.slide.projector.value.ArrayValue; 25 import org.apache.slide.projector.value.LocaleValue; 26 import org.apache.slide.projector.value.StringValue; 27 import org.apache.slide.projector.value.Value; 28 29 public class Message implements Processor, EnvironmentConsumer { 30 private final static String ID = "id"; 31 private final static String KEY = "key"; 32 private final static String ARGUMENTS = "arguments"; 33 private final static String LOCALE = "locale"; 34 35 private final static StringValue DEFAULT_KEY = new StringValue("text"); 36 37 private final static ParameterDescriptor[] parameterDescriptors = new ParameterDescriptor[]{ 38 new ParameterDescriptor(ID, new ParameterMessage("message/parameter/id"), new StringValueDescriptor()), 39 new ParameterDescriptor(KEY, new ParameterMessage("message/parameter/key"), new StringValueDescriptor(), DEFAULT_KEY), 40 new ParameterDescriptor(ARGUMENTS, new ParameterMessage("message/parameter/arguments"), new ArrayValueDescriptor(new StringValueDescriptor()), new ArrayValue(new StringValue[] { new StringValue("") })) 41 }; 42 43 private final static RequiredEnvironmentDescriptor[] requiredEnvironmentDescriptors = new RequiredEnvironmentDescriptor[] { 44 new RequiredEnvironmentDescriptor(LOCALE, Store.SESSION, new DefaultMessage("message/requiredEnvironment/locale"), new LocaleValueDescriptor(), new LocaleValue(Locale.getDefault())) 45 }; 46 47 private final static ResultDescriptor resultDescriptor = new ResultDescriptor( 48 new StateDescriptor[]{ StateDescriptor.OK_DESCRIPTOR }, 49 new ResultEntryDescriptor[]{new ResultEntryDescriptor( 50 SimpleProcessor.OUTPUT, new LocalizedMessage("message/result/output"), 51 ContentType.PLAIN_TEXT, true)}); 52 53 public Result process(Map parameter, Context context) throws Exception { 54 String id = parameter.get(ID).toString(); 55 String key = parameter.get(KEY).toString(); 56 Value[] arguments = ((ArrayValue)parameter.get(ARGUMENTS)).getArray(); 57 Locale locale = ((LocaleValue)context.getStore(Store.SESSION).get(LOCALE)).getLocale(); 58 return new Result(StateDescriptor.OK, SimpleProcessor.OUTPUT, new StringValue(MessageManager.getText(id, key, arguments, locale, id))); 59 } 60 61 public ParameterDescriptor[] getParameterDescriptors() { 62 return parameterDescriptors; 63 } 64 65 public ResultDescriptor getResultDescriptor() { 66 return resultDescriptor; 67 } 68 69 public RequiredEnvironmentDescriptor[] getRequiredEnvironmentDescriptors() { 70 return requiredEnvironmentDescriptors; 71 } 72 } | Popular Tags |