View Javadoc

1   /*
2    * Copyright 2010 FatWire Corporation. All Rights Reserved.
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 com.fatwire.gst.foundation.taglib;
17  
18  import java.io.IOException;
19  
20  import javax.servlet.jsp.JspException;
21  import javax.servlet.jsp.tagext.JspTag;
22  import javax.servlet.jsp.tagext.SimpleTagSupport;
23  
24  import COM.FutureTense.Interfaces.ICS;
25  
26  import com.fatwire.gst.foundation.include.DefaultIncludeService;
27  import com.fatwire.gst.foundation.include.Include;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * Includes a template,element or page in a jsp page.
34   * 
35   * @author Dolf Dijkstra
36   * @since Apr 13, 2011
37   */
38  public final class IncludeTag extends GsfSimpleTag {
39  
40      static final Log LOG = LogFactory.getLog(IncludeTag.class.getPackage().getName());
41  
42      private String name;
43      private boolean silent = false;
44  
45      /*
46       * (non-Javadoc)
47       * 
48       * @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
49       */
50      @Override
51      public void doTag() throws JspException, IOException {
52  
53          final ICS ics = getICS();
54  
55          final Include inc = discover(name);
56          if (inc != null) {
57              inc.include(ics);
58          } else if (!silent) {
59              throw new IllegalStateException("Can't find object to include with the name " + name);
60          }
61  
62          super.doTag();
63      }
64  
65      protected Include discover(final String name) {
66          final DefaultIncludeService s = findService();
67          if (s == null)
68              throw new IllegalStateException(
69                      "Can't find DefaultIncludeService from a parent tag. Does your Action have a @InjectForRequest field of type IncludeService."
70                              + " This is required to make use of the include tag in your jsp.");
71              
72          return s.find(name);
73      }
74  
75      protected DefaultIncludeService findService() {
76          final JspTag parent = SimpleTagSupport.findAncestorWithClass(this, PageTag.class);
77          if (parent instanceof PageTag) {
78              final PageTag t = (PageTag) parent;
79              return t.getJspIncludeService();
80          }
81          throw new IllegalStateException("Cannot find a parent JSP tag of type PageTag. Is the include tag nested in a gsf:root tag?");
82  
83      }
84  
85      /**
86       * @return the name
87       */
88      public String getName() {
89          return name;
90      }
91  
92      /**
93       * @param name the name to set
94       */
95      public void setName(final String name) {
96          this.name = name;
97      }
98  
99      /**
100      * @return the silent
101      */
102     public boolean isSilent() {
103         return silent;
104     }
105 
106     /**
107      * @param silent the silent to set
108      */
109     public void setSilent(final boolean silent) {
110         this.silent = silent;
111     }
112 
113 }