Path: blob/master/src/java.desktop/share/classes/com/sun/beans/decoder/StringElementHandler.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* This class is intended to handle <string> element.28* This element specifies {@link String} values.29* The result value is created from text of the body of this element.30* For example:<pre>31* <string>description</string></pre>32* is equivalent to {@code "description"} in Java code.33* The value of inner element is calculated34* before adding to the string using {@link String#valueOf(Object)}.35* Note that all characters are used including whitespaces (' ', '\t', '\n', '\r').36* So the value of the element<pre>37* <string><true></string></pre>38* is not equal to the value of the element<pre>39* <string>40* <true>41* </string></pre>42* <p>The following attribute is supported:43* <dl>44* <dt>id45* <dd>the identifier of the variable that is intended to store the result46* </dl>47*48* @since 1.749*50* @author Sergey A. Malenkov51*/52public class StringElementHandler extends ElementHandler {53private StringBuilder sb = new StringBuilder();54private ValueObject value = ValueObjectImpl.NULL;5556/**57* Adds the character that contained in this element.58*59* @param ch the character60*/61@Override62public final void addCharacter(char ch) {63if (this.sb == null) {64throw new IllegalStateException("Could not add chararcter to evaluated string element");65}66this.sb.append(ch);67}6869/**70* Adds the string value of the argument to the string value of this element.71*72* @param argument the value of the element that contained in this one73*/74@Override75protected final void addArgument(Object argument) {76if (this.sb == null) {77throw new IllegalStateException("Could not add argument to evaluated string element");78}79this.sb.append(argument);80}8182/**83* Returns the value of this element.84*85* @return the value of this element86*/87@Override88protected final ValueObject getValueObject() {89if (this.sb != null) {90try {91this.value = ValueObjectImpl.create(getValue(this.sb.toString()));92}93catch (RuntimeException exception) {94getOwner().handleException(exception);95}96finally {97this.sb = null;98}99}100return this.value;101}102103/**104* Returns the text of the body of this element.105* This method evaluates value from text of the body,106* and should be overridden in those handlers107* that extend behavior of this element.108*109* @param argument the text of the body110* @return evaluated value111*/112protected Object getValue(String argument) {113return argument;114}115}116117118