diff options
| author | Benjamin Culkin <scorpress@gmail.com> | 2024-05-19 17:56:33 -0400 |
|---|---|---|
| committer | Benjamin Culkin <scorpress@gmail.com> | 2024-05-19 17:56:33 -0400 |
| commit | aedc34d55462a75e329bbf342251ff6504cd117e (patch) | |
| tree | bcc8f1f2352582717b484df302aeea6696b8f000 /projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOString.java | |
Initial import from SVN
Diffstat (limited to 'projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOString.java')
| -rw-r--r-- | projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOString.java | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOString.java b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOString.java new file mode 100644 index 0000000..ef5b771 --- /dev/null +++ b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOString.java @@ -0,0 +1,136 @@ +/* +Wotonomy: OpenStep design patterns for pure Java applications. +Copyright (C) 2000 Blacksmith, Inc. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, see http://www.gnu.org +*/ + +package net.wotonomy.web; + +import java.text.Format; + +import net.wotonomy.foundation.NSDictionary; +import net.wotonomy.foundation.NSNumberFormatter; +import net.wotonomy.foundation.NSTimestampFormatter; + +/** +* WOString renders a dynamically generated string in the output. +* Bindings are: +* <ul> +* <li>value: a property returning a value which will be rendered as the +* output. If a formatter is not used, then the value must be convertable +* to a String with toString().</li> +* <li>escapeHTML: a property returning a value convertable to a Boolean +* indicating whether the any html characters in the output should be +* escaped so they are shown as html characters rather than interpreted +* as html.</li> +* <li>formatter: a property returning a Format object that will be +* used to format the value into a String.</li> +* <li>dateformat: a property returning a DateFormat object that will be +* used to format the value into a String.</li> +* <li>numberformat: a property returning a NumberFormat object that will be +* used to format the value into a String. Not yet implemented.</li> +* <li>valueWhenEmpty: a property returning a String that will be used +* in place of an empty or null value. Not yet implemented.</li> +* </ul> +* +* @author michael@mpowers.net +* @author $Author: cgruber $ +* @version $Revision: 905 $ +*/ +public class WOString extends WODynamicElement +{ + protected Object value; + protected boolean escapeHTML; + protected Format formatter; + protected String dateformat; + protected String numberformat; + protected Object valueWhenEmpty; + + /** + * The default constructor. + */ + protected WOString () + { + } + + public WOString ( + String aName, NSDictionary anAssociationMap, WOElement aRootElement) + { + super( aName, anAssociationMap, aRootElement ); + escapeHTML = true; + } + + public void appendToResponse (WOResponse aResponse, WOContext aContext) + { + WOComponent c = aContext.component(); + numberformat = stringForProperty("numberformat", c ); + dateformat = stringForProperty("dateformat", c ); + formatter = (Format) valueForProperty("formatter", c ); + escapeHTML = booleanForProperty("escapeHTML", c ); + value = valueForProperty("value", c ); + valueWhenEmpty = valueForProperty("valueWhenEmpty", c ); + + Object result = value; + if ( result != null ) + { + if ( formatter != null ) + { + try + { + result = formatter.format( result ); + } + catch ( IllegalArgumentException exc ) + { + } + } + if ( dateformat != null ) + { + try + { + result = new NSTimestampFormatter( dateformat ).format( result ); + } + catch ( IllegalArgumentException exc ) + { + } + } + if ( numberformat != null ) + { + try + { + result = new NSNumberFormatter( numberformat ).format( result ); + } + catch ( IllegalArgumentException exc ) + { + } + } + } + if ( result == null ) + { + result = valueWhenEmpty; + if ( result == null ) + { + result = "nil"; + } + } + if ( escapeHTML ) + { + aResponse.appendContentHTMLString( result.toString() ); + } + else + { + aResponse.appendContentString( result.toString() ); + } + } +} |
