001: /*
002: * $Id: BeanContext.java,v 1.5 2002/06/06 15:02:07 valis Exp $
003: *
004: * ===========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.context;
052:
053: import org.apache.commons.jexl.Expression;
054: import org.apache.commons.jexl.ExpressionFactory;
055: import org.apache.commons.jexl.context.HashMapContext;
056:
057: import java.util.ArrayList;
058: import java.util.Collection;
059: import java.util.List;
060: import java.util.ListIterator;
061: import java.util.Map;
062: import java.util.Set;
063:
064: /**
065: * A Context that provides access to a graph of objects (beans and collections)
066: * using Jexl (JSTL expression language + extenstions) syntax.
067: *
068: * Unlike the other contexts in JGen, this is not easily creatable from a
069: * datasource, and is intended to be used where JGen is embedded in another
070: * framework.
071: *
072: * @author <a href="james@jamestaylor.org">James Taylor</a>
073: */
074: public class BeanContext extends GraphContext implements Map {
075: /** Key used to store root object when building a context for a list item */
076: public static final String ROOT_ITEM_KEY = "root";
077:
078: /** The JexlContext */
079: private HashMapContext jexlContext = new HashMapContext();
080:
081: /**
082: * Creates empty context.
083: */
084: public BeanContext() {
085: }
086:
087: /**
088: * Creates context from specified hashtable.
089: *
090: * @param context parent context
091: * @param values Map of initial values to populate context with
092: */
093: public BeanContext(Context context, Map values) {
094: setParent(context);
095:
096: if (values != null) {
097: this .jexlContext.setVars(values);
098: }
099: }
100:
101: /**
102: * Tries to resolve given string in this context as XPath expression,
103: * using JPath. If the XPath expression selects an object, the result
104: * of evaluating its 'toString' method is returned, otherwise the path
105: * is passed along to the parent context (if defined).
106: *
107: * @param path XPath/JPath expression
108: *
109: * @return String representation of result of xpath execution or result
110: * from the parent or null
111: */
112:
113: public String getValue(String path) {
114: // Evaluate path against our context
115:
116: Object o = evaluatePath(path);
117:
118: // If o is null, that indicates that the path expression did not
119: // select an object in the context. This is perfectly acceptable,
120: // and we pass the path to the parent context.
121:
122: if (o == null) {
123: return getValueFromParent(path);
124: }
125:
126: // If o was not null, we return it's string representation, which is
127: // hopefully something meaningfull. But even if it is not the default
128: // implementation of Object.toString() should give a value which will
129: // allow the user to see that something is wrong with the path or
130: // context.
131:
132: else {
133: return o.toString();
134: }
135: }
136:
137: /**
138: * Tries to resolve given string in this context as Jexl expression.
139: * If the path expression evaluates to a non-empty list, a list of contexts
140: * corresponding to each value in that list will be returned. Otherwise
141: * the path is passed to the parent context. If there is no parent
142: * context, null is returned.
143: *
144: * The wrapping contexts will have the single element 'root' which is the
145: * object selected as a list item.
146: *
147: * @param path Jexl expression
148: *
149: * @return List of objects (wrapped in BeanContext) selected by path or
150: * result from the parent or null
151: */
152: public List getValueList(String path) {
153: // Evaluate path against our context
154:
155: Object o = evaluatePath(path);
156:
157: // If o is null, that indicates that the path expression did not
158: // select an object in the context. This is perfectly acceptable,
159: // and we pass the path to the parent context.
160:
161: if (o == null) {
162: return getValueListFromParent(path);
163: }
164:
165: // If o is a list, we build a list of contexts for each object it
166: // contains. If o is a single object we'll just build a context list
167: // with one item.
168:
169: ArrayList contextList = new ArrayList();
170:
171: if (o instanceof List) {
172: ListIterator iter = ((List) o).listIterator();
173:
174: while (iter.hasNext()) {
175: addToContextList(iter.next(), contextList);
176: }
177: } else {
178: addToContextList(o, contextList);
179: }
180:
181: return contextList;
182: }
183:
184: /**
185: * Build a BeanContext around and object and add it to the provided list.
186: *
187: * @param o Object to add
188: * @param contextList List to add context to
189: */
190: private void addToContextList(Object o, List contextList) {
191: BeanContext newContext = new BeanContext(this , null);
192:
193: newContext.put(ROOT_ITEM_KEY, o);
194:
195: contextList.add(newContext);
196: }
197:
198: /**
199: * Evaluate a path as a Jexl expression on the contained context.
200: *
201: * @param path Expression to evaluate
202: * @return Object if found or null
203: */
204: private Object evaluatePath(String path) {
205: try {
206: // Parse the path into a Jexl Expression
207:
208: Expression expr = ExpressionFactory.createExpression(path);
209:
210: // Execute the expression against our context
211:
212: Object o = expr.evaluate(jexlContext);
213:
214: return o;
215: } catch (Exception e) {
216: // Expression could not be parsed or evaluated as Jexl, so return
217: // null which will cause the path to be passed up to that parent
218:
219: return null;
220: }
221: }
222:
223: // ------------------------------ interface Map, delegate to HashMapContext
224:
225: public int size() {
226: return jexlContext.size();
227: }
228:
229: public boolean isEmpty() {
230: return jexlContext.isEmpty();
231: }
232:
233: public boolean containsKey(Object key) {
234: return jexlContext.containsKey(key);
235: }
236:
237: public boolean containsValue(Object value) {
238: return jexlContext.containsValue(value);
239: }
240:
241: public Object get(Object key) {
242: return jexlContext.get(key);
243: }
244:
245: public Object put(Object key, Object value) {
246: return jexlContext.put(key, value);
247: }
248:
249: public Object remove(Object key) {
250: return jexlContext.remove(key);
251: }
252:
253: public void putAll(Map t) {
254: jexlContext.putAll(t);
255: }
256:
257: public void clear() {
258: jexlContext.clear();
259: }
260:
261: public Set keySet() {
262: return jexlContext.keySet();
263: }
264:
265: public Collection values() {
266: return jexlContext.values();
267: }
268:
269: public Set entrySet() {
270: return jexlContext.entrySet();
271: }
272:
273: public boolean equals(Object o) {
274: return jexlContext.equals(o);
275: }
276:
277: public int hashCode() {
278: return jexlContext.hashCode();
279: }
280:
281: }
|