View Javadoc

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