1 23 24 package org.apache.slide.projector.processor.tree; 25 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 35 import org.apache.slide.projector.Context; 36 import org.apache.slide.projector.EnvironmentConsumer; 37 import org.apache.slide.projector.Processor; 38 import org.apache.slide.projector.Result; 39 import org.apache.slide.projector.Store; 40 import org.apache.slide.projector.descriptor.LocaleValueDescriptor; 41 import org.apache.slide.projector.descriptor.ParameterDescriptor; 42 import org.apache.slide.projector.descriptor.RequiredEnvironmentDescriptor; 43 import org.apache.slide.projector.descriptor.ResultDescriptor; 44 import org.apache.slide.projector.descriptor.ResultEntryDescriptor; 45 import org.apache.slide.projector.descriptor.StateDescriptor; 46 import org.apache.slide.projector.descriptor.StringValueDescriptor; 47 import org.apache.slide.projector.descriptor.TreeValueDescriptor; 48 import org.apache.slide.projector.engine.ProcessorManager; 49 import org.apache.slide.projector.i18n.DefaultMessage; 50 import org.apache.slide.projector.i18n.MessageManager; 51 import org.apache.slide.projector.i18n.MessageNotFoundException; 52 import org.apache.slide.projector.i18n.ParameterMessage; 53 import org.apache.slide.projector.processor.SimpleProcessor; 54 import org.apache.slide.projector.value.ArrayValue; 55 import org.apache.slide.projector.value.LocaleValue; 56 import org.apache.slide.projector.value.MapValue; 57 import org.apache.slide.projector.value.StringValue; 58 import org.apache.slide.projector.value.Value; 59 60 64 public class TreeLocalizer implements Processor, EnvironmentConsumer { 65 private static Logger logger = Logger.getLogger(TreeLocalizer.class.getName()); 66 67 public final static String LOCALE = "locale"; 68 69 protected final static String IMAGE_IDENTIFIER = "image"; 70 71 private final static ParameterDescriptor[] parameterDescriptors = new ParameterDescriptor[] { 72 new ParameterDescriptor(SimpleProcessor.INPUT, new ParameterMessage("treeLocalizer/input"), new TreeValueDescriptor(new StringValueDescriptor())) 73 }; 74 75 private final static ResultDescriptor resultDescriptor = new ResultDescriptor( 76 new StateDescriptor[] { StateDescriptor.OK_DESCRIPTOR }, 77 new ResultEntryDescriptor[] { 78 new ResultEntryDescriptor(SimpleProcessor.OUTPUT, new DefaultMessage("treeLocalizer/output"), ArrayValue.CONTENT_TYPE, false) 79 }); 80 81 private final static RequiredEnvironmentDescriptor[] requiredEnvironmentDescriptors = new RequiredEnvironmentDescriptor[] { 82 new RequiredEnvironmentDescriptor(LOCALE, Store.SESSION, new DefaultMessage("treeLocalized/requiredEnvironment/locale"), new LocaleValueDescriptor(), new LocaleValue(Locale.getDefault())) 83 }; 84 85 public Result process(Map parameter, Context context) throws Exception { 86 Value []resources = ((ArrayValue)parameter.get(SimpleProcessor.INPUT)).getArray(); 87 Locale locale = ((LocaleValue)context.getStore(Store.SESSION).get(LOCALE)).getLocale(); 88 return new Result(StateDescriptor.OK, SimpleProcessor.OUTPUT, localize(resources, locale, context)); 89 } 90 91 private ArrayValue localize(Value []resources, Locale locale, Context context) throws Exception { 92 List localizedResources = new ArrayList (); 93 for ( int i = 0; i < resources.length; i++ ) { 94 if ( resources[i] instanceof StringValue ) { 95 Map localizedMap = new HashMap (); 96 try { 97 localizedMap.put(TreeRenderer.ID, ProcessorManager.getInstance().process(ProcessorManager.URL, resources[i], context)); 98 Map entries = MessageManager.getEntries(((StringValue)resources[i]).toString(), locale); 99 for ( Iterator j = entries.entrySet().iterator(); j.hasNext(); ) { 100 Map.Entry entry = (Map.Entry )j.next(); 101 String key = (String )entry.getKey(); 102 String value = (String )entry.getValue(); 103 if ( key.startsWith(IMAGE_IDENTIFIER) ) { 104 key = key.substring(IMAGE_IDENTIFIER.length()+1); 105 localizedMap.put(key, ProcessorManager.getInstance().process(ProcessorManager.BINARY, value, "url", context)); 106 } else { 107 localizedMap.put(key, new StringValue(value)); 108 } 109 } 110 } catch ( MessageNotFoundException exception ) { 111 logger.log(Level.SEVERE, "No message found while localizing tree", exception); 112 } 113 localizedResources.add(new MapValue(localizedMap)); 114 } else if ( resources[i] instanceof ArrayValue ) { 115 localizedResources.add(localize(((ArrayValue)resources[i]).getArray(), locale, context)); 116 } 117 } 118 Value[] localizedResourceArray = new Value[localizedResources.size()]; 119 return new ArrayValue((Value [])localizedResources.toArray(localizedResourceArray)); 120 } 121 122 public ParameterDescriptor[] getParameterDescriptors() { 123 return parameterDescriptors; 124 } 125 126 public ResultDescriptor getResultDescriptor() { 127 return resultDescriptor; 128 } 129 130 public RequiredEnvironmentDescriptor[] getRequiredEnvironmentDescriptors() { 131 return requiredEnvironmentDescriptors; 132 } 133 } 134 | Popular Tags |