KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > table > TableArrayRenderer


1 package org.apache.slide.projector.processor.table;
2
3 import java.util.Map JavaDoc;
4
5 import org.apache.slide.projector.ConfigurationException;
6 import org.apache.slide.projector.Context;
7 import org.apache.slide.projector.ProcessException;
8 import org.apache.slide.projector.Result;
9 import org.apache.slide.projector.descriptor.NumberValueDescriptor;
10 import org.apache.slide.projector.descriptor.ParameterDescriptor;
11 import org.apache.slide.projector.i18n.ErrorMessage;
12 import org.apache.slide.projector.i18n.ParameterMessage;
13 import org.apache.slide.projector.processor.TemplateArrayRenderer;
14 import org.apache.slide.projector.value.NumberValue;
15 import org.apache.slide.projector.value.StreamableValue;
16 import org.apache.slide.projector.value.StringValue;
17
18 public class TableArrayRenderer extends TemplateArrayRenderer {
19     final private static String JavaDoc HEADER_FRAGMENT = "header";
20     final private static String JavaDoc FOOTER_FRAGMENT = "footer";
21     final private static String JavaDoc FIRST_FRAGMENT = "first";
22     final private static String JavaDoc LAST_FRAGMENT = "last";
23     final private static String JavaDoc EVEN_FRAGMENT = "even";
24     final private static String JavaDoc DEFAULT_FRAGMENT = "default";
25     final private static String JavaDoc EMPTY_FRAGMENT = "empty";
26
27     final private static String JavaDoc OFFSET = "offset";
28     final private static String JavaDoc LENGTH = "length";
29
30     private Template headerTemplate;
31     private Template footerTemplate;
32     private Template firstTemplate;
33     private Template lastTemplate;
34     private Template evenTemplate;
35     private Template defaultTemplate;
36     private Template emptyTemplate;
37
38     private ParameterDescriptor []parameterDescriptors;
39
40     public TableArrayRenderer() {
41         setRequiredFragments(new String JavaDoc[] { DEFAULT_FRAGMENT });
42         setOptionalFragments(new String JavaDoc[] { EMPTY_FRAGMENT, HEADER_FRAGMENT, FOOTER_FRAGMENT, FIRST_FRAGMENT, LAST_FRAGMENT, EVEN_FRAGMENT });
43     }
44
45     public void configure(StreamableValue config) throws ConfigurationException {
46         super.configure(config);
47         ParameterDescriptor []parentParameterDescriptors = super.getParameterDescriptors();
48         // remove fragment parameter
49
parameterDescriptors = new ParameterDescriptor[parentParameterDescriptors.length+1];
50         int index = 0;
51         for ( int i = 0; i < parentParameterDescriptors.length; i++ ) {
52             ParameterDescriptor descriptor = parentParameterDescriptors[i];
53             if ( descriptor.getName() != FRAGMENT ) {
54                 parameterDescriptors[index] = parentParameterDescriptors[i];
55                 index++;
56             }
57         }
58         parameterDescriptors[parentParameterDescriptors.length-1] = new ParameterDescriptor(OFFSET, new ParameterMessage("tableRenderer/offset"), new NumberValueDescriptor(), new NumberValue(new Integer JavaDoc(0)));
59         parameterDescriptors[parentParameterDescriptors.length] = new ParameterDescriptor(LENGTH, new ParameterMessage("tableRenderer/length"), new NumberValueDescriptor(), new NumberValue(new Integer JavaDoc(0)));
60
61         headerTemplate = getOptionalFragment(HEADER_FRAGMENT);
62         footerTemplate = getOptionalFragment(FOOTER_FRAGMENT);
63         firstTemplate = getOptionalFragment(FIRST_FRAGMENT);
64         lastTemplate = getOptionalFragment(LAST_FRAGMENT);
65         evenTemplate = getOptionalFragment(EVEN_FRAGMENT);
66         emptyTemplate = getOptionalFragment(EMPTY_FRAGMENT);
67         try {
68             defaultTemplate = getRequiredFragment(DEFAULT_FRAGMENT);
69         } catch ( ProcessException exception ) {
70             throw new ConfigurationException(new ErrorMessage("tableRenderer/fragmentsMissing"), exception);
71         }
72     }
73
74     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
75         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(1024);
76         int offset = ((NumberValue)parameter.get(OFFSET)).getNumber().intValue();
77         int length = ((NumberValue)parameter.get(LENGTH)).getNumber().intValue();
78         parameter.remove(OFFSET);
79         parameter.remove(LENGTH);
80         int maxIndex = getMaxIndex(parameter);
81         offset = Math.min(offset, maxIndex);
82         if ( length > 0 ) {
83             length = Math.min(length, maxIndex-offset);
84         } else {
85             length = maxIndex-offset;
86         }
87         if ( maxIndex == 0 ) {
88             emptyTemplate.evaluate(buffer, parameter);
89         } else {
90             if ( headerTemplate != null ) headerTemplate.evaluate(buffer, parameter);
91             for ( int i = 0; i < length; i++ ) {
92                 if ( i == 0 && firstTemplate != null ) {
93                     firstTemplate.evaluate(buffer, parameter, i+offset);
94                 } else if ( i == length -1 && lastTemplate != null ) {
95                     lastTemplate.evaluate(buffer, parameter, i+offset);
96                 } else if ( evenTemplate != null && i%2 == 0 ) {
97                     evenTemplate.evaluate(buffer, parameter, i+offset);
98                 } else {
99                     defaultTemplate.evaluate(buffer, parameter, i+offset);
100                 }
101             }
102             if ( footerTemplate != null ) footerTemplate.evaluate(buffer, parameter);
103         }
104         return new Result(OK, OUTPUT, new StringValue(buffer.toString(), defaultTemplate.getContentType(), false ));
105     }
106
107     public ParameterDescriptor[] getParameterDescriptors() {
108         return parameterDescriptors;
109     }
110 }
Popular Tags