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.lang.reflect.Array; 28 29/** 30 * This class is intended to handle <array> element, 31 * that is used to array creation. 32 * The {@code length} attribute specifies the length of the array. 33 * The {@code class} attribute specifies the elements type. 34 * The {@link Object} type is used by default. 35 * For example:<pre> 36 * <array length="10"/></pre> 37 * is equivalent to {@code new Component[10]} in Java code. 38 * The {@code set} and {@code get} methods, 39 * as defined in the {@link java.util.List} interface, 40 * can be used as if they could be applied to array instances. 41 * The {@code index} attribute can thus be used with arrays. 42 * For example:<pre> 43 * <array length="3" class="java.lang.String"> 44 * <void index="1"> 45 * <string>Hello, world</string> 46 * </void> 47 * </array></pre> 48 * is equivalent to the following Java code:<pre> 49 * String[] s = new String[3]; 50 * s[1] = "Hello, world";</pre> 51 * It is possible to omit the {@code length} attribute and 52 * specify the values directly, without using {@code void} tags. 53 * The length of the array is equal to the number of values specified. 54 * For example:<pre> 55 * <array id="array" class="int"> 56 * <int>123</int> 57 * <int>456</int> 58 * </array></pre> 59 * is equivalent to {@code int[] array = {123, 456}} in Java code. 60 * <p>The following attributes are supported: 61 * <dl> 62 * <dt>length 63 * <dd>the array length 64 * <dt>class 65 * <dd>the type of object for instantiation 66 * <dt>id 67 * <dd>the identifier of the variable that is intended to store the result 68 * </dl> 69 * 70 * @since 1.7 71 * 72 * @author Sergey A. Malenkov 73 */ 74final class ArrayElementHandler extends NewElementHandler { 75 private Integer length; 76 77 /** 78 * Parses attributes of the element. 79 * The following attributes are supported: 80 * <dl> 81 * <dt>length 82 * <dd>the array length 83 * <dt>class 84 * <dd>the type of object for instantiation 85 * <dt>id 86 * <dd>the identifier of the variable that is intended to store the result 87 * </dl> 88 * 89 * @param name the attribute name 90 * @param value the attribute value 91 */ 92 @Override 93 public void addAttribute(String name, String value) { 94 if (name.equals("length")) { // NON-NLS: the attribute name 95 this.length = Integer.valueOf(value); 96 } else { 97 super.addAttribute(name, value); 98 } 99 } 100 101 /** 102 * Calculates the value of this element 103 * if the lentgh attribute is set. 104 */ 105 @Override 106 public void startElement() { 107 if (this.length != null) { 108 getValueObject(); 109 } 110 } 111 112 /** 113 * Tests whether the value of this element can be used 114 * as an argument of the element that contained in this one. 115 * 116 * @return {@code true} if the value of this element can be used 117 * as an argument of the element that contained in this one, 118 * {@code false} otherwise 119 */ 120 @Override 121 protected boolean isArgument() { 122 return true; // hack for compatibility 123 } 124 125 126 /** 127 * Creates an instance of the array. 128 * 129 * @param type the base class 130 * @param args the array of arguments 131 * @return the value of this element 132 */ 133 @Override 134 protected ValueObject getValueObject(Class<?> type, Object[] args) { 135 if (type == null) { 136 type = Object.class; 137 } 138 if (this.length != null) { 139 return ValueObjectImpl.create(Array.newInstance(type, this.length)); 140 } 141 Object array = Array.newInstance(type, args.length); 142 for (int i = 0; i < args.length; i++) { 143 Array.set(array, i, args[i]); 144 } 145 return ValueObjectImpl.create(array); 146 } 147} 148