Mega Code Archive

 
Categories / Java / JSP
 

Logo Tag

package com.rntsoft; ///Custom tag import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyTagSupport; import javax.servlet.jsp.tagext.TryCatchFinally; /**  * This tag generates a thumbnail image using HTML img tag, next to text  * message. The user specifies the content of the message and the Heading level  * (i.e.,  * <H1>-  * <H2>)  */ public class LogoTag extends BodyTagSupport implements TryCatchFinally {   private String heading = null;   private String image = null;   //stamp.gif, 42 x 54   private String width = null;   private String height = null;   public int doStartTag() throws JspException {     //this method assumes that attribute properties have been set.     try {       int h = new Integer(heading).intValue();       if (!(h > 0 && h < 7))         throw new JspException(             "The 'heading' attribute value must between 1 and 6 inclusive.");     } catch (Exception e) {       throw new JspException(e.getMessage());     }     return EVAL_BODY_BUFFERED;   }   public int doEndTag() throws JspException {     JspWriter out = pageContext.getOut();     String imgDir = ((HttpServletRequest) pageContext.getRequest())         .getContextPath()         + "/images/";     String message = getBodyContent().getString().trim();     try {       out.println(new StringBuffer("<img src=\"").append(imgDir).append(           image).append("\" width=\"").append(width).append(           "\" height=\"").append(height).append("\" align=\"left\">")           .append("<H").append(heading).append(">").append(message)           .append("</H").append(heading).append(">").toString());     } catch (java.io.IOException io) {     }     return EVAL_PAGE;   }   public void doCatch(Throwable t) {     try {       pageContext.getOut().println(t.getMessage() + "<br />");     } catch (java.io.IOException io) {     }   }   public void doFinally() {     //do nothing here, since we don't have any resources open     //like database connections   }   public void setHeading(String level) {     this.heading = level;   }   public void setImage(String name) {     this.image = name;   }   public void setWidth(String width) {     this.width = width;   }   public void setHeight(String height) {     this.height = height;   }   public void release() {     heading = null;     image = null;     width = null;     height = null;   } } // package com.rntsoft; import java.io.IOException; import javax.servlet.jsp.JspContext; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.SimpleTagSupport; /**  * This tag generates a thumbnail image using HTML img tag, next to text  * message. The user specifies the content of the message and the Heading level  * (i.e.,  * <H1>-  * <H2>)  */ public class SimpleLogoTag extends SimpleTagSupport {   private String heading = null;   private String image = null;   private String width = null;   private String height = null;   public void doTag() throws JspException, IOException {     JspContext jspContext = getJspContext();     //this method assumes that attribute properties have been set.     try {       int h = new Integer(heading).intValue();       if (!(h > 0 && h < 7))         throw new JspException(             "The 'heading' attribute value must between 1 and 6 inclusive.");     } catch (Exception e) {       throw new JspException(e.getMessage());     }     JspWriter out = jspContext.getOut();     String imgDir = (String) jspContext.findAttribute("imgDir");     if (imgDir == null || "".equals(imgDir))       throw new JspException(           "No attribute provided specifying the application's image directory.");     out.println(new StringBuffer("<img src=\"").append(imgDir)         .append(image).append("\" width=\"").append(width).append(             "\" height=\"").append(height).append(             "\" align=\"left\">").append("<H").append(heading)         .append(">").toString());     getJspBody().invoke(null);     out.println(new StringBuffer("</H").append(heading).append(">")         .toString());   }   public void setHeading(String level) {     this.heading = level;   }   public void setImage(String name) {     this.image = name;   }   public void setWidth(String width) {     this.width = width;   }   public void setHeight(String height) {     this.height = height;   } } //logo.tag <%@ tag body-content="scriptless" description="Writes the HTML code for inserting a logo." %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ attribute name="heading" required="true" rtexprvalue=   "true" description="The heading level for the logo."%> <%@ attribute name="image" required="true" rtexprvalue=   "true" description="The image name for the logo."%> <%@ attribute name="width" required="true" rtexprvalue=   "true" description="The image width for the logo."%> <%@ attribute name="height" required="true" rtexprvalue=   "true" description="The image height for the logo."%>   <img src="${imgDir}${image}" width=   "${width}" height="${height}" align="left"> <H${heading}>   <jsp:doBody/></H${heading}> //logoTest.jsp <%@ taglib uri="rntsoft.com.tags" prefix="cbck" %> <html> <head><title></title></head> <body> <% session.setAttribute("imgDir",(request.getContextPath() + "/images/")) %> <cbck:logo heading="2" image="stamp.gif" width="42" height="54">Thanks for visiting</cbck:logo> Here's all the other stuff this page contains... </body> </html> //myTag.tld <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <tlib-version>1.0</tlib-version>     <jsp-version>2.0</jsp-version>     <short-name>cbck</short-name>     <uri>rntsoft.com.tags</uri>     <description>Cookbook custom tags</description>      <listener>   <listener-class>com.rntsoft.ReqListener</listener-class>     </listener>   <tag>         <name>logo</name>         <tag-class>com.rntsoft.LogoTag</tag-class>         <body-content>scriptless</body-content>         <description>This tag writes a logo inside the JSP.</description>         <attribute>             <name>heading</name>             <required>true</required>             <rtexprvalue>true</rtexprvalue>             <description>The heading level for the logo; 1 through 6.</description>         </attribute>                  <attribute>             <name>image</name>         <required>true</required>         <rtexprvalue>true</rtexprvalue>         <description>The image name for the logo.</description>         </attribute>           <attribute>             <name>width</name>         <required>true</required>         <rtexprvalue>true</rtexprvalue>         <description>The image width for the logo.</description>         </attribute>           <attribute>             <name>height</name>         <required>true</required>         <rtexprvalue>true</rtexprvalue>         <description>The image height for the logo.</description>         </attribute>     </tag> </taglib>