From aedc34d55462a75e329bbf342251ff6504cd117e Mon Sep 17 00:00:00 2001 From: Benjamin Culkin Date: Sun, 19 May 2024 17:56:33 -0400 Subject: Initial import from SVN --- .../main/java/net/wotonomy/web/WOHyperlink.java | 249 +++++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOHyperlink.java (limited to 'projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOHyperlink.java') diff --git a/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOHyperlink.java b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOHyperlink.java new file mode 100644 index 0000000..d0f3ff7 --- /dev/null +++ b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOHyperlink.java @@ -0,0 +1,249 @@ +/* + 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: + * + * + * 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 string 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(""); + //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(""); + } + + 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); + } + +} -- cgit v1.2.3