1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5 *
6 * Portions Copyright Apache Software Foundation.
7 *
8 * The contents of this file are subject to the terms of either the GNU
9 * General Public License Version 2 only ("GPL") or the Common Development
10 * and Distribution License("CDDL") (collectively, the "License"). You
11 * may not use this file except in compliance with the License. You can obtain
12 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
13 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
14 * language governing permissions and limitations under the License.
15 *
16 * When distributing the software, include this License Header Notice in each
17 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
18 * Sun designates this particular file as subject to the "Classpath" exception
19 * as provided by Sun in the GPL Version 2 section of the License file that
20 * accompanied this code. If applicable, add the following below the License
21 * Header, with the fields enclosed by brackets [] replaced by your own
22 * identifying information: "Portions Copyrighted [year]
23 * [name of copyright owner]"
24 *
25 * Contributor(s):
26 *
27 * If you wish your version of this file to be governed by only the CDDL or
28 * only the GPL Version 2, indicate your decision by adding "[Contributor]
29 * elects to include this software in this distribution under the [CDDL or GPL
30 * Version 2] license." If you don't indicate a single choice of license, a
31 * recipient has the option to distribute your version of this file under
32 * either the CDDL, the GPL Version 2 or to extend the choice of license to
33 * its licensees as provided above. However, if you add GPL Version 2 code
34 * and therefore, elected the GPL Version 2 license, then the option applies
35 * only if the new code is made subject to such option by the copyright
36 * holder.
37 */
38
39 package javax.servlet.jsp.tagext;
40
41 import java.util.Hashtable;
42
43 /**
44 * The (translation-time only) attribute/value information for a tag instance.
45 *
46 * <p>
47 * TagData is only used as an argument to the isValid, validate, and
48 * getVariableInfo methods of TagExtraInfo, which are invoked at
49 * translation time.
50 */
51
52 public class TagData implements Cloneable {
53
54 /**
55 * Distinguished value for an attribute to indicate its value
56 * is a request-time expression (which is not yet available because
57 * TagData instances are used at translation-time).
58 */
59
60 public static final Object REQUEST_TIME_VALUE = new Object();
61
62
63 /**
64 * Constructor for TagData.
65 *
66 * <p>
67 * A typical constructor may be
68 * <pre>
69 * static final Object[][] att = {{"connection", "conn0"}, {"id", "query0"}};
70 * static final TagData td = new TagData(att);
71 * </pre>
72 *
73 * All values must be Strings except for those holding the
74 * distinguished object REQUEST_TIME_VALUE.
75
76 * @param atts the static attribute and values. May be null.
77 */
78 public TagData(Object[] atts[]) {
79 if (atts == null) {
80 attributes = new Hashtable();
81 } else {
82 attributes = new Hashtable(atts.length);
83 }
84
85 if (atts != null) {
86 for (int i = 0; i < atts.length; i++) {
87 attributes.put(atts[i][0], atts[i][1]);
88 }
89 }
90 }
91
92 /**
93 * Constructor for a TagData.
94 *
95 * If you already have the attributes in a hashtable, use this
96 * constructor.
97 *
98 * @param attrs A hashtable to get the values from.
99 */
100 public TagData(Hashtable attrs) {
101 this.attributes = attrs;
102 }
103
104 /**
105 * The value of the tag's id attribute.
106 *
107 * @return the value of the tag's id attribute, or null if no such
108 * attribute was specified.
109 */
110
111 public String getId() {
112 return getAttributeString(TagAttributeInfo.ID);
113 }
114
115 /**
116 * The value of the attribute.
117 * If a static value is specified for an attribute that accepts a
118 * request-time attribute expression then that static value is returned,
119 * even if the value is provided in the body of a <jsp:attribute> action.
120 * The distinguished object REQUEST_TIME_VALUE is only returned if
121 * the value is specified as a request-time attribute expression
122 * or via the <jsp:attribute> action with a body that contains
123 * dynamic content (scriptlets, scripting expressions, EL expressions,
124 * standard actions, or custom actions). Returns null if the attribute
125 * is not set.
126 *
127 * @param attName the name of the attribute
128 * @return the attribute's value
129 */
130
131 public Object getAttribute(String attName) {
132 return attributes.get(attName);
133 }
134
135 /**
136 * Set the value of an attribute.
137 *
138 * @param attName the name of the attribute
139 * @param value the value.
140 */
141 public void setAttribute(String attName,
142 Object value) {
143 attributes.put(attName, value);
144 }
145
146 /**
147 * Get the value for a given attribute.
148 *
149 * @param attName the name of the attribute
150 * @return the attribute value string
151 * @throws ClassCastException if attribute value is not a String
152 */
153
154 public String getAttributeString(String attName) {
155 Object o = attributes.get(attName);
156 if (o == null) {
157 return null;
158 } else {
159 return (String) o;
160 }
161 }
162
163 /**
164 * Enumerates the attributes.
165 *
166 *@return An enumeration of the attributes in a TagData
167 */
168 public java.util.Enumeration getAttributes() {
169 return attributes.keys();
170 };
171
172 // private data
173
174 private Hashtable attributes; // the tagname/value map
175 }