package org.myrobotlab.document.transformer;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.myrobotlab.document.Document;

/**
 * This stage will use a regex to find a pattern in a string field and store the
 * matched text into the output field.
 * 
 * @author kwatters
 *
 */
public class RegexExtractor extends AbstractStage {

	private String inputField = null;
	private String outputField = null;
	private String regex = null;
	
	private Pattern pattern;
	
	@Override
	public void startStage(StageConfiguration config) {
		// TODO Much more stuff like group support and field mapping for the groups
		if (config != null) {
			inputField = config.getProperty("inputField", "text");
			outputField = config.getProperty("outputField", "entity");
			regex = config.getProperty("regex");			
		}
		pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE);
	}

	@Override
	public List<Document> processDocument(Document doc) {
		// TODO Auto-generated method stub
		for (Object o : doc.getField(inputField)) {
			String text = o.toString();
			Matcher matcher = pattern.matcher(text);
			while (matcher.find()) {
				// TODO: test me!
				String match = text.substring(matcher.start(), matcher.end());
				doc.setField(outputField, match);
			}
		}
		// this stage doesn't emit child docs.
		return null;
	}

	@Override
	public void stopStage() {
		// TODO Auto-generated method stub

	}

	@Override
	public void flush() {
		// TODO Auto-generated method stub
	}

	public String getInputField() {
		return inputField;
	}

	public void setInputField(String inputField) {
		this.inputField = inputField;
	}

	public String getOutputField() {
		return outputField;
	}

	public void setOutputField(String outputField) {
		this.outputField = outputField;
	}

	public String getRegex() {
		return regex;
	}

	public void setRegex(String regex) {
		this.regex = regex;
	}

}

@@ -39,16 +39,44 @@
 ormer;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -323,17 +323,172 @@
 ield.%0A *
- 
+%0A * The list of keepGroups tells the RegexEtractor which groups from the regular expression to keep.%0A * Groups are concatenated to form the output value.%0A *
 %0A * @aut
@@ -499,16 +499,24 @@
 kwatters
+, dmeehl
 %0A *%0A */%0A
@@ -631,32 +631,74 @@
 utField = null;%0A
+%09private List%3CInteger%3E keepGroups = null;%0A
 %09private String 
@@ -809,86 +809,8 @@
 ) %7B%0A
-%09%09// TODO Much more stuff like group support and field mapping for the groups%0A
 %09%09if
@@ -953,50 +953,360 @@
 %22);%0A
-%09%09%09regex = config.getProperty(%22regex%22);%09%09%09
+      List%3CString%3E keepGroupsStr = config.getListParam(%22keepGroups%22);%0A%09%09%09regex = config.getProperty(%22regex%22);%0A%0A      keepGroups = new ArrayList%3CInteger%3E();%0A      if (keepGroupsStr == null) %7B%0A        keepGroups.add(1);%0A      %7D else %7B%0A        for (String groupNum : keepGroupsStr) %7B%0A          keepGroups.add(Integer.parseInt(groupNum));%0A        %7D%0A      %7D
 %0A%09%09%7D
@@ -1481,42 +1481,112 @@
 %7B%0A%09%09
-// TODO Auto-generated method stub
+if (!doc.hasField(inputField))%7B%0A%09%09%09return null;%0A%09%09%7D%0A%0A    List%3CString%3E matches = new ArrayList%3CString%3E();
 %0A%09%09f
@@ -1710,13 +1710,10 @@
 %0A%09%09%09
-while
+if
  (ma
@@ -1722,118 +1722,284 @@
 her.
-find()) %7B%0A%09%09%09%09// TODO: test me!%0A%09%09%09%09String match = text.substring(matcher.start(), matcher.end());%0A%09%09%09%09doc.set
+matches() && matcher.groupCount() %3E 0) %7B%0A%09%09%09%09String match = %22%22;%0A        for (Integer num : keepGroups) %7B%0A          match += matcher.group(num);%0A        %7D%0A        matches.add(match);%0A%09%09%09%7D%0A%09%09%7D%0A%0A    doc.removeField(outputField);%0A    for (String match : matches) %7B%0A      doc.addTo
 Fiel
@@ -2021,24 +2021,22 @@
 match);%0A
-%09%09%09%7D%0A%09%09%7D
+    %7D%0A
 %0A%09%09// th
@@ -2131,119 +2131,44 @@
  %7B%0A%09
-%09// TODO Auto-generated method stub%0A%0A%09%7D%0A%0A%09@Override%0A%09public void flush() %7B%0A%09%09// TODO Auto-generated method stub
+%7D%0A%0A%09@Override%0A%09public void flush() %7B
 %0A%09%7D%0A

Add group support.