KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > form > Control


1 package org.apache.slide.projector.processor.form;
2
3 import java.util.Map JavaDoc;
4
5 import org.apache.slide.projector.ConfigurationException;
6 import org.apache.slide.projector.ContentType;
7 import org.apache.slide.projector.Context;
8 import org.apache.slide.projector.ProcessException;
9 import org.apache.slide.projector.Processor;
10 import org.apache.slide.projector.Result;
11 import org.apache.slide.projector.URI;
12 import org.apache.slide.projector.descriptor.AnyValueDescriptor;
13 import org.apache.slide.projector.descriptor.ParameterDescriptor;
14 import org.apache.slide.projector.descriptor.ResultDescriptor;
15 import org.apache.slide.projector.descriptor.ResultEntryDescriptor;
16 import org.apache.slide.projector.descriptor.StateDescriptor;
17 import org.apache.slide.projector.descriptor.StringValueDescriptor;
18 import org.apache.slide.projector.descriptor.URIValueDescriptor;
19 import org.apache.slide.projector.engine.ProcessorManager;
20 import org.apache.slide.projector.i18n.DefaultMessage;
21 import org.apache.slide.projector.i18n.ErrorMessage;
22 import org.apache.slide.projector.i18n.ParameterMessage;
23 import org.apache.slide.projector.processor.TemplateRenderer;
24 import org.apache.slide.projector.util.ProcessorHelper;
25 import org.apache.slide.projector.value.NullValue;
26 import org.apache.slide.projector.value.StreamableValue;
27 import org.apache.slide.projector.value.StringValue;
28 import org.apache.slide.projector.value.URIValue;
29
30 /**
31  * @version $Revision: 1.4 $
32  */

