KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > core > Link


1 package org.apache.slide.projector.processor.core;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import org.apache.slide.projector.Constants;
8 import org.apache.slide.projector.Context;
9 import org.apache.slide.projector.HttpContext;
10 import org.apache.slide.projector.ProcessException;
11 import org.apache.slide.projector.Processor;
12 import org.apache.slide.projector.Result;
13 import org.apache.slide.projector.URI;
14 import org.apache.slide.projector.descriptor.BooleanValueDescriptor;
15 import org.apache.slide.projector.descriptor.MapValueDescriptor;
16 import org.apache.slide.projector.descriptor.ParameterDescriptor;
17 import org.apache.slide.projector.descriptor.ResultDescriptor;
18 import org.apache.slide.projector.descriptor.StateDescriptor;
19 import org.apache.slide.projector.descriptor.StringValueDescriptor;
20 import org.apache.slide.projector.descriptor.URIValueDescriptor;
21 import org.apache.slide.projector.i18n.ErrorMessage;
22 import org.apache.slide.projector.i18n.ParameterMessage;
23 import org.apache.slide.projector.processor.SimpleProcessor;
24 import org.apache.slide.projector.processor.process.Process;
25 import org.apache.slide.projector.value.BooleanValue;
26 import org.apache.slide.projector.value.MapValue;
27 import org.apache.slide.projector.value.NullValue;
28 import org.apache.slide.projector.value.StringValue;
29 import org.apache.slide.projector.value.Value;
30
31 public class Link implements Processor {
32     private final static String JavaDoc PROCESSOR = "processor";
33     private final static String JavaDoc CONTINUE = "continue";
34     private final static String JavaDoc PARAMETERS = "parameterS";
35     
36     private final static ParameterDescriptor[] parameterDescriptors = new ParameterDescriptor[] {
37             new ParameterDescriptor(PROCESSOR, new ParameterMessage("link/parameter/processor"), new URIValueDescriptor()),
38             new ParameterDescriptor(Process.STEP, new ParameterMessage("link/parameter/step"), new StringValueDescriptor(), new NullValue()),
39             new ParameterDescriptor(CONTINUE, new ParameterMessage("link/parameter/continue"), new BooleanValueDescriptor(), BooleanValue.TRUE),
40             new ParameterDescriptor(PARAMETERS, new ParameterMessage("link/parameter/parameters"), MapValueDescriptor.ANY, new MapValue(new HashMap JavaDoc()))
41     };
42     private final static ResultDescriptor resultDescriptor = new ResultDescriptor(new StateDescriptor[] { StateDescriptor.OK_DESCRIPTOR });
43     
44     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
45         URI uri = (URI)parameter.get(PROCESSOR);
46         boolean continueProcess = ((BooleanValue)parameter.get(CONTINUE)).booleanValue();
47         MapValue parameterMap = (MapValue)parameter.get(PARAMETERS);
48         Value step = (Value)parameter.get(Process.STEP);
49         StringBuffer JavaDoc linkBuffer = new StringBuffer JavaDoc(128);
50         if ( context instanceof HttpContext ) {
51             linkBuffer.append(((HttpContext)context).getContextPath()).append(uri);
52             boolean first = true;
53             if ( continueProcess ) {
54                 parameterMap.getMap().put(Constants.PROCESS_ID_PARAMETER, context.getProcessId());
55             }
56             if ( step != null ) {
57                 parameterMap.getMap().put(Process.STEP, step);
58             }
59             for ( Iterator JavaDoc i = parameterMap.getMap().entrySet().iterator(); i.hasNext(); ) {
60                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
61                 if ( first ) {
62                     linkBuffer.append('?');
63                     first = false;
64                 } else {
65                     linkBuffer.append('&');
66                 }
67                 linkBuffer.append(entry.getKey());
68                 linkBuffer.append('=');
69                 linkBuffer.append(entry.getValue());
70             }
71         } else {
72             throw new ProcessException(new ErrorMessage("httpContextRequired"));
73         }
74         return new Result(StateDescriptor.OK, SimpleProcessor.OUTPUT, new StringValue(linkBuffer.toString()));
75     }
76
77     public ParameterDescriptor[] getParameterDescriptors() {
78         return parameterDescriptors;
79     }
80
81     public ResultDescriptor getResultDescriptor() {
82         return resultDescriptor;
83     }
84 }
85
Popular Tags