001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.servlet.jsp.tagext;
019:
020: import java.util.Hashtable;
021:
022: /**
023: * The (translation-time only) attribute/value information for a tag instance.
024: *
025: * <p>
026: * TagData is only used as an argument to the isValid, validate, and
027: * getVariableInfo methods of TagExtraInfo, which are invoked at
028: * translation time.
029: */
030:
031: public class TagData implements Cloneable {
032:
033: /**
034: * Distinguished value for an attribute to indicate its value
035: * is a request-time expression (which is not yet available because
036: * TagData instances are used at translation-time).
037: */
038:
039: public static final Object REQUEST_TIME_VALUE = new Object();
040:
041: /**
042: * Constructor for TagData.
043: *
044: * <p>
045: * A typical constructor may be
046: * <pre>
047: * static final Object[][] att = {{"connection", "conn0"}, {"id", "query0"}};
048: * static final TagData td = new TagData(att);
049: * </pre>
050: *
051: * All values must be Strings except for those holding the
052: * distinguished object REQUEST_TIME_VALUE.
053:
054: * @param atts the static attribute and values. May be null.
055: */
056: public TagData(Object[] atts[]) {
057: if (atts == null) {
058: attributes = new Hashtable<String, Object>();
059: } else {
060: attributes = new Hashtable<String, Object>(atts.length);
061: }
062:
063: if (atts != null) {
064: for (int i = 0; i < atts.length; i++) {
065: attributes.put((String) atts[i][0], atts[i][1]);
066: }
067: }
068: }
069:
070: /**
071: * Constructor for a TagData.
072: *
073: * If you already have the attributes in a hashtable, use this
074: * constructor.
075: *
076: * @param attrs A hashtable to get the values from.
077: */
078: public TagData(Hashtable<String, Object> attrs) {
079: this .attributes = attrs;
080: }
081:
082: /**
083: * The value of the tag's id attribute.
084: *
085: * @return the value of the tag's id attribute, or null if no such
086: * attribute was specified.
087: */
088:
089: public String getId() {
090: return getAttributeString(TagAttributeInfo.ID);
091: }
092:
093: /**
094: * The value of the attribute.
095: * If a static value is specified for an attribute that accepts a
096: * request-time attribute expression then that static value is returned,
097: * even if the value is provided in the body of a <jsp:attribute> action.
098: * The distinguished object REQUEST_TIME_VALUE is only returned if
099: * the value is specified as a request-time attribute expression
100: * or via the <jsp:attribute> action with a body that contains
101: * dynamic content (scriptlets, scripting expressions, EL expressions,
102: * standard actions, or custom actions). Returns null if the attribute
103: * is not set.
104: *
105: * @param attName the name of the attribute
106: * @return the attribute's value
107: */
108:
109: public Object getAttribute(String attName) {
110: return attributes.get(attName);
111: }
112:
113: /**
114: * Set the value of an attribute.
115: *
116: * @param attName the name of the attribute
117: * @param value the value.
118: */
119: public void setAttribute(String attName, Object value) {
120: attributes.put(attName, value);
121: }
122:
123: /**
124: * Get the value for a given attribute.
125: *
126: * @param attName the name of the attribute
127: * @return the attribute value string
128: * @throws ClassCastException if attribute value is not a String
129: */
130:
131: public String getAttributeString(String attName) {
132: Object o = attributes.get(attName);
133: if (o == null) {
134: return null;
135: } else {
136: return (String) o;
137: }
138: }
139:
140: /**
141: * Enumerates the attributes.
142: *
143: *@return An enumeration of the attributes in a TagData
144: */
145: public java.util.Enumeration<String> getAttributes() {
146: return attributes.keys();
147: };
148:
149: // private data
150:
151: private Hashtable<String, Object> attributes; // the tagname/value map
152: }
|