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.components.treeprocessor.sitemap;
018:
019: import java.util.Map;
020:
021: import org.apache.avalon.framework.activity.Disposable;
022: import org.apache.avalon.framework.component.ComponentException;
023: import org.apache.avalon.framework.component.ComponentManager;
024: import org.apache.avalon.framework.component.ComponentSelector;
025: import org.apache.avalon.framework.component.Composable;
026: import org.apache.avalon.framework.parameters.Parameters;
027: import org.apache.cocoon.components.treeprocessor.InvokeContext;
028: import org.apache.cocoon.components.treeprocessor.ParameterizableProcessingNode;
029: import org.apache.cocoon.components.treeprocessor.SimpleSelectorProcessingNode;
030: import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
031: import org.apache.cocoon.environment.Environment;
032: import org.apache.cocoon.matching.Matcher;
033: import org.apache.cocoon.sitemap.PatternException;
034:
035: /**
036: *
037: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
038: * @version CVS $Id: MatchNode.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040:
041: public class MatchNode extends SimpleSelectorProcessingNode implements
042: ParameterizableProcessingNode, Composable, Disposable {
043:
044: /** The 'pattern' attribute */
045: private VariableResolver pattern;
046:
047: /** The 'name' for the variable anchor */
048: private String name;
049:
050: /** The matcher, if it's ThreadSafe */
051: private Matcher threadSafeMatcher;
052:
053: private Map parameters;
054:
055: private ComponentManager manager;
056:
057: public MatchNode(String type, VariableResolver pattern, String name)
058: throws PatternException {
059: super (type);
060: this .pattern = pattern;
061: this .name = name;
062: }
063:
064: public void setParameters(Map parameterMap) {
065: this .parameters = parameterMap;
066: }
067:
068: public void compose(ComponentManager manager)
069: throws ComponentException {
070: this .manager = manager;
071: this .setSelector((ComponentSelector) manager
072: .lookup(Matcher.ROLE + "Selector"));
073:
074: // Get matcher if it's ThreadSafe
075: this .threadSafeMatcher = (Matcher) this
076: .getThreadSafeComponent();
077: }
078:
079: public final boolean invoke(Environment env, InvokeContext context)
080: throws Exception {
081:
082: // Perform any common invoke functionality
083: super .invoke(env, context);
084:
085: Map objectModel = env.getObjectModel();
086:
087: String resolvedPattern = pattern.resolve(context, objectModel);
088: Parameters resolvedParams = VariableResolver.buildParameters(
089: this .parameters, context, objectModel);
090:
091: Map result = null;
092:
093: if (this .threadSafeMatcher != null) {
094: // Avoid select() and try/catch block (faster !)
095: result = this .threadSafeMatcher.match(resolvedPattern,
096: objectModel, resolvedParams);
097: } else {
098: // Get matcher from selector
099: Matcher matcher = (Matcher) this .selector
100: .select(this .componentName);
101: try {
102: result = matcher.match(resolvedPattern, objectModel,
103: resolvedParams);
104: } finally {
105: this .selector.release(matcher);
106: }
107: }
108:
109: if (result != null) {
110: if (getLogger().isDebugEnabled()) {
111: getLogger().debug(
112: "Matcher '" + this .componentName
113: + "' matched pattern '" + this .pattern
114: + "' at " + this .getLocation());
115: }
116:
117: // Invoke children with the matcher results
118: return this .invokeNodes(children, env, context, name,
119: result);
120: } else {
121: // Matcher failed
122: return false;
123: }
124: }
125:
126: /**
127: * Disposable Interface
128: */
129: public void dispose() {
130: this.manager.release(this.selector);
131: }
132: }
|