01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.components.treeprocessor.sitemap;
18:
19: import org.apache.avalon.framework.configuration.Configuration;
20: import org.apache.avalon.framework.thread.ThreadSafe;
21: import org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNodeBuilder;
22: import org.apache.cocoon.components.treeprocessor.ProcessingNode;
23: import org.apache.cocoon.components.treeprocessor.SimpleSelectorProcessingNode;
24: import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
25: import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory;
26: import org.apache.cocoon.matching.Matcher;
27: import org.apache.cocoon.matching.PreparableMatcher;
28:
29: /**
30: *
31: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
32: * @version CVS $Id: MatchNodeBuilder.java 433543 2006-08-22 06:22:54Z crossley $
33: */
34:
35: public class MatchNodeBuilder extends
36: AbstractParentProcessingNodeBuilder implements ThreadSafe {
37:
38: private static final String SELECTOR_ROLE = Matcher.ROLE
39: + "Selector";
40:
41: public ProcessingNode buildNode(Configuration config)
42: throws Exception {
43:
44: String pattern = config.getAttribute("pattern", null);
45: String name = config.getAttribute("name", null);
46:
47: String type = this .treeBuilder.getTypeForStatement(config,
48: SELECTOR_ROLE);
49:
50: // Get the type and class for this matcher
51: ComponentsSelector selector = (ComponentsSelector) this .manager
52: .lookup(SELECTOR_ROLE);
53:
54: Class clazz = null;
55: try {
56: // Find matcher class
57: Matcher matcher = (Matcher) selector.select(type);
58: clazz = matcher.getClass();
59: selector.release(matcher);
60: } finally {
61: this .manager.release(selector);
62: }
63:
64: // PreparableMatcher are only prepared if pattern doesn't need request-time resolution.
65: boolean preparable = PreparableMatcher.class
66: .isAssignableFrom(clazz)
67: && !VariableResolverFactory.needsResolve(pattern);
68:
69: // Instanciate appropriate node
70: SimpleSelectorProcessingNode node;
71: VariableResolver patternResolver = VariableResolverFactory
72: .getResolver(pattern, this .manager);
73:
74: if (preparable) {
75: node = new PreparableMatchNode(type,
76: VariableResolverFactory.unescape(pattern), name);
77: } else {
78: node = new MatchNode(type, patternResolver, name);
79: }
80:
81: this .treeBuilder.setupNode(node, config);
82:
83: // Get all children
84: ProcessingNode[] children = buildChildNodes(config);
85:
86: node.setChildren(children);
87:
88: return node;
89: }
90: }
|