KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > selection > NamedPatternsSelector


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * Abstract class for selectors that select a value when it matches
32  * some patterns associated to the select expression.
33  *
34  * @see BrowserSelector
35  * @see HostSelector
36  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
37  * @version CVS $Id: NamedPatternsSelector.java 30932 2004-07-29 17:35:38Z vgritsenko $
38  */

39
40 public abstract class NamedPatternsSelector extends AbstractLogEnabled
41   implements Configurable, ThreadSafe, Selector {
42
43     /**
44      * Association of names to String[] of values.
45      */

46     private Map JavaDoc strings;
47
48     /**
49      * Setup the association from expressions to a list of patterns. The configuration
50      * should look like :
51      * &lt;pre&gt;
52      * &lt;map:selector name="foo" SRC="..."&gt;
53      * &lt;confName nameAttr="expression" valueAttr="pattern"/&gt;
54      * ... others (expression, pattern) associations ...
55      * &lt;/map:selector&gt;
56      * &lt;/pre&gt;
57      *
58      * @param conf the configuration
59      * @param confName the name of children of <code>conf</code> that will be used to
60      * build associations
61      * @param nameAttr the name of the attribute that holds the expression
62      * @param valueAttr the name of the attribute that holds the pattern
63      */

64     protected void configure(Configuration conf, String JavaDoc confName, String JavaDoc nameAttr, String JavaDoc valueAttr)
65       throws ConfigurationException {
66         Configuration confs[] = conf.getChildren(confName);
67         Map JavaDoc configMap = new HashMap JavaDoc();
68
69         // Build a list of strings for each name
70
for (int i = 0; i < confs.length; i++) {
71             String JavaDoc name = confs[i].getAttribute(nameAttr);
72             String JavaDoc value = confs[i].getAttribute(valueAttr);
73
74             // Get value list for this name
75
List JavaDoc nameList = (List JavaDoc)configMap.get(name);
76             if (nameList == null) {
77                 nameList = new ArrayList JavaDoc();
78                 configMap.put(name, nameList);
79             }
80
81             // Add the current value
82
nameList.add(value);
83         }
84
85         // Convert lists to arrays for faster lookup
86
Iterator JavaDoc entries = configMap.entrySet().iterator();
87         while(entries.hasNext()) {
88             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entries.next();
89             List JavaDoc nameList = (List JavaDoc)entry.getValue();
90             entry.setValue(nameList.toArray(new String JavaDoc[nameList.size()]));
91         }
92
93         this.strings = configMap;
94     }
95
96     /**
97      * Checks if <code>value</code> is a substring of one of the patterns associated
98      * to <code>expression</code>
99      *
100      * @param expression the expression that is selected
101      * @param value the value to check
102      * @return true if <code>value</code> matches one of the patterns
103      */

104     protected boolean checkPatterns(String JavaDoc expression, String JavaDoc value) {
105         if (value == null) {
106             getLogger().debug("No value given -- failing.");
107             return false;
108         }
109         // Get patterns for 'expression'
110
String JavaDoc[] patterns = (String JavaDoc[])this.strings.get(expression);
111         if (patterns == null) {
112             getLogger().warn("No configuration for expression '" + expression + "' -- failing.");
113             return false;
114         }
115
116         // Does a pattern match 'value' ?
117
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         // No match
125
return false;
126     }
127
128 }
129
Popular Tags