KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.slide.projector.processor;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.logging.Logger JavaDoc;
6
7 import org.apache.slide.projector.ConfigurationException;
8 import org.apache.slide.projector.Context;
9 import org.apache.slide.projector.ProcessException;
10 import org.apache.slide.projector.Result;
11 import org.apache.slide.projector.descriptor.ArrayValueDescriptor;
12 import org.apache.slide.projector.descriptor.ParameterDescriptor;
13 import org.apache.slide.projector.descriptor.ResultDescriptor;
14 import org.apache.slide.projector.descriptor.ResultEntryDescriptor;
15 import org.apache.slide.projector.descriptor.StateDescriptor;
16 import org.apache.slide.projector.i18n.DefaultMessage;
17 import org.apache.slide.projector.i18n.ErrorMessage;
18 import org.apache.slide.projector.i18n.ParameterMessage;
19 import org.apache.slide.projector.value.ArrayValue;
20 import org.apache.slide.projector.value.StreamableValue;
21 import org.apache.slide.projector.value.StringValue;
22
23 public class TemplateArrayRenderer extends TemplateRenderer {
24     private static Logger JavaDoc logger = Logger.getLogger(TemplateArrayRenderer.class.getName());
25
26     private ParameterDescriptor[] parameterDescriptors;
27
28     public void configure(StreamableValue config) throws ConfigurationException {
29         super.configure(config);
30         parameterDescriptors = new ParameterDescriptor[parameterDescriptions.size()];
31         int counter = 0;
32         for ( Iterator JavaDoc i = parameterDescriptions.iterator(); i.hasNext(); ) {
33             ParameterDescriptor entryDescriptor = (ParameterDescriptor)i.next();
34             if ( entryDescriptor.getName() == FRAGMENT ) {
35                 parameterDescriptors[counter] = entryDescriptor;
36             } else {
37                 parameterDescriptors[counter] = new ParameterDescriptor(entryDescriptor.getName(), new ParameterMessage("templateArrayRenderer/parameter"), new ArrayValueDescriptor(entryDescriptor.getValueDescriptor()));
38             }
39             counter++;
40         }
41     }
42
43     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
44         String JavaDoc fragment = ((StringValue)parameter.get(FRAGMENT)).toString();
45         Template template = getRequiredFragment(fragment);
46         parameter.remove(FRAGMENT);
47         if ( template == null ) throw new ProcessException(new ErrorMessage("templateArrayRenderer/fragmentNotFound", new String JavaDoc[] { fragment }));
48         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(template.getLength());
49         for ( int i = 0; i < getMaxIndex(parameter); i++ ) {
50             template.evaluate(buffer, parameter, i);
51         }
52         return new Result(OK, OUTPUT, new StringValue(buffer.toString(), template.getContentType(), false));
53     }
54
55     public ParameterDescriptor[] getParameterDescriptors() {
56         return parameterDescriptors;
57     }
58
59     public ResultDescriptor getResultDescriptor() {
60         return new ResultDescriptor(
61                 new StateDescriptor[] { StateDescriptor.OK_DESCRIPTOR },
62                 new ResultEntryDescriptor[]{
63                     new ResultEntryDescriptor(OUTPUT, new DefaultMessage("templateArrayRenderer/result/output"), "*", true)
64                 });
65     }
66
67     protected int getMaxIndex(Map JavaDoc parameter) {
68         int maxIndex = 0;
69         for ( Iterator JavaDoc i = parameter.values().iterator(); i.hasNext(); ) {
70             ArrayValue resurce = (ArrayValue)i.next();
71             if ( resurce.getArray().length > maxIndex ) maxIndex = resurce.getArray().length;
72         }
73         return maxIndex;
74     }
75 }
Popular Tags