ObjectTag.java
1 // HTMLParser Library $Name: v1_6_20060319 $ - A java-based parser for HTML 2 // http://sourceforge.org/projects/htmlparser 3 // Copyright (C) 2004 Enrico Triolo 4 // 5 // Revision Control Information 6 // 7 // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tags/ObjectTag.java,v $ 8 // $Author: derrickoswald $ 9 // $Date: 2004/07/02 00:49:29 $ 10 // $Revision: 1.2 $ 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; 30 import java.util.Hashtable; 31 import java.util.Vector; 32 33 import org.htmlparser.Attribute; 34 import org.htmlparser.Node; 35 import org.htmlparser.Tag; 36 import org.htmlparser.nodes.TextNode; 37 import org.htmlparser.nodes.TagNode; 38 import org.htmlparser.util.NodeList; 39 import org.htmlparser.util.SimpleNodeIterator; 40 41 /** 42 * ObjectTag represents an <Object> tag. 43 * It extends a basic tag by providing accessors to the 44 * type, codetype, codebase, classid, data, height, width, standby attributes and parameters. 45 */ 46 public class ObjectTag extends CompositeTag 47 { 48 /** 49 * The set of names handled by this tag. 50 */ 51 private static final String[] mIds = new String[] {"OBJECT"}; 52 53 /** 54 * The set of end tag names that indicate the end of this tag. 55 */ 56 private static final String[] mEndTagEnders = new String[] {"BODY", "HTML"}; 57 58 /** 59 * Create a new object tag. 60 */ 61 public ObjectTag () 62 { 63 } 64 65 /** 66 * Return the set of names handled by this tag. 67 * @return The names to be matched that create tags of this type. 68 */ 69 public String[] getIds () 70 { 71 return (mIds); 72 } 73 74 /** 75 * Return the set of end tag names that cause this tag to finish. 76 * @return The names of following end tags that stop further scanning. 77 */ 78 public String[] getEndTagEnders () 79 { 80 return (mEndTagEnders); 81 } 82 83 /** 84 * Extract the object <code>PARAM</code> tags from the child list. 85 * @return The list of object parameters (keys and values are String objects). 86 */ 87 public Hashtable createObjectParamsTable () 88 { 89 NodeList kids; 90 Node node; 91 Tag tag; 92 String paramName; 93 String paramValue; 94 Hashtable ret; 95 96 ret = new Hashtable (); 97 kids = getChildren (); 98 if (null != kids) 99 for (int i = 0; i < kids.size (); i++) 100 { 101 node = children.elementAt(i); 102 if (node instanceof Tag) 103 { 104 tag = (Tag)node; 105 if (tag.getTagName().equals ("PARAM")) 106 { 107 paramName = tag.getAttribute ("NAME"); 108 if (null != paramName && 0 != paramName.length ()) 109 { 110 paramValue = tag.getAttribute ("VALUE"); 111 ret.put (paramName.toUpperCase(),paramValue); 112 } 113 } 114 } 115 } 116 117 return (ret); 118 } 119 120 /** 121 * Get the classid of the object. 122 * @return The value of the <code>CLASSID</code> attribute. 123 */ 124 public String getObjectClassId () 125 { 126 return getAttribute ("CLASSID"); 127 } 128 129 /** 130 * Get the codebase of the object. 131 * @return The value of the <code>CODEBASE</code> attribute. 132 */ 133 public String getObjectCodeBase () 134 { 135 return getAttribute ("CODEBASE"); 136 } 137 138 /** 139 * Get the codetype of the object. 140 * @return The value of the <code>CODETYPE</code> attribute. 141 */ 142 public String getObjectCodeType () 143 { 144 return getAttribute ("CODETYPE"); 145 } 146 147 /** 148 * Get the data of the object. 149 * @return The value of the <code>DATA</code> attribute. 150 */ 151 public String getObjectData () 152 { 153 return getAttribute ("DATA"); 154 } 155 156 /** 157 * Get the height of the object. 158 * @return The value of the <code>HEIGHT</code> attribute. 159 */ 160 public String getObjectHeight () 161 { 162 return getAttribute ("HEIGHT"); 163 } 164 165 /** 166 * Get the standby of the object. 167 * @return The value of the <code>STANDBY</code> attribute. 168 */ 169 public String getObjectStandby () 170 { 171 return getAttribute ("STANDBY"); 172 } 173 174 /** 175 * Get the type of the object. 176 * @return The value of the <code>TYPE</code> attribute. 177 */ 178 public String getObjectType () 179 { 180 return getAttribute ("TYPE"); 181 } 182 183 /** 184 * Get the width of the object. 185 * @return The value of the <code>WIDTH</code> attribute. 186 */ 187 public String getObjectWidth () 188 { 189 return getAttribute ("WIDTH"); 190 } 191 192 /** 193 * Get the object parameters. 194 * @return The list of parameter values (keys and values are String objects). 195 */ 196 public Hashtable getObjectParams () 197 { 198 return createObjectParamsTable (); 199 } 200 201 /** 202 * Get the <code>PARAM<code> tag with the given name. 203 * @param key The object parameter name to get. 204 * @return The value of the parameter or <code>null</code> if there is no parameter of that name. 205 */ 206 public String getParameter (String key) 207 { 208 return ((String)(getObjectParams ().get (key.toUpperCase ()))); 209 } 210 211 /** 212 * Get an enumeration over the (String) parameter names. 213 * @return An enumeration of the <code>PARAM<code> tag <code>NAME<code> attributes. 214 */ 215 public Enumeration getParameterNames () 216 { 217 return getObjectParams ().keys (); 218 } 219 220 /** 221 * Set the <code>CLASSID<code> attribute. 222 * @param newClassId The new classid. 223 */ 224 public void setObjectClassId (String newClassId) 225 { 226 setAttribute ("CLASSID", newClassId); 227 } 228 229 /** 230 * Set the <code>CODEBASE<code> attribute. 231 * @param newCodeBase The new codebase. 232 */ 233 public void setObjectCodeBase (String newCodeBase) 234 { 235 setAttribute ("CODEBASE", newCodeBase); 236 } 237 238 /** 239 * Set the <code>CODETYPE<code> attribute. 240 * @param newCodeType The new codetype. 241 */ 242 public void setObjectCodeType (String newCodeType) 243 { 244 setAttribute ("CODETYPE", newCodeType); 245 } 246 247 /** 248 * Set the <code>DATA<code> attribute. 249 * @param newData The new data. 250 */ 251 public void setObjectData (String newData) 252 { 253 setAttribute ("DATA", newData); 254 } 255 256 /** 257 * Set the <code>HEIGHT<code> attribute. 258 * @param newHeight The new height. 259 */ 260 public void setObjectHeight (String newHeight) 261 { 262 setAttribute ("HEIGHT", newHeight); 263 } 264 265 /** 266 * Set the <code>STANDBY<code> attribute. 267 * @param newStandby The new standby. 268 */ 269 public void setObjectStandby (String newStandby) 270 { 271 setAttribute ("STANDBY", newStandby); 272 } 273 274 /** 275 * Set the <code>TYPE<code> attribute. 276 * @param newType The new type. 277 */ 278 public void setObjectType (String newType) 279 { 280 setAttribute ("TYPE", newType); 281 } 282 283 /** 284 * Set the <code>WIDTH<code> attribute. 285 * @param newWidth The new width. 286 */ 287 public void setObjectWidth (String newWidth) 288 { 289 setAttribute ("WIDTH", newWidth); 290 } 291 292 /** 293 * Set the enclosed <code>PARAM<code> children. 294 * @param newObjectParams The new parameters. 295 */ 296 public void setObjectParams (Hashtable newObjectParams) 297 { 298 NodeList kids; 299 Node node; 300 Tag tag; 301 String paramName; 302 String paramValue; 303 Vector attributes; 304 TextNode string; 305 306 kids = getChildren (); 307 if (null == kids) 308 kids = new NodeList (); 309 else 310 // erase objectParams from kids 311 for (int i = 0; i < kids.size (); ) 312 { 313 node = kids.elementAt (i); 314 if (node instanceof Tag) 315 if (((Tag)node).getTagName ().equals ("PARAM")) 316 { 317 kids.remove (i); 318 // remove whitespace too 319 if (i < kids.size ()) 320 { 321 node = kids.elementAt (i); 322 if (node instanceof TextNode) 323 { 324 string = (TextNode)node; 325 if (0 == string.getText ().trim ().length ()) 326 kids.remove (i); 327 } 328 } 329 } 330 else 331 i++; 332 else 333 i++; 334 } 335 336 // add newObjectParams to kids 337 for (Enumeration e = newObjectParams.keys (); e.hasMoreElements (); ) 338 { 339 attributes = new Vector (); // should the tag copy the attributes? 340 paramName = (String)e.nextElement (); 341 paramValue = (String)newObjectParams.get (paramName); 342 attributes.addElement (new Attribute ("PARAM", null)); 343 attributes.addElement (new Attribute (" ")); 344 attributes.addElement (new Attribute ("VALUE", paramValue, '"')); 345 attributes.addElement (new Attribute (" ")); 346 attributes.addElement (new Attribute ("NAME", paramName.toUpperCase (), '"')); 347 tag = new TagNode (null, 0, 0, attributes); 348 kids.add (tag); 349 } 350 351 //set kids as new children 352 setChildren (kids); 353 } 354 355 /** 356 * Output a string representing this object tag. 357 * @return A string showing the contents of the object tag. 358 */ 359 public String toString () 360 { 361 Hashtable parameters; 362 Enumeration params; 363 String paramName; 364 String paramValue; 365 boolean found; 366 Node node; 367 StringBuffer ret; 368 369 ret = new StringBuffer (500); 370 ret.append ("Object Tag\n"); 371 ret.append ("**********\n"); 372 ret.append ("ClassId = "); 373 ret.append (getObjectClassId ()); 374 ret.append ("\n"); 375 ret.append ("CodeBase = "); 376 ret.append (getObjectCodeBase ()); 377 ret.append ("\n"); 378 ret.append ("CodeType = "); 379 ret.append (getObjectCodeType ()); 380 ret.append ("\n"); 381 ret.append ("Data = "); 382 ret.append (getObjectData ()); 383 ret.append ("\n"); 384 ret.append ("Height = "); 385 ret.append (getObjectHeight ()); 386 ret.append ("\n"); 387 ret.append ("Standby = "); 388 ret.append (getObjectStandby ()); 389 ret.append ("\n"); 390 ret.append ("Type = "); 391 ret.append (getObjectType ()); 392 ret.append ("\n"); 393 ret.append ("Width = "); 394 ret.append (getObjectWidth ()); 395 ret.append ("\n"); 396 parameters = getObjectParams (); 397 params = parameters.keys (); 398 if (null == params) 399 ret.append ("No Params found.\n"); 400 else 401 for (int cnt = 0; params.hasMoreElements (); cnt++) 402 { 403 paramName = (String)params.nextElement (); 404 paramValue = (String)parameters.get (paramName); 405 ret.append (cnt); 406 ret.append (": Parameter name = "); 407 ret.append (paramName); 408 ret.append (", Parameter value = "); 409 ret.append (paramValue); 410 ret.append ("\n"); 411 } 412 found = false; 413 for (SimpleNodeIterator e = children (); e.hasMoreNodes ();) 414 { 415 node = e.nextNode (); 416 if (node instanceof Tag) 417 if (((Tag)node).getTagName ().equals ("PARAM")) 418 continue; 419 if (!found) 420 ret.append ("Miscellaneous items :\n"); 421 else 422 ret.append (" "); 423 found = true; 424 ret.append (node.toString ()); 425 } 426 if (found) 427 ret.append ("\n"); 428 ret.append ("End of Object Tag\n"); 429 ret.append ("*****************\n"); 430 431 return (ret.toString ()); 432 } 433 }