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