KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tags > FormTag


1 // HTMLParser Library $Name: v1_5_20050313 $ - 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: 2004/07/18 21:31:20 $
10
// $Revision: 1.49 $
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     public static final String JavaDoc POST="POST";
39     public static final String JavaDoc GET="GET";
40     
41     /**
42      * This is the derived form location, based on action.
43      */

44     protected String JavaDoc mFormLocation;
45
46     /**
47      * The set of names handled by this tag.
48      */

49     private static final String JavaDoc[] mIds = new String JavaDoc[] {"FORM"};
50
51     /**
52      * The set of end tag names that indicate the end of this tag.
53      */

54     private static final String JavaDoc[] mEndTagEnders = new String JavaDoc[] {"HTML", "BODY", "TABLE"};
55
56     /**
57      * Create a new form tag.
58      */

59     public FormTag ()
60     {
61         mFormLocation = null;
62     }
63
64     /**
65      * Return the set of names handled by this tag.
66      * @return The names to be matched that create tags of this type.
67      */

68     public String JavaDoc[] getIds ()
69     {
70         return (mIds);
71     }
72
73     /**
74      * Return the set of tag names that cause this tag to finish.
75      * @return The names of following tags that stop further scanning.
76      */

77     public String JavaDoc[] getEnders ()
78     {
79         return (mIds);
80     }
81
82     /**
83      * Return the set of end tag names that cause this tag to finish.
84      * @return The names of following end tags that stop further scanning.
85      */

86     public String JavaDoc[] getEndTagEnders ()
87     {
88         return (mEndTagEnders);
89     }
90
91     /**
92      * Get the list of input fields.
93      * @return Input elements in the form.
94      */

95     public NodeList getFormInputs()
96     {
97         return (searchFor (InputTag.class, true));
98     }
99
100     /**
101      * Get the list of text areas.
102      * @return Textarea elements in the form.
103      */

104     public NodeList getFormTextareas()
105     {
106         return (searchFor (TextareaTag.class, true));
107     }
108
109     /**
110      * Get the value of the action attribute.
111      * @return The submit url of the form.
112      */

113     public String JavaDoc getFormLocation()
114     {
115         if (null == mFormLocation)
116             // ... is it true that without an ACTION the default is to send it back to the same page?
117
mFormLocation = extractFormLocn ();
118
119         return (mFormLocation);
120     }
121
122     /**
123      * Set the form location. Modification of this element will cause the HTML rendering
124      * to change as well (in a call to toHTML()).
125      * @param url The new FORM location
126      */

127     public void setFormLocation(String JavaDoc url)
128     {
129         mFormLocation = url;
130         setAttribute ("ACTION", url);
131     }
132
133     /**
134      * Returns the method of the form, GET or POST.
135      * @return String The method of the form (GET if nothing is specified).
136      */

137     public String JavaDoc getFormMethod()
138     {
139         String JavaDoc ret;
140         
141         ret = getAttribute("METHOD");
142         if (null == ret)
143             ret = GET;
144
145         return (ret);
146     }
147
148     /**
149      * Get the input tag in the form corresponding to the given name
150      * @param name The name of the input tag to be retrieved
151      * @return Tag The input tag corresponding to the name provided
152      */

153     public InputTag getInputTag (String JavaDoc name)
154     {
155         InputTag inputTag;
156         boolean found;
157         String JavaDoc inputTagName;
158         
159         inputTag = null;
160         found = false;
161         for (SimpleNodeIterator e = getFormInputs().elements();e.hasMoreNodes() && !found;)
162         {
163             inputTag = (InputTag)e.nextNode();
164             inputTagName = inputTag.getAttribute("NAME");
165             if (inputTagName!=null && inputTagName.equalsIgnoreCase(name))
166                 found=true;
167         }
168         if (found)
169             return (inputTag);
170         else
171             return (null);
172     }
173
174     /**
175      * Get the value of the name attribute.
176      * @return String The name of the form
177      */

178     public String JavaDoc getFormName()
179     {
180         return (getAttribute("NAME"));
181     }
182
183     /**
184      * Find the textarea tag matching the given name
185      * @param name Name of the textarea tag to be found within the form
186      */

187     public TextareaTag getTextAreaTag(String JavaDoc name)
188     {
189         TextareaTag textareaTag=null;
190         boolean found = false;
191         for (SimpleNodeIterator e=getFormTextareas ().elements();e.hasMoreNodes() && !found;)
192         {
193             textareaTag = (TextareaTag)e.nextNode();
194             String JavaDoc textAreaName = textareaTag.getAttribute("NAME");
195             if (textAreaName!=null && textAreaName.equals(name))
196                 found = true;
197         }
198         if (found)
199             return (textareaTag);
200         else
201             return (null);
202     }
203
204     /**
205      * @return A textual representation of the form tag.
206      */

207     public String JavaDoc toString()
208     {
209         return "FORM TAG : Form at "+getFormLocation()+"; begins at : "+getStartPosition ()+"; ends at : "+getEndPosition ();
210     }
211     
212     /**
213      * Extract the location of the image, given the tag, and the url
214      * of the html page in which this tag exists.
215      */

216     public String JavaDoc extractFormLocn ()
217     {
218         String JavaDoc ret;
219         
220         ret = getAttribute("ACTION");
221         if (null == ret)
222             ret = "";
223         else if (null != getPage ())
224             ret = getPage ().getAbsoluteURL (ret);
225         
226         return (ret);
227     }
228 }
229
Popular Tags