1 27 package org.htmlparser.visitors; 28 29 import java.util.Locale ; 30 31 import org.htmlparser.Text; 32 33 public class StringFindingVisitor extends NodeVisitor 34 { 35 private String stringToFind; 36 private int foundCount; 37 private boolean multipleSearchesWithinStrings; 38 private Locale locale; 39 40 public StringFindingVisitor(String stringToFind) 41 { 42 this (stringToFind, null); 43 } 44 45 public StringFindingVisitor(String stringToFind, Locale locale) 46 { 47 this.locale = (null == locale) ? Locale.ENGLISH : locale; 48 this.stringToFind = stringToFind.toUpperCase (this.locale); 49 foundCount = 0; 50 multipleSearchesWithinStrings = false; 51 } 52 53 public void doMultipleSearchesWithinStrings() 54 { 55 multipleSearchesWithinStrings = true; 56 } 57 58 public void visitStringNode(Text stringNode) 59 { 60 String stringToBeSearched = stringNode.getText().toUpperCase(locale); 61 if (!multipleSearchesWithinStrings && 62 stringToBeSearched.indexOf(stringToFind) != -1) { 63 foundCount++; 64 } else if (multipleSearchesWithinStrings) { 65 int index = -1; 66 do { 67 index = stringToBeSearched.indexOf(stringToFind, index+1); 68 if (index!=-1) 69 foundCount++; 70 } while (index != -1); 71 } 72 } 73 74 public boolean stringWasFound() 75 { 76 return (0 != stringFoundCount()); 77 } 78 79 public int stringFoundCount() 80 { 81 return foundCount; 82 } 83 84 } 85 | Popular Tags |