001 /** 002 * Java Gui Builder - A library to build GUIs using an XML file. 003 * Copyright 2002, 2003 (C) François Beausoleil 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 */ 019 020 package jgb.handlers.swing; 021 022 import jgb.builder.TagHandler; 023 import org.xml.sax.SAXException; 024 025 import java.lang.reflect.Field; 026 import java.util.HashMap; 027 import java.util.Map; 028 029 /** 030 * @since 0.1a 031 * @author Francois Beausoleil, <a href="mailto:fbos@users.sourceforge.net">fbos@users.sourceforge.net</a> 032 */ 033 public class ConstantTagHandler extends AbstractValueTagHandler { 034 /** 035 * The suffix to use when searching for the current constant's class in the context. 036 */ 037 public static final String CLASS_SUFFIX = ".class"; 038 039 /** 040 * The suffix to use when searching for the current constant's value in the context. 041 */ 042 public static final String VALUE_SUFFIX = ".value"; 043 044 /** 045 * The name of the name attribute. 046 */ 047 protected static final String ATTR_NAME = "name"; 048 049 public void enterElement(Map atts) throws SAXException { 050 String constantName = (String)atts.get(ATTR_NAME); 051 052 if (false == getConstantsMap().containsKey(constantName + CLASS_SUFFIX) 053 || false == getConstantsMap().containsKey(constantName + VALUE_SUFFIX)) { 054 try { 055 findConstantThroughReflection(constantName); 056 return; 057 } catch (Exception e) { 058 throwParsingException("Could not find constant named " + constantName, e); 059 } 060 } 061 062 Class constantClass = (Class)getConstantsMap().get(constantName + CLASS_SUFFIX); 063 Object constantValue = getConstantsMap().get(constantName + VALUE_SUFFIX); 064 065 updateContextParameters(constantClass, constantValue); 066 } 067 068 protected void exitElement() throws SAXException { 069 // NOP 070 } 071 072 private Map getConstantsMap() { 073 Map constantsMap = (Map)tagContext.get(TagHandler.CONSTANTS_MAP_KEY); 074 if (constantsMap == null) { 075 constantsMap = new HashMap(); 076 tagContext.put(TagHandler.CONSTANTS_MAP_KEY, constantsMap); 077 } 078 return constantsMap; 079 } 080 081 private void findConstantThroughReflection(String constantName) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException { 082 int lastPeriod = constantName.lastIndexOf('.'); 083 String className = constantName.substring(0, lastPeriod); 084 085 Class constantDeclClass = Class.forName(className); 086 Field constantField = constantDeclClass.getField(constantName.substring(lastPeriod + 1)); 087 Object constantValue = constantField.get(null); 088 089 putConstant(constantName, constantField.getType(), constantValue); 090 updateContextParameters(constantField.getType(), constantValue); 091 } 092 093 private void putConstant(String constantName, Class constantClass, Object constantValue) { 094 getConstantsMap().put(constantName + CLASS_SUFFIX, constantClass); 095 getConstantsMap().put(constantName + VALUE_SUFFIX, constantValue); 096 } 097 }