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

Quick Search    Search Deep

Source code: com/aendvari/tethys/tag/model/ModelTreeTag.java


1   /*
2    * ModelTreeTag.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.model;
11  
12  import java.lang.reflect.Method;
13  
14  import javax.servlet.jsp.*;
15  import javax.servlet.jsp.tagext.*;
16  
17  import com.aendvari.common.model.*;
18  
19  import com.aendvari.tethys.context.*;
20  import com.aendvari.tethys.context.model.*;
21  
22  import com.aendvari.tethys.tag.*;
23  import com.aendvari.tethys.tag.context.*;
24  
25  
26  /**
27   * <p>A base class for all tags requiring context.</p>
28   *
29   * @author  Trevor Milne
30   *
31   */
32  
33  public class ModelTreeTag extends ContextTag implements ModelTreeAncestorTag
34  {
35    /* Variables. */
36  
37  
38    /** The path to the context in the ModelTree. */
39    protected String path;
40  
41    /** The {@link ModelContext} object for this tag. */
42    protected ModelContext modelContext;
43  
44    /** The method to retrieve the context object from this tag. */
45    protected static Method modelContextMethod;
46  
47  
48    /* Constructors. */
49  
50    /**
51     * Default constructor, all subclasses are required to only define a public
52     * constructor with the same signature, and to call the superclass constructor.
53     *
54     */
55  
56    public ModelTreeTag()
57    {
58      super();
59  
60      // retrieve context method
61      if (modelContextMethod == null)
62      {
63        try
64        {
65          modelContextMethod = ModelTreeAncestorTag.class.getMethod("getModelContext", null);
66        }
67        catch (NoSuchMethodException exception)
68        {
69          // should not happen
70        }
71      }
72  
73      path = null;
74      modelContext = null;
75    }
76  
77    public void setPath( String path )
78    {
79      this.path = path;
80    }
81  
82    public String getPath()
83    {
84      return path;
85    }
86  
87    public ModelContext getModelContext() { return modelContext; }
88  
89      /**
90       * Release all allocated resources.
91     *
92       */
93  
94      public void release()
95      {
96          super.release();
97  
98          path = null;
99      modelContext = null;
100     }
101 
102   /**
103    * Establishes the {@link Context} of this tag.
104    * The internal 'modelContext' is set to {@link Context} object associated with
105    * the <code>context</code> string.
106    *
107    * @param    contextMap      The context map to search.
108    *
109    * @exception  JspTagException    The context could not be established.
110    *
111    */
112 
113   protected void establishModelContext()
114     throws JspTagException
115   {
116     // get the context map
117     ContextMap contextMap = ModelTagData.getData(
118       pageContext, getDataScope()).getModelContextMap();
119 
120     // determine the context for this tag
121     modelContext = (ModelContext)determineContext(
122       contextMap, ModelTreeAncestorTag.class, modelContextMethod);
123   }
124 
125   /**
126    * Returns the parent {@link Context} for this tag.
127    *
128    * @return              A {@link ModelContext} instance representing the parent context,
129    *                   null if none found.
130    *
131      * @exception  JspTagException    If the context cannot be determined.
132    *
133    */
134 
135   public ModelContext getParentModelContext()
136     throws JspTagException
137   {
138     // get the context map
139     ContextMap contextMap = ModelTagData.getData(
140       pageContext, getDataScope()).getModelContextMap();
141 
142     // determine the parent context
143     Context context = getParentContext(
144       contextMap, this, ModelTreeAncestorTag.class, modelContextMethod);
145 
146     return (ModelContext)context;
147   }
148 
149   /**
150    * Returns the {@link ModelNode} for the specified path.
151    *
152    * @param    path        The path to the {@link ModelNode}.
153    * @param    allowNull      True returns null when node can not be found, false throws an exception.
154    *
155    * @exception  JspTagException    The node could not be found.
156    *
157    */
158 
159   protected ModelNode getModelNode(String path, boolean allowNull)
160     throws JspTagException
161   {
162     // get the model tree
163     ModelTree modelTree = modelContext.getModelNode().getOwnerModelTree();
164 
165     // get the value from the path
166     ModelNode modelNode = modelTree.getNode(modelContext.getModelNode(), path);
167 
168     // throw exception if node could not be found
169     if ((modelNode == null) && (!allowNull))
170     {
171       throw new JspTagException(
172         this.getClass().getName() + ": The model node \"" +
173         path + "\" for context \"" + modelContext.getLocation() + "\" cannot be found.");
174     }
175 
176     return modelNode;
177   }
178 }
179