KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > matching > modular > WildcardMatcher


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.matching.modular;
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.parameters.Parameters;
22 import org.apache.avalon.framework.component.ComponentSelector;
23 import org.apache.avalon.framework.component.ComponentException;
24 import org.apache.avalon.framework.component.ComponentManager;
25 import org.apache.avalon.framework.component.Composable;
26
27 import org.apache.cocoon.components.modules.input.InputModule;
28
29 import org.apache.cocoon.matching.AbstractWildcardMatcher;
30
31 import java.util.Map JavaDoc;
32
33 /**
34  * Matches against a wildcard expression. Needs an input module to
35  * obtain value to match against.
36  *
37  * <p><b>Global and local configuration</b></p>
38  * <table border="1">
39  * <tr><td><code>input-module</code></td><td>Name of the input module used to obtain the value</td></tr>
40  * <tr><td><code>parameter-name</code></td><td>Name of the parameter to match * against</td></tr>
41  * </table>
42  *
43  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
44  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
45  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
46  * @version CVS $Id: WildcardMatcher.java 30932 2004-07-29 17:35:38Z vgritsenko $
47  */

48 public class WildcardMatcher extends AbstractWildcardMatcher
49     implements Configurable, Composable
50 {
51
52     /** The component manager instance */
53     protected ComponentManager manager;
54
55     private String JavaDoc defaultParam;
56     private String JavaDoc defaultInput = "request-param"; // default to request parameters
57
private Configuration inputConf = null; // will become an empty configuration object
58
// during configure() so why bother here...
59
String JavaDoc INPUT_MODULE_ROLE = InputModule.ROLE;
60     String JavaDoc INPUT_MODULE_SELECTOR = INPUT_MODULE_ROLE+"Selector";
61
62     /**
63      * Set the current <code>ComponentManager</code> instance used by this
64      * <code>Composable</code>.
65      */

66     public void compose(ComponentManager manager) throws ComponentException {
67         this.manager=manager;
68     }
69
70     public void configure(Configuration config) throws ConfigurationException {
71         this.defaultParam = config.getChild("parameter-name").getValue(null);
72         this.inputConf = config.getChild("input-module");
73         this.defaultInput = this.inputConf.getAttribute("name",this.defaultInput);
74     }
75
76     protected String JavaDoc getMatchString(Map JavaDoc objectModel, Parameters parameters) {
77
78         String JavaDoc paramName = parameters.getParameter("parameter-name", this.defaultParam);
79         String JavaDoc inputName = parameters.getParameter("input-module", this.defaultInput);
80
81         if (paramName == null) {
82             if (getLogger().isWarnEnabled())
83                 getLogger().warn("No parameter name given. Trying to continue");
84         }
85         if (inputName == null) {
86             if (getLogger().isWarnEnabled())
87                 getLogger().warn("No input module given. FAILING");
88             return null;
89         }
90
91         InputModule input = null;
92         ComponentSelector inputSelector = null;
93         Object JavaDoc result = null;
94
95         // one could test whether the input module is ThreadSafe and
96
// keep a reference for that instance. Then one would need
97
// to implement Disposable in order to release it at EOL
98
// That would probably speed up things a lot. Especially, since
99
// matchers are invoked very often.
100
// Perhaps a CachingWildcardMatcher ?
101

102         try {
103             // obtain input module
104
inputSelector=(ComponentSelector) this.manager.lookup(INPUT_MODULE_SELECTOR);
105             if (inputName != null && inputSelector != null && inputSelector.hasComponent(inputName)){
106                 input = (InputModule) inputSelector.select(inputName);
107             }
108             if (input != null) {
109                 result = input.getAttribute(paramName, this.inputConf, objectModel);
110             }
111         } catch (Exception JavaDoc e) {
112             if (getLogger().isWarnEnabled())
113                 getLogger().warn("A problem occurred acquiring Parameter '" + paramName
114                                  + "' from '" + inputName + "': " + e.getMessage());
115         } finally {
116             // release components
117
if (inputSelector != null) {
118                 if (input != null)
119                     inputSelector.release(input);
120                 this.manager.release(inputSelector);
121             }
122         }
123
124         if (getLogger().isDebugEnabled())
125             getLogger().debug(" using "+inputName+" obtained value "+result);
126
127         if (result instanceof String JavaDoc) {
128             return (String JavaDoc) result;
129         } else {
130             return result.toString();
131         }
132     }
133 }
134
Popular Tags