KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/projector/src/java/org/apache/slide/projector/processor/Router.java,v 1.3 2004/07/28 09:48:18 ib Exp $
3  * $Revision: 1.3 $
4  * $Date: 2004/07/28 09:48:18 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.projector.processor;
25
26 import java.io.InputStream JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33
34 import org.apache.slide.projector.ConfigurableProcessor;
35 import org.apache.slide.projector.ConfigurationException;
36 import org.apache.slide.projector.Context;
37 import org.apache.slide.projector.Result;
38 import org.apache.slide.projector.descriptor.ParameterDescriptor;
39 import org.apache.slide.projector.descriptor.ResultDescriptor;
40 import org.apache.slide.projector.descriptor.StateDescriptor;
41 import org.apache.slide.projector.descriptor.StringValueDescriptor;
42 import org.apache.slide.projector.i18n.DefaultMessage;
43 import org.apache.slide.projector.i18n.ParameterMessage;
44 import org.apache.slide.projector.value.StreamableValue;
45 import org.apache.slide.projector.value.StringValue;
46 import org.xml.sax.InputSource JavaDoc;
47 import org.xml.sax.helpers.AttributesImpl JavaDoc;
48
49 import de.zeigermann.xml.simpleImporter.DefaultSimpleImportHandler;
50 import de.zeigermann.xml.simpleImporter.SimpleImporter;
51 import de.zeigermann.xml.simpleImporter.SimplePath;
52
53 /**
54  * The Router class
55  *
56  */

57 public class Router implements ConfigurableProcessor {
58     private static Logger JavaDoc logger = Logger.getLogger(Router.class.getName());
59
60     private static ResultDescriptor resultDescriptor;
61     private static ParameterDescriptor []parameterDescriptors;
62     private Map JavaDoc routes = new HashMap JavaDoc();
63
64     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
65         String JavaDoc value = ((StringValue)parameter.get(SimpleProcessor.INPUT)).toString();
66         return new Result((String JavaDoc)routes.get(value));
67     }
68
69     public ParameterDescriptor[] getParameterDescriptors() {
70         return parameterDescriptors;
71     }
72
73     public ResultDescriptor getResultDescriptor() {
74         return resultDescriptor;
75     }
76
77     public void configure(StreamableValue config) throws ConfigurationException {
78         try {
79             InputStream JavaDoc configuration = config.getInputStream();
80             SimpleImporter importer = new SimpleImporter();
81             ConfigurationHandler handler = new ConfigurationHandler();
82             importer.addSimpleImportHandler(handler);
83             importer.parse(new InputSource JavaDoc(configuration));
84             parameterDescriptors = new ParameterDescriptor[] {
85                 new ParameterDescriptor(SimpleProcessor.INPUT, new ParameterMessage(handler.getDescription()), handler.getValueDesriptor())
86             };
87             resultDescriptor = new ResultDescriptor(handler.getStateDescriptors());
88         } catch (Exception JavaDoc exception) {
89             logger.log(Level.SEVERE, "Error while parsing router configuration", exception);
90         }
91     }
92
93     public class ConfigurationHandler extends DefaultSimpleImportHandler {
94         private StringValueDescriptor valueDesriptor = new StringValueDescriptor();
95         private List JavaDoc states = new ArrayList JavaDoc();
96         private String JavaDoc description;
97
98         public void startElement(SimplePath path, String JavaDoc name, AttributesImpl JavaDoc attributes, String JavaDoc leadingCDdata) {
99             if (path.matches("routes")) {
100                 description = attributes.getValue("description");
101             } else if (path.matches("route")) {
102                 String JavaDoc value = attributes.getValue("value");
103                 String JavaDoc description = attributes.getValue("description");
104                 String JavaDoc state = attributes.getValue("state");
105                 valueDesriptor.addAllowedValue(value);
106                 states.add(new StateDescriptor(value, new DefaultMessage(description)));
107                 routes.put(value, state);
108             }
109         }
110
111         public StringValueDescriptor getValueDesriptor() {
112             return valueDesriptor;
113         }
114
115         public String JavaDoc getDescription() {
116             return description;
117         }
118
119         public StateDescriptor[] getStateDescriptors() {
120             return (StateDescriptor[])states.toArray(new StateDescriptor[0]);
121         }
122     }
123 }
Popular Tags