View Javadoc

1   /*
2    * $Id: UseAttributeTag.java 727715 2008-12-18 13:06:06Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  package org.apache.tiles.jsp.taglib;
24  
25  import javax.servlet.jsp.JspException;
26  import javax.servlet.jsp.tagext.TagData;
27  import javax.servlet.jsp.tagext.TagExtraInfo;
28  import javax.servlet.jsp.tagext.VariableInfo;
29  
30  
31  /***
32   * Exposes am attribute as a scripting variable within the page.
33   *
34   * @since Tiles 1.0
35   * @version $Rev: 727715 $ $Date: 2008-12-18 14:06:06 +0100 (Thu, 18 Dec 2008) $
36   */
37  public class UseAttributeTag extends AttributeTagSupport {
38  
39      /***
40       * Class name of object.
41       */
42      private String classname = null;
43  
44      /***
45       * Get class name.
46       *
47       * @return class name
48       */
49      public String getClassname() {
50          return classname;
51  
52      }
53  
54      /***
55       * Set the class name.
56       *
57       * @param name The new class name.
58       */
59      public void setClassname(String name) {
60          this.classname = name;
61      }
62  
63      /*** {@inheritDoc} */
64      protected void reset() {
65          super.reset();
66          classname = null;
67          id = null;
68      }
69  
70      /***
71       * Expose the requested attribute from attribute context.
72       *
73       * @throws JspException if a JSP exception has occurred
74       */
75      public void execute() throws JspException {
76          pageContext.setAttribute(getScriptingVariable(), attribute.getValue(), scope);
77      }
78  
79      /***
80       * Returns the scripting variable to use.
81       *
82       * @return The scripting variable.
83       */
84      public String getScriptingVariable() {
85          return id == null ? getName() : id;
86      }
87  
88  
89      /***
90       * Implementation of <code>TagExtraInfo</code> which identifies
91       * the scripting object(s) to be made visible.
92       */
93      public static class Tei extends TagExtraInfo {
94  
95          /*** {@inheritDoc} */
96          public VariableInfo[] getVariableInfo(TagData data) {
97              String classname = data.getAttributeString("classname");
98              if (classname == null) {
99                  classname = "java.lang.Object";
100             }
101 
102             String id = data.getAttributeString("id");
103             if (id == null) {
104                 id = data.getAttributeString("name");
105             }
106 
107             return new VariableInfo[] {
108                 new VariableInfo(id, classname, true, VariableInfo.AT_END)
109             };
110 
111         }
112     }
113 }