827N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
827N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
827N/A *
827N/A * This code is free software; you can redistribute it and/or modify it
827N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
827N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
827N/A *
827N/A * This code is distributed in the hope that it will be useful, but WITHOUT
827N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
827N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
827N/A * version 2 for more details (a copy is included in the LICENSE file that
827N/A * accompanied this code).
827N/A *
827N/A * You should have received a copy of the GNU General Public License version
827N/A * 2 along with this work; if not, write to the Free Software Foundation,
827N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
827N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
827N/A */
827N/Apackage com.sun.beans.decoder;
827N/A
827N/Aimport java.lang.reflect.Array;
827N/A
827N/A/**
827N/A * This class is intended to handle <array> element,
827N/A * that is used to array creation.
827N/A * The {@code length} attribute specifies the length of the array.
827N/A * The {@code class} attribute specifies the elements type.
827N/A * The {@link Object} type is used by default.
827N/A * For example:<pre>
827N/A * &lt;array length="10"/&gt;</pre>
827N/A * is equivalent to {@code new Component[10]} in Java code.
827N/A * The {@code set} and {@code get} methods,
827N/A * as defined in the {@link java.util.List} interface,
827N/A * can be used as if they could be applied to array instances.
827N/A * The {@code index} attribute can thus be used with arrays.
827N/A * For example:<pre>
827N/A * &lt;array length="3" class="java.lang.String"&gt;
827N/A * &lt;void index="1"&gt;
827N/A * &lt;string&gt;Hello, world&lt;/string&gt;
827N/A * &lt;/void&gt;
827N/A * &lt;/array&gt;</pre>
827N/A * is equivalent to the following Java code:<pre>
827N/A * String[] s = new String[3];
827N/A * s[1] = "Hello, world";</pre>
827N/A * It is possible to omit the {@code length} attribute and
827N/A * specify the values directly, without using {@code void} tags.
827N/A * The length of the array is equal to the number of values specified.
827N/A * For example:<pre>
827N/A * &lt;array id="array" class="int"&gt;
827N/A * &lt;int&gt;123&lt;/int&gt;
827N/A * &lt;int&gt;456&lt;/int&gt;
827N/A * &lt;/array&gt;</pre>
827N/A * is equivalent to {@code int[] array = {123, 456}} in Java code.
827N/A * <p>The following atributes are supported:
827N/A * <dl>
827N/A * <dt>length
827N/A * <dd>the array length
827N/A * <dt>class
827N/A * <dd>the type of object for instantiation
827N/A * <dt>id
827N/A * <dd>the identifier of the variable that is intended to store the result
827N/A * </dl>
827N/A *
827N/A * @since 1.7
827N/A *
827N/A * @author Sergey A. Malenkov
827N/A */
827N/Afinal class ArrayElementHandler extends NewElementHandler {
827N/A private Integer length;
827N/A
827N/A /**
827N/A * Parses attributes of the element.
827N/A * The following atributes are supported:
827N/A * <dl>
827N/A * <dt>length
827N/A * <dd>the array length
827N/A * <dt>class
827N/A * <dd>the type of object for instantiation
827N/A * <dt>id
827N/A * <dd>the identifier of the variable that is intended to store the result
827N/A * </dl>
827N/A *
827N/A * @param name the attribute name
827N/A * @param value the attribute value
827N/A */
827N/A @Override
827N/A public void addAttribute(String name, String value) {
827N/A if (name.equals("length")) { // NON-NLS: the attribute name
827N/A this.length = Integer.valueOf(value);
827N/A } else {
827N/A super.addAttribute(name, value);
827N/A }
827N/A }
827N/A
827N/A /**
827N/A * Calculates the value of this element
827N/A * if the lentgh attribute is set.
827N/A */
827N/A @Override
827N/A public void startElement() {
827N/A if (this.length != null) {
827N/A getValueObject();
827N/A }
827N/A }
827N/A
827N/A /**
827N/A * Creates an instance of the array.
827N/A *
827N/A * @param type the base class
827N/A * @param args the array of arguments
827N/A * @return the value of this element
827N/A */
827N/A @Override
827N/A protected ValueObject getValueObject(Class<?> type, Object[] args) {
827N/A if (type == null) {
827N/A type = Object.class;
827N/A }
827N/A if (this.length != null) {
827N/A return ValueObjectImpl.create(Array.newInstance(type, this.length));
827N/A }
827N/A Object array = Array.newInstance(type, args.length);
827N/A for (int i = 0; i < args.length; i++) {
827N/A Array.set(array, i, args[i]);
827N/A }
827N/A return ValueObjectImpl.create(array);
827N/A }
827N/A}