1 16 package org.apache.cocoon.selection; 17 18 import org.apache.avalon.framework.configuration.Configurable; 19 import org.apache.avalon.framework.configuration.Configuration; 20 import org.apache.avalon.framework.configuration.ConfigurationException; 21 import org.apache.avalon.framework.logger.AbstractLogEnabled; 22 import org.apache.avalon.framework.thread.ThreadSafe; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 39 40 public abstract class NamedPatternsSelector extends AbstractLogEnabled 41 implements Configurable, ThreadSafe, Selector { 42 43 46 private Map strings; 47 48 64 protected void configure(Configuration conf, String confName, String nameAttr, String valueAttr) 65 throws ConfigurationException { 66 Configuration confs[] = conf.getChildren(confName); 67 Map configMap = new HashMap (); 68 69 for (int i = 0; i < confs.length; i++) { 71 String name = confs[i].getAttribute(nameAttr); 72 String value = confs[i].getAttribute(valueAttr); 73 74 List nameList = (List )configMap.get(name); 76 if (nameList == null) { 77 nameList = new ArrayList (); 78 configMap.put(name, nameList); 79 } 80 81 nameList.add(value); 83 } 84 85 Iterator entries = configMap.entrySet().iterator(); 87 while(entries.hasNext()) { 88 Map.Entry entry = (Map.Entry )entries.next(); 89 List nameList = (List )entry.getValue(); 90 entry.setValue(nameList.toArray(new String [nameList.size()])); 91 } 92 93 this.strings = configMap; 94 } 95 96 104 protected boolean checkPatterns(String expression, String value) { 105 if (value == null) { 106 getLogger().debug("No value given -- failing."); 107 return false; 108 } 109 String [] patterns = (String [])this.strings.get(expression); 111 if (patterns == null) { 112 getLogger().warn("No configuration for expression '" + expression + "' -- failing."); 113 return false; 114 } 115 116 for (int i = 0; i < patterns.length; i++) { 118 if (value.indexOf(patterns[i]) != -1) { 119 getLogger().debug(expression + " selected value " + value); 120 return true; 121 } 122 } 123 124 return false; 126 } 127 128 } 129 | Popular Tags |