[UIMA-2390] - applied Renaud's patch
git-svn-id: https://svn.apache.org/repos/asf/uima/addons/trunk@1427122 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/RegularExpressionAnnotator/src/main/java/org/apache/uima/annotator/regex/impl/ConceptFileParser_impl.java b/RegularExpressionAnnotator/src/main/java/org/apache/uima/annotator/regex/impl/ConceptFileParser_impl.java
index 4598299..104a39c 100644
--- a/RegularExpressionAnnotator/src/main/java/org/apache/uima/annotator/regex/impl/ConceptFileParser_impl.java
+++ b/RegularExpressionAnnotator/src/main/java/org/apache/uima/annotator/regex/impl/ConceptFileParser_impl.java
@@ -21,6 +21,7 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
+import java.util.regex.Matcher;
import org.apache.incubator.uima.regex.AnnotationDocument;
import org.apache.incubator.uima.regex.ConceptDocument;
@@ -39,8 +40,8 @@
import org.apache.uima.annotator.regex.Feature;
import org.apache.uima.annotator.regex.FilterFeature;
import org.apache.uima.annotator.regex.Position;
-import org.apache.uima.annotator.regex.Rule;
import org.apache.uima.annotator.regex.RegexVariables;
+import org.apache.uima.annotator.regex.Rule;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlOptions;
@@ -100,7 +101,30 @@
if (varArray.length > 0) {
variables = new RegexVariables_impl();
for (int i = 0; i < varArray.length; i++) {
- String value = varArray[i].getValue().replaceAll("\\\\", "\\\\\\\\");
+ String value = varArray[i].getValue();
+
+ // replace (existing) variables in variables
+ Matcher matcher = RegexVariables.VARIABLE_REGEX_PATTERN.matcher(value);
+ int pos = 0;
+ while (matcher.find(pos)) {
+ // replace variable value
+ String varName = matcher.group().substring(3,
+ matcher.group().length() - 1);
+ String substitutionValue = variables.getVariableValue(varName);
+ if (substitutionValue == null) {
+ throw new ResourceInitializationException(
+ ResourceInitializationException.ERROR_INITIALIZING_FROM_DESCRIPTOR,
+ new String[] {
+ varName,
+ conceptFilePathName
+ + ", referencing a variable that has not been defined " });
+ }
+ value = value.replaceAll(RegexVariables.VARIABLE_REGEX_BEGIN + varName
+ + "}", substitutionValue);
+
+ pos = matcher.end();// current end match position
+ }
+
variables.addVariable(varArray[i].getName(), value);
}
}
diff --git a/RegularExpressionAnnotator/src/main/java/org/apache/uima/annotator/regex/impl/Rule_impl.java b/RegularExpressionAnnotator/src/main/java/org/apache/uima/annotator/regex/impl/Rule_impl.java
index e149ed3..0242e06 100644
--- a/RegularExpressionAnnotator/src/main/java/org/apache/uima/annotator/regex/impl/Rule_impl.java
+++ b/RegularExpressionAnnotator/src/main/java/org/apache/uima/annotator/regex/impl/Rule_impl.java
@@ -367,8 +367,8 @@
// create variable expression that must be replaced
String variablePattern = RegexVariables.VARIABLE_REGEX_BEGIN
+ variableName + RegexVariables.VARIABLE_REGEX_END;
- // replace variable with the variable value
- this.regex = this.regex.replaceAll(variablePattern, varValue);
+ // replace variable with the variable value. quote for . and $
+ this.regex = this.regex.replaceAll(variablePattern, Matcher.quoteReplacement(varValue));
} else {
throw new RegexAnnotatorConfigException(
"regex_annotator_error_variable_not_found", new Object[] {
diff --git a/RegularExpressionAnnotator/src/test/java/org/apache/uima/annotator/regex/TestVariables.java b/RegularExpressionAnnotator/src/test/java/org/apache/uima/annotator/regex/TestVariables.java
index d0d4c50..50cceda 100644
--- a/RegularExpressionAnnotator/src/test/java/org/apache/uima/annotator/regex/TestVariables.java
+++ b/RegularExpressionAnnotator/src/test/java/org/apache/uima/annotator/regex/TestVariables.java
@@ -39,7 +39,7 @@
*
* @throws Exception
*/
- public void testVariablesConept1() throws Exception {
+ public void testVariablesConcept1() throws Exception {
// create annotation tester with the regex annotator specifier
AnnotatorTester annotTester = new AnnotatorTester(JUnitExtension
@@ -63,7 +63,7 @@
*
* @throws Exception
*/
- public void testVariablesConept2() throws Exception {
+ public void testVariablesConcept2() throws Exception {
// create annotation tester with the regex annotator specifier
AnnotatorTester annotTester = new AnnotatorTester(JUnitExtension
@@ -83,11 +83,11 @@
}
/**
- * Test regex variable replacement for variable concept 3
+ * Test regex variable replacement for variable concept 3. Tests escaping
*
* @throws Exception
*/
- public void testVariablesConept3() throws Exception {
+ public void testVariablesConcept3() throws Exception {
// create annotation tester with the regex annotator specifier
AnnotatorTester annotTester = new AnnotatorTester(JUnitExtension
@@ -106,6 +106,53 @@
}
/**
+ * Test regex variable replacement for variable concept 3. Tests dollar signs
+ *
+ * @throws Exception
+ */
+ public void testVariablesConcept4() throws Exception {
+
+ // create annotation tester with the regex annotator specifier
+ AnnotatorTester annotTester = new AnnotatorTester(JUnitExtension
+ .getFile("variables/RegExAnnotVariables.xml"));
+
+ CAS cas = annotTester.performTest("this is the end. my only friend, the end", "en");
+
+ // define result interested in
+ String[] tofs = { "org.apache.uima.TestAnnot" };
+
+ // compare results
+ File outputFile = new File(JUnitExtension.getFile("variables"),
+ "variablesConcept4_testoutput.txt");
+ AnnotatorTester.checkResult(cas, tofs, JUnitExtension
+ .getFile("variables/variablesConcept4Ref.txt"), outputFile);
+ }
+
+ /**
+ * Test regex variable in variable replacement
+ *
+ * @throws Exception
+ */
+ public void testVariablesInVariables() throws Exception {
+
+ // create annotation tester with the regex annotator specifier
+ AnnotatorTester annotTester = new AnnotatorTester(
+ JUnitExtension.getFile("variables/RegExAnnotVariablesInVariables.xml"));
+
+ CAS cas = annotTester.performTest("Monday regex with variables in variables Thursday",
+ "en");
+
+ // define result interested in
+ String[] tofs = { "org.apache.uima.TestAnnot" };
+
+ // compare results
+ File outputFile = new File(JUnitExtension.getFile("variables"),
+ "variablesInVariables_testoutput.txt");
+ AnnotatorTester.checkResult(cas, tofs,
+ JUnitExtension.getFile("variables/variablesInVariablesRef.txt"), outputFile);
+ }
+
+ /**
* Test regex variable replacement for variable concept 2
*
* @throws Exception
diff --git a/RegularExpressionAnnotator/src/test/resources/variables/variables.xml b/RegularExpressionAnnotator/src/test/resources/variables/variables.xml
index 76844ba..ad5de30 100644
--- a/RegularExpressionAnnotator/src/test/resources/variables/variables.xml
+++ b/RegularExpressionAnnotator/src/test/resources/variables/variables.xml
@@ -24,6 +24,8 @@
<variable name="month1" value="January"/>
<variable name="Last_Day" value="Sunday"/>
<variable name="escaping" value="Jan\."/>
+ <variable name="endSeparator" value="end($|\.)"/>
+
</variables>
<concept name="Variables1">
@@ -62,4 +64,16 @@
</createAnnotations>
</concept>
+ <concept name="Variables4">
+ <rules>
+ <rule regEx="this is the \v{endSeparator} my only friend, the \v{endSeparator}" matchStrategy="matchAll" matchType="uima.tcas.DocumentAnnotation"/>
+ </rules>
+ <createAnnotations>
+ <annotation id="testannot1" type="org.apache.uima.TestAnnot">
+ <begin group="0"/>
+ <end group="0"/>
+ </annotation>
+ </createAnnotations>
+ </concept>
+
</conceptSet>