1/* 2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25package com.sun.beans.decoder; 26 27/** 28 * This class is intended to handle <string> element. 29 * This element specifies {@link String} values. 30 * The result value is created from text of the body of this element. 31 * For example:<pre> 32 * <string>description</string></pre> 33 * is equivalent to {@code "description"} in Java code. 34 * The value of inner element is calculated 35 * before adding to the string using {@link String#valueOf(Object)}. 36 * Note that all characters are used including whitespaces (' ', '\t', '\n', '\r'). 37 * So the value of the element<pre> 38 * <string><true></string></pre> 39 * is not equal to the value of the element<pre> 40 * <string> 41 * <true> 42 * </string></pre> 43 * <p>The following attribute is supported: 44 * <dl> 45 * <dt>id 46 * <dd>the identifier of the variable that is intended to store the result 47 * </dl> 48 * 49 * @since 1.7 50 * 51 * @author Sergey A. Malenkov 52 */ 53public class StringElementHandler extends ElementHandler { 54 private StringBuilder sb = new StringBuilder(); 55 private ValueObject value = ValueObjectImpl.NULL; 56 57 /** 58 * Adds the character that contained in this element. 59 * 60 * @param ch the character 61 */ 62 @Override 63 public final void addCharacter(char ch) { 64 if (this.sb == null) { 65 throw new IllegalStateException("Could not add chararcter to evaluated string element"); 66 } 67 this.sb.append(ch); 68 } 69 70 /** 71 * Adds the string value of the argument to the string value of this element. 72 * 73 * @param argument the value of the element that contained in this one 74 */ 75 @Override 76 protected final void addArgument(Object argument) { 77 if (this.sb == null) { 78 throw new IllegalStateException("Could not add argument to evaluated string element"); 79 } 80 this.sb.append(argument); 81 } 82 83 /** 84 * Returns the value of this element. 85 * 86 * @return the value of this element 87 */ 88 @Override 89 protected final ValueObject getValueObject() { 90 if (this.sb != null) { 91 try { 92 this.value = ValueObjectImpl.create(getValue(this.sb.toString())); 93 } 94 catch (RuntimeException exception) { 95 getOwner().handleException(exception); 96 } 97 finally { 98 this.sb = null; 99 } 100 } 101 return this.value; 102 } 103 104 /** 105 * Returns the text of the body of this element. 106 * This method evaluates value from text of the body, 107 * and should be overridden in those handlers 108 * that extend behavior of this element. 109 * 110 * @param argument the text of the body 111 * @return evaluated value 112 */ 113 protected Object getValue(String argument) { 114 return argument; 115 } 116} 117