You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-dev@jakarta.apache.org by ba...@apache.org on 2003/05/05 18:02:45 UTC

cvs commit: jakarta-taglibs-sandbox/unstandard/src/org/apache/taglibs/unstandard InvokeTag.java BindTag.java

bayard      2003/05/05 09:02:45

  Modified:    unstandard/src/org/apache/taglibs/unstandard InvokeTag.java
                        BindTag.java
  Log:
  Can now output values to the screen.
  
  Revision  Changes    Path
  1.2       +15 -7     jakarta-taglibs-sandbox/unstandard/src/org/apache/taglibs/unstandard/InvokeTag.java
  
  Index: InvokeTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs-sandbox/unstandard/src/org/apache/taglibs/unstandard/InvokeTag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- InvokeTag.java	23 Apr 2003 06:22:11 -0000	1.1
  +++ InvokeTag.java	5 May 2003 16:02:44 -0000	1.2
  @@ -56,7 +56,10 @@
   import java.lang.reflect.Method;
   import java.lang.reflect.InvocationTargetException;
   
  +import java.io.IOException;
  +
   import javax.servlet.jsp.JspException;
  +import javax.servlet.jsp.JspWriter;
   import javax.servlet.jsp.JspTagException;
   import javax.servlet.jsp.tagext.BodyTagSupport;
   import javax.servlet.jsp.PageContext;
  @@ -69,11 +72,9 @@
    * Invokes a method on an object. Version one of the tag will 
    * just invoke a single method on an object and put the result into 
    * another variable.
  - * Later versions will allow chains of methods, parameters, 
  - * and outputting the values to the screen.
  + * Later versions will allow chains of methods, parameters.
    *
    * @author <a href="bayard@apache.org">Henri Yandell</a>
  - * @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
    */
   public class InvokeTag extends BodyTagSupport {
   
  @@ -105,7 +106,17 @@
               Class clss = target.getClass();
               Method mthd = clss.getMethod(method, new Class[0]);
               Object result = mthd.invoke(target, new Object[0]);
  -            pageContext.setAttribute( var, result);
  +            if(var == null && result != null) {
  +                try {
  +                    JspWriter writer = pageContext.getOut();
  +                    writer.print(result.toString());
  +                } catch (IOException e) {
  +                    throw new JspTagException("IOException occured: " + 
  +                                  e.getMessage());
  +                }
  +            } else {
  +                pageContext.setAttribute( var, result);
  +            }
           } catch(NoSuchMethodException e) {
               throw new JspTagException("NoSuchMethodException occured: " + 
           			      e.getMessage());
  @@ -154,9 +165,6 @@
           	ExpressionEvaluatorManager.evaluate( "var", elVar,
           					     String.class, this,
           					     this.pageContext );
  -            if( r == null ) {
  -        	throw new NullAttributeException( TAG_NAME, "var" );
  -            }
               var = ((String) r);
           }	
   
  
  
  
  1.2       +23 -30    jakarta-taglibs-sandbox/unstandard/src/org/apache/taglibs/unstandard/BindTag.java
  
  Index: BindTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs-sandbox/unstandard/src/org/apache/taglibs/unstandard/BindTag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BindTag.java	23 Apr 2003 08:11:25 -0000	1.1
  +++ BindTag.java	5 May 2003 16:02:44 -0000	1.2
  @@ -56,7 +56,10 @@
   import java.lang.reflect.Field;
   import java.lang.reflect.InvocationTargetException;
   
  +import java.io.IOException;
  +
   import javax.servlet.jsp.JspException;
  +import javax.servlet.jsp.JspWriter;
   import javax.servlet.jsp.JspTagException;
   import javax.servlet.jsp.tagext.BodyTagSupport;
   import javax.servlet.jsp.PageContext;
  @@ -74,15 +77,6 @@
    * Once this is finished it will be basically be a version of c:out for 
    * normal Java fields.
    *
  - * TODO: If target is specified, we know the type. So make type optional.
  - * The tld already says it is, but it is lying. Possibly even wrap 
  - * target and type together with special behaviour for types that are 
  - * Strings. However that seems difficult. If we want length() on 
  - * StringBuffer object, and the value in target is a String, I'm 
  - * not sure if we can tell the difference.
  - *
  - * TODO: Allow var to be optional, so people may simple output values.
  - *
    * @author <a href="bayard@apache.org">Henri Yandell</a>
    */
   public class BindTag extends BodyTagSupport {
  @@ -118,11 +112,26 @@
               // it does not handle bean-ness however.
               // if type throws exception, we can try with java.lang on 
               // the front
  -            Class clss = Class.forName(type);
  +            Class clss = null;
  +            if(target != null && type == null) {
  +                clss = target.getClass();
  +            } else {
  +                clss = Class.forName(type);
  +            }
   
               Field fld = clss.getField(field);
               Object result = fld.get(target);
  -            pageContext.setAttribute( var, result);
  +            if(var == null && result != null) {
  +                try {
  +                    JspWriter writer = pageContext.getOut();
  +                    writer.print(result.toString());
  +                } catch (IOException e) {
  +                    throw new JspTagException("IOException occured: " + 
  +                                  e.getMessage());
  +                }
  +            } else {
  +                pageContext.setAttribute( var, result);
  +            }
           } catch(ClassNotFoundException e) {
               throw new JspTagException("ClassNotFoundException occured: " + 
           			      e.getMessage());
  @@ -137,20 +146,7 @@
           			      e.getMessage());
           }
   
  -
  -        return EVAL_PAGE;
  -        /*
  -        try {
  -            if (getBodyContent() != null) {
  -        	getBodyContent().writeOut(getPreviousOut());
  -        	getBodyContent().clear();
  -            }
  -        } catch (IOException e) {
  -            throw new JspTagException("IOException occured: " + 
  -        			      e.getMessage());
  -        }
           return EVAL_PAGE;
  -        */
       }
           
       // attribute methods
  @@ -174,9 +170,6 @@
           	ExpressionEvaluatorManager.evaluate( "var", elVar,
           					     String.class, this,
           					     this.pageContext );
  -            if( r == null ) {
  -        	throw new NullAttributeException( TAG_NAME, "var" );
  -            }
               var = ((String) r);
           }	
   
  @@ -198,9 +191,6 @@
           	ExpressionEvaluatorManager.evaluate( "type", elType, 
           					     String.class, this,
           					     this.pageContext );
  -            if( r == null ) {
  -        	throw new NullAttributeException( TAG_NAME, "type" );
  -            }
               type = ((String) r);
           }	
   
  @@ -213,5 +203,8 @@
               target = ((Object) r);
           }	
   
  +        if( target == null && type == null ) {
  +        	throw new JspTagException( "Illegal for target and type to both evaluate to null. ");
  +        }
       }
   }
  
  
  

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