Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/tethys/tag/context/ContextBodyTag.java


1   /*
2    * ContextBodyTag.java
3    *
4    * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5    *
6    * See the file LICENSE for terms of use.
7    *
8    */
9   
10  package com.aendvari.tethys.tag.context;
11  
12  import java.lang.reflect.Method;
13  
14  import javax.servlet.jsp.JspTagException;
15  import javax.servlet.jsp.tagext.*;
16  
17  import com.aendvari.tethys.context.*;
18  
19  import com.aendvari.tethys.tag.*;
20  import com.aendvari.tethys.tag.data.*;
21  
22  
23  /**
24   * <p>A base class for all body tags requiring context.</p>
25   *
26   * @author  Trevor Milne
27   *
28   */
29  
30  public abstract class ContextBodyTag extends DataBodyTag implements ContextAncestorTag
31  {
32    /* Parameters. */
33  
34    /** The relative context in which this tag resides. */
35    protected String context;
36  
37  
38    /* Constructors. */
39  
40    /**
41     * Default constructor, all subclasses are required to only define a public
42     * constructor with the same signature, and to call the superclass constructor.
43     *
44     */
45  
46    public ContextBodyTag()
47    {
48      super();
49  
50      context = null;
51    }
52  
53  
54    /* Accessors. */
55  
56    public String getContext()
57    {
58      return context;
59    }
60  
61    public void setContext(String context)
62    {
63      this.context = context;
64    }
65  
66    /**
67     * Determines the {@link Context} of this tag.
68     * The <code>context</code> parameter is used to select the {@link Context}.
69     *
70     * @param    contextMap      The context map to search.
71     * @param    ancestorClass    The class type of parent context tags.
72     * @param    contextMethod    The {@link Method} to retrieve the tag {@link Context}.
73     *
74     * @return              The {@link Context} object for this tag.
75     *
76     * @exception  JspTagException    The context could not be established.
77     *
78     */
79  
80    protected Context determineContext(ContextMap contextMap, Class ancestorClass, Method contextMethod)
81      throws JspTagException
82    {
83      // determine the context for this tag
84      Context contextObject = ContextUtil.determineContext(
85        this, ancestorClass, contextMethod, contextMap, context, true);
86  
87      // throw exception if context could not be found
88      if (contextObject == null)
89      {
90        throw new JspTagException(
91          this.getClass().getName() + ": The context \"" + context + "\" cannot be found.");
92      }
93  
94      return contextObject;
95    }
96  
97    /**
98     * Returns the parent {@link Context} for this tag.
99     *
100    * @param    contextMap      The context map to search.
101    * @param    tagSupport      The tag to get the parent of.
102    * @param    ancestorClass    The class type of the parent tag.
103    * @param    contextMethod    The {@link Method} to retrieve the tag {@link Context}.
104    *
105    * @return              A {@link Context} instance representing the parent context,
106    *                   null if none found.
107    *
108      * @exception  JspTagException    If the context cannot be determined.
109    *
110    */
111 
112   public Context getParentContext(
113     ContextMap contextMap, TagSupport tagSupport, Class ancestorClass, Method contextMethod)
114       throws JspTagException
115   {
116     // get parent tag
117     TagSupport parentTag = TagUtil.getParentTag(tagSupport, ancestorClass);
118 
119     // use base if no parent tag
120     if (parentTag == null)
121     {
122       return contextMap.getDefaultContext();
123     }
124 
125     // get parent context
126     Context parentContext = ContextUtil.getTagContext(parentTag, contextMethod);
127 
128     return parentContext;
129   }
130 
131     /**
132      * Release all allocated resources.
133    *
134      */
135 
136     public void release()
137     {
138         super.release();
139 
140         context = null;
141     }
142 }
143