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 javax.servlet.ServletRequest; // javadoc
19  import javax.servlet.jsp.JspException;
20  
21  import org.apache.portals.bridges.struts.PortletServlet;
22  import org.apache.portals.bridges.struts.config.PortletURLTypes; //javadoc
23  import org.apache.strutsel.taglib.utils.EvalHelper;
24  // javadoc
25  
26  /***
27   * Supports the Struts html-el:link tag to be used within a Portlet context.
28   * 
29   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
30   * @version $Id: ELLinkTag.java 280960 2005-09-15 00:27:56 +0200 (Thu, 15 Sep 2005) ate $
31   */
32  public class ELLinkTag extends org.apache.strutsel.taglib.html.ELLinkTag 
33  {
34      /***
35       * Indicates which type of a url must be generated: action, render or resource.
36       * <p>If not specified, the type will be determined by
37       * {@link PortletURLTypes#getType(String)}</p>.
38       */
39      protected PortletURLTypes.URLType urlType = null;
40          
41      /***
42       * @return "true" if an ActionURL must be rendered
43       */
44      public String getActionURL()
45      {
46          return urlType != null && urlType.equals(PortletURLTypes.URLType.ACTION) ? "true" : "false";
47      }
48  
49      private String actionURL;
50      /***
51       * Render an ActionURL when set to "true"
52       * <p>
53       * Supports jstl expression language.
54       * </p>
55       * @param actionURL when (evaluated to) "true" renders an ActionURL
56       */
57      public void setActionURL(String actionURL)
58      {
59          // delay evaluation of urlType to doStartTag
60          this.actionURL = actionURL;
61      }
62      
63      public String getRenderURL()
64      {
65          return urlType != null && urlType.equals(PortletURLTypes.URLType.RENDER) ? "true" : "false";
66      }
67          
68      private String renderURL;
69      /***
70       * Render a RenderURL when set to "true"
71       * <p>
72       * Supports jstl expression language.
73       * </p>
74       * @param renderURL when (evaluated to) "true" renders a RenderURL
75       */
76      public void setRenderURL(String renderURL)
77      {
78          // delay evaluation of urlType to doStartTag
79          this.renderURL = renderURL;
80      }
81  
82      public String getResourceURL()
83      {
84          return urlType != null && urlType.equals(PortletURLTypes.URLType.RESOURCE) ? "true" : "false";
85      }
86          
87      private String resourceURL;
88      
89      /***
90       * Render a ResourceURL when set to "true"
91       * <p>
92       * Supports jstl expression language.
93       * </p>
94       * @param resourceURL when (evaluated to) "true" renders a ResourceURL
95       */
96      public void setResourceURL(String resourceURL)
97      {
98          // delay evaluation of urlType to doStartTag
99          this.resourceURL = resourceURL;
100     }
101 
102     /***
103      * Generates a PortletURL or a ResourceURL for the link when in the context of a
104      * {@link PortletServlet#isPortletRequest(ServletRequest) PortletRequest}, otherwise
105      * the default behaviour is maintained.
106      * @return the link url
107      * @exception JspException if a JSP exception has occurred
108      */
109     protected String calculateURL() throws JspException 
110     {
111         if ( PortletServlet.isPortletRequest(pageContext.getRequest() ))
112         {
113             String url = super.calculateURL();
114             
115             // process embedded anchor
116             String anchor = null;
117             int hash = url.indexOf('#');
118             if ( hash > -1 )
119             {
120                 // save embedded anchor to be appended later and strip it from the url
121                 anchor = url.substring(hash);
122                 url = url.substring(0,hash);
123             }
124             
125             url = TagsSupport.getURL(pageContext, url, urlType);
126 
127             if ( anchor != null )
128             {
129                 url = url + anchor;
130             }
131             return url;
132         }
133         else
134         {
135             return super.calculateURL();
136         }
137     }
138     
139     public int doStartTag() throws JspException {
140         evaluateExpressions();
141         return(super.doStartTag());
142     }
143     
144     /***
145      * Resolve the {@link #actionURL}, {@link #renderURL} and {@link #resourceURL} attributes
146      * using the Struts JSTL expression evaluation engine ({@link EvalHelper}).
147      * @exception JspException if a JSP exception has occurred
148      */
149     private void evaluateExpressions() throws JspException {
150         Boolean value;
151 
152         value = EvalHelper.evalBoolean("actionURL", actionURL,this, pageContext);
153         if ( value != null && value.booleanValue() )
154         {
155             urlType = PortletURLTypes.URLType.ACTION;
156         }
157         if ( urlType == null )
158         {
159             value = EvalHelper.evalBoolean("renderURL", renderURL,this, pageContext);
160             if ( value != null && value.booleanValue() )
161             {
162                 urlType = PortletURLTypes.URLType.RENDER;
163             }            
164         }
165         if ( urlType == null )
166         {
167             value = EvalHelper.evalBoolean("resourceURL", resourceURL,this, pageContext);
168             if ( value != null && value.booleanValue() )
169             {
170                 urlType = PortletURLTypes.URLType.RESOURCE;
171             }            
172         }
173     }
174     
175     public void release() {
176 
177         super.release();
178         urlType = null;
179         actionURL = null;
180         renderURL = null;
181         resourceURL = null;
182     }
183 }