Path: blob/master/src/java.desktop/share/classes/com/sun/beans/decoder/ElementHandler.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;2526/**27* The base class for element handlers.28*29* @since 1.730*31* @author Sergey A. Malenkov32*33* @see DocumentHandler34*/35public abstract class ElementHandler {36private DocumentHandler owner;37private ElementHandler parent;3839private String id;4041/**42* Returns the document handler that creates this element handler.43*44* @return the owner document handler45*/46public final DocumentHandler getOwner() {47return this.owner;48}4950/**51* Sets the document handler that creates this element handler.52* The owner document handler should be set after instantiation.53* Such approach is used to simplify the extensibility.54*55* @param owner the owner document handler56* @see DocumentHandler#startElement57*/58final void setOwner(DocumentHandler owner) {59if (owner == null) {60throw new IllegalArgumentException("Every element should have owner");61}62this.owner = owner;63}6465/**66* Returns the element handler that contains this one.67*68* @return the parent element handler69*/70public final ElementHandler getParent() {71return this.parent;72}7374/**75* Sets the element handler that contains this one.76* The parent element handler should be set after instantiation.77* Such approach is used to simplify the extensibility.78*79* @param parent the parent element handler80* @see DocumentHandler#startElement81*/82final void setParent(ElementHandler parent) {83this.parent = parent;84}8586/**87* Returns the value of the variable with specified identifier.88*89* @param id the identifier90* @return the value of the variable91*/92protected final Object getVariable(String id) {93if (id.equals(this.id)) {94ValueObject value = getValueObject();95if (value.isVoid()) {96throw new IllegalStateException("The element does not return value");97}98return value.getValue();99}100return (this.parent != null)101? this.parent.getVariable(id)102: this.owner.getVariable(id);103}104105/**106* Returns the value of the parent element.107*108* @return the value of the parent element109*/110protected Object getContextBean() {111if (this.parent != null) {112ValueObject value = this.parent.getValueObject();113if (!value.isVoid()) {114return value.getValue();115}116throw new IllegalStateException("The outer element does not return value");117} else {118Object value = this.owner.getOwner();119if (value != null) {120return value;121}122throw new IllegalStateException("The topmost element does not have context");123}124}125126/**127* Parses attributes of the element.128* By default, the following attribute is supported:129* <dl>130* <dt>id131* <dd>the identifier of the variable that is intended to store the result132* </dl>133*134* @param name the attribute name135* @param value the attribute value136*/137public void addAttribute(String name, String value) {138if (name.equals("id")) { // NON-NLS: the attribute name139this.id = value;140} else {141throw new IllegalArgumentException("Unsupported attribute: " + name);142}143}144145/**146* This method is called before parsing of the element's body.147* All attributes are parsed at this point.148* By default, do nothing.149*/150public void startElement() {151}152153/**154* This method is called after parsing of the element's body.155* By default, it calculates the value of this element.156* The following tasks are executing for any non-void value:157* <ol>158* <li>If the {@code id} attribute is set159* the value of the variable with the specified identifier160* is set to the value of this element.</li>161* <li>This element is used as an argument of parent element if it is possible.</li>162* </ol>163*164* @see #isArgument165*/166public void endElement() {167// do nothing if no value returned168ValueObject value = getValueObject();169if (!value.isVoid()) {170if (this.id != null) {171this.owner.setVariable(this.id, value.getValue());172}173if (isArgument()) {174if (this.parent != null) {175this.parent.addArgument(value.getValue());176} else {177this.owner.addObject(value.getValue());178}179}180}181}182183/**184* Adds the character that contained in this element.185* By default, only whitespaces are acceptable.186*187* @param ch the character188*/189public void addCharacter(char ch) {190if ((ch != ' ') && (ch != '\n') && (ch != '\t') && (ch != '\r')) {191throw new IllegalStateException("Illegal character with code " + (int) ch);192}193}194195/**196* Adds the argument that is used to calculate the value of this element.197* By default, no arguments are acceptable.198*199* @param argument the value of the element that contained in this one200*/201protected void addArgument(Object argument) {202throw new IllegalStateException("Could not add argument to simple element");203}204205/**206* Tests whether the value of this element can be used207* as an argument of the element that contained in this one.208*209* @return {@code true} if the value of this element can be used210* as an argument of the element that contained in this one,211* {@code false} otherwise212*/213protected boolean isArgument() {214return this.id == null;215}216217/**218* Returns the value of this element.219*220* @return the value of this element221*/222protected abstract ValueObject getValueObject();223}224225226