001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.selection;
018:
019: import org.apache.avalon.framework.configuration.Configurable;
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.ConfigurationException;
022: import org.apache.avalon.framework.logger.AbstractLogEnabled;
023: import org.apache.avalon.framework.thread.ThreadSafe;
024:
025: import java.util.ArrayList;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030:
031: /**
032: * Abstract class for selectors that select a value when it matches
033: * some patterns associated to the select expression.
034: *
035: * @see BrowserSelector
036: * @see HostSelector
037: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
038: * @version CVS $Id: NamedPatternsSelector.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040:
041: public abstract class NamedPatternsSelector extends AbstractLogEnabled
042: implements Configurable, ThreadSafe, Selector {
043:
044: /**
045: * Association of names to String[] of values.
046: */
047: private Map strings;
048:
049: /**
050: * Setup the association from expressions to a list of patterns. The configuration
051: * should look like :
052: * <pre>
053: * <map:selector name="foo" src="...">
054: * <confName nameAttr="expression" valueAttr="pattern"/>
055: * ... others (expression, pattern) associations ...
056: * </map:selector>
057: * </pre>
058: *
059: * @param conf the configuration
060: * @param confName the name of children of <code>conf</code> that will be used to
061: * build associations
062: * @param nameAttr the name of the attribute that holds the expression
063: * @param valueAttr the name of the attribute that holds the pattern
064: */
065: protected void configure(Configuration conf, String confName,
066: String nameAttr, String valueAttr)
067: throws ConfigurationException {
068: Configuration confs[] = conf.getChildren(confName);
069: Map configMap = new HashMap();
070:
071: // Build a list of strings for each name
072: for (int i = 0; i < confs.length; i++) {
073: String name = confs[i].getAttribute(nameAttr);
074: String value = confs[i].getAttribute(valueAttr);
075:
076: // Get value list for this name
077: List nameList = (List) configMap.get(name);
078: if (nameList == null) {
079: nameList = new ArrayList();
080: configMap.put(name, nameList);
081: }
082:
083: // Add the current value
084: nameList.add(value);
085: }
086:
087: // Convert lists to arrays for faster lookup
088: Iterator entries = configMap.entrySet().iterator();
089: while (entries.hasNext()) {
090: Map.Entry entry = (Map.Entry) entries.next();
091: List nameList = (List) entry.getValue();
092: entry.setValue(nameList
093: .toArray(new String[nameList.size()]));
094: }
095:
096: this .strings = configMap;
097: }
098:
099: /**
100: * Checks if <code>value</code> is a substring of one of the patterns associated
101: * to <code>expression</code>
102: *
103: * @param expression the expression that is selected
104: * @param value the value to check
105: * @return true if <code>value</code> matches one of the patterns
106: */
107: protected boolean checkPatterns(String expression, String value) {
108: if (value == null) {
109: getLogger().debug("No value given -- failing.");
110: return false;
111: }
112: // Get patterns for 'expression'
113: String[] patterns = (String[]) this .strings.get(expression);
114: if (patterns == null) {
115: getLogger().warn(
116: "No configuration for expression '" + expression
117: + "' -- failing.");
118: return false;
119: }
120:
121: // Does a pattern match 'value' ?
122: for (int i = 0; i < patterns.length; i++) {
123: if (value.indexOf(patterns[i]) != -1) {
124: getLogger().debug(
125: expression + " selected value " + value);
126: return true;
127: }
128: }
129:
130: // No match
131: return false;
132: }
133:
134: }
|