1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. 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 package javax.servlet.jsp.tagext; 19 20 import java.util.Hashtable; 21 22 /** 23 * The (translation-time only) attribute/value information for a tag instance. 24 * 25 * <p> 26 * TagData is only used as an argument to the isValid, validate, and 27 * getVariableInfo methods of TagExtraInfo, which are invoked at 28 * translation time. 29 */ 30 31 public class TagData implements Cloneable { 32 33 /** 34 * Distinguished value for an attribute to indicate its value 35 * is a request-time expression (which is not yet available because 36 * TagData instances are used at translation-time). 37 */ 38 39 public static final Object REQUEST_TIME_VALUE = new Object(); 40 41 42 /** 43 * Constructor for TagData. 44 * 45 * <p> 46 * A typical constructor may be 47 * <pre> 48 * static final Object[][] att = {{"connection", "conn0"}, {"id", "query0"}}; 49 * static final TagData td = new TagData(att); 50 * </pre> 51 * 52 * All values must be Strings except for those holding the 53 * distinguished object REQUEST_TIME_VALUE. 54 55 * @param atts the static attribute and values. May be null. 56 */ 57 public TagData(Object[] atts[]) { 58 if (atts == null) { 59 attributes = new Hashtable<String, Object>(); 60 } else { 61 attributes = new Hashtable<String, Object>(atts.length); 62 } 63 64 if (atts != null) { 65 for (int i = 0; i < atts.length; i++) { 66 attributes.put((String) atts[i][0], atts[i][1]); 67 } 68 } 69 } 70 71 /** 72 * Constructor for a TagData. 73 * 74 * If you already have the attributes in a hashtable, use this 75 * constructor. 76 * 77 * @param attrs A hashtable to get the values from. 78 */ 79 public TagData(Hashtable<String, Object> attrs) { 80 this.attributes = attrs; 81 } 82 83 /** 84 * The value of the tag's id attribute. 85 * 86 * @return the value of the tag's id attribute, or null if no such 87 * attribute was specified. 88 */ 89 90 public String getId() { 91 return getAttributeString(TagAttributeInfo.ID); 92 } 93 94 /** 95 * The value of the attribute. 96 * If a static value is specified for an attribute that accepts a 97 * request-time attribute expression then that static value is returned, 98 * even if the value is provided in the body of a <jsp:attribute> action. 99 * 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 <jsp:attribute> 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 }