001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.core;
017:
018: import java.lang.reflect.Field;
019: import java.lang.reflect.Modifier;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: /**
024: * This class is used to expose Java constants to jstl as a HashMap. Have constants classes extend this class to expose them to jstl
025: * as a HashMap
026: *
027: *
028: */
029: public class JstlConstants extends HashMap {
030: private static final long serialVersionUID = 959692943635271761L;
031: private boolean initialised = false;
032:
033: public JstlConstants() {
034: publishFields(this , this .getClass());
035: initialised = true;
036: }
037:
038: /**
039: * Publishes all of the static, final, non-private fields of the given Class as entries in the given HashMap instance
040: *
041: * @param constantMap
042: * @param c
043: */
044: protected void publishFields(Map constantMap, Class c) {
045: Field[] fields = c.getDeclaredFields();
046: for (Field field : fields) {
047: int modifier = field.getModifiers();
048:
049: // publish values of static, final, non-private members
050: if (Modifier.isStatic(modifier)
051: && Modifier.isFinal(modifier)
052: && !Modifier.isPrivate(modifier)) {
053: try {
054: String fieldName = field.getName();
055:
056: constantMap.put(fieldName, field.get(null));
057: } catch (IllegalAccessException e) {
058: }
059: }
060: }
061:
062: // publish values of appropriate fields of member classes
063: publishMemberClassFields(constantMap, c);
064: }
065:
066: /**
067: * Publishes all of the static, final, non-private fields of the non-anonymous member classes of the given Class as entries in
068: * the given HashMap instance
069: *
070: * @param constantMap
071: * @param c
072: */
073: protected void publishMemberClassFields(Map constantMap, Class c) {
074: Class[] memberClasses = c.getClasses();
075:
076: for (Class memberClass : memberClasses) {
077: if (!memberClass.isAnonymousClass()) {
078: String memberPrefix = memberClass.getSimpleName();
079:
080: Map subclassMap = new HashMap();
081: publishFields(subclassMap, memberClass);
082: constantMap.put(memberClass.getSimpleName(),
083: subclassMap);
084: }
085: }
086: }
087:
088: /**
089: * @see java.util.Map#clear()
090: */
091: @Override
092: public void clear() {
093: if (!initialised)
094: super .clear();
095: else
096: throw new UnsupportedOperationException(
097: "Cannot modify this map");
098: }
099:
100: @Override
101: public Object put(Object key, Object value) {
102: if (!initialised)
103: return super .put(key, value);
104: else
105: throw new UnsupportedOperationException(
106: "Cannot modify this map");
107: }
108:
109: @Override
110: public void putAll(Map m) {
111: if (!initialised)
112: super .putAll(m);
113: else
114: throw new UnsupportedOperationException(
115: "Cannot modify this map");
116: }
117:
118: @Override
119: public Object remove(Object key) {
120: if (!initialised)
121: return super .remove(key);
122: else
123: throw new UnsupportedOperationException(
124: "Cannot modify this map");
125: }
126: }
|