KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > TemplateMapRenderer


1 package org.apache.slide.projector.processor;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.logging.Logger JavaDoc;
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 JavaDoc 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 JavaDoc parameterDescriptorList = new ArrayList JavaDoc();
34         for ( Iterator JavaDoc i = parameterDescriptions.iterator(); i.hasNext(); ) {
35             ParameterDescriptor entryDescriptor = (ParameterDescriptor)i.next();
36             // add all map parameters to optional parameters
37
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 JavaDoc parameter, Context context) throws Exception JavaDoc {
53         String JavaDoc fragment = ((StringValue)parameter.get(FRAGMENT)).toString();
54         Template template = getRequiredFragment(fragment);
55         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(template.getLength());
56         Map JavaDoc parameterMap = new HashMap JavaDoc();
57         parameterMap.putAll(parameter);
58         for ( Iterator JavaDoc j = ((MapValue)parameter.get(SimpleProcessor.INPUT)).getMap().entrySet().iterator(); j.hasNext(); ) {
59             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)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