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 jgb.builder.utils.MethodCall;
024    import org.xml.sax.SAXException;
025    
026    import java.util.Map;
027    
028    /**
029     * @since 0.2a
030     * @author Francois Beausoleil, <a href="mailto:fbos@users.sourceforge.net">fbos@users.sourceforge.net</a>
031     */
032    public class MethodCallTagHandler extends AbstractTagHandler {
033        /**
034         * The name of the method attribute.
035         */
036        protected static final String ATTR_METHOD = "method";
037    
038        public void enterElement(Map atts) throws SAXException {
039            Object refObject = getReferenceObject(atts);
040            String methodName = getMethodName(atts);
041            String refObjectClassName = refObject.getClass().getName();
042            MethodCall methodCall = createMethodCall(refObjectClassName, methodName, refObject);
043            putAccumulator(tagContext, methodCall);
044        }
045    
046        public void exitElement() throws SAXException {
047            MethodCall methodCall = getContextAccumulator(tagContext);
048            try {
049                Object value = methodCall.call();
050                tagContext.put(TagHandler.CALL_RETURN_CLASS_KEY, methodCall.getReturnValueType());
051                tagContext.put(TagHandler.CALL_RETURN_VALUE_KEY, value);
052            } catch (Exception e) {
053                throwParsingException("could not execute method call", e);
054            }
055    
056            tagContext.remove(TagHandler.CURRENT_PARM_ACC_KEY);
057        }
058    
059        protected MethodCall getContextAccumulator(Map tagContext) {
060            MethodCall methodCall = (MethodCall)tagContext.get(TagHandler.CURRENT_PARM_ACC_KEY);
061            return methodCall;
062        }
063    
064        protected MethodCall createMethodCall(String refObjectClassName, String methodName, Object refObject) throws SAXException {
065            try {
066                return new jgb.builder.utils.MethodCall(refObjectClassName, methodName, refObject);
067            } catch (Exception e) {
068                throwParsingException("could not instantiate method call on "
069                        + "objects of class " + refObjectClassName
070                        + " with method of name " + methodName, e);
071            }
072    
073            return null;
074        }
075    
076        protected void putAccumulator(Map tagContext, jgb.builder.utils.MethodCall methodCall) {
077            tagContext.put(TagHandler.CURRENT_PARM_ACC_KEY, methodCall);
078        }
079    
080        protected Object getReferenceObject(Map atts) {
081            Object refObject;
082            if ((String)atts.get(ATTR_REFID) == null) {
083                refObject = getCurrentObject();
084            } else {
085                refObject = getObject((String)atts.get(ATTR_REFID));
086            }
087            return refObject;
088        }
089    
090        protected String getMethodName(Map atts) {
091            String methodName = (String)atts.get(ATTR_METHOD);
092            return methodName;
093        }
094    }