KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > treeprocessor > variables > VariableResolver


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.variables;
17
18 import java.util.Collections JavaDoc;
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.parameters.Parameters;
24 import org.apache.cocoon.components.treeprocessor.InvokeContext;
25 import org.apache.cocoon.sitemap.PatternException;
26 import org.apache.cocoon.sitemap.SitemapParameters;
27 import org.apache.cocoon.util.location.Locatable;
28 import org.apache.cocoon.util.location.Location;
29
30 /**
31  * Utility class for handling {...} pattern substitutions in sitemap statements.
32  *
33  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
34  * @version CVS $Id: VariableResolver.java 232400 2005-08-12 22:14:26Z sylvain $
35  */

36 public abstract class VariableResolver {
37
38     public static final Map JavaDoc EMPTY_MAP = Collections.unmodifiableMap(new java.util.HashMap JavaDoc(0));
39
40     protected final String JavaDoc originalExpr;
41     
42     protected VariableResolver(String JavaDoc expr) {
43         this.originalExpr = expr;
44     }
45
46     public final String JavaDoc toString() {
47         return this.originalExpr;
48     }
49
50     /**
51      * Compare two VariableResolvers
52      */

53     public boolean equals(Object JavaDoc object) {
54         if (object instanceof VariableResolver) {
55             VariableResolver other = (VariableResolver)object;
56             return (this.originalExpr == null && other.originalExpr == null) ||
57                    (this.originalExpr.equals(other.originalExpr));
58         } else {
59             return false;
60         }
61     }
62
63     /**
64      * generate HashCode
65      * needed to determine uniqueness within hashtables
66      */

67     public int hashCode() {
68         return this.originalExpr == null ? 0 : this.originalExpr.hashCode();
69     }
70
71     /**
72      * Resolve all {...} patterns using the values given in the object model.
73      */

74     public String JavaDoc resolve(Map JavaDoc objectModel) throws PatternException {
75         return resolve(null, objectModel);
76     }
77
78     /**
79      * Resolve all {...} patterns using the values given in the list of maps and the object model.
80      */

81     public abstract String JavaDoc resolve(InvokeContext context, Map JavaDoc objectModel) throws PatternException;
82
83     /**
84      * Build a <code>Parameters</code> object from a Map of named <code>VariableResolver</code>s and
85      * a list of Maps used for resolution.
86      *
87      * @return a fully resolved <code>Parameters</code>.
88      */

89     public static Parameters buildParameters(Map JavaDoc expressions, InvokeContext context, Map JavaDoc objectModel) throws PatternException {
90         Location location;
91         if (expressions instanceof Locatable) {
92             location = ((Locatable)expressions).getLocation();
93         } else {
94             location = Location.UNKNOWN;
95         }
96         
97         if (expressions == null || expressions.size() == 0 && location.equals(Location.UNKNOWN)) {
98             return Parameters.EMPTY_PARAMETERS;
99         }
100
101         SitemapParameters result = new SitemapParameters(location);
102
103         Iterator JavaDoc iter = expressions.entrySet().iterator();
104         while (iter.hasNext()) {
105             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
106             result.setParameter(
107                 ((VariableResolver)entry.getKey()).resolve(context, objectModel),
108                 ((VariableResolver)entry.getValue()).resolve(context, objectModel)
109             );
110         }
111
112         return result;
113     }
114
115     /**
116      * Build a <code>Map</code> from a Map of named <code>ListOfMapResolver</code>s and
117      * a list of Maps used for resolution.
118      *
119      * @return a fully resolved <code>Map</code>.
120      */

121     public static Map JavaDoc buildMap(Map JavaDoc expressions, InvokeContext context, Map JavaDoc objectModel) throws PatternException {
122         int size;
123         if (expressions == null || (size = expressions.size()) == 0) {
124             return EMPTY_MAP;
125         }
126
127         Map JavaDoc result;
128         if ( expressions instanceof Locatable ) {
129             result = new SitemapParameters.LocatedHashMap(((Locatable)expressions).getLocation(), size);
130         } else {
131             result = new HashMap JavaDoc(size);
132         }
133
134         Iterator JavaDoc iter = expressions.entrySet().iterator();
135         while (iter.hasNext()) {
136             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
137             result.put(
138                 ((VariableResolver)entry.getKey()).resolve(context, objectModel),
139                 ((VariableResolver)entry.getValue()).resolve(context, objectModel)
140             );
141         }
142
143         return result;
144     }
145
146 // /**
147
// * Release a <code>Map</code> of expressions.
148
// */
149
// public static void release(Map expressions) {
150
// Iterator iter = expressions.entrySet().iterator();
151
// while (iter.hasNext()) {
152
// Map.Entry entry = (Map.Entry)iter.next();
153
// ((VariableResolver)entry.getKey()).release();
154
// ((VariableResolver)entry.getValue()).release();
155
// }
156
// }
157
}
158
Popular Tags