/ org.htmlparser / src / org / htmlparser / tags / FormTag.java
FormTag.java
  1  // HTMLParser Library $Name: v1_6_20060319 $ - A java-based parser for HTML
  2  // http://sourceforge.org/projects/htmlparser
  3  // Copyright (C) 2004 Somik Raha
  4  //
  5  // Revision Control Information
  6  //
  7  // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tags/FormTag.java,v $
  8  // $Author: derrickoswald $
  9  // $Date: 2005/04/10 23:20:45 $
 10  // $Revision: 1.50 $
 11  //
 12  // This library is free software; you can redistribute it and/or
 13  // modify it under the terms of the GNU Lesser General Public
 14  // License as published by the Free Software Foundation; either
 15  // version 2.1 of the License, or (at your option) any later version.
 16  //
 17  // This library is distributed in the hope that it will be useful,
 18  // but WITHOUT ANY WARRANTY; without even the implied warranty of
 19  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 20  // Lesser General Public License for more details.
 21  //
 22  // You should have received a copy of the GNU Lesser General Public
 23  // License along with this library; if not, write to the Free Software
 24  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 25  //
 26  
 27  package org.htmlparser.tags;
 28  
 29  import org.htmlparser.util.NodeList;
 30  import org.htmlparser.util.SimpleNodeIterator;
 31  
 32  /**
 33   * Represents a FORM tag.
 34   * @author ili
 35   */
 36  public class FormTag extends CompositeTag
 37  {
 38      /**
 39       * The {@value} method.
 40       * @see #getFormMethod
 41       */
 42      public static final String POST = "POST";
 43  
 44      /**
 45       * The {@value} method.
 46       * @see #getFormMethod
 47       */
 48      public static final String GET = "GET";
 49      
 50      /**
 51       * This is the derived form location, based on action.
 52       */
 53      protected String mFormLocation;
 54  
 55      /**
 56       * The set of names handled by this tag.
 57       */
 58      private static final String[] mIds = new String[] {"FORM"};
 59  
 60      /**
 61       * The set of end tag names that indicate the end of this tag.
 62       */
 63      private static final String[] mEndTagEnders = new String[] {"HTML", "BODY", "TABLE"};
 64  
 65      /**
 66       * Create a new form tag.
 67       */
 68      public FormTag ()
 69      {
 70          mFormLocation = null;
 71      }
 72  
 73      /**
 74       * Return the set of names handled by this tag.
 75       * @return The names to be matched that create tags of this type.
 76       */
 77      public String[] getIds ()
 78      {
 79          return (mIds);
 80      }
 81  
 82      /**
 83       * Return the set of tag names that cause this tag to finish.
 84       * @return The names of following tags that stop further scanning.
 85       */
 86      public String[] getEnders ()
 87      {
 88          return (mIds);
 89      }
 90  
 91      /**
 92       * Return the set of end tag names that cause this tag to finish.
 93       * @return The names of following end tags that stop further scanning.
 94       */
 95      public String[] getEndTagEnders ()
 96      {
 97          return (mEndTagEnders);
 98      }
 99  
100      /**
101       * Get the list of input fields.
102       * @return Input elements in the form.
103       */
104      public NodeList getFormInputs()
105      {
106          return (searchFor (InputTag.class, true));
107      }
108  
109      /**
110       * Get the list of text areas.
111       * @return Textarea elements in the form.
112       */
113      public NodeList getFormTextareas()
114      {
115          return (searchFor (TextareaTag.class, true));
116      }
117  
118      /**
119       * Get the value of the action attribute.
120       * @return The submit url of the form.
121       */
122      public String getFormLocation()
123      {
124          if (null == mFormLocation)
125              // ... is it true that without an ACTION the default is to send it back to the same page?
126              mFormLocation = extractFormLocn ();
127  
128          return (mFormLocation);
129      }
130  
131      /**
132       * Set the form location. Modification of this element will cause the HTML rendering
133       * to change as well (in a call to toHTML()).
134       * @param url The new FORM location
135       */
136      public void setFormLocation(String url)
137      {
138          mFormLocation = url;
139          setAttribute ("ACTION", url);
140      }
141  
142      /**
143       * Returns the method of the form, GET or POST.
144       * @return String The method of the form (GET if nothing is specified).
145       */
146      public String getFormMethod()
147      {
148          String ret;
149          
150          ret = getAttribute("METHOD");
151          if (null == ret)
152              ret = GET;
153  
154          return (ret);
155      }
156  
157      /**
158       * Get the input tag in the form corresponding to the given name
159       * @param name The name of the input tag to be retrieved
160       * @return Tag The input tag corresponding to the name provided
161       */
162      public InputTag getInputTag (String name)
163      {
164          InputTag inputTag;
165          boolean found;
166          String inputTagName;
167          
168          inputTag = null;
169          found = false;
170          for (SimpleNodeIterator e = getFormInputs().elements();e.hasMoreNodes() && !found;)
171          {
172              inputTag = (InputTag)e.nextNode();
173              inputTagName = inputTag.getAttribute("NAME");
174              if (inputTagName!=null && inputTagName.equalsIgnoreCase(name))
175                  found=true;
176          }
177          if (found)
178              return (inputTag);
179          else
180              return (null);
181      }
182  
183      /**
184       * Get the value of the name attribute.
185       * @return String The name of the form
186       */
187      public String getFormName()
188      {
189          return (getAttribute("NAME"));
190      }
191  
192      /**
193       * Find the textarea tag matching the given name
194       * @param name Name of the textarea tag to be found within the form.
195       * @return The <code>TEXTAREA</code> tag with the matching name.
196       */
197      public TextareaTag getTextAreaTag(String name)
198      {
199          TextareaTag textareaTag=null;
200          boolean found = false;
201          for (SimpleNodeIterator e=getFormTextareas ().elements();e.hasMoreNodes() && !found;)
202          {
203              textareaTag = (TextareaTag)e.nextNode();
204              String textAreaName = textareaTag.getAttribute("NAME");
205              if (textAreaName!=null && textAreaName.equals(name))
206                  found = true;
207          }
208          if (found)
209              return (textareaTag);
210          else
211              return (null);
212      }
213  
214      /**
215       * Return a string representation of the contents of this <code>FORM</code> tag suitable for debugging.
216       * @return A textual representation of the form tag.
217       */
218      public String toString()
219      {
220          return "FORM TAG : Form at "+getFormLocation()+"; begins at : "+getStartPosition ()+"; ends at : "+getEndPosition ();
221      }
222      
223      /**
224       * Extract the <code>ACTION</code> attribute as an absolute URL.
225       * @return The URL the form is to be submitted to.
226       */
227      public String extractFormLocn ()
228      {
229          String ret;
230          
231          ret = getAttribute("ACTION");
232          if (null == ret)
233              ret = "";
234          else if (null != getPage ())
235              ret = getPage ().getAbsoluteURL (ret);
236          
237          return (ret);
238      }
239  }