Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/RuntimeCollective/webapps/tag/CountryOutputTag.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/tag/CountryOutputTag.java,v 1.4 2003/09/30 15:13:16 joe Exp $
2    * $Revision: 1.4 $
3    * $Date: 2003/09/30 15:13:16 $
4    *
5    * ====================================================================
6    *
7    * Josephine : http://www.runtime-collective.com/josephine/index.html
8    *
9    * Copyright (C) 2003 Runtime Collective
10   * 
11   * This product includes software developed by the
12   * Apache Software Foundation (http://www.apache.org/).
13   *
14   * This library is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU Lesser General Public
16   * License as published by the Free Software Foundation; either
17   * version 2.1 of the License, or (at your option) any later version.
18   *
19   * This library is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   * Lesser General Public License for more details.
23   *
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this library; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27   *
28   */
29  
30  package com.RuntimeCollective.webapps.tag;
31  
32  import com.RuntimeCollective.webapps.Countries;
33  
34  import java.io.IOException;
35  import java.io.PrintStream;
36   
37  import javax.servlet.jsp.JspWriter;
38  import javax.servlet.jsp.PageContext;
39  import javax.servlet.jsp.JspException;
40  import javax.servlet.jsp.JspTagException;
41  import javax.servlet.jsp.tagext.Tag;
42  import javax.servlet.jsp.tagext.TagSupport;
43  import javax.servlet.http.HttpServletRequest; 
44  import javax.servlet.http.HttpServletResponse; 
45  import org.apache.struts.action.Action;
46  import org.apache.commons.beanutils.PropertyUtils;
47  import org.apache.struts.util.RequestUtils;
48  import org.apache.struts.util.ResponseUtils;
49  import org.apache.struts.taglib.html.BaseHandlerTag;
50  
51  
52  /** A custom JSP tag that outputs the full name of a country, given its ISO code.
53   * <ul>
54   * <li> <code> name </code> - The name of the bean (in any scope) that holds the code. [Mandatory]. </li>
55   * <li> <code> property </code> - The property of the named bean that holds the code. [Mandatory]. </li>
56   * 
57   * @version $Id: CountryOutputTag.java,v 1.4 2003/09/30 15:13:16 joe Exp $
58   */
59  public class CountryOutputTag extends BaseHandlerTag {
60  
61      // == Properties =================================================== 
62  
63      /** The name of the bean (in any scope) that holds the code. [Mandatory]. */
64      protected String name = "";
65      /** Get The name of the bean (in any scope) that holds the code. [Mandatory]. */
66      public String getName() { return this.name; }
67      /** Set The name of the bean (in any scope) that holds the code. [Mandatory]. */
68      public void setName(String name) { this.name = name; }
69  
70      /** The property of the named bean that holds the code. [Mandatory]. */
71      protected String property = "";
72      /** Get the property of the named bean that holds the code. [Mandatory]. */
73      public String getProperty() { return this.property; }
74      /** Set the property of the named bean that holds the code. [Mandatory]. */
75      public void setProperty(String property) { this.property = property; }
76  
77      // == Tag methods ==================================
78  
79      public int doStartTag() throws JspException {
80    
81    // The name and code of the country
82    String code=null;
83  
84    // Find the bean that carries the codee
85    Object bean = pageContext.findAttribute(name);
86    if (bean == null) throw new JspException ("Can't access bean! "+name);
87    try {
88        // find the code
89        code = (String) PropertyUtils.getProperty(bean, property).toString();
90    } catch (Exception e) {
91        throw new JspException ("Error in country output tag: "+e);
92    }
93  
94    // write the name.
95    try {
96        pageContext.getOut().println( Countries.getNameForCode( code ) );
97    } catch (IOException e) {
98        throw new JspTagException("I/O Exception " + e.getMessage() );
99    }
100   
101   return SKIP_BODY;
102     }
103     
104     
105     public void release() {
106   super.release();
107   property = "";
108   name = "";
109     }
110 }
111 
112 
113 
114