1 package org.apache.oro.text; 2 3 59 60 import java.io.*; 61 import java.util.*; 62 63 import org.apache.oro.text.regex.*; 64 65 141 public final class MatchActionProcessor { 142 private Pattern __fieldSeparator = null; 143 private PatternCompiler __compiler; 144 private PatternMatcher __matcher; 145 private Vector __patterns = new Vector(); 147 private Vector __actions = new Vector(); 148 149 private MatchAction __defaultAction = new DefaultMatchAction(); 150 151 162 public MatchActionProcessor(PatternCompiler compiler, PatternMatcher matcher) 163 { 164 __compiler = compiler; 165 __matcher = matcher; 166 } 167 168 174 public MatchActionProcessor() { 175 this(new Perl5Compiler(), new Perl5Matcher()); 176 } 177 178 179 189 public void addAction(String pattern, int options, MatchAction action) 190 throws MalformedPatternException 191 { 192 if(pattern != null) 193 __patterns.addElement(__compiler.compile(pattern, options)); 194 else 195 __patterns.addElement(null); 196 __actions.addElement(action); 197 } 198 199 209 public void addAction(String pattern, int options) 210 throws MalformedPatternException 211 { 212 addAction(pattern, options, __defaultAction); 213 } 214 215 223 public void addAction(String pattern) throws MalformedPatternException { 224 addAction(pattern, 0); 225 } 226 227 228 236 public void addAction(String pattern, MatchAction action) 237 throws MalformedPatternException 238 { 239 addAction(pattern, 0, action); 240 } 241 242 251 public void setFieldSeparator(String separator, int options) 252 throws MalformedPatternException 253 { 254 if(separator == null) { 255 __fieldSeparator = null; 256 return; 257 } 258 __fieldSeparator = __compiler.compile(separator, options); 259 } 260 261 262 270 public void setFieldSeparator(String separator) 271 throws MalformedPatternException 272 { 273 setFieldSeparator(separator, 0); 274 } 275 276 277 300 public void processMatches(InputStream input, OutputStream output) 301 throws IOException 302 { 303 int patternCount, current; 304 LineNumberReader reader = 305 new LineNumberReader(new InputStreamReader(input)); 306 PrintWriter writer = new PrintWriter(output); 307 MatchActionInfo info = new MatchActionInfo(); 308 Object obj; 309 Pattern pattern; 310 MatchAction action; 311 312 info.matcher = __matcher; 314 info.fieldSeparator = __fieldSeparator; 315 info.input = reader; 316 info.output = writer; 317 info.fields = null; 318 patternCount = __patterns.size(); 319 320 info.lineNumber = 0; 321 322 while((info.line = reader.readLine()) != null) { 323 info.charLine = info.line.toCharArray(); 324 for(current=0; current < patternCount; current++) { 325 obj = __patterns.elementAt(current); 326 if(obj != null) { 328 pattern = (Pattern)__patterns.elementAt(current); 329 if(__matcher.contains(info.charLine, pattern)) { 330 info.match = __matcher.getMatch(); 331 info.lineNumber = reader.getLineNumber(); 332 if(__fieldSeparator != null) 333 info.fields = Util.split(__matcher, __fieldSeparator, info.line); 334 action = (MatchAction)__actions.elementAt(current); 335 action.processMatch(info); 336 } 337 } else { 338 info.match = null; 339 info.lineNumber = reader.getLineNumber(); 340 if(__fieldSeparator != null) 341 info.fields = Util.split(__matcher, __fieldSeparator, info.line); 342 action = (MatchAction)__actions.elementAt(current); 343 action.processMatch(info); 344 } 345 } 346 } 347 348 writer.flush(); 350 reader.close(); 351 } 352 353 354 } 355 | Popular Tags |