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: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.rendering;
020:
021: import org.apache.beehive.netui.util.Bundle;
022: import org.apache.beehive.netui.util.logging.Logger;
023:
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: abstract public class AbstractAttributeState extends AbstractTagState {
028: private static final Logger logger = Logger
029: .getInstance(AbstractAttributeState.class);
030:
031: /**
032: * String constant for the empty string.
033: */
034: public static final String EMPTY_STRING = "";
035:
036: /**
037: * The integer type constant indentifying the General Attributes <code>AbstractBaseTag</code>
038: * reserves 0-9 for indentification.
039: */
040: public static final int ATTR_GENERAL = 0;
041:
042: private HashMap generalMap = null; // the map of general attributes
043:
044: public HashMap getGeneralAttributeMap() {
045: return generalMap;
046: }
047:
048: /**
049: * This method will return the map that represents the passed in <code>type</code>. The boolean flag
050: * </code>createIfNull</code> indicates that the map should be created or not if it's null. This
051: * class defines two maps defined by <code>@see #ATTR_GENERAL</code> and <code>ATTR_GENERAL_EXPRESSION</code>
052: * @param type <code>integer</code> type indentifying the map to be created.
053: * @param createIfNull <code>boolean</code> flag indicating if the map should be created if it doesn't exist.
054: * @return The map or null
055: * @see #ATTR_GENERAL
056: */
057: public Map selectMap(int type, boolean createIfNull) {
058: Map ret = null;
059: if (type == ATTR_GENERAL) {
060: if (generalMap == null && createIfNull)
061: generalMap = new HashMap();
062: ret = generalMap;
063: }
064:
065: return ret;
066: }
067:
068: public void clear() {
069: if (generalMap != null)
070: generalMap.clear();
071: }
072:
073: /**
074: * Register a name/value pair into a named attribute map. The base type
075: * supports the <code>ATTR_GENERAL<code> named map. Subclasses may add additional maps
076: * enabling attributes to be treated with different behavior.
077: * @param type an integer key identifying the map.
078: * @param attrName the name of the attribute
079: * @param value the value of the attribute
080: */
081: public void registerAttribute(int type, String attrName,
082: String value, boolean ignoreEmpty) {
083: assert (attrName != null);
084:
085: // If the value is null or the empty string ignore the expression.
086: if (value == null)
087: return;
088: if (ignoreEmpty && "".equals(value))
089: return;
090:
091: Map map = selectMap(type, true);
092: if (map == null) {
093: String s = Bundle.getString("Tags_ParameterAccessError",
094: new Object[] { new Integer(type), attrName });
095: logger.error(s);
096: return;
097: }
098: map.put(attrName, value);
099: }
100:
101: public void registerAttribute(int type, String attrName,
102: String value) {
103: registerAttribute(type, attrName, value, true);
104: }
105:
106: /**
107: * Remove a previously registered attribute value from map.
108: * @param type an integer key indentifying the map
109: * @param attrName the name of the attribute to remove from the specified map
110: */
111: public void removeAttribute(int type, String attrName) {
112: Map map = selectMap(type, false);
113: if (map == null) {
114: String s = Bundle.getString("Tags_ParameterAccessError",
115: new Object[] { new Integer(type), attrName });
116: logger.error(s);
117: return;
118: }
119:
120: map.remove(attrName);
121: }
122:
123: /**
124: * Return a named attribute value from the specified attribute map.
125: * @param type an integer key indentifying the map
126: * @param attrName the name of the attribute we will get the value from.
127: * @return a string value of the attribute if set or null.
128: */
129: public String getAttribute(int type, String attrName) {
130: Map map = selectMap(type, false);
131: if (map == null)
132: return null;
133: return (String) map.get(attrName);
134: }
135: }
|