001 /*
002 * Copyright (c) 2005 Marko Karppinen & Co. LLC
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a
005 * copy of this software and associated documentation files (the "Software"),
006 * to deal in the Software without restriction, including without limitation
007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
008 * and/or sell copies of the Software, and to permit persons to whom the
009 * Software is furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
017 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020 * DEALINGS IN THE SOFTWARE.
021 */
022
023 package fi.karppinen.xml;
024
025 import gnu.xml.pipeline.EventConsumer;
026 import gnu.xml.pipeline.EventFilter;
027
028 import java.lang.reflect.InvocationTargetException;
029 import java.lang.reflect.Method;
030
031 import org.apache.log4j.Logger;
032 import org.xml.sax.SAXException;
033
034 /**
035 * Invokes a method named in a PI. Eg. <code><?method <var>doFoo</var>?></code>
036 *
037 * @version $Id: MethodInvocator.java,v 1.2 2006/12/01 12:37:00 hsivonen Exp $
038 * @author hsivonen
039 */
040 public class MethodInvocator extends EventFilter {
041 private static final Logger log4j = Logger.getLogger(MethodInvocator.class);
042
043 private Object obj;
044
045 private Class<? extends Object> clazz;
046
047 /**
048 * @param obj the object whose methods to invoke
049 */
050 public MethodInvocator(Object obj) {
051 super();
052 this.obj = obj;
053 this.clazz = obj.getClass();
054 setContentHandler(this);
055 }
056
057 /**
058 * @param obj the object whose methods to invoke
059 */
060 public MethodInvocator(Object obj, EventConsumer ec) {
061 super(ec);
062 this.obj = obj;
063 this.clazz = obj.getClass();
064 setContentHandler(this);
065 }
066
067 /**
068 * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
069 */
070 public void processingInstruction(String target, String data)
071 throws SAXException {
072 if ("method".equals(target)) {
073 String trimmed = data.trim();
074 try {
075 Method method = clazz.getMethod(trimmed, null);
076 method.invoke(obj, null);
077 } catch (SecurityException e) {
078 log4j.error("SecurityException", e);
079 throw new SAXException(e);
080 } catch (NoSuchMethodException e) {
081 log4j.error("NoSuchMethodException", e);
082 throw new SAXException(e);
083 } catch (IllegalArgumentException e) {
084 log4j.error("IllegalArgumentException", e);
085 throw new SAXException(e);
086 } catch (IllegalAccessException e) {
087 log4j.error("IllegalAccessException", e);
088 throw new SAXException(e);
089 } catch (InvocationTargetException e) {
090 Throwable t = e.getTargetException();
091 log4j.error("Throwable", t);
092 if (t instanceof SAXException) {
093 throw (SAXException)t;
094 } else if(t instanceof Exception) {
095 throw new SAXException((Exception)t);
096 } else {
097 throw new SAXException(e);
098 }
099 }
100 } else {
101 super.processingInstruction(target, data);
102 }
103 }
104 }