KickJava   Java API By Example, From Geeks To Geeks.

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


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.selection.Selector;
31
32
33 /**
34  * Locationmap select statement.
35  */

36 public final class SelectNode extends AbstractNode {
37     
38     // the containing LocatorNode
39
private final LocatorNode m_ln;
40     
41     // the selector that does the work
42
private Selector m_selector;
43     
44     // the type of selector for this node
45
private String JavaDoc m_type;
46     
47     // the locations to test against
48
private AbstractNode[] m_nodes;
49     
50     
51     public SelectNode(LocatorNode ln, ServiceManager manager) {
52         super(manager);
53         m_ln = ln;
54     }
55     
56     public void build(Configuration configuration) throws ConfigurationException {
57         
58         super.build(configuration);
59         
60         // get the selector
61
m_type = configuration.getAttribute("type",m_ln.getDefaultSelector());
62         try {
63             final ServiceSelector selectors = (ServiceSelector) super.m_manager.lookup(Selector.ROLE + "Selector");
64             m_selector = (Selector) selectors.select(m_type);
65         } catch (ServiceException e) {
66             final String JavaDoc message = "Unable to get Selector of type " + m_type;
67             throw new ConfigurationException(message,e);
68         }
69         
70         // build the child nodes
71
final Configuration[] children = configuration.getChildren();
72         final List JavaDoc nodes = new ArrayList JavaDoc(children.length);
73         for (int i = 0; i < children.length; i++) {
74             AbstractNode node = null;
75             String JavaDoc name = children[i].getName();
76             if (name.equals("location")) {
77                 node = new LocationNode(m_ln, super.m_manager);
78             }
79             else if (name.equals("match")) {
80                 node = new MatchNode(m_ln, super.m_manager);
81             }
82             else if (name.equals("select")) {
83                 node = new SelectNode(m_ln, super.m_manager);
84             }
85             else if (!name.equals("parameter")) {
86                 final String JavaDoc message = "Unknown select node child:" + name;
87                 throw new ConfigurationException(message);
88             }
89             if (node != null) {
90                 node.enableLogging(getLogger());
91                 node.build(children[i]);
92                 nodes.add(node);
93             }
94         }
95         m_nodes = (AbstractNode[]) nodes.toArray(new AbstractNode[nodes.size()]);
96     }
97     
98     public String JavaDoc locate(Map JavaDoc om, InvokeContext context) throws Exception JavaDoc {
99         
100         Parameters parameters = resolveParameters(context,om);
101         for (int i = 0; i < m_nodes.length; i++) {
102             String JavaDoc location = m_nodes[i].locate(om,context);
103             if (m_selector.select(location,om,parameters)) {
104                 if (getLogger().isDebugEnabled()) {
105                     getLogger().debug("selected: " + location);
106                 }
107                 return location;
108             }
109         }
110         return null;
111     }
112 }
Popular Tags