View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.portals.bridges.struts.taglib;
17  
18  import java.io.IOException;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.jsp.JspException;
22  import javax.servlet.jsp.JspWriter;
23  import javax.servlet.jsp.tagext.TagSupport;
24  
25  /***
26   * Generate a script tag for use within a Portlet environment.
27   * <p>
28   * The src attribute is resolved to a context relative path and may contain
29   * a relative path (prefixed with one or more ../ elements).
30   * </p>
31   * <p>
32   * Note: works equally well within a Portlet context as a Web application context.
33   * </p>
34   * 
35   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
36   * @version $Id: ScriptTag.java 188222 2005-01-20 02:41:15 +0100 (Thu, 20 Jan 2005) ate $
37   */
38  public class ScriptTag extends TagSupport 
39  {
40      /***
41       * The language attribute for the script tag.
42       * <p>
43       * Defaults to "Javascript1.1"
44       * </p>
45       */
46      protected String language;
47  
48      /***
49       * The script src path.
50       * <p>
51       * May contain a relative path (prefixed with one or more ../ elements).<br/>
52       * </p>
53       */
54      protected String src;
55  
56      public String getLanguage()
57      {
58          return language;
59      }
60      public void setLanguage(String language)
61      {
62          this.language = language;
63      }
64      public String getSrc()
65      {
66          return src;
67      }
68      public void setSrc(String src)
69      {
70          this.src = src;
71      }
72  
73      public int doStartTag() throws JspException
74      {
75          StringBuffer buffer = new StringBuffer("<script language=\"");
76          if (language != null)
77              buffer.append(language);
78          else
79              buffer.append("Javascript1.1");
80          buffer.append("\" src=\"");
81          if (src.startsWith("/"))
82          {
83              buffer.append(((HttpServletRequest) pageContext.getRequest())
84                      .getContextPath());
85          		buffer.append(src);
86          }
87          else
88          {
89              buffer.append(TagsSupport.getContextRelativeURL(pageContext,src,true));
90          }
91          buffer.append("\"/></script>");
92          JspWriter writer = pageContext.getOut();
93          try
94          {
95              writer.print(buffer.toString());
96          } catch (IOException e)
97          {
98              throw new JspException(e);
99          }
100         return (SKIP_BODY);
101     }
102 
103     public int doEndTag()
104     {
105         return EVAL_PAGE;
106     }
107 
108     public void release() 
109     {
110         super.release();
111         language = null;
112         src = null;
113     }
114 }