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