1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 //
19 // This source code implements specifications defined by the Java
20 // Community Process. In order to remain compliant with the specification
21 // DO NOT add / change / or delete method signatures!
22 //
23
24 package javax.servlet.jsp.tagext;
25
26 import java.util.Hashtable;
27
28 /**
29 * The (translation-time only) attribute/value information for a tag instance.
30 *
31 * <p>
32 * TagData is only used as an argument to the isValid, validate, and
33 * getVariableInfo methods of TagExtraInfo, which are invoked at
34 * translation time.
35 */
36
37 public class TagData implements Cloneable {
38
39 /**
40 * Distinguished value for an attribute to indicate its value
41 * is a request-time expression (which is not yet available because
42 * TagData instances are used at translation-time).
43 */
44
45 public static final Object REQUEST_TIME_VALUE = new Object();
46
47
48 /**
49 * Constructor for TagData.
50 *
51 * <p>
52 * A typical constructor may be
53 * <pre>
54 * static final Object[][] att = {{"connection", "conn0"}, {"id", "query0"}};
55 * static final TagData td = new TagData(att);
56 * </pre>
57 *
58 * All values must be Strings except for those holding the
59 * distinguished object REQUEST_TIME_VALUE.
60
61 * @param atts the static attribute and values. May be null.
62 */
63 public TagData(Object[] atts[]) {
64 if (atts == null) {
65 attributes = new Hashtable();
66 } else {
67 attributes = new Hashtable(atts.length);
68 }
69
70 if (atts != null) {
71 for (int i = 0; i < atts.length; i++) {
72 attributes.put(atts[i][0], atts[i][1]);
73 }
74 }
75 }
76
77 /**
78 * Constructor for a TagData.
79 *
80 * If you already have the attributes in a hashtable, use this
81 * constructor.
82 *
83 * @param attrs A hashtable to get the values from.
84 */
85 public TagData(Hashtable attrs) {
86 this.attributes = attrs;
87 }
88
89 /**
90 * The value of the tag's id attribute.
91 *
92 * @return the value of the tag's id attribute, or null if no such
93 * attribute was specified.
94 */
95
96 public String getId() {
97 return getAttributeString(TagAttributeInfo.ID);
98 }
99
100 /**
101 * The value of the attribute.
102 * If a static value is specified for an attribute that accepts a
103 * request-time attribute expression then that static value is returned,
104 * even if the value is provided in the body of a <jsp:attribute> action.
105 * The distinguished object REQUEST_TIME_VALUE is only returned if
106 * the value is specified as a request-time attribute expression
107 * or via the <jsp:attribute> action with a body that contains
108 * dynamic _content (scriptlets, scripting expressions, EL expressions,
109 * standard actions, or custom actions). Returns null if the attribute
110 * is not set.
111 *
112 * @param attName the name of the attribute
113 * @return the attribute's value
114 */
115
116 public Object getAttribute(String attName) {
117 return attributes.get(attName);
118 }
119
120 /**
121 * Set the value of an attribute.
122 *
123 * @param attName the name of the attribute
124 * @param value the value.
125 */
126 public void setAttribute(String attName,
127 Object value) {
128 attributes.put(attName, value);
129 }
130
131 /**
132 * Get the value for a given attribute.
133 *
134 * @param attName the name of the attribute
135 * @return the attribute value string
136 * @throws ClassCastException if attribute value is not a String
137 */
138
139 public String getAttributeString(String attName) {
140 Object o = attributes.get(attName);
141 if (o == null) {
142 return null;
143 } else {
144 return (String) o;
145 }
146 }
147
148 /**
149 * Enumerates the attributes.
150 *
151 *@return An enumeration of the attributes in a TagData
152 */
153 public java.util.Enumeration getAttributes() {
154 return attributes.keys();
155 };
156
157 // private data
158
159 private Hashtable attributes; // the tagname/value map
160 }