1 27 package org.htmlparser.filters; 28 29 import java.util.regex.Matcher ; 30 import java.util.regex.Pattern ; 31 32 import org.htmlparser.Node; 33 import org.htmlparser.NodeFilter; 34 import org.htmlparser.Text; 35 36 67 public class RegexFilter implements NodeFilter 68 { 69 72 public static final int MATCH = 1; 73 74 77 public static final int LOOKINGAT = 2; 78 79 82 public static final int FIND = 3; 83 84 87 protected String mPatternString; 88 89 92 protected Pattern mPattern; 93 94 98 protected int mStrategy; 99 100 104 public RegexFilter () 105 { 106 this (".*", FIND); 107 } 108 109 114 public RegexFilter (String pattern) 115 { 116 this (pattern, FIND); 117 } 118 119 130 public RegexFilter (String pattern, int strategy) 131 { 132 setPattern (pattern); 133 setStrategy (strategy); 134 } 135 136 140 public String getPattern () 141 { 142 return (mPatternString); 143 } 144 145 149 public void setPattern (String pattern) 150 { 151 mPatternString = pattern; 152 mPattern = Pattern.compile (pattern); 153 } 154 155 159 public int getStrategy () 160 { 161 return (mStrategy); 162 } 163 164 168 public void setStrategy (int strategy) 169 { 170 mStrategy = strategy; 171 } 172 173 177 public boolean accept (Node node) 178 { 179 String string; 180 Matcher matcher; 181 boolean ret; 182 183 ret = false; 184 if (node instanceof Text) 185 { 186 string = ((Text)node).getText (); 187 matcher = mPattern.matcher (string); 188 switch (mStrategy) 189 { 190 case MATCH: 191 ret = matcher.matches (); 192 break; 193 case LOOKINGAT: 194 ret = matcher.lookingAt (); 195 break; 196 case FIND: 197 ret = matcher.find (); 198 break; 199 } 200 } 201 202 return (ret); 203 } 204 } 205 | Popular Tags |