1/* 2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25package com.sun.beans.decoder; 26 27import java.beans.Expression; 28 29import static java.util.Locale.ENGLISH; 30 31/** 32 * This class is intended to handle <object> element. 33 * This element looks like <void> element, 34 * but its value is always used as an argument for element 35 * that contains this one. 36 * <p>The following attributes are supported: 37 * <dl> 38 * <dt>class 39 * <dd>the type is used for static methods and fields 40 * <dt>method 41 * <dd>the method name 42 * <dt>property 43 * <dd>the property name 44 * <dt>index 45 * <dd>the property index 46 * <dt>field 47 * <dd>the field name 48 * <dt>idref 49 * <dd>the identifier to refer to the variable 50 * <dt>id 51 * <dd>the identifier of the variable that is intended to store the result 52 * </dl> 53 * 54 * @since 1.7 55 * 56 * @author Sergey A. Malenkov 57 */ 58class ObjectElementHandler extends NewElementHandler { 59 private String idref; 60 private String field; 61 private Integer index; 62 private String property; 63 private String method; 64 65 /** 66 * Parses attributes of the element. 67 * The following attributes are supported: 68 * <dl> 69 * <dt>class 70 * <dd>the type is used for static methods and fields 71 * <dt>method 72 * <dd>the method name 73 * <dt>property 74 * <dd>the property name 75 * <dt>index 76 * <dd>the property index 77 * <dt>field 78 * <dd>the field name 79 * <dt>idref 80 * <dd>the identifier to refer to the variable 81 * <dt>id 82 * <dd>the identifier of the variable that is intended to store the result 83 * </dl> 84 * 85 * @param name the attribute name 86 * @param value the attribute value 87 */ 88 @Override 89 public final void addAttribute(String name, String value) { 90 if (name.equals("idref")) { // NON-NLS: the attribute name 91 this.idref = value; 92 } else if (name.equals("field")) { // NON-NLS: the attribute name 93 this.field = value; 94 } else if (name.equals("index")) { // NON-NLS: the attribute name 95 this.index = Integer.valueOf(value); 96 addArgument(this.index); // hack for compatibility 97 } else if (name.equals("property")) { // NON-NLS: the attribute name 98 this.property = value; 99 } else if (name.equals("method")) { // NON-NLS: the attribute name 100 this.method = value; 101 } else { 102 super.addAttribute(name, value); 103 } 104 } 105 106 /** 107 * Calculates the value of this element 108 * if the field attribute or the idref attribute is set. 109 */ 110 @Override 111 public final void startElement() { 112 if ((this.field != null) || (this.idref != null)) { 113 getValueObject(); 114 } 115 } 116 117 /** 118 * Tests whether the value of this element can be used 119 * as an argument of the element that contained in this one. 120 * 121 * @return {@code true} if the value of this element can be used 122 * as an argument of the element that contained in this one, 123 * {@code false} otherwise 124 */ 125 @Override 126 protected boolean isArgument() { 127 return true; // hack for compatibility 128 } 129 130 /** 131 * Creates the value of this element. 132 * 133 * @param type the base class 134 * @param args the array of arguments 135 * @return the value of this element 136 * @throws Exception if calculation is failed 137 */ 138 @Override 139 protected final ValueObject getValueObject(Class<?> type, Object[] args) throws Exception { 140 if (this.field != null) { 141 return ValueObjectImpl.create(FieldElementHandler.getFieldValue(getContextBean(), this.field)); 142 } 143 if (this.idref != null) { 144 return ValueObjectImpl.create(getVariable(this.idref)); 145 } 146 Object bean = getContextBean(); 147 String name; 148 if (this.index != null) { 149 name = (args.length == 2) 150 ? PropertyElementHandler.SETTER 151 : PropertyElementHandler.GETTER; 152 } else if (this.property != null) { 153 name = (args.length == 1) 154 ? PropertyElementHandler.SETTER 155 : PropertyElementHandler.GETTER; 156 157 if (0 < this.property.length()) { 158 name += this.property.substring(0, 1).toUpperCase(ENGLISH) + this.property.substring(1); 159 } 160 } else { 161 name = (this.method != null) && (0 < this.method.length()) 162 ? this.method 163 : "new"; // NON-NLS: the constructor marker 164 } 165 Expression expression = new Expression(bean, name, args); 166 return ValueObjectImpl.create(expression.getValue()); 167 } 168} 169