1 23 24 package org.apache.slide.projector.processor; 25 26 import java.io.InputStream ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 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 ; 47 import org.xml.sax.helpers.AttributesImpl ; 48 49 import de.zeigermann.xml.simpleImporter.DefaultSimpleImportHandler; 50 import de.zeigermann.xml.simpleImporter.SimpleImporter; 51 import de.zeigermann.xml.simpleImporter.SimplePath; 52 53 57 public class Router implements ConfigurableProcessor { 58 private static Logger logger = Logger.getLogger(Router.class.getName()); 59 60 private static ResultDescriptor resultDescriptor; 61 private static ParameterDescriptor []parameterDescriptors; 62 private Map routes = new HashMap (); 63 64 public Result process(Map parameter, Context context) throws Exception { 65 String value = ((StringValue)parameter.get(SimpleProcessor.INPUT)).toString(); 66 return new Result((String )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 configuration = config.getInputStream(); 80 SimpleImporter importer = new SimpleImporter(); 81 ConfigurationHandler handler = new ConfigurationHandler(); 82 importer.addSimpleImportHandler(handler); 83 importer.parse(new InputSource (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 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 states = new ArrayList (); 96 private String description; 97 98 public void startElement(SimplePath path, String name, AttributesImpl attributes, String leadingCDdata) { 99 if (path.matches("routes")) { 100 description = attributes.getValue("description"); 101 } else if (path.matches("route")) { 102 String value = attributes.getValue("value"); 103 String description = attributes.getValue("description"); 104 String 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 getDescription() { 116 return description; 117 } 118 119 public StateDescriptor[] getStateDescriptors() { 120 return (StateDescriptor[])states.toArray(new StateDescriptor[0]); 121 } 122 } 123 } | Popular Tags |