View Javadoc

1   package com.sri.common.taglib;
2   
3   import javax.servlet.http.HttpServletRequest;
4   import javax.servlet.jsp.JspException;
5   import javax.servlet.jsp.tagext.BodyTagSupport;
6   import java.io.IOException;
7   
8   /***
9    * Creates a Dynamic-Link tag that allows for switching of CSS
10   * depending on what application we're dealing with.
11   *
12   * @author Michael Rimov
13   */
14  public class CssTag extends BodyTagSupport {
15  
16      /***
17  	 * 
18  	 */
19  	private static final long serialVersionUID = 1L;
20  	/***
21       * Number for the initial buffer size for StringBuffer
22       * assembling the output of the tag.
23       */
24      private final int STRING_BUFFER_SIZE = 128;
25  
26      /***
27       * Default constructor.
28       */
29      public CssTag() {
30          super();
31      }
32  
33      /***
34       * Process the end tag for this instance.
35       *
36       * @return indication of whether to continue evaluating the JSP page.
37       * @throws JspException if an error occurred while processing this tag
38       */
39      public int doEndTag() throws JspException {
40          StringBuffer resultString = new StringBuffer(STRING_BUFFER_SIZE);
41          String context = pageContext.getServletContext().getServletContextName();
42  
43          HttpServletRequest httpRequest = ((HttpServletRequest) pageContext.getRequest());
44          resultString.append(httpRequest.getContextPath());
45          resultString.append("/jsp");
46  
47          // tomcat and resin handle this differently: resin includes "/" in front of context
48          if (!context.startsWith("/")) {
49              resultString.append("/");
50          }
51  
52          resultString.append(context);
53          resultString.append(".css");
54  
55          try {
56              pageContext.getOut().print(resultString.toString());
57          } catch (IOException ex) {
58              throw new JspException("Error writing output to stream", ex);
59          }
60  
61          return EVAL_PAGE;
62      }
63  
64  
65  }