Commits

Anonymous committed d8e2cdb

Adding TextTag... refactoring ParameterizedTag's...

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@126573baa09-0c28-0410-bef9-dab3c582ae83

Comments (0)

Files changed (6)

src/java/com/opensymphony/webwork/views/jsp/ActionTag.java

 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.jsp.JspException;
-import javax.servlet.jsp.tagext.TagSupport;
 
 
 /**
  * @author <a href="mailto:plightbo@hotmail.com">Pat Lightbody</a>
  * @author Matt Ho <a href="mailto:matt@indigoegg.com">&lt;matt@indigoegg.com&gt;</a>
  */
-public class ActionTag extends TagSupport implements WebWorkStatics, ParameterizedTag, Renderer {
+public class ActionTag extends ParameterizedTagSupport implements WebWorkStatics, Renderer {
     //~ Static fields/initializers /////////////////////////////////////////////
 
     private static final Log log = LogFactory.getLog(ActionTag.class);
     //~ Instance fields ////////////////////////////////////////////////////////
 
     ActionProxy proxy;
-    Map params;
     String name;
     String namespace;
     boolean executeResult;
         this.namespace = namespace;
     }
 
-    public Map getParams() {
-        return params;
-    }
-
-    public void addParam(String key, Object value) {
-        if (params == null) {
-            params = new HashMap();
-        }
-
-        params.put(key, value);
-    }
-
     public int doEndTag() throws JspException {
         // execute the action and save the proxy (and the namespace) as instance variables
         executeAction();

src/java/com/opensymphony/webwork/views/jsp/ParameterizedTagSupport.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.webwork.views.jsp;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.jsp.tagext.TagSupport;
+
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: jcarreira
+ * Date: Oct 16, 2003
+ * Time: 11:00:38 PM
+ * To change this template use Options | File Templates.
+ */
+public abstract class ParameterizedTagSupport extends TagSupport implements ParameterizedTag {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    Map params;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public Map getParams() {
+        if (params == null) {
+            params = new HashMap();
+        }
+
+        return params;
+    }
+
+    public void addParam(String key, Object value) {
+        if (key != null) {
+            Map myParams = getParams();
+
+            if (value == null) {
+                myParams.remove(key);
+            } else {
+                params.put(key, value);
+            }
+        }
+    }
+}

src/java/com/opensymphony/webwork/views/jsp/URLTag.java

 package com.opensymphony.webwork.views.jsp;
 
 import com.opensymphony.webwork.views.util.UrlHelper;
+
 import com.opensymphony.xwork.ActionContext;
 import com.opensymphony.xwork.util.OgnlValueStack;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import java.io.IOException;
+
+import java.util.HashMap;
+import java.util.Map;
+
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.jsp.JspException;
 import javax.servlet.jsp.PageContext;
-import javax.servlet.jsp.tagext.TagSupport;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
 
 
 /**
  * @author Jason Carreira (jason@zenfrog.com)
  * @version $Revision$
  */
-public class URLTag extends TagSupport implements ParameterizedTag {
+public class URLTag extends ParameterizedTagSupport {
     //~ Static fields/initializers /////////////////////////////////////////////
 
     private static final Log log = LogFactory.getLog(URLTag.class);
 
     //~ Instance fields ////////////////////////////////////////////////////////
 
-    protected Map params;
-    protected String value;
     protected String page;
+    protected String value;
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
-    public Map getParams() {
-        return params;
-    }
-
     public void setPage(String page) {
         this.page = page;
     }
         this.value = value;
     }
 
-    public void addParam(String key, Object value) {
-        if (params == null) {
-            params = new HashMap();
-        }
-
-        if (value == null) {
-            params.remove(key);
-        } else {
-            params.put(key, value);
-        }
-    }
-
     public int doEndTag() throws JspException {
         HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
         HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();

src/java/com/opensymphony/webwork/views/jsp/ui/AbstractUITag.java

 package com.opensymphony.webwork.views.jsp.ui;
 
 import com.opensymphony.webwork.config.Configuration;
-import com.opensymphony.webwork.views.jsp.ParameterizedTag;
+import com.opensymphony.webwork.views.jsp.ParameterizedTagSupport;
 import com.opensymphony.webwork.views.velocity.VelocityManager;
-import com.opensymphony.webwork.dispatcher.ServletDispatcher;
-import com.opensymphony.webwork.dispatcher.SessionMap;
-import com.opensymphony.webwork.dispatcher.ApplicationMap;
-import com.opensymphony.webwork.ServletActionContext;
+
 import com.opensymphony.xwork.util.OgnlValueStack;
 
 import org.apache.commons.logging.Log;
 import java.io.Writer;
 
 import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.Map;
 import java.util.Properties;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpServletRequest;
 import javax.servlet.jsp.JspException;
-import javax.servlet.jsp.tagext.TagSupport;
 
 
 /**
  * @version $Id$
  * @author Matt Ho <a href="mailto:matt@enginegreen.com">&lt;matt@enginegreen.com&gt;</a>
  */
-public abstract class AbstractUITag extends TagSupport implements ParameterizedTag {
+public abstract class AbstractUITag extends ParameterizedTagSupport {
     //~ Static fields/initializers /////////////////////////////////////////////
 
     private static final Log LOG = LogFactory.getLog(AbstractUITag.class);
 
     //~ Instance fields ////////////////////////////////////////////////////////
 
-    protected Map params = new HashMap();
-    protected String themeAttr;
-    protected String templateAttr;
-    protected String nameAttr;
-    protected String valueAttr;
     protected String labelAttr;
     protected String labelPositionAttr;
+    protected String nameAttr;
+    protected String templateAttr;
+    protected String themeAttr;
+    protected String valueAttr;
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
-    public void setTheme(String aName) {
-       themeAttr = aName;
-    }
-
-    public void setTemplate(String aName) {
-       templateAttr = aName;
-    }
-
     public void setLabel(String aLabel) {
-       labelAttr = aLabel;
+        labelAttr = aLabel;
     }
 
-    public void setName(String aName) {
-       nameAttr = aName;
+    public void setLabelposition(String aLabelPosition) {
+        labelPositionAttr = aLabelPosition;
     }
 
-    public void setValue(String aValue) {
-       valueAttr = aValue;
+    public void setName(String aName) {
+        nameAttr = aName;
     }
 
-    public void setLabelposition(String aLabelPosition) {
-       labelPositionAttr = aLabelPosition;
+    public void setTemplate(String aName) {
+        templateAttr = aName;
     }
 
-    /**
-     * com.opensymphony.webwork.views.jsp.ParameterizedTag implementation
-     * @return a Map of user specified Map parameters
-     * @see ParameterizedTag
-     */
-    public Map getParams() {
-        return params;
+    public void setTheme(String aName) {
+        themeAttr = aName;
     }
 
-    /**
-     * com.opensymphony.webwork.views.jsp.ParameterizedTag implementation
-     * @param key the String name of a parameter to add
-     * @param value the value associated with that parameter
-     * @see ParameterizedTag
-     */
-    public void addParam(String key, Object value) {
-        this.getParams().put(key, value);
+    public void setValue(String aValue) {
+        valueAttr = aValue;
     }
 
     public int doEndTag() throws JspException {
         }
     }
 
-    protected void evaluateParams(OgnlValueStack stack) {
-        Object name = null;
-        if (nameAttr != null) {
-            name = stack.findValue(nameAttr, String.class);
-            addParam("name", name);
-        }
-
-        if (labelAttr != null) {
-            addParam("label", stack.findValue(labelAttr, String.class));
-        }
-
-        if (labelPositionAttr != null) {
-            addParam("labelPosition", stack.findValue(labelPositionAttr, String.class));
-        }
-
-        if (valueAttr != null) {
-            addParam("value", stack.findValue(valueAttr, String.class));
-        } else if (name != null) {
-            addParam("value", stack.findValue(name.toString(), String.class));
-        }
-
-        evaluateExtraParams(stack);
-   }
-
-    protected void evaluateExtraParams(OgnlValueStack stack) {
-    }
-
-    protected OgnlValueStack getValueStack() {
-        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
-        OgnlValueStack stack = (OgnlValueStack) req.getAttribute("webwork.valueStack");
-        if (stack == null) {
-            stack = new OgnlValueStack();
-            HttpServletResponse res = (HttpServletResponse) pageContext.getResponse();
-            Map extraContext = ServletDispatcher.createContextMap(req.getParameterMap(),
-                    new SessionMap(req.getSession()),
-                    new ApplicationMap(pageContext.getServletContext()),
-                    req,
-                    res,
-                    pageContext.getServletConfig());
-            extraContext.put(ServletActionContext.PAGE_CONTEXT, pageContext);
-            stack.getContext().putAll(extraContext);
-            req.setAttribute("webwork.valueStack", stack);
-        }
-        return stack;
-    }
-
     public int doStartTag() throws JspException {
         /**
          * Migrated instantiation of the params HashMap to here from the constructor to facilitate implementation of the
          * release() method.
          */
-        this.params = new HashMap();
+        getParams().clear();
 
         return EVAL_BODY_INCLUDE;
     }
         return buildTemplateName(templateAttr, getDefaultTemplate());
     }
 
+    protected OgnlValueStack getValueStack() {
+        return UITagUtil.getValueStack(pageContext);
+    }
+
     /**
      *
      * @param myTemplate
         }
     }
 
+    protected void evaluateExtraParams(OgnlValueStack stack) {
+    }
+
+    protected void evaluateParams(OgnlValueStack stack) {
+        Object name = null;
+
+        if (nameAttr != null) {
+            name = stack.findValue(nameAttr, String.class);
+            addParam("name", name);
+        }
+
+        if (labelAttr != null) {
+            addParam("label", stack.findValue(labelAttr, String.class));
+        }
+
+        if (labelPositionAttr != null) {
+            addParam("labelPosition", stack.findValue(labelPositionAttr, String.class));
+        }
+
+        if (valueAttr != null) {
+            addParam("value", stack.findValue(valueAttr, String.class));
+        } else if (name != null) {
+            addParam("value", stack.findValue(name.toString(), String.class));
+        }
+
+        evaluateExtraParams(stack);
+    }
+
     protected void mergeTemplate(String templateName) throws Exception {
         Template t = velocityEngine.getTemplate(templateName);
         Context context = VelocityManager.createContext(pageContext.getServletConfig(), pageContext.getRequest(), pageContext.getResponse());
          * the request, it might also make sense for consistency to send the page and res and any others.
          */
         context.put("tag", this);
-        context.put("parameters", params);
+        context.put("parameters", getParams());
 
         t.merge(context, outputWriter);
     }

src/java/com/opensymphony/webwork/views/jsp/ui/TextTag.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.webwork.views.jsp.ui;
+
+import com.opensymphony.webwork.views.jsp.ParameterizedTag;
+
+import com.opensymphony.xwork.util.OgnlValueStack;
+
+import java.io.IOException;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.tagext.BodyTagSupport;
+
+
+/**
+ * Access a i18n-ized message. The message must be in a resource bundle
+ * with the same name as the action that it is associated with. In practice
+ * this means that you should create a properties file in the same package
+ * as your Java class with the same name as your class, but with .properties
+ * extension.
+ *
+ * See examples for further info on how to use.
+ *
+ * If the named message is not found, then the body of the tag will be used as default message.
+ * If no body is used, then the name of the message will be used.
+ *
+ * @author Jason Carreira
+ */
+public class TextTag extends BodyTagSupport implements ParameterizedTag {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    List values;
+    String nameAttr;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public void setName(String name) {
+        this.nameAttr = name;
+    }
+
+    public Map getParams() {
+        return null;
+    }
+
+    public void addParam(String key, Object value) {
+        addParam(value);
+    }
+
+    public void addParam(Object value) {
+        if (value == null) {
+            return;
+        }
+
+        if (values == null) {
+            values = new ArrayList();
+        }
+
+        values.add(value);
+    }
+
+    // BodyTag implementation ----------------------------------------
+    public int doEndTag() throws JspException {
+        OgnlValueStack stack = UITagUtil.getValueStack(pageContext);
+
+        String defaultMessage;
+
+        if ((bodyContent != null) && (bodyContent.getString().trim().length() > 0)) {
+            defaultMessage = bodyContent.getString().trim();
+        } else {
+            defaultMessage = nameAttr;
+        }
+
+        String expression = "text(" + nameAttr + ", " + defaultMessage;
+
+        if (values != null) {
+            ListValueHolder listValueHolder = new ListValueHolder(values);
+            stack.push(listValueHolder);
+            expression = expression + ", textTagListValueHolderList";
+        }
+
+        expression = expression + ")";
+
+        String msg = (String) stack.findValue(expression, String.class);
+
+        try {
+            pageContext.getOut().write(msg);
+        } catch (IOException e) {
+            e.printStackTrace();
+            throw new JspException(e);
+        }
+
+        return EVAL_PAGE;
+    }
+
+    public int doStartTag() throws JspException {
+        values = null;
+
+        return super.doStartTag();
+    }
+
+    //~ Inner Classes //////////////////////////////////////////////////////////
+
+    private class ListValueHolder {
+        // try to give it an uncommon name
+        private List textTagListValueHolderList;
+
+        public ListValueHolder(List textTagListValueHolderList) {
+            this.textTagListValueHolderList = textTagListValueHolderList;
+        }
+
+        public List getTextTagListValueHolderList() {
+            return textTagListValueHolderList;
+        }
+    }
+}

src/java/com/opensymphony/webwork/views/jsp/ui/UITagUtil.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.webwork.views.jsp.ui;
+
+import com.opensymphony.webwork.ServletActionContext;
+import com.opensymphony.webwork.dispatcher.ApplicationMap;
+import com.opensymphony.webwork.dispatcher.ServletDispatcher;
+import com.opensymphony.webwork.dispatcher.SessionMap;
+
+import com.opensymphony.xwork.util.OgnlValueStack;
+
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.jsp.PageContext;
+
+
+/**
+ * Util class to encapsulate UI tag utils
+ */
+public class UITagUtil {
+    //~ Constructors ///////////////////////////////////////////////////////////
+
+    private UITagUtil() {
+    }
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public static OgnlValueStack getValueStack(PageContext pageContext) {
+        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
+        OgnlValueStack stack = (OgnlValueStack) req.getAttribute("webwork.valueStack");
+
+        if (stack == null) {
+            stack = new OgnlValueStack();
+
+            HttpServletResponse res = (HttpServletResponse) pageContext.getResponse();
+            Map extraContext = ServletDispatcher.createContextMap(req.getParameterMap(), new SessionMap(req.getSession()), new ApplicationMap(pageContext.getServletContext()), req, res, pageContext.getServletConfig());
+            extraContext.put(ServletActionContext.PAGE_CONTEXT, pageContext);
+            stack.getContext().putAll(extraContext);
+            req.setAttribute("webwork.valueStack", stack);
+        }
+
+        return stack;
+    }
+}