struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dgra...@apache.org
Subject cvs commit: jakarta-struts/src/share/org/apache/struts/taglib/logic EmptyTag.java
Date Sat, 10 May 2003 17:37:44 GMT
dgraham     2003/05/10 10:37:44

  Modified:    src/share/org/apache/struts/taglib/bean DefineTag.java
               src/share/org/apache/struts/util RequestUtils.java
               src/share/org/apache/struts/taglib/tiles/util TagUtils.java
               src/share/org/apache/struts/taglib/logic EmptyTag.java
  Log:
  Refactored converting a scope name into its integer constant
  into a new method RequestUtils.getScope(String).  Changed the
  DefineTag, EmptyTag, and TagUtils classes to use the new
  method.
  
  Revision  Changes    Path
  1.20      +12 -10    jakarta-struts/src/share/org/apache/struts/taglib/bean/DefineTag.java
  
  Index: DefineTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/bean/DefineTag.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- DefineTag.java	22 Sep 2002 06:32:45 -0000	1.19
  +++ DefineTag.java	10 May 2003 17:37:43 -0000	1.20
  @@ -277,12 +277,14 @@
   
           // Expose this value as a scripting variable
           int inScope = PageContext.PAGE_SCOPE;
  -        if ("request".equals(toScope))
  -            inScope = PageContext.REQUEST_SCOPE;
  -        else if ("session".equals(toScope))
  -            inScope = PageContext.SESSION_SCOPE;
  -        else if ("application".equals(toScope))
  -            inScope = PageContext.APPLICATION_SCOPE;
  +        try {
  +			if (toScope != null) {
  +				inScope = RequestUtils.getScope(toScope);
  +			}
  +		} catch (JspException e) {
  +			//  toScope was invalid name so we default to PAGE_SCOPE
  +		}
  +            
           pageContext.setAttribute(id, value, inScope);
   
           // Continue processing this page
  
  
  
  1.100     +46 -24    jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java
  
  Index: RequestUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v
  retrieving revision 1.99
  retrieving revision 1.100
  diff -u -r1.99 -r1.100
  --- RequestUtils.java	6 May 2003 23:39:47 -0000	1.99
  +++ RequestUtils.java	10 May 2003 17:37:43 -0000	1.100
  @@ -145,7 +145,13 @@
       private static Method encode = null;
       
       /**
  -     * Initialize the encode variable with the 1.4 method if available
  +     * Maps lowercase JSP scope names to their PageContext integer constant values. 
  +     */
  +    private static Map scopes = new HashMap();
  +    
  +    /**
  +     * Initialize the encode variable with the 1.4 method if available.  Also set up the
  +     * scope map values.
        */
       static {
           try {
  @@ -155,6 +161,11 @@
           } catch (NoSuchMethodException e) {
               log.debug("Could not find Java 1.4 encode method.  Using deprecated version.",
e);
           }
  +        
  +        scopes.put("page", new Integer(PageContext.PAGE_SCOPE));
  +        scopes.put("request", new Integer(PageContext.REQUEST_SCOPE));
  +        scopes.put("session", new Integer(PageContext.SESSION_SCOPE));
  +        scopes.put("application", new Integer(PageContext.APPLICATION_SCOPE));
       }
   
       // --------------------------------------------------------- Public Methods
  @@ -789,34 +800,45 @@
        *
        * @param pageContext Page context to be searched
        * @param name Name of the bean to be retrieved
  -     * @param scope Scope to be searched (page, request, session, application)
  +     * @param scopeName Scope to be searched (page, request, session, application)
        *  or <code>null</code> to use <code>findAttribute()</code>
