KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > util > ScriptVariableParser


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.util;
6
7 import com.opensymphony.module.propertyset.PropertySet;
8
9 import com.opensymphony.provider.BeanProvider;
10 import com.opensymphony.provider.bean.DefaultBeanProvider;
11
12 import java.util.Map JavaDoc;
13
14
15 /**
16  * @author Hani Suleiman (hani@formicary.net)
17  * Date: Oct 14, 2003
18  * Time: 11:58:12 PM
19  */

20 public class ScriptVariableParser {
21     //~ Static fields/initializers /////////////////////////////////////////////
22

23     private static BeanProvider beanProvider = new DefaultBeanProvider();
24
25     //~ Methods ////////////////////////////////////////////////////////////////
26

27     public static Object JavaDoc getVariableFromMaps(String JavaDoc var, Map JavaDoc transientVars, PropertySet ps) {
28         int firstDot = var.indexOf('.');
29         String JavaDoc actualVar = var;
30
31         if (firstDot != -1) {
32             actualVar = var.substring(0, firstDot);
33         }
34
35         Object JavaDoc o = transientVars.get(actualVar);
36
37         if (o == null) {
38             o = ps.getAsActualType(actualVar);
39         }
40
41         if (firstDot != -1) {
42             o = beanProvider.getProperty(o, var.substring(firstDot + 1));
43         }
44
45         return o;
46     }
47
48     /**
49        * Parses a string for instances of "${foo}" and returns a string with all instances replaced
50        * with the string value of the foo object (<b>foo.toString()</b>). If the string being passed
51        * in only refers to a single variable and contains no other characters (for example: ${foo}),
52        * then the actual object is returned instead of converting it to a string.
53        */

54     public static Object JavaDoc translateVariables(String JavaDoc s, Map JavaDoc transientVars, PropertySet ps) {
55         String JavaDoc temp = s.trim();
56
57         if (temp.startsWith("${") && temp.endsWith("}") && (temp.indexOf('$', 1) == -1)) {
58             // the string is just a variable reference, don't convert it to a string
59
String JavaDoc var = temp.substring(2, temp.length() - 1);
60
61             return getVariableFromMaps(var, transientVars, ps);
62         } else {
63             // the string passed in contains multiple variables (or none!) and should be treated as a string
64
while (true) {
65                 int x = s.indexOf("${");
66                 int y = s.indexOf("}", x);
67
68                 if ((x != -1) && (y != -1)) {
69                     String JavaDoc var = s.substring(x + 2, y);
70                     String JavaDoc t = null;
71                     Object JavaDoc o = getVariableFromMaps(var, transientVars, ps);
72
73                     if (o != null) {
74                         t = o.toString();
75                     }
76
77                     if (t != null) {
78                         s = s.substring(0, x) + t + s.substring(y + 1);
79                     } else {
80                         // the variable doesn't exist, so don't display anything
81
s = s.substring(0, x) + s.substring(y + 1);
82                     }
83                 } else {
84                     break;
85                 }
86             }
87
88             return s;
89         }
90     }
91 }
92
Popular Tags