KickJava   Java API By Example, From Geeks To Geeks.

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


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.configuration.Configuration;
19 import org.apache.avalon.framework.thread.ThreadSafe;
20 import org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNodeBuilder;
21 import org.apache.cocoon.components.treeprocessor.ProcessingNode;
22 import org.apache.cocoon.components.treeprocessor.SimpleSelectorProcessingNode;
23 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
24 import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory;
25 import org.apache.cocoon.matching.Matcher;
26 import org.apache.cocoon.matching.PreparableMatcher;
27
28 /**
29  *
30  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
31  * @version CVS $Id: MatchNodeBuilder.java 30932 2004-07-29 17:35:38Z vgritsenko $
32  */

33
34 public class MatchNodeBuilder extends AbstractParentProcessingNodeBuilder
35   implements ThreadSafe {
36
37     private static final String JavaDoc SELECTOR_ROLE = Matcher.ROLE + "Selector";
38
39     public ProcessingNode buildNode(Configuration config) throws Exception JavaDoc {
40
41         String JavaDoc pattern = config.getAttribute("pattern", null);
42         String JavaDoc name = config.getAttribute("name", null);
43
44         String JavaDoc type = this.treeBuilder.getTypeForStatement(config, SELECTOR_ROLE);
45
46         // Get the type and class for this matcher
47
ComponentsSelector selector = (ComponentsSelector)this.manager.lookup(SELECTOR_ROLE);
48
49         Class JavaDoc clazz = null;
50         try {
51             // Find matcher class
52
Matcher matcher = (Matcher)selector.select(type);
53             clazz = matcher.getClass();
54             selector.release(matcher);
55         } finally {
56             this.manager.release(selector);
57         }
58
59         // PreparableMatcher are only prepared if pattern doesn't need request-time resolution.
60
boolean preparable =
61             PreparableMatcher.class.isAssignableFrom(clazz) &&
62             !VariableResolverFactory.needsResolve(pattern);
63
64         // Instanciate appropriate node
65
SimpleSelectorProcessingNode node;
66         VariableResolver patternResolver = VariableResolverFactory.getResolver(pattern, this.manager);
67
68         if (preparable) {
69             node = new PreparableMatchNode(type, VariableResolverFactory.unescape(pattern),name);
70         } else {
71             node = new MatchNode(type, patternResolver,name);
72         }
73
74         this.treeBuilder.setupNode(node, config);
75
76         // Get all children
77
ProcessingNode[] children = buildChildNodes(config);
78
79         node.setChildren(children);
80
81         return node;
82     }
83 }
84
Popular Tags