KickJava   Java API By Example, From Geeks To Geeks.

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


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.service.ServiceManager;
26 import org.apache.cocoon.components.treeprocessor.InvokeContext;
27 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
28 import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory;
29 import org.apache.cocoon.sitemap.PatternException;
30 import org.apache.excalibur.source.SourceUtil;
31
32
33 /**
34  * Top level locate statement containing
35  * <code>MatchNode</code>s and <code>SelectNode</code>s.
36  *
37  * <p>
38  * A locator defines a common location context for its
39  * contained location statements. Location strings resolved within
40  * the context of a given locator will be prefixed with the
41  * locator's base location. This base location is specified
42  * using the <code>base</code> attribute on the <code>&lt;locator&gt;</code>
43  * element and defaults to <code>.</code>.
44  * </p>
45  */

46 public final class LocatorNode extends AbstractNode {
47     
48     // the containing LocationMap
49
private final LocationMap m_lm;
50     
51     // location base resolver
52
private VariableResolver m_baseLocation;
53     
54     // the contained Match- and SelectNodes
55
private AbstractNode[] m_nodes;
56
57     public LocatorNode(final LocationMap lm, final ServiceManager manager) {
58         super(manager);
59         m_lm = lm;
60     }
61     
62     public void build(final Configuration configuration) throws ConfigurationException {
63         
64         super.build(configuration);
65
66         // get the base attribute
67
String JavaDoc base = configuration.getAttribute("base", null);
68         if (base != null) {
69             try {
70                 m_baseLocation = VariableResolverFactory.getResolver(base, super.m_manager);
71             } catch (PatternException e) {
72                 final String JavaDoc message = "Illegal pattern syntax for locator attribute 'base'" +
73                         " at " + configuration.getLocation();
74                 throw new ConfigurationException(message);
75             }
76         }
77         
78         // get the child nodes
79
final Configuration[] children = configuration.getChildren();
80         final List JavaDoc nodes = new ArrayList JavaDoc(children.length);
81         for (int i = 0; i < children.length; i++) {
82             AbstractNode node = null;
83             if (children[i].getName().equals("match")) {
84                 node = new MatchNode(this, super.m_manager);
85             }
86             else if (children[i].getName().equals("select")) {
87                 node = new SelectNode(this, super.m_manager);
88             }
89             else {
90                 final String JavaDoc message = "Illegal locator node child: "
91                     + children[i].getName();
92                 throw new ConfigurationException(message);
93             }
94             node.enableLogging(getLogger());
95             node.build(children[i]);
96             nodes.add(node);
97         }
98         m_nodes = (AbstractNode[]) nodes.toArray(new AbstractNode[nodes.size()]);
99     }
100     
101     /**
102      * Loop over the list of match and select nodes and call their
103      * respective <code>locate()</code> methods returning the first
104      * non-null result.
105      */

106     public String JavaDoc locate(Map JavaDoc om, InvokeContext context) throws Exception JavaDoc {
107
108         // resolve the base location
109
String JavaDoc base = null;
110         if (m_baseLocation != null) {
111             base = m_baseLocation.resolve(context, om);
112             if (base != null && base.charAt(base.length()-1) != '/') {
113                 base = base + "/";
114             }
115         }
116
117         for (int i = 0; i < m_nodes.length; i++) {
118             String JavaDoc location = m_nodes[i].locate(om, context);
119             if (location != null) {
120                 if (base != null && base.length() != 0) {
121                     if (location.charAt(0) != '/' && SourceUtil.indexOfSchemeColon(location) == -1) {
122                         location = base + location;
123                     }
124                 }
125                 if (getLogger().isDebugEnabled()) {
126                     getLogger().debug("located: " + location);
127                 }
128                 return location;
129             }
130         }
131         return null;
132     }
133     
134     String JavaDoc getDefaultMatcher() {
135         return m_lm.getDefaultMatcher();
136     }
137     
138     String JavaDoc getDefaultSelector() {
139         return m_lm.getDefaultSelector();
140     }
141     
142 }
143
Popular Tags