KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > locationmap > lm > MatchNode


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation or its licensors,
3  * as applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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.forrest.locationmap.lm;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.avalon.framework.parameters.Parameters;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.avalon.framework.service.ServiceSelector;
29 import org.apache.cocoon.components.treeprocessor.InvokeContext;
30 import org.apache.cocoon.matching.Matcher;
31
32 /**
33  * Locationmap match statement.
34  *
35  * <p>
36  * The &lt;match&gt; element has one required <code>pattern</code> attribute
37  * which identifies the pattern the associated Matcher should match
38  * against and one optional <code>type</code> attribute that identifies
39  * the Matcher that is to do the matching.
40  * </p>
41  *
42  * Match statements can contain <code>&lt;match&gt;</code>,
43  * <code>&lt;select&gt;</code> and <code>&lt;location&gt;</code>
44  * child statements.
45  *
46  * <p>
47  * Match nodes can be parametrized using <code>&lt;parameter&gt;</code> child elements.
48  * </p>
49  */

50 public final class MatchNode extends AbstractNode {
51     
52     // the containing LocatorNode
53
private final LocatorNode m_ln;
54     
55     // the Matcher that does the work
56
private Matcher m_matcher;
57     
58     // the type of Matcher for this node
59
private String JavaDoc m_type;
60     
61     // the pattern to match
62
private String JavaDoc m_pattern;
63     
64     // the child nodes
65
private AbstractNode[] m_nodes;
66     
67     public MatchNode(final LocatorNode ln, final ServiceManager manager) {
68         super(manager);
69         m_ln = ln;
70     }
71     
72     public void build(final Configuration configuration) throws ConfigurationException {
73         
74         super.build(configuration);
75         
76         // get the matcher
77
m_type = configuration.getAttribute("type",m_ln.getDefaultMatcher());
78         try {
79             ServiceSelector matchers = (ServiceSelector) super.m_manager.lookup(Matcher.ROLE + "Selector");
80             m_matcher = (Matcher) matchers.select(m_type);
81         } catch (ServiceException e) {
82             final String JavaDoc message = "Unable to get Matcher of type " + m_type;
83             throw new ConfigurationException(message,e);
84         }
85         
86         // get the matcher pattern
87
m_pattern = configuration.getAttribute("pattern");
88         
89         // get the child nodes
90
final Configuration[] children = configuration.getChildren();
91         final List JavaDoc nodes = new ArrayList JavaDoc(children.length);
92         for (int i = 0; i < children.length; i++) {
93             AbstractNode node = null;
94             String JavaDoc name = children[i].getName();
95             if (name.equals("location")) {
96                 node = new LocationNode(m_ln, super.m_manager);
97             }
98             else if (name.equals("match")) {
99                 node = new MatchNode(m_ln,super.m_manager);
100             }
101             else if (name.equals("select")) {
102                 node = new SelectNode(m_ln, super.m_manager);
103             }
104             else if (!name.equals("parameter")) {
105                 final String JavaDoc message =
106                     "Unknown match node child: " + name;
107                 throw new ConfigurationException(message);
108             }
109             if (node != null) {
110                 node.enableLogging(getLogger());
111                 node.build(children[i]);
112                 nodes.add(node);
113             }
114         }
115         m_nodes = (AbstractNode[]) nodes.toArray(new AbstractNode[nodes.size()]);
116     }
117     
118     public String JavaDoc locate(Map JavaDoc om, InvokeContext context) throws Exception JavaDoc {
119         
120         Parameters parameters = resolveParameters(context,om);
121         Map JavaDoc substitutions = m_matcher.match(m_pattern,om,parameters);
122         if (substitutions != null) {
123             if (getLogger().isDebugEnabled()) {
124                 getLogger().debug("matched: " + m_pattern);
125             }
126             context.pushMap(null,substitutions);
127             for (int i = 0; i < m_nodes.length; i++) {
128                 String JavaDoc location = m_nodes[i].locate(om,context);
129                 if (location != null) {
130                     return location;
131                 }
132             }
133             context.popMap();
134         }
135         return null;
136     }
137     
138 }
Popular Tags