View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Blacksmith, Inc.
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.web;
20  
21  import java.text.Format;
22  
23  import net.wotonomy.foundation.NSDictionary;
24  import net.wotonomy.foundation.NSNumberFormatter;
25  import net.wotonomy.foundation.NSTimestampFormatter;
26  
27  /***
28  * WOString renders a dynamically generated string in the output.
29  * Bindings are:
30  * <ul>
31  * <li>value: a property returning a value which will be rendered as the 
32  * output.  If a formatter is not used, then the value must be convertable
33  * to a String with toString().</li>
34  * <li>escapeHTML: a property returning a value convertable to a Boolean
35  * indicating whether the any html characters in the output should be
36  * escaped so they are shown as html characters rather than interpreted
37  * as html.</li>
38  * <li>formatter: a property returning a Format object that will be
39  * used to format the value into a String.</li>
40  * <li>dateformat: a property returning a DateFormat object that will be
41  * used to format the value into a String.</li>
42  * <li>numberformat: a property returning a NumberFormat object that will be
43  * used to format the value into a String.  Not yet implemented.</li>
44  * <li>valueWhenEmpty: a property returning a String that will be used
45  * in place of an empty or null value.  Not yet implemented.</li>
46  * </ul>
47  *
48  * @author michael@mpowers.net
49  * @author $Author: cgruber $
50  * @version $Revision: 905 $
51  */
52  public class WOString extends WODynamicElement
53  {
54  	protected Object value;
55  	protected boolean escapeHTML;
56  	protected Format formatter;
57  	protected String dateformat;
58  	protected String numberformat;
59      protected Object valueWhenEmpty;
60  
61  	/***
62  	* The default constructor.
63  	*/
64      protected WOString ()
65      {
66      }
67      
68      public WOString (
69      	String aName, NSDictionary anAssociationMap, WOElement aRootElement)
70      {
71      	super( aName, anAssociationMap, aRootElement );
72  	    escapeHTML = true;
73      }
74  
75      public void appendToResponse (WOResponse aResponse, WOContext aContext)
76      {	
77          WOComponent c = aContext.component();
78          numberformat = stringForProperty("numberformat", c );
79          dateformat = stringForProperty("dateformat", c );
80          formatter = (Format) valueForProperty("formatter", c );
81          escapeHTML = booleanForProperty("escapeHTML", c );
82          value = valueForProperty("value", c );
83          valueWhenEmpty = valueForProperty("valueWhenEmpty", c );
84          
85          Object result = value;
86  	    if ( result != null )
87          {
88              if ( formatter != null )
89              {
90                  try
91                  {
92                      result = formatter.format( result );
93                  }
94                  catch ( IllegalArgumentException exc )
95                  {
96                  }
97              }
98              if ( dateformat != null )
99              {
100                 try
101                 {
102                     result = new NSTimestampFormatter( dateformat ).format( result );
103                 }
104                 catch ( IllegalArgumentException exc )
105                 {
106                 }
107             }
108             if ( numberformat != null )
109             {
110                 try
111                 {
112                     result = new NSNumberFormatter( numberformat ).format( result );
113                 }
114                 catch ( IllegalArgumentException exc )
115                 {
116                 }
117             }
118         }
119 	    if ( result == null )
120 	    {
121             result = valueWhenEmpty;
122             if ( result == null )
123             {
124                 result = "nil";
125             }
126 	    }
127     	if ( escapeHTML )
128 	    {
129     		aResponse.appendContentHTMLString( result.toString() );
130 	    }
131 	    else
132 	    {
133     		aResponse.appendContentString( result.toString() );
134 		}
135     }
136 }