KickJava   Java API By Example, From Geeks To Geeks.

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


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

41 public class PreparableMatchNode extends SimpleSelectorProcessingNode
42                                  implements ParameterizableProcessingNode, Composable, Disposable {
43
44     /** The 'pattern' attribute */
45     private String JavaDoc pattern;
46
47     /** The 'name' for the variable anchor */
48     private String JavaDoc name;
49
50     private Object JavaDoc preparedPattern;
51
52     private Map JavaDoc parameters;
53
54     /** The matcher, if it's ThreadSafe */
55     private PreparableMatcher threadSafeMatcher;
56
57     protected ComponentManager manager;
58
59     public PreparableMatchNode(String JavaDoc type, String JavaDoc pattern, String JavaDoc name) throws PatternException {
60         super(type);
61         this.pattern = pattern;
62         this.name = name;
63     }
64
65     public void setParameters(Map JavaDoc parameterMap) {
66         this.parameters = parameterMap;
67     }
68
69
70     public void compose(ComponentManager manager) throws ComponentException {
71         this.manager = manager;
72         setSelector((ComponentSelector)manager.lookup(Matcher.ROLE + "Selector"));
73
74         // Prepare the pattern, and keep matcher if ThreadSafe
75
PreparableMatcher matcher = (PreparableMatcher)selector.select(componentName);
76
77         if (matcher instanceof ThreadSafe) {
78             this.threadSafeMatcher = matcher;
79         }
80
81         try {
82             this.preparedPattern = matcher.preparePattern(this.pattern);
83
84         } catch(PatternException pe) {
85             String JavaDoc msg = "Invalid pattern '" + this.pattern + "' for matcher at " + this.getLocation();
86             throw new ComponentException(null, msg, pe);
87
88         } finally {
89             if (this.threadSafeMatcher == null) {
90                 selector.release(matcher);
91             }
92         }
93     }
94
95     public final boolean invoke(Environment env, InvokeContext context)
96     throws Exception JavaDoc {
97       
98         // Perform any common invoke functionality
99
super.invoke(env, context);
100
101         Map JavaDoc objectModel = env.getObjectModel();
102         Parameters resolvedParams = VariableResolver.buildParameters(
103             this.parameters, context, objectModel
104         );
105
106         Map JavaDoc result = null;
107
108         if (this.threadSafeMatcher != null) {
109             // Avoid select() and try/catch block (faster !)
110
result = this.threadSafeMatcher.preparedMatch(preparedPattern, objectModel, resolvedParams);
111
112         } else {
113             // Get matcher from selector
114
PreparableMatcher matcher = (PreparableMatcher)this.selector.select(this.componentName);
115             try {
116                 result = matcher.preparedMatch(preparedPattern, objectModel, resolvedParams);
117
118             } finally {
119                 this.selector.release(matcher);
120             }
121         }
122
123         if (result != null) {
124             if (getLogger().isDebugEnabled()) {
125                 getLogger().debug("Matcher '" + this.componentName + "' matched prepared pattern '" +
126                     this.pattern + "' at " + this.getLocation());
127             }
128
129             // Invoke children with the matcher results
130
return this.invokeNodes(children, env, context, name, result);
131
132         } else {
133             // Matcher failed
134
return false;
135         }
136     }
137
138     /**
139      * Disposable Interface
140      */

141     public void dispose() {
142         if (this.threadSafeMatcher != null) {
143             selector.release(this.threadSafeMatcher);
144             this.threadSafeMatcher = null;
145         }
146         if (this.selector != null) {
147             this.manager.release(this.selector);
148             this.selector = null;
149         }
150         this.manager = null;
151     }
152 }
153
Popular Tags