instead
        * @return JavaBean in the specified page context
        * @exception JspException if an invalid scope name
        *  is requested
        */
  -    public static Object lookup(PageContext pageContext, String name, String scope)
  +    public static Object lookup(PageContext pageContext, String name, String scopeName)
           throws JspException {
  -
  -        Object bean = null;
  -        if (scope == null) {
  -            bean = pageContext.findAttribute(name);
  -        } else if (scope.equalsIgnoreCase("page")) {
  -            bean = pageContext.getAttribute(name, PageContext.PAGE_SCOPE);
  -        } else if (scope.equalsIgnoreCase("request")) {
  -            bean = pageContext.getAttribute(name, PageContext.REQUEST_SCOPE);
  -        } else if (scope.equalsIgnoreCase("session")) {
  -            bean = pageContext.getAttribute(name, PageContext.SESSION_SCOPE);
  -        } else if (scope.equalsIgnoreCase("application")) {
  -            bean = pageContext.getAttribute(name, PageContext.APPLICATION_SCOPE);
  -        } else {
  -            JspException e = new JspException(messages.getMessage("lookup.scope", scope));
  -            saveException(pageContext, e);
  -            throw e;
  +            
  +        if (scopeName == null) {
  +        	return pageContext.findAttribute(name);
  +        }
  +        
  +        try {
  +        	return pageContext.getAttribute(name, getScope(scopeName));
  +        
  +        } catch (JspException e) {
  +        	saveException(pageContext, e);
  +        	throw e;
           }
   
  -        return (bean);
  +    }
  +    
  +    /**
  +     * Converts the scope name into its corresponding PageContext constant value.
  +     * @param scopeName Can be "page", "request", "session", or "application" in any
  +     * case.
  +     * @return The constant representing the scope (ie. PageContext.REQUEST_SCOPE).
  +     * @throws JspException if the scopeName is not a valid name.
  +     * @since Struts 1.1
  +     */
  +    public static int getScope(String scopeName) throws JspException {
  +		Integer scope = (Integer) scopes.get(scopeName.toLowerCase());
  +
  +		if (scope == null) {
  +			throw new JspException(messages.getMessage("lookup.scope", scope));
  +		}
   
  +		return scope.intValue();
       }
   
       /**
  
  
  
  1.6       +46 -37    jakarta-struts/src/share/org/apache/struts/taglib/tiles/util/TagUtils.java
  
  Index: TagUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/tiles/util/TagUtils.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TagUtils.java	27 Feb 2003 19:18:56 -0000	1.5
  +++ TagUtils.java	10 May 2003 17:37:44 -0000	1.6
  @@ -7,7 +7,7 @@
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -75,17 +75,22 @@
   import org.apache.struts.tiles.FactoryNotFoundException;
   import org.apache.struts.tiles.NoSuchDefinitionException;
   import org.apache.struts.tiles.TilesUtil;
  +import org.apache.struts.util.RequestUtils;
   
   /**
    * Collection of utilities.
    * This class also serves as an interface between Components and Struts. If
    * you want to rip away Struts, simply reimplement some methods in this class.
    * You can copy them from Struts.
  + * 
  + * @author Cedric Dumoulin
  + * @author David Graham
    */
   public class TagUtils {
   
       /** Debug flag */
  -    static public final boolean debug = true;
  +    public static final boolean debug = true;
  +    
       /**
       * Get scope value from string value
       * @param scopeName Scope as a String.
  @@ -93,26 +98,19 @@
       * @return Scope as an <code>int</code>, or <code>defaultValue</code>
if scope is <code>null</code>.
       * @throws JspException Scope name is not recognized as a valid scope.
       */
  -    static public int getScope(String scopeName, int defaultValue) throws JspException
{
  -        if (scopeName == null)
  +    public static int getScope(String scopeName, int defaultValue) throws JspException
{
  +        if (scopeName == null) {
               return defaultValue;
  -        else if (scopeName.equals("request")) {
  -            return PageContext.REQUEST_SCOPE;
  -        } else if (scopeName.equals("page")) {
  -            return PageContext.PAGE_SCOPE;
  -        } else if (scopeName.equals("session")) {
  -            return PageContext.SESSION_SCOPE;
  -        } else if (scopeName.equals("application")) {
  -            return PageContext.APPLICATION_SCOPE;
  -        } else if (scopeName.equals("component")) {
  +        }
  +        
  +        if (scopeName.equalsIgnoreCase("component")) {
               return ComponentConstants.COMPONENT_SCOPE;
  -        } else if (scopeName.equals("template")) {
  +        } else if (scopeName.equalsIgnoreCase("template")) {
               return ComponentConstants.COMPONENT_SCOPE;
  -        } else if (scopeName.equals("tile")) {
  +        } else if (scopeName.equalsIgnoreCase("tile")) {
               return ComponentConstants.COMPONENT_SCOPE;
           } else {
  -            throw new JspException(
  -                "Error - scope translation tag : unrecognized scope '" + scopeName + "'");
  +            return RequestUtils.getScope(scopeName);
           }
       }
   
  @@ -132,10 +130,14 @@
        * @exception NoSuchMethodException if an accessor method for this
        *  propety cannot be found
        */
  -    public static Object getProperty(Object bean, String name)
  -        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
  -        return (PropertyUtils.getProperty(bean, name));
  -    }
  +	public static Object getProperty(Object bean, String name)
  +		throws
  +			IllegalAccessException,
  +			InvocationTargetException,
  +			NoSuchMethodException {
  +
  +		return PropertyUtils.getProperty(bean, name);
  +	}
   
       /**
        * Retrieve bean from page context, using specified scope.
  @@ -150,12 +152,14 @@
        */
       public static Object retrieveBean(String beanName, String scopeName, PageContext pageContext)
           throws JspException {
  +        
           if (scopeName == null) {
               return findAttribute(beanName, pageContext);
  -        } // end if
  +        }
   
           // Default value doesn't matter because we have already check it
           int scope = getScope(scopeName, PageContext.PAGE_SCOPE);
  +        
           //return pageContext.getAttribute( beanName, scope );
           return getAttribute(beanName, scope, pageContext);
       }
  @@ -168,13 +172,14 @@
        * @return Requested bean or <code>null</code> if not found.
        */
       public static Object findAttribute(String beanName, PageContext pageContext) {
  -        Object attribute;
           ComponentContext compContext = ComponentContext.getContext(pageContext.getRequest());
  +        
           if (compContext != null) {
  -            attribute = compContext.findAttribute(beanName, pageContext);
  -            if (attribute != null)
  +            Object attribute = compContext.findAttribute(beanName, pageContext);
  +            if (attribute != null) {
                   return attribute;
  -        } // end if
  +            }
  +        }
   
           // Search in pageContext scopes
           return pageContext.findAttribute(beanName);
  @@ -213,7 +218,7 @@
        *  IllegalAccessException, IllegalArgumentException,
        *  InvocationTargetException, or NoSuchMethodException
        */
  -    static public Object getRealValueFromBean(
  +    public static Object getRealValueFromBean(
           String beanName,
           String beanProperty,
           String beanScope,
  @@ -223,11 +228,13 @@
           try {
               Object realValue;
               Object bean = retrieveBean(beanName, beanScope, pageContext);
  -            if (bean != null && beanProperty != null)
  +            if (bean != null && beanProperty != null) {
                   realValue = getProperty(bean, beanProperty);
  -            else
  +            } else {
                   realValue = bean; // value can be null
  +            }
               return realValue;
  +            
           } catch (NoSuchMethodException ex) {
               throw new JspException(
                   "Error - component.PutAttributeTag : Error while retrieving value from
bean '"
  @@ -273,7 +280,7 @@
        *
        * @exception JspException Scope name is not recognized as a valid scope
        */
  -    static public void setAttribute(
  +    public static void setAttribute(
           PageContext pageContext,
           String name,
           Object value,
  @@ -304,7 +311,7 @@
        *
        * @exception JspException Scope name is not recognized as a valid scope
        */
  -    static public void setAttribute(PageContext pageContext, String name, Object beanValue)
  +    public static void setAttribute(PageContext pageContext, String name, Object beanValue)
           throws JspException {
           pageContext.setAttribute(name, beanValue, PageContext.REQUEST_SCOPE);
       }
  @@ -327,11 +334,13 @@
        */
       public static ComponentDefinition getComponentDefinition(String name, PageContext pageContext)
           throws JspException {
  +            
           try {
               return TilesUtil.getDefinition(
                   name,
                   pageContext.getRequest(),
                   pageContext.getServletContext());
  +                
           } catch (NoSuchDefinitionException ex) {
               throw new JspException(
                   "Error : Can't get component definition for '"
  @@ -339,14 +348,14 @@
                       + "'. Check if this name exist in component definitions.");
           } catch (FactoryNotFoundException ex) { // factory not found.
               throw new JspException(ex.getMessage());
  -        } // end catch
  -        catch (DefinitionsFactoryException ex) {
  +            
  +        } catch (DefinitionsFactoryException ex) {
               if (debug)
                   ex.printStackTrace();
               // Save exception to be able to show it later
               saveException(pageContext, ex);
               throw new JspException(ex.getMessage());
  -        } // end catch
  +        }
       }
   
   }
  
  
  
  1.6       +35 -36    jakarta-struts/src/share/org/apache/struts/taglib/logic/EmptyTag.java
  
  Index: EmptyTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/logic/EmptyTag.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- EmptyTag.java	26 Oct 2002 04:44:28 -0000	1.5
  +++ EmptyTag.java	10 May 2003 17:37:44 -0000	1.6
  @@ -7,7 +7,7 @@
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -59,7 +59,6 @@
    *
    */
   
  -
   package org.apache.struts.taglib.logic;
   
   import java.util.Collection;
  @@ -67,16 +66,15 @@
   import javax.servlet.jsp.JspException;
   import org.apache.struts.util.RequestUtils;
   
  -
   /**
    * Evalute the nested body content of this tag if the specified value
    * is empty for this request.
    *
    * @author Martin Cooper
  + * @author David Graham
    * @version $Revision$ $Date$
    * @since Struts 1.1
    */
  -
   public class EmptyTag extends ConditionalTagBase {
   
   
  @@ -109,35 +107,36 @@
        * @exception JspException if a JSP exception occurs
        */
       protected boolean condition(boolean desired) throws JspException {
  +		if (this.name == null) {
  +			JspException e =
  +				new JspException(messages.getMessage("empty.noNameAttribute"));
  +			RequestUtils.saveException(pageContext, e);
  +			throw e;
  +		}
  +
  +		boolean empty = true;
  +		Object value = RequestUtils.lookup(pageContext, name, property, scope);
  +        
  +		if (value != null) {
  +            
  +			if (value instanceof String) {
  +				String strValue = (String) value;
  +				empty = (strValue.length() < 1);
  +                
  +			} else if (value instanceof Collection) {
  +				Collection collValue = (Collection) value;
  +				empty = collValue.isEmpty();
  +                
  +			} else if (value instanceof Map) {
  +				Map mapValue = (Map) value;
  +				empty = mapValue.isEmpty();
  +                
  +			} else {
  +				empty = false;
  +			}
  +		}
   
  -        // Evaluate the presence of the specified value
  -        boolean empty = true;
  -        if (name != null) {
  -            Object value =
  -                RequestUtils.lookup(pageContext, name, property, scope);
  -            if (value != null) {
  -                if (value instanceof String) {
  -                    String strValue = (String)value;
  -                    empty = (strValue.length() < 1);
  -                } else if (value instanceof Collection) {
  -                    Collection collValue = (Collection)value;
  -                    empty = collValue.isEmpty();
  -                } else if (value instanceof Map) {
  -                    Map mapValue = (Map)value;
  -                    empty = mapValue.isEmpty();
  -                } else {
  -                    empty = false;
  -                }
  -            }
  -        } else {
  -            JspException e = new JspException
  -                (messages.getMessage("empty.noNameAttribute"));
  -            RequestUtils.saveException(pageContext, e);
  -            throw e;
  -        }
  -
  -        return (empty == desired);
  -
  +		return (empty == desired);
       }
   
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org


Mime
View raw message