Skip to content

Commit c8fbb56

Browse files
committed
Fix escaping in RegexParameterizedPatternBuilder. Standardize group building code.
1 parent 826c357 commit c8fbb56

3 files changed

Lines changed: 21 additions & 62 deletions

File tree

‎api/src/main/java/org/ocpsoft/rewrite/param/RegexParameterizedPatternBuilder.java‎

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import org.ocpsoft.rewrite.context.EvaluationContext;
3030
import org.ocpsoft.rewrite.event.Rewrite;
3131
import org.ocpsoft.rewrite.exception.ParameterizationException;
32+
import org.ocpsoft.rewrite.param.RegexParameterizedPatternParser.RegexGroup;
3233
import org.ocpsoft.rewrite.util.ParameterUtils;
33-
import org.ocpsoft.rewrite.util.ParseTools;
3434
import org.ocpsoft.rewrite.util.ParseTools.CaptureType;
3535
import org.ocpsoft.rewrite.util.ParseTools.CapturingGroup;
3636

@@ -91,34 +91,9 @@ public RegexParameterizedPatternBuilder(final CaptureType type, final String def
9191
Assert.notNull(pattern, "Pattern must not be null");
9292

9393
this.defaultParameterPattern = defaultParameterPattern;
94-
9594
this.pattern = pattern;
96-
chars = pattern.toCharArray();
97-
98-
if (chars.length > 0)
99-
{
100-
int parameterIndex = 0;
101-
int cursor = 0;
102-
while (cursor < chars.length)
103-
{
104-
switch (chars[cursor])
105-
{
106-
case '{':
107-
int startPos = cursor;
108-
CapturingGroup group = ParseTools.balancedCapture(chars, startPos, chars.length - 1, type);
109-
cursor = group.getEnd();
110-
111-
groups.add(new RegexGroup(group, parameterIndex++));
112-
113-
break;
114-
115-
default:
116-
break;
117-
}
118-
119-
cursor++;
120-
}
121-
}
95+
this.chars = pattern.toCharArray();
96+
this.groups.addAll(RegexParameterizedPatternParser.getGroups(type, chars));
12297
}
12398

12499
@Override
@@ -274,38 +249,6 @@ public String toString()
274249
return new String(chars);
275250
}
276251

277-
class RegexGroup
278-
{
279-
private final CapturingGroup capture;
280-
private final int index;
281-
282-
public RegexGroup(final CapturingGroup capture, int index)
283-
{
284-
this.capture = capture;
285-
this.index = index;
286-
}
287-
288-
public int getIndex()
289-
{
290-
return index;
291-
}
292-
293-
public String getName()
294-
{
295-
return new String(capture.getCaptured());
296-
}
297-
298-
public CapturingGroup getCapture()
299-
{
300-
return capture;
301-
}
302-
303-
@Override
304-
public String toString()
305-
{
306-
return "RegexParameter [name=" + getName() + ", capture=" + capture + "]";
307-
}
308-
}
309252

310253
@Override
311254
public String getPattern()

‎api/src/main/java/org/ocpsoft/rewrite/param/RegexParameterizedPatternParser.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ public RegexParameterizedPatternParser(final CaptureType type, final String defa
9696
this.pattern = pattern;
9797
this.chars = pattern.toCharArray();
9898
this.type = type;
99+
this.groups.addAll(getGroups(type, chars));
100+
}
99101

102+
protected static List<RegexGroup> getGroups(final CaptureType type, final char[] chars)
103+
{
104+
List<RegexGroup> groups = new ArrayList<RegexGroup>();
100105
int cursor = 0;
101106
try {
102107

@@ -148,8 +153,9 @@ else if (chars.length - 1 == cursor)
148153
catch (Exception e)
149154
{
150155
throw new ParameterizedPatternSyntaxException("Error parsing parameterized pattern due to: " + e.getMessage(),
151-
pattern, cursor);
156+
new String(chars), cursor);
152157
}
158+
return groups;
153159
}
154160

155161
@Override
@@ -286,7 +292,7 @@ public ParameterizedPatternBuilder getBuilder()
286292
return builder;
287293
}
288294

289-
class RegexGroup
295+
static class RegexGroup
290296
{
291297
private final CapturingGroup capture;
292298
private final int index;

‎api/src/test/java/org/ocpsoft/rewrite/param/ParameterizedPatternTest.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ public void testEscapingParamsWithNewlines()
205205
Assert.assertTrue(parameterized.parse("beginning middle end").matches());
206206
}
207207

208+
@Test
209+
public void testEscapingParamsWithNewlines2()
210+
{
211+
String pattern = "beginning \\{\n ...\n } end";
212+
ParameterizedPatternParser parameterized = new RegexParameterizedPatternParser(CaptureType.BRACE, pattern);
213+
214+
Assert.assertEquals(0, parameterized.getRequiredParameterNames().size());
215+
Assert.assertTrue(parameterized.parse("beginning {\n ...\n } end").matches());
216+
}
217+
208218
@Test
209219
public void testEscapingWithNewlines()
210220
{

0 commit comments

Comments
 (0)