KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > rendering > AbstractAttributeState


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.rendering;
19
20 import org.apache.beehive.netui.util.Bundle;
21 import org.apache.beehive.netui.util.logging.Logger;
22
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 abstract public class AbstractAttributeState extends AbstractTagState
27 {
28     private static final Logger logger = Logger.getInstance(AbstractAttributeState.class);
29
30     /**
31      * String constant for the empty string.
32      */

33     public static final String JavaDoc EMPTY_STRING = "";
34
35     /**
36      * The integer type constant indentifying the General Attributes <code>AbstractBaseTag</code>
37      * reserves 0-9 for indentification.
38      */

39     public static final int ATTR_GENERAL = 0;
40
41     private HashMap JavaDoc generalMap = null; // the map of general attributes
42

43     public HashMap JavaDoc getGeneralAttributeMap()
44     {
45         return generalMap;
46     }
47
48     /**
49      * This method will return the map that represents the passed in <code>type</code>. The boolean flag
50      * </code>createIfNull</code> indicates that the map should be created or not if it's null. This
51      * class defines two maps defined by <code>@see #ATTR_GENERAL</code> and <code>ATTR_GENERAL_EXPRESSION</code>
52      * @param type <code>integer</code> type indentifying the map to be created.
53      * @param createIfNull <code>boolean</code> flag indicating if the map should be created if it doesn't exist.
54      * @return The map or null
55      * @see #ATTR_GENERAL
56      */

57     public Map JavaDoc selectMap(int type, boolean createIfNull)
58     {
59         Map JavaDoc ret = null;
60         if (type == ATTR_GENERAL) {
61             if (generalMap == null && createIfNull)
62                 generalMap = new HashMap JavaDoc();
63             ret = generalMap;
64         }
65
66         return ret;
67     }
68
69     public void clear()
70     {
71         if (generalMap != null)
72             generalMap.clear();
73     }
74
75     /**
76      * Register a name/value pair into a named attribute map. The base type
77      * supports the <code>ATTR_GENERAL<code> named map. Subclasses may add additional maps
78      * enabling attributes to be treated with different behavior.
79      * @param type an integer key identifying the map.
80      * @param attrName the name of the attribute
81      * @param value the value of the attribute
82      */

83     public void registerAttribute(int type, String JavaDoc attrName, String JavaDoc value, boolean ignoreEmpty)
84     {
85         assert(attrName != null);
86         
87         // If the value is null or the empty string ignore the expression.
88
if (value == null)
89             return;
90         if (ignoreEmpty && "".equals(value))
91             return;
92
93         Map JavaDoc map = selectMap(type, true);
94         if (map == null) {
95             String JavaDoc s = Bundle.getString("Tags_ParameterAccessError",
96                     new Object JavaDoc[]{new Integer JavaDoc(type), attrName});
97             logger.error(s);
98             return;
99         }
100         map.put(attrName, value);
101     }
102
103     public void registerAttribute(int type, String JavaDoc attrName, String JavaDoc value)
104     {
105         registerAttribute(type, attrName, value, true);
106     }
107
108     /**
109      * Remove a previously registered attribute value from map.
110      * @param type an integer key indentifying the map
111      * @param attrName the name of the attribute to remove from the specified map
112      */

113     public void removeAttribute(int type, String JavaDoc attrName)
114     {
115         Map JavaDoc map = selectMap(type, false);
116         if (map == null) {
117             String JavaDoc s = Bundle.getString("Tags_ParameterAccessError",
118                     new Object JavaDoc[]{new Integer JavaDoc(type), attrName});
119             logger.error(s);
120             return;
121         }
122
123         map.remove(attrName);
124     }
125
126     /**
127      * Return a named attribute value from the specified attribute map.
128      * @param type an integer key indentifying the map
129      * @param attrName the name of the attribute we will get the value from.
130      * @return a string value of the attribute if set or null.
131      */

132     public String JavaDoc getAttribute(int type, String JavaDoc attrName)
133     {
134         Map JavaDoc map = selectMap(type, false);
135         if (map == null)
136             return null;
137         return (String JavaDoc) map.get(attrName);
138     }
139 }
140
Popular Tags