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.beans.XMLDecoder;
827N/A
827N/A/**
827N/A * This class is intended to handle <java> element.
827N/A * Each element that appears in the body of this element
827N/A * is evaluated in the context of the decoder itself.
827N/A * Typically this outer context is used to retrieve the owner of the decoder,
827N/A * which can be set before reading the archive.
827N/A * <p>The following atributes are supported:
827N/A * <dl>
827N/A * <dt>version
827N/A * <dd>the Java version (not supported)
827N/A * <dt>class
827N/A * <dd>the type of preferable parser (not supported)
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 * @see DocumentHandler#getOwner
827N/A * @see DocumentHandler#setOwner
827N/A * @since 1.7
827N/A *
827N/A * @author Sergey A. Malenkov
827N/A */
827N/Afinal class JavaElementHandler extends ElementHandler {
827N/A private Class<?> type;
827N/A private ValueObject value;
827N/A
827N/A /**
827N/A * Parses attributes of the element.
827N/A * The following atributes are supported:
827N/A * <dl>
827N/A * <dt>version
827N/A * <dd>the Java version (not supported)
827N/A * <dt>class
827N/A * <dd>the type of preferable parser (not supported)
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("version")) { // NON-NLS: the attribute name
827N/A // unsupported attribute
827N/A } else if (name.equals("class")) { // NON-NLS: the attribute name
827N/A // check class for owner
827N/A this.type = getOwner().findClass(value);
827N/A } else {
827N/A super.addAttribute(name, value);
827N/A }
827N/A }
827N/A
827N/A /**
827N/A * Adds the argument to the list of readed objects.
827N/A *
827N/A * @param argument the value of the element that contained in this one
827N/A */
827N/A @Override
827N/A protected void addArgument(Object argument) {
827N/A getOwner().addObject(argument);
827N/A }
827N/A
827N/A /**
827N/A * Tests whether the value of this element can be used
827N/A * as an argument of the element that contained in this one.
827N/A *
827N/A * @return {@code true} if the value of this element should be used
827N/A * as an argument of the element that contained in this one,
827N/A * {@code false} otherwise
827N/A */
827N/A @Override
827N/A protected boolean isArgument() {
827N/A return false; // do not use owner as object
827N/A }
827N/A
827N/A /**
827N/A * Returns the value of this element.
827N/A *
827N/A * @return the value of this element
827N/A */
827N/A @Override
827N/A protected ValueObject getValueObject() {
827N/A if (this.value == null) {
827N/A this.value = ValueObjectImpl.create(getValue());
827N/A }
827N/A return this.value;
827N/A }
827N/A
827N/A /**
827N/A * Returns the owner of the owner document handler
827N/A * as a value of &lt;java&gt; element.
827N/A *
827N/A * @return the owner of the owner document handler
827N/A */
827N/A private Object getValue() {
827N/A Object owner = getOwner().getOwner();
827N/A if ((this.type == null) || isValid(owner)) {
827N/A return owner;
827N/A }
827N/A if (owner instanceof XMLDecoder) {
827N/A XMLDecoder decoder = (XMLDecoder) owner;
827N/A owner = decoder.getOwner();
827N/A if (isValid(owner)) {
827N/A return owner;
827N/A }
827N/A }
827N/A throw new IllegalStateException("Unexpected owner class: " + owner.getClass().getName());
827N/A }
827N/A
827N/A /**
827N/A * Validates the owner of the &lt;java&gt; element.
827N/A * The owner is valid if it is {@code null} or an instance
827N/A * of the class specified by the {@code class} attribute.
827N/A *
827N/A * @param owner the owner of the &lt;java&gt; element
827N/A * @return {@code true} if the {@code owner} is valid;
827N/A * {@code false} otherwise
827N/A */
827N/A private boolean isValid(Object owner) {
827N/A return (owner == null) || this.type.isInstance(owner);
827N/A }
827N/A}