View Javadoc

1   // $Id: InstanceOfTag.java,v 1.1 2004/06/09 19:26:40 powerpete Exp $
2   // [JMP, 04.06.2004] created this file.
3   package com.pnpconsult.zeiterfassung.tags;
4   
5   import javax.servlet.jsp.JspException;
6   import javax.servlet.jsp.tagext.TagSupport;
7   
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  import org.apache.struts.taglib.tiles.util.TagUtils;
11  
12  /***
13   * @author <a href="mailto:powerpete@users.sf.net">Moritz Petersen </a>
14   * @version $Id: InstanceOfTag.java,v 1.1 2004/06/09 19:26:40 powerpete Exp $
15   * 
16   * @jsp.tag name="instanceOf"
17   */
18  public class InstanceOfTag extends TagSupport
19  {
20      private static final Log LOG = LogFactory.getLog(InstanceOfTag.class);
21      private String name;
22      private String className;
23  
24      public int doStartTag() throws JspException
25      {
26          Object bean = TagUtils.findAttribute(name, pageContext);
27          if (bean != null && isInstanceOf(bean, className))
28          {
29              return EVAL_BODY_INCLUDE;
30          }
31          return SKIP_BODY;
32      }
33  
34      static boolean isInstanceOf(Object obj, String className)
35      {
36          try
37          {
38              return Class.forName(className).isInstance(obj);
39          }
40          catch (ClassNotFoundException e)
41          {
42              LOG.error("Class not found: " + className, e);
43              return false;
44          }
45      }
46  
47      /***
48       * @jsp.attribute required="true" rtexprvalue="false"
49       */
50      public String getClassName()
51      {
52          return className;
53      }
54  
55      public void setClassName(String className)
56      {
57          this.className = className;
58      }
59  
60      /***
61       * @jsp.attribute required="true" rtexprvalue="false"
62       */
63      public String getName()
64      {
65          return name;
66      }
67  
68      public void setName(String name)
69      {
70          this.name = name;
71      }
72  }