Frames | No Frames |
1: /* gnu.java.beans.decoder.ObjectHandler 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: /** An ObjectHandler parses the <object> tag and thereby creates various 45: * Context implementations. 46: * 47: * @author Robert Schuster 48: * 49: */ 50: public class ObjectHandler extends AbstractElementHandler 51: { 52: /** 53: * XXX: Can all results be stored with an object id? 54: * 55: * 56: * @param PersistenceParser 57: */ 58: ObjectHandler(ElementHandler parent) 59: { 60: super(parent, true); 61: } 62: 63: protected Context startElement(Attributes attributes, ExceptionListener exceptionListener) 64: throws AssemblyException 65: { 66: String className = attributes.getValue("class"); 67: String methodName = attributes.getValue("method"); 68: String fieldName = attributes.getValue("field"); 69: String index = attributes.getValue("index"); 70: String propertyName = attributes.getValue("property"); 71: String id = attributes.getValue("id"); 72: String idRef = attributes.getValue("idref"); 73: 74: /* first check if we just want to access an existing object (idref present) 75: * 76: * note: <object idref="foo" method="bar"/> is not valid to call method "bar" 77: * on the object with id "foo". Instead this should return the object "foo" 78: * itself. The right way to this is: 79: * <object idref="foo"> 80: * <object method="bar"/> 81: * </object> 82: * 83: * This means that if idref is present class, method, field, index and 84: * property are obsolete. 85: */ 86: if (idRef != null) 87: // reactivates an existing object and giving it another name if id exists 88: return new ObjectContext(id, getObject(idRef)); 89: 90: // decides whether we are in a static (className present) or dynamic context 91: if (className != null) 92: { 93: try 94: { 95: Class klass = instantiateClass(className); 96: 97: // class name exists which means that we are in a static context. 98: // so we may want to ... 99: // access a static field if the fieldName exists 100: if (fieldName != null) 101: { 102: try 103: { 104: return new ObjectContext(id, 105: klass.getField(fieldName).get(null)); 106: } 107: catch (NoSuchFieldException nsfe) 108: { 109: throw new AssemblyException(nsfe); 110: } 111: catch (IllegalAccessException iae) 112: { 113: throw new AssemblyException(iae); 114: } 115: } 116: 117: // (falling through is important!) 118: // run a constructor if methodName is "new" or null 119: if (methodName == null || methodName.equals("new")) 120: return new ConstructorContext(id, klass); 121: 122: // (falling through is important!) 123: // run a static method on the given class (if methodName exists, which is implied already) 124: return new StaticMethodContext(id, klass, methodName); 125: // XXX: should fail if unexpected attributes are present? 126: } 127: catch (ClassNotFoundException cnfe) 128: { 129: throw new AssemblyException(cnfe); 130: } 131: } 132: else 133: { 134: // className does not exist which means we are in the context of 135: // some object and want to ... 136: // access the get(int index) method if index != null 137: if (index != null) 138: { 139: try 140: { 141: // Note: http://java.sun.com/products/jfc/tsc/articles/persistence3/ says 142: // that <void index="4"/> will make up a get()-call. But this is wrong because 143: // <void/> tags never return values (to the surrounding context) 144: return new IndexContext(id, Integer.parseInt(index)); 145: } 146: catch (NumberFormatException nfe) 147: { 148: throw new AssemblyException(nfe); 149: } 150: } 151: 152: // access a method if methodName exists 153: if (methodName != null) 154: return new MethodContext(id, methodName); 155: 156: // (falling through is important!) 157: // access a property if a propertyName exists 158: if (propertyName != null && propertyName.length() > 0) 159: // this is reported as an ordinary method access where the propertyName is 160: // converted into a 'getter'-method name: convert first character of property name 161: // to upper case and prepend 'get' 162: // Note: This will be a getter-method because the <object> tag implies that a return 163: // value is expected. 164: return new PropertyContext(id, propertyName); 165: } 166: 167: throw new AssemblyException(new IllegalArgumentException("Wrong or missing attributes for <object> tag.")); 168: } 169: }