Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
WOString |
|
| 4.333333333333333;4.333 |
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 | 0 | protected WOString () |
65 | 0 | { |
66 | 0 | } |
67 | ||
68 | public WOString ( |
|
69 | String aName, NSDictionary anAssociationMap, WOElement aRootElement) |
|
70 | { |
|
71 | 0 | super( aName, anAssociationMap, aRootElement ); |
72 | 0 | escapeHTML = true; |
73 | 0 | } |
74 | ||
75 | public void appendToResponse (WOResponse aResponse, WOContext aContext) |
|
76 | { |
|
77 | 0 | WOComponent c = aContext.component(); |
78 | 0 | numberformat = stringForProperty("numberformat", c ); |
79 | 0 | dateformat = stringForProperty("dateformat", c ); |
80 | 0 | formatter = (Format) valueForProperty("formatter", c ); |
81 | 0 | escapeHTML = booleanForProperty("escapeHTML", c ); |
82 | 0 | value = valueForProperty("value", c ); |
83 | 0 | valueWhenEmpty = valueForProperty("valueWhenEmpty", c ); |
84 | ||
85 | 0 | Object result = value; |
86 | 0 | if ( result != null ) |
87 | { |
|
88 | 0 | if ( formatter != null ) |
89 | { |
|
90 | try |
|
91 | { |
|
92 | 0 | result = formatter.format( result ); |
93 | } |
|
94 | 0 | catch ( IllegalArgumentException exc ) |
95 | { |
|
96 | 0 | } |
97 | } |
|
98 | 0 | if ( dateformat != null ) |
99 | { |
|
100 | try |
|
101 | { |
|
102 | 0 | result = new NSTimestampFormatter( dateformat ).format( result ); |
103 | } |
|
104 | 0 | catch ( IllegalArgumentException exc ) |
105 | { |
|
106 | 0 | } |
107 | } |
|
108 | 0 | if ( numberformat != null ) |
109 | { |
|
110 | try |
|
111 | { |
|
112 | 0 | result = new NSNumberFormatter( numberformat ).format( result ); |
113 | } |
|
114 | 0 | catch ( IllegalArgumentException exc ) |
115 | { |
|
116 | 0 | } |
117 | } |
|
118 | } |
|
119 | 0 | if ( result == null ) |
120 | { |
|
121 | 0 | result = valueWhenEmpty; |
122 | 0 | if ( result == null ) |
123 | { |
|
124 | 0 | result = "nil"; |
125 | } |
|
126 | } |
|
127 | 0 | if ( escapeHTML ) |
128 | { |
|
129 | 0 | aResponse.appendContentHTMLString( result.toString() ); |
130 | 0 | } |
131 | else |
|
132 | { |
|
133 | 0 | aResponse.appendContentString( result.toString() ); |
134 | } |
|
135 | 0 | } |
136 | } |