home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / HOTJAVA_ / HOTJAVA / CLASSSRC / BROWSER / FORMTA~1.JAV < prev    next >
Encoding:
Text File  |  1995-08-11  |  5.0 KB  |  204 lines

  1. /*
  2.  * @(#)FormTagRef.java    1.15 95/04/07 Jonathan Payne
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package browser;
  21.  
  22. import java.util.Hashtable;
  23. import awt.RadioGroup;
  24. import java.util.Vector;
  25. import browser.forms.FormItem;
  26. import net.www.html.Tag;
  27. import net.www.html.TagRef;
  28. import net.www.html.URL;
  29. import browser.forms.submit;
  30.  
  31. /**
  32.  * An instance of class FormTagRef is created for each occurrence of
  33.  * a <form> tag in an html document.  It serves as the definition of
  34.  * an html FORM object as it appears in an html document.
  35.  * @version 1.15, 07 Apr 1995
  36.  * @author Jonathan Payne
  37.  */
  38.  
  39. public class FormTagRef extends WRTagRef {
  40.     /**
  41.      * The WRWindow this tag ref appears in.  This is needed to
  42.      * perform the PushURL that is done when the form is submitted.
  43.      */
  44.     WRWindow    win;
  45.  
  46.     /** The vector of <input> items that appear in this form. */
  47.     Vector    inputItems;
  48.  
  49.     /** The url of the document this form appears in. */
  50.     URL        url;
  51.  
  52.     /**
  53.      * The different radio groups (by name) that appear in this hash
  54.      * table.
  55.      */
  56.     Hashtable    radioGroups;
  57.  
  58.     /**
  59.      * The current textarea or select tag we're processing.
  60.      */
  61.     TagRef    currentInput;
  62.  
  63.     public FormTagRef(Tag t, int pos, boolean isEnd) {
  64.     super(t, pos, isEnd);
  65.     }
  66.  
  67.     public void apply(WRFormatter f) {
  68.     win = (WRWindow) f.win;
  69.     url = win.document().url();
  70.     if (isEnd) {
  71.         f.popForm();
  72.     } else {
  73.         f.pushForm(this);
  74.     }
  75.     }
  76.  
  77.     /**
  78.      * Adds an input item to this form.  This is called by input
  79.      * items which are created as a result of <input> tags that
  80.      * appear in html documents.
  81.      */
  82.     public void addInputItem(FormItem item) {
  83.     if (inputItems == null) {
  84.         inputItems = new Vector();
  85.     }
  86.     inputItems.addElement(item);
  87.     }
  88.  
  89.     /**
  90.      * Returns the radio group associated with the specified name.
  91.      * If there is no RadioGroup by that name, one is created.
  92.      */
  93.     public RadioGroup getRadioGroup(String name) {
  94.     if (radioGroups == null) {
  95.         radioGroups = new Hashtable();
  96.     }
  97.  
  98.     RadioGroup  group = (RadioGroup) radioGroups.get(name);
  99.  
  100.     if (group == null) {
  101.         group = new RadioGroup();
  102.         radioGroups.put(name, group);
  103.     }
  104.  
  105.     return group;
  106.     }
  107.  
  108.     /**
  109.      * Set the current input item tag.
  110.      * @exception Exception if there's already a select tag
  111.      */
  112.     public void setInputItem(TagRef r) {
  113.     if (currentInput != null && r != null) {
  114.         throw new Exception("nested " + r.tag + " tags not allowed");
  115.     }
  116.     currentInput = r;
  117.     }
  118.  
  119.     /**
  120.      * Get the current select tag or return null if we're currently
  121.      * not processing one.
  122.      */
  123.     public TagRef getInputItem() {
  124.     return currentInput;
  125.     }
  126.  
  127.     /**
  128.      * Submits this form.  It walks through each input item asking
  129.      * it for its FormString.  Some form items may choose not to
  130.      * return a value.
  131.      */
  132.     public void submit() {
  133.     int cnt = inputItems.size();
  134.     int i = 0;
  135.     String    query;
  136.     URL    submitUrl;
  137.     String    action = getAttribute("action");
  138.     boolean posting;
  139.  
  140.     try {
  141.         posting = getAttribute("method").toLowerCase().equals("post");
  142.     } catch (NullPointerException e) {
  143.         posting = false;
  144.     }
  145.  
  146.     if (action != null) {
  147.         submitUrl = new URL(url, action);
  148.     } else {
  149.         submitUrl = url;
  150.     }
  151.  
  152.     query = "";
  153.  
  154.     try {
  155.         boolean doneOne = false;
  156.  
  157.         while (--cnt >= 0) {
  158.         FormItem    fi = (FormItem) inputItems.elementAt(i++);
  159.  
  160.         /* items without names don't get reported */
  161.         if (fi.getName() == null) {
  162.             continue;
  163.         }
  164.         String    thisResult = fi.getFormString();
  165.  
  166.         if (thisResult != null) {
  167.             if (doneOne) {
  168.             query += "&";
  169.             }
  170.             query += thisResult;
  171.             doneOne = true;
  172.         }
  173.         }
  174.         if (posting) {
  175.         submitUrl = new URL(submitUrl, query, url);
  176.         } else {
  177.         /* REMIND: This belongs in class URL. */
  178.         String    currentUrl = submitUrl.toExternalForm();
  179.         int    questionIndex;
  180.  
  181.         if ((questionIndex = currentUrl.lastIndexOf('?')) != -1) {
  182.             currentUrl = currentUrl.substring(0, questionIndex);
  183.         }
  184.         submitUrl = new URL(null, currentUrl + "?" + query);
  185.         }
  186.         win.pushURL(submitUrl);
  187.     } catch (Exception e) {
  188.         e.printStackTrace();
  189.     }
  190.     }
  191.  
  192.     /** Resets all the form input items to their default value. */
  193.     public void reset() {
  194.     int cnt = inputItems.size();
  195.     int i = 0;
  196.  
  197.     while (--cnt >= 0) {
  198.         FormItem    fi = (FormItem) inputItems.elementAt(i++);
  199.  
  200.         fi.reset();
  201.     }
  202.     }
  203. }
  204.