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        /**
043         * Constructor for TagData.
044         *
045         * <p>
046         * A typical constructor may be
047         * <pre>
048         * static final Object[][] att = {{"connection", "conn0"}, {"id", "query0"}};
049         * static final TagData td = new TagData(att);
050         * </pre>
051         *
052         * All values must be Strings except for those holding the
053         * distinguished object REQUEST_TIME_VALUE.
054    
055         * @param atts the static attribute and values.  May be null.
056         */
057        public TagData(Object[] atts[]) {
058            if (atts == null) {
059                attributes = new Hashtable<String, Object>();
060            } else {
061                attributes = new Hashtable<String, Object>(atts.length);
062            }
063    
064            if (atts != null) {
065                for (int i = 0; i < atts.length; i++) {
066                    attributes.put((String) atts[i][0], atts[i][1]);
067                }
068            }
069        }
070    
071        /**
072         * Constructor for a TagData.
073         *
074         * If you already have the attributes in a hashtable, use this
075         * constructor. 
076         *
077         * @param attrs A hashtable to get the values from.
078         */
079        public TagData(Hashtable<String, Object> attrs) {
080            this.attributes = attrs;
081        }
082    
083        /**
084         * The value of the tag's id attribute.
085         *
086         * @return the value of the tag's id attribute, or null if no such
087         *     attribute was specified.
088         */
089    
090        public String getId() {
091            return getAttributeString(TagAttributeInfo.ID);
092        }
093    
094        /**
095         * The value of the attribute.
096         * If a static value is specified for an attribute that accepts a
097         * request-time attribute expression then that static value is returned,
098         * even if the value is provided in the body of a <jsp:attribute> action.
099         * The distinguished object REQUEST_TIME_VALUE is only returned if
100         * the value is specified as a request-time attribute expression
101         * or via the &lt;jsp:attribute&gt; action with a body that contains
102         * dynamic content (scriptlets, scripting expressions, EL expressions, 
103         * standard actions, or custom actions).  Returns null if the attribute 
104         * is not set. 
105         *
106         * @param attName the name of the attribute
107         * @return the attribute's value
108         */
109    
110        public Object getAttribute(String attName) {
111            return attributes.get(attName);
112        }
113    
114        /**
115         * Set the value of an attribute.
116         *
117         * @param attName the name of the attribute
118         * @param value the value.
119         */
120        public void setAttribute(String attName,
121                                 Object value) {
122            attributes.put(attName, value);
123        }
124    
125        /**
126         * Get the value for a given attribute.
127         *
128         * @param attName the name of the attribute
129         * @return the attribute value string
130         * @throws ClassCastException if attribute value is not a String
131         */
132    
133        public String getAttributeString(String attName) {
134            Object o = attributes.get(attName);
135            if (o == null) {
136                return null;
137            } else {
138                return (String) o;
139            }       
140        }
141    
142        /**
143         * Enumerates the attributes.
144         *
145         *@return An enumeration of the attributes in a TagData
146         */
147        public java.util.Enumeration<String> getAttributes() {
148            return attributes.keys();
149        };
150    
151        // private data
152    
153        private Hashtable<String, Object> attributes; // the tagname/value map
154    }