KickJava   Java API By Example, From Geeks To Geeks.

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


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/AppletTag.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/02 00:49:28 $
10
// $Revision: 1.41 $
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 java.util.Enumeration JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import org.htmlparser.Attribute;
34 import org.htmlparser.Node;
35 import org.htmlparser.Tag;
36 import org.htmlparser.Text;
37 import org.htmlparser.nodes.TagNode;
38 import org.htmlparser.util.NodeList;
39 import org.htmlparser.util.SimpleNodeIterator;
40
41 /**
42  * AppletTag represents an <Applet> tag.
43  * It extends a basic tag by providing accessors to the class, codebase,
44  * archive and parameters.
45  */

46 public class AppletTag
47     extends
48         CompositeTag
49 {
50     /**
51      * The set of names handled by this tag.
52      */

53     private static final String JavaDoc[] mIds = new String JavaDoc[] {"APPLET"};
54
55     /**
56      * The set of end tag names that indicate the end of this tag.
57      */

58     private static final String JavaDoc[] mEndTagEnders = new String JavaDoc[] {"BODY", "HTML"};
59
60     /**
61      * Create a new applet tag.
62      */

63     public AppletTag ()
64     {
65     }
66
67     /**
68      * Return the set of names handled by this tag.
69      * @return The names to be matched that create tags of this type.
70      */

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

80     public String JavaDoc[] getEndTagEnders ()
81     {
82         return (mEndTagEnders);
83     }
84
85     /**
86      * Extract the applet <code>PARAM</code> tags from the child list.
87      * @return The list of applet parameters (keys and values are String objects).
88      */

89     public Hashtable JavaDoc createAppletParamsTable ()
90     {
91         NodeList kids;
92         Node node;
93         Tag tag;
94         String JavaDoc paramName;
95         String JavaDoc paramValue;
96         Hashtable JavaDoc ret;
97
98         ret = new Hashtable JavaDoc ();
99         kids = getChildren ();
100         if (null != kids)
101             for (int i = 0; i < kids.size (); i++)
102             {
103                 node = children.elementAt(i);
104                 if (node instanceof Tag)
105                 {
106                     tag = (Tag)node;
107                     if (tag.getTagName().equals ("PARAM"))
108                     {
109                         paramName = tag.getAttribute ("NAME");
110                         if (null != paramName && 0 != paramName.length ())
111                         {
112                             paramValue = tag.getAttribute ("VALUE");
113                             ret.put (paramName,paramValue);
114                         }
115                     }
116                 }
117             }
118
119         return (ret);
120     }
121
122     /**
123      * Get the class name of the applet.
124      * @return The value of the <code>CODE</code> attribute.
125      */

126     public String JavaDoc getAppletClass ()
127     {
128         return (getAttribute ("CODE"));
129     }
130
131     /**
132      * Get the applet parameters.
133      * @return The list of parameter values (keys and values are String objects).
134      */

135     public Hashtable JavaDoc getAppletParams ()
136     {
137         return (createAppletParamsTable ());
138     }
139
140     /**
141      * Get the jar file of the applet.
142      * @return The value of the <code>ARCHIVE</code> attribute, or <code>null</code> if it wasn't specified.
143      */

144     public String JavaDoc getArchive()
145     {
146         return (getAttribute ("ARCHIVE"));
147     }
148
149     /**
150      * Get the code base of the applet.
151      * @return The value of the <code>CODEBASE</code> attribute, or <code>null</code> if it wasn't specified.
152      */

153     public String JavaDoc getCodeBase()
154     {
155         return (getAttribute ("CODEBASE"));
156     }
157
158     /**
159      * Get the <code>PARAM<code> tag with the given name.
160      * <em>NOTE: This was called (erroneously) getAttribute() in previous versions.</em>
161      * @param key The applet parameter name to get.
162      * @return The value of the parameter or <code>null</code> if there is no parameter of that name.
163      */

164     public String JavaDoc getParameter (String JavaDoc key)
165     {
166         return ((String JavaDoc)(getAppletParams ().get (key)));
167     }
168
169     /**
170      * Get an enumeration over the (String) parameter names.
171      * @return An enumeration of the <code>PARAM<code> tag <code>NAME<code> attributes.
172      */

173     public Enumeration JavaDoc getParameterNames ()
174     {
175         return (getAppletParams ().keys ());
176     }
177
178     /**
179      * Set the <code>CODE<code> attribute.
180      * @param newAppletClass The new applet class.
181      */

182     public void setAppletClass (String JavaDoc newAppletClass)
183     {
184         setAttribute ("CODE", newAppletClass);
185     }
186
187     /**
188      * Set the enclosed <code>PARM<code> children.
189      * @param newAppletParams The new parameters.
190      */

191     public void setAppletParams (Hashtable JavaDoc newAppletParams)
192     {
193         NodeList kids;
194         Node node;
195         Tag tag;
196         String JavaDoc paramName;
197         String JavaDoc paramValue;
198         Vector JavaDoc attributes;
199         Text string;
200
201         kids = getChildren ();
202         if (null == kids)
203             kids = new NodeList ();
204         else
205             // erase appletParams from kids
206
for (int i = 0; i < kids.size (); )
207             {
208                 node = kids.elementAt (i);
209                 if (node instanceof Tag)
210                     if (((Tag)node).getTagName ().equals ("PARAM"))
211                     {
212                         kids.remove (i);
213                         // remove whitespace too
214
if (i < kids.size ())
215                         {
216                             node = kids.elementAt (i);
217                             if (node instanceof Text)
218                             {
219                                 string = (Text)node;
220                                 if (0 == string.getText ().trim ().length ())
221                                     kids.remove (i);
222                             }
223                         }
224                     }
225                     else
226                         i++;
227                 else
228                     i++;
229             }
230
231         // add newAppletParams to kids
232
for (Enumeration JavaDoc e = newAppletParams.keys (); e.hasMoreElements (); )
233         {
234             attributes = new Vector JavaDoc (); // should the tag copy the attributes?
235
paramName = (String JavaDoc)e.nextElement ();
236             paramValue = (String JavaDoc)newAppletParams.get (paramName);
237             attributes.addElement (new Attribute ("PARAM", null));
238             attributes.addElement (new Attribute (" "));
239             attributes.addElement (new Attribute ("VALUE", paramValue, '"'));
240             attributes.addElement (new Attribute (" "));
241             attributes.addElement (new Attribute ("NAME", paramName, '"'));
242             tag = new TagNode (null, 0, 0, attributes);
243             kids.add (tag);
244         }
245
246         //set kids as new children
247
setChildren (kids);
248     }
249
250     /**
251      * Set the <code>ARCHIVE<code> attribute.
252      * @param newArchive The new archive file.
253      */

254     public void setArchive (String JavaDoc newArchive)
255     {
256         setAttribute ("ARCHIVE", newArchive);
257     }
258
259     /**
260      * Set the <code>CODEBASE<code> attribute.
261      * @param newCodeBase The new applet code base.
262      */

263     public void setCodeBase (String JavaDoc newCodeBase)
264     {
265         setAttribute ("CODEBASE", newCodeBase);
266     }
267
268     /**
269      * Output a string representing this applet tag.
270      * @return A string showing the contents of the applet tag.
271      */

272     public String JavaDoc toString ()
273     {
274         Hashtable JavaDoc parameters;
275         Enumeration JavaDoc params;
276         String JavaDoc paramName;
277         String JavaDoc paramValue;
278         boolean found;
279         Node node;
280         StringBuffer JavaDoc ret;
281
282         ret = new StringBuffer JavaDoc(500);
283         ret.append ("Applet Tag\n");
284         ret.append ("**********\n");
285         ret.append ("Class Name = ");
286         ret.append (getAppletClass ());
287         ret.append ("\n");
288         ret.append ("Archive = ");
289         ret.append (getArchive ());
290         ret.append ("\n");
291         ret.append ("Codebase = ");
292         ret.append (getCodeBase ());
293         ret.append ("\n");
294         parameters = getAppletParams ();
295         params = parameters.keys ();
296         if (null == params)
297             ret.append ("No Params found.\n");
298         else
299             for (int cnt = 0; params.hasMoreElements (); cnt++)
300             {
301                 paramName = (String JavaDoc)params.nextElement ();
302                 paramValue = (String JavaDoc)parameters.get (paramName);
303                 ret.append (cnt);
304                 ret.append (": Parameter name = ");
305                 ret.append (paramName);
306                 ret.append (", Parameter value = ");
307                 ret.append (paramValue);
308                 ret.append ("\n");
309             }
310         found = false;
311         for (SimpleNodeIterator e = children (); e.hasMoreNodes ();)
312         {
313             node = e.nextNode ();
314             if (node instanceof Tag)
315                 if (((Tag)node).getTagName ().equals ("PARAM"))
316                     continue;
317             if (!found)
318                 ret.append ("Miscellaneous items :\n");
319             else
320                 ret.append (" ");
321             found = true;
322             ret.append (node.toString ());
323         }
324         if (found)
325             ret.append ("\n");
326         ret.append ("End of Applet Tag\n");
327         ret.append ("*****************\n");
328
329         return (ret.toString ());
330     }
331 }
332
Popular Tags