1 27 package org.htmlparser.tags; 28 29 import java.util.Locale ; 30 31 import org.htmlparser.Node; 32 import org.htmlparser.NodeFilter; 33 import org.htmlparser.Text; 34 import org.htmlparser.nodes.AbstractNode; 35 import org.htmlparser.nodes.TagNode; 36 import org.htmlparser.Tag; 37 import org.htmlparser.scanners.CompositeTagScanner; 38 import org.htmlparser.util.NodeList; 39 import org.htmlparser.util.SimpleNodeIterator; 40 import org.htmlparser.visitors.NodeVisitor; 41 42 48 public class CompositeTag extends TagNode 49 { 50 54 protected Tag mEndTag; 55 56 59 protected final static CompositeTagScanner mDefaultCompositeScanner = new CompositeTagScanner (); 60 61 public CompositeTag () 62 { 63 setThisScanner (mDefaultCompositeScanner); 64 } 65 66 70 public SimpleNodeIterator children () 71 { 72 SimpleNodeIterator ret; 73 74 if (null != getChildren ()) 75 ret = getChildren ().elements (); 76 else 77 ret = (new NodeList ()).elements (); 78 79 return (ret); 80 } 81 82 87 public Node getChild (int index) 88 { 89 return ( 90 (null == getChildren ()) ? null : 91 getChildren ().elementAt (index)); 92 } 93 94 98 public Node [] getChildrenAsNodeArray () 99 { 100 return ( 101 (null == getChildren ()) ? new Node[0] : 102 getChildren ().toNodeArray ()); 103 } 104 105 109 public void removeChild (int i) 110 { 111 if (null != getChildren ()) 112 getChildren ().remove (i); 113 } 114 115 120 public SimpleNodeIterator elements() 121 { 122 return ( 123 (null == getChildren ()) ? new NodeList ().elements () : 124 getChildren ().elements ()); 125 } 126 127 public String toPlainTextString() { 128 StringBuffer stringRepresentation = new StringBuffer (); 129 for (SimpleNodeIterator e=children();e.hasMoreNodes();) { 130 stringRepresentation.append(e.nextNode().toPlainTextString()); 131 } 132 return stringRepresentation.toString(); 133 } 134 135 protected void putChildrenInto(StringBuffer sb) 136 { 137 Node node; 138 for (SimpleNodeIterator e = children (); e.hasMoreNodes ();) 139 { 140 node = e.nextNode (); 141 sb.append (node.toHtml ()); 144 } 145 } 146 147 protected void putEndTagInto(StringBuffer sb) 148 { 149 sb.append(getEndTag ().toHtml()); 152 } 153 154 public String toHtml() { 155 StringBuffer sb = new StringBuffer (); 156 sb.append (super.toHtml ()); 157 if (!isEmptyXmlTag()) 158 { 159 putChildrenInto(sb); 160 if (null != getEndTag ()) putEndTagInto(sb); 162 } 163 return sb.toString(); 164 } 165 166 171 public Tag searchByName(String name) { 172 Node node; 173 Tag tag = null; 174 boolean found = false; 175 for (SimpleNodeIterator e = children();e.hasMoreNodes() && !found;) { 176 node = e.nextNode(); 177 if (node instanceof Tag) 178 { 179 tag = (Tag)node; 180 String nameAttribute = tag.getAttribute("NAME"); 181 if (nameAttribute!=null && nameAttribute.equals(name)) 182 found=true; 183 } 184 } 185 if (found) 186 return tag; 187 else 188 return null; 189 } 190 191 205 public NodeList searchFor (String searchString) 206 { 207 return (searchFor (searchString, false)); 208 } 209 210 225 public NodeList searchFor (String searchString, boolean caseSensitive) 226 { 227 return (searchFor (searchString, caseSensitive, Locale.ENGLISH)); 228 } 229 230 246 public NodeList searchFor (String searchString, boolean caseSensitive, Locale locale) 247 { 248 Node node; 249 String text; 250 NodeList ret; 251 252 ret = new NodeList (); 253 254 if (!caseSensitive) 255 searchString = searchString.toUpperCase (locale); 256 for (SimpleNodeIterator e = children (); e.hasMoreNodes (); ) 257 { 258 node = e.nextNode (); 259 text = node.toPlainTextString (); 260 if (!caseSensitive) 261 text = text.toUpperCase (locale); 262 if (-1 != text.indexOf (searchString)) 263 ret.add (node); 264 } 265 266 return (ret); 267 } 268 269 277 public NodeList searchFor (Class classType, boolean recursive) 278 { 279 return ( 280 (null == getChildren ()) ? new NodeList () : 281 getChildren ().searchFor (classType, recursive)); 282 } 283 284 293 public int findPositionOf (String text) 294 { 295 return (findPositionOf (text, Locale.ENGLISH)); 296 } 297 298 307 public int findPositionOf (String text, Locale locale) 308 { 309 Node node; 310 int loc; 311 312 loc = 0; 313 text = text.toUpperCase (locale); 314 for (SimpleNodeIterator e = children (); e.hasMoreNodes (); ) 315 { 316 node = e.nextNode (); 317 if (-1 != node.toPlainTextString ().toUpperCase (locale).indexOf (text)) 318 return loc; 319 loc++; 320 } 321 return -1; 322 } 323 324 334 public int findPositionOf(Node searchNode) { 335 Node node; 336 int loc = 0; 337 for (SimpleNodeIterator e=children();e.hasMoreNodes();) { 338 node = e.nextNode(); 339 if (node==searchNode) { 340 return loc; 341 } 342 loc++; 343 } 344 return -1; 345 } 346 347 352 public Node childAt (int index) 353 { 354 return ( 355 (null == getChildren ()) ? null : 356 getChildren ().elementAt (index)); 357 } 358 359 392 public void collectInto (NodeList list, NodeFilter filter) 393 { 394 super.collectInto (list, filter); 395 for (SimpleNodeIterator e = children(); e.hasMoreNodes ();) 396 e.nextNode ().collectInto (list, filter); 397 if ((null != getEndTag ()) && (this != getEndTag ())) getEndTag ().collectInto (list, filter); 399 } 400 401 public String getChildrenHTML() { 402 StringBuffer buff = new StringBuffer (); 403 for (SimpleNodeIterator e = children();e.hasMoreNodes();) { 404 AbstractNode node = (AbstractNode)e.nextNode(); 405 buff.append(node.toHtml()); 406 } 407 return buff.toString(); 408 } 409 410 420 public void accept (NodeVisitor visitor) 421 { 422 SimpleNodeIterator children; 423 Node child; 424 425 if (visitor.shouldRecurseSelf ()) 426 visitor.visitTag (this); 427 if (visitor.shouldRecurseChildren ()) 428 { 429 if (null != getChildren ()) 430 { 431 children = children (); 432 while (children.hasMoreNodes ()) 433 { 434 child = children.nextNode (); 435 child.accept (visitor); 436 } 437 } 438 if ((null != getEndTag ()) && (this != getEndTag ())) getEndTag ().accept (visitor); 440 } 441 } 442 443 public int getChildCount() 444 { 445 NodeList children; 446 447 children = getChildren (); 448 449 return ((null == children) ? 0 : children.size ()); 450 } 451 452 public Tag getEndTag() 453 { 454 return (mEndTag); 455 } 456 457 public void setEndTag (Tag end) 458 { 459 mEndTag = end; 460 } 461 462 469 public Text[] digupStringNode(String searchText) { 470 NodeList nodeList = searchFor(searchText); 471 NodeList stringNodes = new NodeList(); 472 for (int i=0;i<nodeList.size();i++) { 473 Node node = nodeList.elementAt(i); 474 if (node instanceof Text) { 475 stringNodes.add(node); 476 } else { 477 if (node instanceof CompositeTag) { 478 CompositeTag ctag = (CompositeTag)node; 479 Text[] nodes = ctag.digupStringNode(searchText); 480 for (int j=0;j<nodes.length;j++) 481 stringNodes.add(nodes[j]); 482 } 483 } 484 } 485 Text[] stringNode = new Text[stringNodes.size()]; 486 for (int i=0;i<stringNode.length;i++) { 487 stringNode[i] = (Text)stringNodes.elementAt(i); 488 } 489 return stringNode; 490 } 491 492 public String toString () 493 { 494 StringBuffer ret; 495 496 ret = new StringBuffer (1024); 497 toString (0, ret); 498 499 return (ret.toString ()); 500 } 501 502 506 public String getText () 507 { 508 String ret; 509 510 ret = super.toHtml (); 511 ret = ret.substring (1, ret.length () - 1); 512 513 return (ret); 514 } 515 516 520 public String getStringText () 521 { 522 String ret; 523 int start = getEndPosition (); 524 int end = mEndTag.getStartPosition (); 525 ret = getPage ().getText (start, end); 526 527 return (ret); 528 } 529 530 public void toString (int level, StringBuffer buffer) 531 { 532 Node node; 533 534 for (int i = 0; i < level; i++) 535 buffer.append (" "); 536 buffer.append (super.toString ()); 537 buffer.append (System.getProperty ("line.separator")); 538 for (SimpleNodeIterator e = children (); e.hasMoreNodes ();) 539 { 540 node = e.nextNode (); 541 if (node instanceof CompositeTag) 542 ((CompositeTag)node).toString (level + 1, buffer); 543 else 544 { 545 for (int i = 0; i <= level; i++) 546 buffer.append (" "); 547 buffer.append (node); 548 buffer.append (System.getProperty ("line.separator")); 549 } 550 } 551 552 if ((null != getEndTag ()) && (this != getEndTag ())) { 556 for (int i = 0; i <= level; i++) 557 buffer.append (" "); 558 buffer.append (getEndTag ().toString ()); 559 buffer.append (System.getProperty ("line.separator")); 560 } 561 } 562 } 563 | Popular Tags |