[jboss-svn-commits] JBL Code SVN: r15069 - in labs/jbossrules/trunk/drools-compiler/src: test/java/org/drools/lang/dsl and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Sep 12 20:48:25 EDT 2007
Author: tirelli
Date: 2007-09-12 20:48:25 -0400 (Wed, 12 Sep 2007)
New Revision: 15069
Modified:
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/dsl/DefaultDSLMappingEntry.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/lang/dsl/DefaultDSLMappingEntryTest.java
Log:
JBRULES-1164: fixing DSL files with embedded () on key
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/dsl/DefaultDSLMappingEntry.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/dsl/DefaultDSLMappingEntry.java 2007-09-12 22:06:32 UTC (rev 15068)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/dsl/DefaultDSLMappingEntry.java 2007-09-13 00:48:25 UTC (rev 15069)
@@ -51,8 +51,12 @@
// it will return variables:
// This, pattern, easy, say
//
- static final Pattern varFinder = Pattern.compile( "(^|[^\\\\])\\{([(\\\\\\{)|[^\\{]]*?)\\}",
+ static final Pattern VAR_FINDER = Pattern.compile( "(^|[^\\\\])\\{([(\\\\\\{)|[^\\{]]*?)\\}",
Pattern.MULTILINE | Pattern.DOTALL );
+
+ // following pattern is used to find all the non-escaped parenthesis in the input key
+ // to correctly calculate the variables offset
+ private static final Pattern PAREN_FINDER = Pattern.compile( "(^\\(|[^\\\\]\\(|\\G\\()" );
public DefaultDSLMappingEntry() {
this( DSLMappingEntry.ANY,
@@ -110,9 +114,10 @@
if ( key != null ) {
int substr = 0;
- // escape '$' to avoid errors
- final Matcher m = varFinder.matcher( key.replaceAll( "\\$",
- "\\\\\\$" ) );
+ // escape '$' to avoid errors
+ final String escapedKey = key.replaceAll( "\\$",
+ "\\\\\\$" );
+ final Matcher m = VAR_FINDER.matcher( escapedKey );
// retrieving variables list and creating key pattern
final StringBuffer buf = new StringBuffer();
@@ -124,10 +129,21 @@
counter++;
}
+ int lastMatch = 0;
while ( m.find() ) {
if ( this.variables == Collections.EMPTY_MAP ) {
this.variables = new HashMap( 2 );
}
+
+ // calculating and fixing variable offset
+ String before = escapedKey.substring( lastMatch, m.start() );
+ lastMatch = m.end()+1;
+ Matcher m2 = PAREN_FINDER.matcher( before );
+ while( m2.find() ) {
+ counter++;
+ }
+
+ // creating capture group for variable
this.variables.put( m.group( 2 ),
new Integer( counter++ ) );
m.appendReplacement( buf,
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/lang/dsl/DefaultDSLMappingEntryTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/lang/dsl/DefaultDSLMappingEntryTest.java 2007-09-12 22:06:32 UTC (rev 15068)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/lang/dsl/DefaultDSLMappingEntryTest.java 2007-09-13 00:48:25 UTC (rev 15069)
@@ -208,4 +208,20 @@
"name in ( 'Edson', 'Bob' )",
result );
}
+
+ public void testExpandWithParethesis() {
+ final String inputKey = "((H|h)e|(S|s)he) \\(is\\) (a|an) $xx {attribute} (man|woman)";
+ final String inputValue = "Person( attribute == \"{attribute}\" )";
+
+ this.entry = new DefaultDSLMappingEntry( DSLMappingEntry.CONDITION,
+ null,
+ inputKey,
+ inputValue );
+
+ String result = this.entry.getKeyPattern().matcher( "he (is) a $xx handsome man" ).replaceAll( this.entry.getValuePattern() );
+ assertEquals( result,
+ "Person( attribute == \"handsome\" )",
+ result );
+ }
+
}
More information about the jboss-svn-commits
mailing list