KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > treeprocessor > sitemap > MatchNode


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.components.treeprocessor.sitemap;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.activity.Disposable;
21 import org.apache.avalon.framework.component.ComponentException;
22 import org.apache.avalon.framework.component.ComponentManager;
23 import org.apache.avalon.framework.component.ComponentSelector;
24 import org.apache.avalon.framework.component.Composable;
25 import org.apache.avalon.framework.parameters.Parameters;
26 import org.apache.cocoon.components.treeprocessor.InvokeContext;
27 import org.apache.cocoon.components.treeprocessor.ParameterizableProcessingNode;
28 import org.apache.cocoon.components.treeprocessor.SimpleSelectorProcessingNode;
29 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
30 import org.apache.cocoon.environment.Environment;
31 import org.apache.cocoon.matching.Matcher;
32 import org.apache.cocoon.sitemap.PatternException;
33
34 /**
35  *
36  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
37  * @version CVS $Id: MatchNode.java 30932 2004-07-29 17:35:38Z vgritsenko $
38  */

39
40 public class MatchNode extends SimpleSelectorProcessingNode
41         implements ParameterizableProcessingNode, Composable, Disposable {
42
43     /** The 'pattern' attribute */
44     private VariableResolver pattern;
45
46     /** The 'name' for the variable anchor */
47     private String JavaDoc name;
48
49     /** The matcher, if it's ThreadSafe */
50     private Matcher threadSafeMatcher;
51
52     private Map JavaDoc parameters;
53
54     private ComponentManager manager;
55
56     public MatchNode(String JavaDoc type, VariableResolver pattern, String JavaDoc name) throws PatternException {
57         super(type);
58         this.pattern = pattern;
59         this.name = name;
60     }
61
62     public void setParameters(Map JavaDoc parameterMap) {
63         this.parameters = parameterMap;
64     }
65
66     public void compose(ComponentManager manager) throws ComponentException {
67         this.manager = manager;
68         this.setSelector((ComponentSelector)manager.lookup(Matcher.ROLE + "Selector"));
69
70         // Get matcher if it's ThreadSafe
71
this.threadSafeMatcher = (Matcher)this.getThreadSafeComponent();
72     }
73
74     public final boolean invoke(Environment env, InvokeContext context)
75       throws Exception JavaDoc {
76     
77         // Perform any common invoke functionality
78
super.invoke(env, context);
79
80         Map JavaDoc objectModel = env.getObjectModel();
81
82         String JavaDoc resolvedPattern = pattern.resolve(context, objectModel);
83         Parameters resolvedParams = VariableResolver.buildParameters(this.parameters, context, objectModel);
84
85         Map JavaDoc result = null;
86
87         if (this.threadSafeMatcher != null) {
88             // Avoid select() and try/catch block (faster !)
89
result = this.threadSafeMatcher.match(resolvedPattern, objectModel, resolvedParams);
90         } else {
91             // Get matcher from selector
92
Matcher matcher = (Matcher)this.selector.select(this.componentName);
93             try {
94                 result = matcher.match(resolvedPattern, objectModel, resolvedParams);
95             } finally {
96                 this.selector.release(matcher);
97             }
98         }
99
100         if (result != null) {
101             if (getLogger().isDebugEnabled()) {
102                 getLogger().debug("Matcher '" + this.componentName + "' matched pattern '" + this.pattern +
103                     "' at " + this.getLocation());
104             }
105
106             // Invoke children with the matcher results
107
return this.invokeNodes(children, env, context, name, result);
108         } else {
109             // Matcher failed
110
return false;
111         }
112     }
113
114     /**
115      * Disposable Interface
116      */

117     public void dispose() {
118         this.manager.release(this.selector);
119     }
120 }
121
Popular Tags