1 package org.apache.slide.projector.processor; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 import java.util.logging.Logger ; 9 10 import org.apache.slide.projector.ConfigurationException; 11 import org.apache.slide.projector.Context; 12 import org.apache.slide.projector.Result; 13 import org.apache.slide.projector.descriptor.MapValueDescriptor; 14 import org.apache.slide.projector.descriptor.ParameterDescriptor; 15 import org.apache.slide.projector.descriptor.ResultDescriptor; 16 import org.apache.slide.projector.descriptor.ResultEntryDescriptor; 17 import org.apache.slide.projector.descriptor.StateDescriptor; 18 import org.apache.slide.projector.i18n.DefaultMessage; 19 import org.apache.slide.projector.i18n.ParameterMessage; 20 import org.apache.slide.projector.value.MapValue; 21 import org.apache.slide.projector.value.NullValue; 22 import org.apache.slide.projector.value.StreamableValue; 23 import org.apache.slide.projector.value.StringValue; 24 25 public class TemplateMapRenderer extends TemplateRenderer { 26 private static Logger logger = Logger.getLogger(TemplateMapRenderer.class.getName()); 27 28 private ParameterDescriptor[] parameterDescriptors; 29 30 public void configure(StreamableValue config) throws ConfigurationException { 31 super.configure(config); 32 MapValueDescriptor mapValueDescriptor = new MapValueDescriptor(); 33 List parameterDescriptorList = new ArrayList (); 34 for ( Iterator i = parameterDescriptions.iterator(); i.hasNext(); ) { 35 ParameterDescriptor entryDescriptor = (ParameterDescriptor)i.next(); 36 if ( entryDescriptor.getName() == FRAGMENT ) { 38 parameterDescriptorList.add(entryDescriptor); 39 } else { 40 if ( entryDescriptor.isRequired() ) { 41 mapValueDescriptor.addEntryDescriptor(new ParameterDescriptor(entryDescriptor.getName(), new ParameterMessage("templateMapRenderer/mapEntry"), entryDescriptor.getValueDescriptor())); 42 } else { 43 mapValueDescriptor.addEntryDescriptor(new ParameterDescriptor(entryDescriptor.getName(), new ParameterMessage("templateMapRenderer/mapEntry"), entryDescriptor.getValueDescriptor(), entryDescriptor.getDefaultValue() )); 44 parameterDescriptorList.add(new ParameterDescriptor(entryDescriptor.getName(), entryDescriptor.getDescription(), entryDescriptor.getValueDescriptor(), NullValue.NULL)); 45 } 46 } 47 } 48 parameterDescriptorList.add(new ParameterDescriptor(SimpleProcessor.INPUT, new ParameterMessage("templateMapRenderer/input"), mapValueDescriptor)); 49 parameterDescriptors = (ParameterDescriptor[])parameterDescriptorList.toArray(new ParameterDescriptor[parameterDescriptorList.size()]); 50 } 51 52 public Result process(Map parameter, Context context) throws Exception { 53 String fragment = ((StringValue)parameter.get(FRAGMENT)).toString(); 54 Template template = getRequiredFragment(fragment); 55 StringBuffer buffer = new StringBuffer (template.getLength()); 56 Map parameterMap = new HashMap (); 57 parameterMap.putAll(parameter); 58 for ( Iterator j = ((MapValue)parameter.get(SimpleProcessor.INPUT)).getMap().entrySet().iterator(); j.hasNext(); ) { 59 Map.Entry entry = (Map.Entry )j.next(); 60 if ( !(entry.getValue() instanceof NullValue) ) { 61 parameterMap.put(entry.getKey(), entry.getValue()); 62 } 63 } 64 template.evaluate(buffer, parameterMap); 65 return new Result(OK, OUTPUT, new StringValue(buffer.toString(), template.getContentType(), false)); 66 } 67 68 public ParameterDescriptor[] getParameterDescriptors() { 69 return parameterDescriptors; 70 } 71 72 public ResultDescriptor getResultDescriptor() { 73 return new ResultDescriptor( 74 new StateDescriptor[] { StateDescriptor.OK_DESCRIPTOR }, 75 new ResultEntryDescriptor[]{ 76 new ResultEntryDescriptor(OUTPUT, new DefaultMessage("templateMapRenderer/result/output"), "*", true) 77 }); 78 } 79 } | Popular Tags |