001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.workflow.util;
006:
007: import com.opensymphony.module.propertyset.PropertySet;
008:
009: import com.opensymphony.provider.BeanProvider;
010: import com.opensymphony.provider.bean.DefaultBeanProvider;
011:
012: import java.io.Serializable;
013:
014: import java.util.Map;
015:
016: /**
017: * @author Hani Suleiman (hani@formicary.net)
018: * Date: Oct 14, 2003
019: * Time: 11:58:12 PM
020: */
021: public class DefaultVariableResolver implements VariableResolver,
022: Serializable {
023: //~ Static fields/initializers /////////////////////////////////////////////
024:
025: private static final long serialVersionUID = -4819078273560683753L;
026:
027: //~ Instance fields ////////////////////////////////////////////////////////
028:
029: private transient BeanProvider beanProvider = null;
030:
031: //~ Methods ////////////////////////////////////////////////////////////////
032:
033: public void setBeanProvider(BeanProvider beanProvider) {
034: this .beanProvider = beanProvider;
035: }
036:
037: public BeanProvider getBeanProvider() {
038: return beanProvider;
039: }
040:
041: public Object getVariableFromMaps(String var, Map transientVars,
042: PropertySet ps) {
043: int firstDot = var.indexOf('.');
044: String actualVar = var;
045:
046: if (firstDot != -1) {
047: actualVar = var.substring(0, firstDot);
048: }
049:
050: Object o = transientVars.get(actualVar);
051:
052: if (o == null) {
053: o = ps.getAsActualType(actualVar);
054: }
055:
056: if (firstDot != -1) {
057: if (beanProvider == null) {
058: beanProvider = new DefaultBeanProvider();
059: }
060:
061: o = beanProvider
062: .getProperty(o, var.substring(firstDot + 1));
063: }
064:
065: return o;
066: }
067:
068: /**
069: * Parses a string for instances of "${foo}" and returns a string with all instances replaced
070: * with the string value of the foo object (<b>foo.toString()</b>). If the string being passed
071: * in only refers to a single variable and contains no other characters (for example: ${foo}),
072: * then the actual object is returned instead of converting it to a string.
073: */
074: public Object translateVariables(String s, Map transientVars,
075: PropertySet ps) {
076: String temp = s.trim();
077:
078: if (temp.startsWith("${") && temp.endsWith("}")
079: && (temp.indexOf('$', 1) == -1)) {
080: // the string is just a variable reference, don't convert it to a string
081: String var = temp.substring(2, temp.length() - 1);
082:
083: return getVariableFromMaps(var, transientVars, ps);
084: } else {
085: // the string passed in contains multiple variables (or none!) and should be treated as a string
086: while (true) {
087: int x = s.indexOf("${");
088: int y = s.indexOf("}", x);
089:
090: if ((x != -1) && (y != -1)) {
091: String var = s.substring(x + 2, y);
092: String t = null;
093: Object o = getVariableFromMaps(var, transientVars,
094: ps);
095:
096: if (o != null) {
097: t = o.toString();
098: }
099:
100: if (t != null) {
101: s = s.substring(0, x) + t + s.substring(y + 1);
102: } else {
103: // the variable doesn't exist, so don't display anything
104: s = s.substring(0, x) + s.substring(y + 1);
105: }
106: } else {
107: break;
108: }
109: }
110:
111: return s;
112: }
113: }
114: }
|