1 27 package org.htmlparser.scanners; 28 29 import java.util.Vector ; 30 31 import org.htmlparser.Attribute; 32 import org.htmlparser.Node; 33 import org.htmlparser.Tag; 34 import org.htmlparser.lexer.Lexer; 35 import org.htmlparser.lexer.Page; 36 import org.htmlparser.scanners.Scanner; 37 import org.htmlparser.util.NodeList; 38 import org.htmlparser.util.ParserException; 39 40 44 public class CompositeTagScanner extends TagScanner 45 { 46 53 private static final boolean mUseJVMStack = false; 54 55 62 private static final boolean mLeaveEnds = false; 63 64 67 public CompositeTagScanner () 68 { 69 } 70 71 96 public Tag scan (Tag tag, Lexer lexer, NodeList stack) throws ParserException 97 { 98 Node node; 99 Tag next; 100 String name; 101 Scanner scanner; 102 Tag ret; 103 104 ret = tag; 105 106 if (ret.isEmptyXmlTag ()) 107 ret.setEndTag (ret); 108 else 109 do 110 { 111 node = lexer.nextNode (false); 112 if (null != node) 113 { 114 if (node instanceof Tag) 115 { 116 next = (Tag)node; 117 name = next.getTagName (); 118 if (next.isEndTag () && name.equals (ret.getTagName ())) 120 { 121 ret.setEndTag (next); 122 node = null; 123 } 124 else if (isTagToBeEndedFor (ret, next)) { 126 lexer.setPosition (next.getStartPosition ()); 128 node = null; 129 } 130 else if (!next.isEndTag ()) 131 { 132 scanner = next.getThisScanner (); 134 if (null != scanner) 135 { 136 if (mUseJVMStack) 137 { node = scanner.scan (next, lexer, stack); 139 addChild (ret, node); 140 } 141 else 142 { 143 if (scanner == this) 145 { 146 if (next.isEmptyXmlTag ()) 147 { 148 next.setEndTag (next); 149 finishTag (next, lexer); 150 addChild (ret, next); 151 } 152 else 153 { 154 stack.add (ret); 155 ret = next; 156 } 157 } 158 else 159 { node = scanner.scan (next, lexer, stack); 161 addChild (ret, node); 162 } 163 } 164 } 165 else 166 addChild (ret, next); 167 } 168 else 169 { 170 if (!mUseJVMStack && !mLeaveEnds) 171 { 172 Vector attributes = new Vector (); 190 attributes.addElement (new Attribute (name, null)); 191 Tag opener = lexer.getNodeFactory ().createTagNode ( 192 lexer.getPage (), next.getStartPosition (), next.getEndPosition (), 193 attributes); 194 195 scanner = opener.getThisScanner (); 196 if ((null != scanner) && (scanner == this)) 197 { 198 int index = -1; 200 for (int i = stack.size () - 1; (-1 == index) && (i >= 0); i--) 201 { 202 Tag boffo = (Tag)stack.elementAt (i); 205 if (name.equals (boffo.getTagName ())) 206 index = i; 207 else if (isTagToBeEndedFor (boffo, next)) index = i; 209 } 210 if (-1 != index) 211 { 212 finishTag (ret, lexer); 214 addChild ((Tag)stack.elementAt (stack.size () - 1), ret); 215 for (int i = stack.size () - 1; i > index; i--) 216 { 217 Tag fred = (Tag)stack.remove (i); 218 finishTag (fred, lexer); 219 addChild ((Tag)stack.elementAt (i - 1), fred); 220 } 221 ret = (Tag)stack.remove (index); 222 node = null; 223 } 224 else 225 addChild (ret, next); } 227 else 228 addChild (ret, next); } 230 else 231 addChild (ret, next); 232 } 233 } 234 else 235 addChild (ret, node); 236 } 237 238 if (!mUseJVMStack) 239 { 240 if (null == node) 242 { 243 int depth = stack.size (); 244 if (0 != depth) 245 { 246 node = stack.elementAt (depth - 1); 247 if (node instanceof Tag) 248 { 249 Tag precursor = (Tag)node; 250 scanner = precursor.getThisScanner (); 251 if (scanner == this) 252 { 253 stack.remove (depth - 1); 254 finishTag (ret, lexer); 255 addChild (precursor, ret); 256 ret = precursor; 257 } 258 else 259 node = null; } 261 else 262 node = null; } 264 } 265 } 266 } 267 while (null != node); 268 269 finishTag (ret, lexer); 270 271 return (ret); 272 } 273 274 279 protected void addChild (Tag parent, Node child) 280 { 281 if (null == parent.getChildren ()) 282 parent.setChildren (new NodeList ()); 283 child.setParent (parent); 284 parent.getChildren ().add (child); 285 } 286 287 295 protected void finishTag (Tag tag, Lexer lexer) 296 throws 297 ParserException 298 { 299 if (null == tag.getEndTag ()) 300 tag.setEndTag (createVirtualEndTag (tag, lexer, lexer.getPage (), lexer.getCursor ().getPosition ())); 301 tag.getEndTag ().setParent (tag); 302 tag.doSemanticAction (); 303 } 304 305 316 protected Tag createVirtualEndTag (Tag tag, Lexer lexer, Page page, int position) 317 throws 318 ParserException 319 { 320 Tag ret; 321 String name; 322 Vector attributes; 323 324 name = "/" + tag.getRawTagName (); 325 attributes = new Vector (); 326 attributes.addElement (new Attribute (name, (String )null)); 327 ret = lexer.getNodeFactory ().createTagNode ( 328 page, position, position, attributes); 329 330 return (ret); 331 } 332 333 343 public final boolean isTagToBeEndedFor (Tag current, Tag tag) 344 { 345 String name; 346 String [] ends; 347 boolean ret; 348 349 ret = false; 350 351 name = tag.getTagName (); 352 if (tag.isEndTag ()) 353 ends = current.getEndTagEnders (); 354 else 355 ends = current.getEnders (); 356 for (int i = 0; i < ends.length; i++) 357 if (name.equalsIgnoreCase (ends[i])) 358 { 359 ret = true; 360 break; 361 } 362 363 return (ret); 364 } 365 } 366 | Popular Tags |