1 27 package org.htmlparser; 28 29 import java.io.Serializable ; 30 import java.util.Hashtable ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 import java.util.Set ; 34 import java.util.Vector ; 35 36 import org.htmlparser.Attribute; 37 import org.htmlparser.NodeFactory; 38 import org.htmlparser.Remark; 39 import org.htmlparser.Tag; 40 import org.htmlparser.Text; 41 import org.htmlparser.lexer.Page; 42 import org.htmlparser.nodes.AbstractNode; 43 import org.htmlparser.nodes.TextNode; 44 import org.htmlparser.nodes.RemarkNode; 45 import org.htmlparser.nodes.TagNode; 46 import org.htmlparser.tags.AppletTag; 47 import org.htmlparser.tags.BaseHrefTag; 48 import org.htmlparser.tags.BodyTag; 49 import org.htmlparser.tags.Bullet; 50 import org.htmlparser.tags.BulletList; 51 import org.htmlparser.tags.Div; 52 import org.htmlparser.tags.DoctypeTag; 53 import org.htmlparser.tags.FormTag; 54 import org.htmlparser.tags.FrameSetTag; 55 import org.htmlparser.tags.FrameTag; 56 import org.htmlparser.tags.HeadTag; 57 import org.htmlparser.tags.Html; 58 import org.htmlparser.tags.ImageTag; 59 import org.htmlparser.tags.InputTag; 60 import org.htmlparser.tags.JspTag; 61 import org.htmlparser.tags.LabelTag; 62 import org.htmlparser.tags.LinkTag; 63 import org.htmlparser.tags.MetaTag; 64 import org.htmlparser.tags.ObjectTag; 65 import org.htmlparser.tags.OptionTag; 66 import org.htmlparser.tags.ScriptTag; 67 import org.htmlparser.tags.SelectTag; 68 import org.htmlparser.tags.Span; 69 import org.htmlparser.tags.StyleTag; 70 import org.htmlparser.tags.TableColumn; 71 import org.htmlparser.tags.TableHeader; 72 import org.htmlparser.tags.TableRow; 73 import org.htmlparser.tags.TableTag; 74 import org.htmlparser.tags.TextareaTag; 75 import org.htmlparser.tags.TitleTag; 76 import org.htmlparser.util.ParserException; 77 78 93 public class PrototypicalNodeFactory 94 implements 95 Serializable , 96 NodeFactory 97 { 98 101 protected Text mText; 102 103 106 protected Remark mRemark; 107 108 111 protected Tag mTag; 112 113 117 protected Map mBlastocyst; 118 119 122 public PrototypicalNodeFactory () 123 { 124 this (false); 125 } 126 127 132 public PrototypicalNodeFactory (boolean empty) 133 { 134 clear (); 135 mText = new TextNode (null, 0, 0); 136 mRemark = new RemarkNode (null, 0, 0); 137 mTag = new TagNode (null, 0, 0, null); 138 if (!empty) 139 registerTags (); 140 } 141 142 146 public PrototypicalNodeFactory (Tag tag) 147 { 148 this (true); 149 registerTag (tag); 150 } 151 152 156 public PrototypicalNodeFactory (Tag[] tags) 157 { 158 this (true); 159 for (int i = 0; i < tags.length; i++) 160 registerTag (tags[i]); 161 } 162 163 170 public Tag put (String id, Tag tag) 171 { 172 return ((Tag)mBlastocyst.put (id, tag)); 173 } 174 175 180 public Tag get (String id) 181 { 182 return ((Tag)mBlastocyst.get (id)); 183 } 184 185 190 public Tag remove (String id) 191 { 192 return ((Tag)mBlastocyst.remove (id)); 193 } 194 195 198 public void clear () 199 { 200 mBlastocyst = new Hashtable (); 201 } 202 203 207 public Set getTagNames () 208 { 209 return (mBlastocyst.keySet ()); 210 } 211 212 218 public void registerTag (Tag tag) 219 { 220 String ids[]; 221 222 ids = tag.getIds (); 223 for (int i = 0; i < ids.length; i++) 224 put (ids[i], tag); 225 } 226 227 233 public void unregisterTag (Tag tag) 234 { 235 String ids[]; 236 237 ids = tag.getIds (); 238 for (int i = 0; i < ids.length; i++) 239 remove (ids[i]); 240 } 241 242 248 public PrototypicalNodeFactory registerTags () 249 { 250 registerTag (new AppletTag ()); 251 registerTag (new BaseHrefTag ()); 252 registerTag (new Bullet ()); 253 registerTag (new BulletList ()); 254 registerTag (new DoctypeTag ()); 255 registerTag (new FormTag ()); 256 registerTag (new FrameSetTag ()); 257 registerTag (new FrameTag ()); 258 registerTag (new ImageTag ()); 259 registerTag (new InputTag ()); 260 registerTag (new JspTag ()); 261 registerTag (new LabelTag ()); 262 registerTag (new LinkTag ()); 263 registerTag (new MetaTag ()); 264 registerTag (new ObjectTag ()); 265 registerTag (new OptionTag ()); 266 registerTag (new ScriptTag ()); 267 registerTag (new SelectTag ()); 268 registerTag (new StyleTag ()); 269 registerTag (new TableColumn ()); 270 registerTag (new TableHeader ()); 271 registerTag (new TableRow ()); 272 registerTag (new TableTag ()); 273 registerTag (new TextareaTag ()); 274 registerTag (new TitleTag ()); 275 registerTag (new Div ()); 276 registerTag (new Span ()); 277 registerTag (new BodyTag ()); 278 registerTag (new HeadTag ()); 279 registerTag (new Html ()); 280 281 return (this); 282 } 283 284 288 public Text getTextPrototype () 289 { 290 return (mText); 291 } 292 293 297 public void setTextPrototype (Text text) 298 { 299 if (null == text) 300 throw new IllegalArgumentException ("text prototype node cannot be null"); 301 else 302 mText = text; 303 } 304 305 309 public Remark getRemarkPrototype () 310 { 311 return (mRemark); 312 } 313 314 318 public void setRemarkPrototype (Remark remark) 319 { 320 if (null == remark) 321 throw new IllegalArgumentException ("remark prototype node cannot be null"); 322 else 323 mRemark = remark; 324 } 325 326 332 public Tag getTagPrototype () 333 { 334 return (mTag); 335 } 336 337 343 public void setTagPrototype (Tag tag) 344 { 345 if (null == tag) 346 throw new IllegalArgumentException ("tag prototype node cannot be null"); 347 else 348 mTag = tag; 349 } 350 351 355 361 public Text createStringNode (Page page, int start, int end) 362 { 363 Text ret; 364 365 try 366 { 367 ret = (Text)(getTextPrototype ().clone ()); 368 ret.setPage (page); 369 ret.setStartPosition (start); 370 ret.setEndPosition (end); 371 } 372 catch (CloneNotSupportedException cnse) 373 { 374 ret = new TextNode (page, start, end); 375 } 376 377 return (ret); 378 } 379 380 386 public Remark createRemarkNode (Page page, int start, int end) 387 { 388 Remark ret; 389 390 try 391 { 392 ret = (Remark)(getRemarkPrototype ().clone ()); 393 ret.setPage (page); 394 ret.setStartPosition (start); 395 ret.setEndPosition (end); 396 } 397 catch (CloneNotSupportedException cnse) 398 { 399 ret = new RemarkNode (page, start, end); 400 } 401 402 return (ret); 403 } 404 405 416 public Tag createTagNode (Page page, int start, int end, Vector attributes) 417 throws 418 ParserException 419 { 420 Attribute attribute; 421 String id; 422 Tag prototype; 423 Tag ret; 424 425 ret = null; 426 427 if (0 != attributes.size ()) 428 { 429 attribute = (Attribute)attributes.elementAt (0); 430 id = attribute.getName (); 431 if (null != id) 432 { 433 try 434 { 435 id = id.toUpperCase (Locale.ENGLISH); 436 if (!id.startsWith ("/")) 437 { 438 if (id.endsWith ("/")) 439 id = id.substring (0, id.length () - 1); 440 prototype = (Tag)mBlastocyst.get (id); 441 if (null != prototype) 442 { 443 ret = (Tag)prototype.clone (); 444 ret.setPage (page); 445 ret.setStartPosition (start); 446 ret.setEndPosition (end); 447 ret.setAttributesEx (attributes); 448 } 449 } 450 } 451 catch (CloneNotSupportedException cnse) 452 { 453 } 455 } 456 } 457 if (null == ret) 458 { try 460 { 461 ret = (Tag)getTagPrototype ().clone (); 462 ret.setPage (page); 463 ret.setStartPosition (start); 464 ret.setEndPosition (end); 465 ret.setAttributesEx (attributes); 466 } 467 catch (CloneNotSupportedException cnse) 468 { 469 ret = new TagNode (page, start, end, attributes); 470 } 471 } 472 473 return (ret); 474 } 475 } 476 | Popular Tags |