KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
20 import java.util.Iterator 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.logger.AbstractLogEnabled;
26 import org.apache.avalon.framework.parameters.Parameters;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.cocoon.components.treeprocessor.InvokeContext;
29 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
30 import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory;
31 import org.apache.cocoon.sitemap.PatternException;
32
33 /**
34  * Base class for LocationMap nodes. Defines the contract between the
35  * LocationMap and its nodes.
36  */

37 public abstract class AbstractNode extends AbstractLogEnabled {
38     
39     
40     protected final ServiceManager m_manager;
41     
42     // optional parameters defined by the node's configuration
43
private Map JavaDoc m_parameters;
44     
45     
46     public AbstractNode(final ServiceManager manager) {
47         m_manager = manager;
48     }
49     
50     public void build(final Configuration configuration) throws ConfigurationException {
51         m_parameters = getParameters(configuration);
52     }
53     
54     /**
55      * Create a Map of resolvable parameters.
56      *
57      * @param configuration the configuration to build parameters from.
58      * @return a Map of parameters wrapped in VariableResolver objects,
59      * <code>null</code> if the configuration contained no parameters.
60      * @throws ConfigurationException
61      */

62     private final Map JavaDoc getParameters(final Configuration configuration)
63         throws ConfigurationException {
64         
65         final Configuration[] children = configuration.getChildren("parameter");
66         if (children.length == 0) {
67             return null;
68         }
69         final Map JavaDoc parameters = new HashMap JavaDoc();
70         for (int i = 0; i < children.length; i++) {
71             final String JavaDoc name = children[i].getAttribute("name");
72             final String JavaDoc value = children[i].getAttribute("value");
73             try {
74                 parameters.put(
75                     VariableResolverFactory.getResolver(name, m_manager),
76                     VariableResolverFactory.getResolver(value, m_manager));
77             } catch(PatternException pe) {
78                 String JavaDoc msg = "Invalid pattern '" + value + "' at "
79                     + children[i].getLocation();
80                 throw new ConfigurationException(msg, pe);
81             }
82         }
83
84         return parameters;
85     }
86     
87     /**
88      * Resolve the parameters. Also passes the LocationMap special
89      * variables into the Parameters object.
90      *
91      * @param context InvokeContext used during resolution.
92      * @param om object model used during resolution.
93      * @return the resolved parameters or null if this node contains no parameters.
94      * @throws PatternException
95      */

96     protected final Parameters resolveParameters(
97         final InvokeContext context,
98         final Map JavaDoc om) throws PatternException {
99         
100         Parameters parameters = null;
101         if (m_parameters != null) {
102             parameters = VariableResolver.buildParameters(m_parameters,context,om);
103         }
104         else {
105             parameters = new Parameters();
106         }
107         // also pass the anchor map as parameters directly into the components
108
Map JavaDoc anchorMap = context.getMapByAnchor(LocationMap.ANCHOR_NAME);
109         Iterator JavaDoc entries = anchorMap.entrySet().iterator();
110         while (entries.hasNext()) {
111             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entries.next();
112             parameters.setParameter(
113                 "#"+LocationMap.ANCHOR_NAME+":"+entry.getKey(),
114                 entry.getValue().toString());
115         }
116         return parameters;
117     }
118     
119     public abstract String JavaDoc locate(Map JavaDoc objectModel, InvokeContext context) throws Exception JavaDoc;
120         
121 }
122
Popular Tags