1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/*
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.util.Iterator;
import java.util.Map;
import net.wotonomy.foundation.NSArray;
import net.wotonomy.foundation.NSDictionary;
/**
* WOHyperlink renders a dynamically generated hyperlink in the output. Bindings
* are:
* <ul>
* <li>string: a string to be included between the hyperlink tags
* (optional).</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>href: The URL that the hyperlink should point to.</li>
* <li>pageName: The name of the WOComponent that the hyperlink should point
* to.</li>
* <li>directActionName: The name of the direct action to call when the link is
* activated.</li>
* <li>actionClass: The name of the WODirectAction subclass where the direct
* action resides.</li>
* <li>anchorName: The name of the link, for anchor tags.</li>
* <li>action: A pointer to a method on the component that contains this
* element. If the link is activated, the method will be called.
* <li>ref: The name of the anchor to go to inside the resulting page.</li>
* </ul>
*
* The href, pageName and directActionName/actionClass and name properties are
* mutually exclusive and you should only use at most one of them
* simultaneously.
*
* @author ezamudio@nasoft.com
* @author $Author: cgruber $
* @version $Revision: 905 $
*/
public class WOHyperlink extends WODynamicElement {
protected String string;
protected String href;
protected String pageName;
protected String directActionName;
protected String actionClass;
protected String action;
protected boolean escapeHTML;
protected String anchorName;
protected String ref;
protected WOHyperlink() {
super();
}
public WOHyperlink(String aName, NSDictionary aMap, WOElement aRootElement) {
super(aName, aMap, aRootElement);
escapeHTML = true;
}
public void setString(String value) {
string = value;
}
public String string() {
return string;
}
public void setHref(String value) {
href = value;
}
public String href() {
return href;
}
public void setAnchorName(String value) {
anchorName = value;
}
public String anchorName() {
return anchorName;
}
public void setPageName(String value) {
pageName = value;
}
public String pageName() {
return pageName;
}
public void setDirectActionName(String value) {
directActionName = value;
}
public String directActionName() {
return directActionName;
}
public void setActionClass(String value) {
actionClass = value;
}
public String actionClass() {
return actionClass;
}
/** Sets the escapeHTML property. */
public void setEscapeHTML(boolean escape) {
escapeHTML = escape;
}
/**
* If true, inserts escape codes in to the <B>string</B> string so that HTML
* special characters (greater-than, less-than, etc.) appear correctly. If
* false, those characters will get interpreted by the browser. Defaults to
* true.
*/
public boolean escapeHTML() {
return escapeHTML;
}
public String actionURL(WOContext c) {
// Check if the href property is set
if (href() != null) {
return href();
} else if (pageName() != null || associations.objectForKey("action") != null) { // write this component's URL
StringBuffer retval = new StringBuffer(c.componentActionURL());
Map addFields = urlFields(c.component());
if (addFields.size() > 0) {
Iterator enumeration = addFields.keySet().iterator();
retval.append('?');
while (enumeration.hasNext()) {
String encoding = c.response() != null ? c.response().contentEncoding()
: c.request().contentEncoding();
String key = (String) enumeration.next();
try {
retval.append(java.net.URLEncoder.encode(key, encoding));
} catch (java.io.UnsupportedEncodingException ex) {
retval.append(key);
}
retval.append("=");
try {
retval.append(java.net.URLEncoder.encode(addFields.get(key).toString(), encoding));
} catch (java.io.UnsupportedEncodingException e) {
retval.append(addFields.get(key).toString());
}
if (enumeration.hasNext())
retval.append('&');
}
}
return retval.toString();
} else if (directActionName() != null) { // compose the direct action URL
String fullActionName = null;
if (actionClass() != null)
fullActionName = actionClass() + "/" + directActionName();
else
fullActionName = directActionName();
return c.directActionURLForActionNamed(fullActionName, urlFields(c.component()));
}
return null;
}
protected void pullValuesFromParent(WOComponent c) {
string = stringForProperty("string", c);
href = stringForProperty("href", c);
pageName = stringForProperty("pageName", c);
directActionName = stringForProperty("directActionName", c);
actionClass = stringForProperty("actionClass", c);
// action = stringForProperty("action", c);
escapeHTML = booleanForProperty("escapeHTML", c);
anchorName = stringForProperty("anchorName", c);
ref = stringForProperty("ref", c);
}
public void appendToResponse(WOResponse r, WOContext c) {
pullValuesFromParent(c.component());
r.appendContentString("<A");
boolean closeQuotes = false;
// Check if the href property is set
String _href = actionURL(c);
if (_href != null) {
closeQuotes = true;
r.appendContentString(" HREF=\"");
r.appendContentString(_href);
} else if (anchorName() != null) {
r.appendContentString(" NAME=\"");
r.appendContentString(anchorName());
closeQuotes = true;
}
if (ref != null) {
if (!closeQuotes) {
r.appendContentString(" HREF=\"#");
closeQuotes = true;
} else
r.appendContentString("#");
r.appendContentString(ref);
}
if (closeQuotes)
r.appendContentString("\"");
r.appendContentString(additionalHTMLProperties(c.component(), new NSArray(new Object[] { "name", "href",
"pageName", "action", "directActionName", "actionClass", "anchorName", "escapeHTML", "string" })));
r.appendContentString(">");
// Append the string if present
if (string() != null) {
if (escapeHTML())
r.appendContentHTMLString(string());
else
r.appendContentString(string());
}
// If there is a template, call appendToResponse on it
if (rootElement != null) {
rootElement.appendToResponse(r, c);
}
// Close the tag
r.appendContentString("</A>");
}
public WOActionResults invokeAction(WORequest r, WOContext c) {
System.out.println("invoke action with elementID=" + c.elementID() + " senderID=" + c.senderID());
// Check if this element is the target
if (c.senderID().equals(c.elementID())) {
if (pageName() != null) {
return WOApplication.application().pageWithName(pageName(), r);
} else {
WOAssociation ass = (WOAssociation) associations.objectForKey("action");
if (ass != null && ass.path != null) // ??
return (WOActionResults) c.component().performAction(ass.path);
}
}
return null;
}
public void takeValuesFromRequest(WORequest r, WOContext c) {
System.out.println("takeValuesFromRequest elementID=" + c.elementID() + " senderID=" + c.senderID());
super.takeValuesFromRequest(r, c);
}
}
|