Source for gnu.java.beans.decoder.VoidHandler

   1: /* gnu.java.beans.decoder.VoidHandler
   2:    Copyright (C) 2004 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.java.beans.decoder;
  39: 
  40: import java.beans.ExceptionListener;
  41: 
  42: import org.xml.sax.Attributes;
  43: 
  44: public class VoidHandler extends AbstractElementHandler
  45: {
  46:     /**
  47:      * @param PersistenceParser
  48:      */
  49:     VoidHandler(ElementHandler parent)
  50:     {
  51:         super(parent, true);
  52:     }
  53: 
  54:     protected Context startElement(
  55:         Attributes attributes,
  56:         ExceptionListener exceptionListener)
  57:         throws AssemblyException
  58:     {
  59:         Context ctx = startElementImpl(attributes);
  60:         ctx.setStatement(true);
  61: 
  62:         return ctx;
  63:     }
  64: 
  65:     private Context startElementImpl(Attributes attributes)
  66:         throws AssemblyException
  67:     {
  68:         String id = attributes.getValue("id");
  69:         String className = attributes.getValue("class");
  70:         String methodName = attributes.getValue("method");
  71:         String propertyName = attributes.getValue("property");
  72:         String index = attributes.getValue("index");
  73: 
  74:         if (className != null)
  75:         {
  76:             try
  77:             {
  78:                 Class klass = instantiateClass(className);
  79: 
  80:                 // class name exists which means that we are in a static context.
  81:                 // so we may want to ...
  82:                 // run a constructor if methodName is "new" or null
  83:                 if (methodName == null || methodName.equals("new"))
  84:                     // if the id is null the result cannot be by the decoder accessed but the
  85:                     // constructor may have side effects (e.g. registering itself in a global registry)
  86:                     return new ConstructorContext(id, klass);
  87: 
  88:                 // (falling through is important!)
  89:                 // run a static method on the given class (if methodName exists, which is implied already)
  90:                 return new StaticMethodContext(id, klass, methodName);
  91:             }
  92:             catch (ClassNotFoundException cnfe)
  93:             {
  94:                 throw new AssemblyException(cnfe);
  95:             }
  96:         }
  97:         else
  98:         {
  99:             // className does not exist which means we are in the context of
 100:             // some object and want to ...
 101:             // access an element by index
 102:             if (index != null)
 103:             {
 104:                 // note: whether this resolves into get(i) or set(i, o) depends on the
 105:                 // number of arguments and is decided by the ObjectAssembler
 106:                 try
 107:                 {
 108:                     return new IndexContext(id, Integer.parseInt(index));
 109:                 }
 110:                 catch (NumberFormatException nfe)
 111:                 {
 112:                     throw new AssemblyException(nfe);
 113:                 }
 114:             }
 115: 
 116:             // access a method if methodName exists
 117:             if (methodName != null)
 118:                 return new MethodContext(id, methodName);
 119: 
 120:             // (falling through is important!)
 121:             // access a property if a propertyName exists
 122:             if (propertyName != null && propertyName.length() > 0)
 123:                 // this is reported as an ordinary method invocation where the propertyName is
 124:                 // converted into a 'setter'-method name: convert first character of property name
 125:                 // to upper case and prepend 'set'
 126:                 // Note: This will be a setter-method because the <void> tag implies that no return
 127:                 // value is expected (but a side effect)
 128:                 return new PropertyContext(id, propertyName);
 129:         }
 130: 
 131:         // if code reaches this point the tag has wrong attributes. The following test
 132:         // does not make it better but can provide are more specific error message for
 133:         // a common mistake: <void> tags are not allowed to have an idref attribute
 134:         throw new AssemblyException(
 135:             new IllegalArgumentException(
 136:                 (attributes.getValue("idref") == null)
 137:                     ? "Missing attributes for <void> tag"
 138:                     : "<void> does not support 'idref' attribute."));
 139:     }
 140: }