33
34 public class Control extends TemplateRenderer {
35     private final static String JavaDoc NAME = " control";
36
37     public final static String JavaDoc OPTIONAL_VALID_CONTROL = "valid optional";
38     public final static String JavaDoc OPTIONAL_INVALID_CONTROL = "invalid optional";
39     public final static String JavaDoc OPTIONAL_CONTROL = "optional";
40     public final static String JavaDoc REQUIRED_CONTROL = "required";
41     public final static String JavaDoc REQUIRED_VALID_CONTROL = "valid required";
42     public final static String JavaDoc REQUIRED_INVALID_CONTROL = "invalid required";
43
44     public final static String JavaDoc ACTION = "action";
45     public final static String JavaDoc PARAMETER = "parameter";
46     public final static String JavaDoc VALUE = "value";
47     public final static String JavaDoc STATE = "state";
48
49     protected Template optionalFragment, requiredFragment, validOptionalFragment, invalidOptionalFragment, validRequiredFragment, invalidRequiredFragment;
50
51     private ParameterDescriptor[] parameterDescriptors;
52     private final static ResultDescriptor resultDescriptor = new ResultDescriptor(
53             new StateDescriptor[] { StateDescriptor.OK_DESCRIPTOR },
54             new ResultEntryDescriptor[]{
55                 new ResultEntryDescriptor(OUTPUT, new DefaultMessage("control/output"), ContentType.DYNAMIC, true)
56             });
57
58     public Control() {
59         setRequiredFragments(new String JavaDoc[] { OPTIONAL_CONTROL+getName(), REQUIRED_CONTROL+getName() });
60         setOptionalFragments(new String JavaDoc[] { OPTIONAL_VALID_CONTROL+getName(), OPTIONAL_INVALID_CONTROL+getName(),
61                                             REQUIRED_VALID_CONTROL+getName(), REQUIRED_INVALID_CONTROL+getName() });
62     }
63
64     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
65         String JavaDoc state = parameter.get(STATE).toString();
66         Template fragment = optionalFragment;
67         if ( state.equals(REQUIRED_CONTROL) ) {
68             fragment = requiredFragment;
69         }
70         if ( state.equals(OPTIONAL_VALID_CONTROL) ) {
71             fragment = validOptionalFragment;
72         } else if ( state.equals(OPTIONAL_INVALID_CONTROL) ) {
73             fragment = invalidOptionalFragment;
74         } else if ( state.equals(REQUIRED_VALID_CONTROL) ) {
75             fragment = validRequiredFragment;
76         } else if ( state.equals(REQUIRED_INVALID_CONTROL) ) {
77             fragment = invalidRequiredFragment;
78         }
79         return new Result(state, OUTPUT, renderFragment(fragment, parameter));
80     }
81
82     public void configure(StreamableValue config) throws ConfigurationException {
83         super.configure(config);
84         ParameterDescriptor[] parentParameterDescriptors = super.getParameterDescriptors();
85         parameterDescriptors = new ParameterDescriptor[parentParameterDescriptors.length + 3];
86         int counter = 0;
87         for ( int i = 0; i < parentParameterDescriptors.length; i++ ) {
88             if (!parentParameterDescriptors[i].getName().equals(FRAGMENT)) {
89                 parameterDescriptors[counter] = parentParameterDescriptors[i];
90                 counter++;
91             }
92         }
93         parameterDescriptors[parentParameterDescriptors.length - 1] =
94                 new ParameterDescriptor(ACTION, new ParameterMessage("control/action"), new URIValueDescriptor());
95         parameterDescriptors[parentParameterDescriptors.length ] =
96                 new ParameterDescriptor(PARAMETER, new ParameterMessage("control/parameter"), new URIValueDescriptor());
97         parameterDescriptors[parentParameterDescriptors.length + 1] =
98                 new ParameterDescriptor(VALUE, new ParameterMessage("control/value"), new AnyValueDescriptor(), new NullValue());
99         parameterDescriptors[parentParameterDescriptors.length + 2] =
100                 new ParameterDescriptor(STATE, new ParameterMessage("control/state"), new StringValueDescriptor(new String JavaDoc[] {OPTIONAL_VALID_CONTROL, OPTIONAL_INVALID_CONTROL, OPTIONAL_CONTROL, REQUIRED_CONTROL, REQUIRED_VALID_CONTROL, REQUIRED_INVALID_CONTROL}), new StringValue(OPTIONAL));
101
102         try {
103             optionalFragment = getRequiredFragment(OPTIONAL_CONTROL + getName());
104             requiredFragment = getRequiredFragment(REQUIRED_CONTROL + getName());
105         } catch ( ProcessException exception ) {
106             throw new ConfigurationException(new ErrorMessage("control/requiredFragmentMissing", new String JavaDoc[] { OPTIONAL+getName()}), exception);
107         }
108         validOptionalFragment = getOptionalFragment(OPTIONAL_VALID_CONTROL + getName(), optionalFragment);
109         invalidOptionalFragment = getOptionalFragment(OPTIONAL_INVALID_CONTROL + getName(), optionalFragment);
110         validRequiredFragment = getOptionalFragment(REQUIRED_VALID_CONTROL + getName(), requiredFragment);
111         invalidRequiredFragment = getOptionalFragment(REQUIRED_INVALID_CONTROL + getName(), requiredFragment);
112     }
113
114     public ParameterDescriptor[] getParameterDescriptors() {
115         return parameterDescriptors;
116     }
117
118     public ResultDescriptor getResultDescriptor() {
119         return resultDescriptor;
120     }
121
122     static ParameterDescriptor getParameterDescriptor(Map JavaDoc parameter, Context context) throws ProcessException {
123         URI actionUri = (URIValue)parameter.get(ACTION);
124         Processor action = ProcessorManager.getInstance().getProcessor(actionUri);
125         String JavaDoc parameterName = parameter.get(PARAMETER).toString();
126         ParameterDescriptor parameterDescriptor = ProcessorHelper.getParameterDescriptor(action, parameterName);
127         if ( parameterDescriptor == null ) throw new ProcessException(new ErrorMessage("control/actionParameterNotFound", new Object JavaDoc[] { parameterName, actionUri }));
128         return parameterDescriptor;
129     }
130
131     protected String JavaDoc getName() {
132         return NAME;
133     }
134 }
Popular Tags