Path: blob/master/src/java.desktop/share/classes/com/sun/beans/decoder/JavaElementHandler.java
41171 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.beans.decoder;2526import java.beans.XMLDecoder;2728/**29* This class is intended to handle <java> element.30* Each element that appears in the body of this element31* is evaluated in the context of the decoder itself.32* Typically this outer context is used to retrieve the owner of the decoder,33* which can be set before reading the archive.34* <p>The following attributes are supported:35* <dl>36* <dt>version37* <dd>the Java version (not supported)38* <dt>class39* <dd>the type of preferable parser (not supported)40* <dt>id41* <dd>the identifier of the variable that is intended to store the result42* </dl>43*44* @see DocumentHandler#getOwner45* @see DocumentHandler#setOwner46* @since 1.747*48* @author Sergey A. Malenkov49*/50final class JavaElementHandler extends ElementHandler {51private Class<?> type;52private ValueObject value;5354/**55* Parses attributes of the element.56* The following attributes are supported:57* <dl>58* <dt>version59* <dd>the Java version (not supported)60* <dt>class61* <dd>the type of preferable parser (not supported)62* <dt>id63* <dd>the identifier of the variable that is intended to store the result64* </dl>65*66* @param name the attribute name67* @param value the attribute value68*/69@Override70public void addAttribute(String name, String value) {71if (name.equals("version")) { // NON-NLS: the attribute name72// unsupported attribute73} else if (name.equals("class")) { // NON-NLS: the attribute name74// check class for owner75this.type = getOwner().findClass(value);76} else {77super.addAttribute(name, value);78}79}8081/**82* Adds the argument to the list of readed objects.83*84* @param argument the value of the element that contained in this one85*/86@Override87protected void addArgument(Object argument) {88getOwner().addObject(argument);89}9091/**92* Tests whether the value of this element can be used93* as an argument of the element that contained in this one.94*95* @return {@code true} if the value of this element should be used96* as an argument of the element that contained in this one,97* {@code false} otherwise98*/99@Override100protected boolean isArgument() {101return false; // do not use owner as object102}103104/**105* Returns the value of this element.106*107* @return the value of this element108*/109@Override110protected ValueObject getValueObject() {111if (this.value == null) {112this.value = ValueObjectImpl.create(getValue());113}114return this.value;115}116117/**118* Returns the owner of the owner document handler119* as a value of <java> element.120*121* @return the owner of the owner document handler122*/123private Object getValue() {124Object owner = getOwner().getOwner();125if ((this.type == null) || isValid(owner)) {126return owner;127}128if (owner instanceof XMLDecoder) {129XMLDecoder decoder = (XMLDecoder) owner;130owner = decoder.getOwner();131if (isValid(owner)) {132return owner;133}134}135throw new IllegalStateException("Unexpected owner class: " + owner.getClass().getName());136}137138/**139* Validates the owner of the <java> element.140* The owner is valid if it is {@code null} or an instance141* of the class specified by the {@code class} attribute.142*143* @param owner the owner of the <java> element144* @return {@code true} if the {@code owner} is valid;145* {@code false} otherwise146*/147private boolean isValid(Object owner) {148return (owner == null) || this.type.isInstance(owner);149}150}151152153