1 package com.opensymphony.webwork.components; 2 3 import com.opensymphony.webwork.config.Configuration; 4 import com.opensymphony.webwork.util.FastByteArrayOutputStream; 5 import com.opensymphony.xwork.util.OgnlValueStack; 6 import com.opensymphony.xwork.util.TextParseUtil; 7 8 import java.io.PrintWriter ; 9 import java.io.Writer ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 import java.util.Stack ; 14 15 20 public class Component { 21 public static final boolean ALT_SYNTAX = "true".equals(Configuration.getString("webwork.tag.altSyntax")); 22 public static final String COMPONENT_STACK = "__component_stack"; 23 24 protected OgnlValueStack stack; 25 protected Map parameters; 26 protected String id; 27 28 public Component(OgnlValueStack stack) { 29 this.stack = stack; 30 this.parameters = new HashMap (); 31 getComponentStack().push(this); 32 } 33 34 public OgnlValueStack getStack() { 35 return stack; 36 } 37 38 private Stack getComponentStack() { 39 Stack componentStack = (Stack) stack.getContext().get(COMPONENT_STACK); 40 if (componentStack == null) { 41 componentStack = new Stack(); 42 stack.getContext().put(COMPONENT_STACK, componentStack); 43 } 44 return componentStack; 45 } 46 47 public void start(Writer writer) { 48 } 49 50 public void end(Writer writer) { 51 getComponentStack().pop(); 52 } 53 54 public Component findAncestor(Class clazz) { 55 Stack componentStack = getComponentStack(); 56 for (Iterator iterator = componentStack.iterator(); iterator.hasNext();) { 57 Component component = (Component) iterator.next(); 58 if (component.getClass().isAssignableFrom(clazz) && component != this) { 59 return component; 60 } 61 } 62 63 return null; 64 } 65 66 public Component findLastAncestor(Class clazz) { 67 Stack componentStack = getComponentStack(); 68 for (int i = componentStack.size() - 1; i >= 0; i--) { 69 Component component = (Component) componentStack.get(i); 70 if (component.getClass().isAssignableFrom(clazz) && component != this) { 71 return component; 72 } 73 } 74 75 return null; 76 } 77 78 protected String findString(String expr) { 79 return (String ) findValue(expr, String .class); 80 } 81 82 protected Object findValue(String expr) { 83 if (expr == null) { 84 return null; 85 } 86 87 if (ALT_SYNTAX) { 88 if (expr.startsWith("%{") && expr.endsWith("}")) { 90 expr = expr.substring(2, expr.length() - 1); 91 } 92 } 93 94 return getStack().findValue(expr); 95 } 96 97 protected Object findValue(String expr, Class toType) { 98 if (ALT_SYNTAX && toType == String .class) { 99 return translateVariables(expr, getStack()); 100 } else { 101 if (ALT_SYNTAX) { 102 if (expr.startsWith("%{") && expr.endsWith("}")) { 104 expr = expr.substring(2, expr.length() - 1); 105 } 106 } 107 108 return getStack().findValue(expr, toType); 109 } 110 } 111 112 public static String translateVariables(String expression, OgnlValueStack stack) { 113 return TextParseUtil.translateVariables('%', expression, stack); 114 } 115 116 124 public void copyParams(Map params) { 125 stack.push(parameters); 126 stack.push(this); 127 try { 128 for (Iterator iterator = params.entrySet().iterator(); iterator.hasNext();) { 129 Map.Entry entry = (Map.Entry ) iterator.next(); 130 String key = (String ) entry.getKey(); 131 stack.setValue(key, entry.getValue()); 132 } 133 } finally { 134 stack.pop(); 135 stack.pop(); 136 } 137 } 138 139 protected String toString(Throwable t) { 140 FastByteArrayOutputStream bout = new FastByteArrayOutputStream(); 141 PrintWriter wrt = new PrintWriter (bout); 142 t.printStackTrace(wrt); 143 wrt.close(); 144 145 return bout.toString(); 146 } 147 148 public Map getParameters() { 149 return parameters; 150 } 151 152 public void addAllParameters(Map params) { 153 parameters.putAll(params); 154 } 155 156 public void addParameter(String key, Object value) { 157 if (key != null) { 158 Map params = getParameters(); 159 160 if (value == null) { 161 params.remove(key); 162 } else { 163 params.put(key, value); 164 } 165 } 166 } 167 168 public String getId() { 169 return id; 170 } 171 172 public void setId(String id) { 173 this.id = id; 174 } 175 } 176 | Popular Tags |