Skip to content

Commit 67d158b

Browse files
authored
Merge pull request #472 from metafacture/filemapIgnorePattern
Add `FileMap` option to ignore lines matching pattern.
2 parents edfd975 + 51ef2b7 commit 67d158b

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

metamorph/src/main/java/org/metafacture/metamorph/maps/FileMap.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Map;
3636
import java.util.Optional;
3737
import java.util.Set;
38+
import java.util.regex.Matcher;
3839
import java.util.regex.Pattern;
3940

4041
/**
@@ -65,6 +66,7 @@ public final class FileMap extends AbstractReadOnlyMap<String, String> {
6566
private final Map<String, String> map = new HashMap<>();
6667

6768
private ArrayList<String> filenames = new ArrayList<>();
69+
private Matcher ignoreMatcher;
6870
private Pattern split = Pattern.compile("\t", Pattern.LITERAL);
6971
private boolean allowEmptyValues;
7072
private boolean isUninitialized = true;
@@ -109,6 +111,15 @@ public void setExpectedColumns(final int expectedColumns) {
109111
this.expectedColumns = expectedColumns;
110112
}
111113

114+
/**
115+
* Sets the pattern which determines whether a line should be ignored.
116+
*
117+
* @param ignorePattern a Java regular expression
118+
*/
119+
public void setIgnorePattern(final String ignorePattern) {
120+
this.ignoreMatcher = Pattern.compile(ignorePattern).matcher("");
121+
}
122+
112123
/**
113124
* Sets a comma separated list of files which provides the {@link Map}.
114125
*
@@ -168,25 +179,31 @@ private void loadFile(final String file) {
168179

169180
String line;
170181
while ((line = br.readLine()) != null) {
171-
if (line.isEmpty()) {
182+
if (ignore(line)) {
172183
continue;
173184
}
174185

175186
final String[] parts = allowEmptyValues ? split.split(line, -1) : split.split(line);
176-
if (parts.length < minColumns) {
187+
if (ignore(parts.length, minColumns, expColumns)) {
177188
continue;
178189
}
179190

180-
if (expColumns < 0 || parts.length == expColumns) {
181-
map.put(parts[keyColumn], parts[valueColumn]);
182-
}
191+
map.put(parts[keyColumn], parts[valueColumn]);
183192
}
184193
}
185194
catch (final IOException | UncheckedIOException e) {
186195
throw new MorphExecutionException("filemap: cannot read map file", e);
187196
}
188197
}
189198

199+
private boolean ignore(final String line) {
200+
return line.isEmpty() || (ignoreMatcher != null && ignoreMatcher.reset(line).matches());
201+
}
202+
203+
private boolean ignore(final int partsLength, final int minColumns, final int expColumns) {
204+
return partsLength < minColumns || (expColumns > 0 && partsLength != expColumns);
205+
}
206+
190207
private InputStream openStream(final String file) {
191208
return openAsFile(file)
192209
.orElseGet(() -> openAsResource(file)

metamorph/src/main/resources/schemata/metamorph.xsd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,12 @@
660660
</restriction>
661661
</simpleType>
662662
</attribute>
663+
<attribute name="ignorePattern" type="string" use="optional">
664+
<annotation>
665+
<documentation>Sets the pattern which determines whether a line should
666+
be ignored.</documentation>
667+
</annotation>
668+
</attribute>
663669
<attribute ref="xml:base" />
664670
</complexType>
665671
</element>

metamorph/src/test/java/org/metafacture/metamorph/maps/FileMapTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,25 @@ public void shouldLookupValuesInFileBasedMapWithColumnOptions() {
9090
);
9191
}
9292

93+
@Test
94+
public void shouldLookupValuesInFileBasedMapWithIgnorePattern() {
95+
assertMorph(receiver, buildMorph("lookup in", "ignorePattern=\"g.*\""),
96+
i -> {
97+
i.startRecord("1");
98+
i.literal("1", "gw");
99+
i.literal("1", "fj");
100+
i.literal("1", "hk");
101+
i.endRecord();
102+
},
103+
o -> {
104+
o.get().startRecord("1");
105+
o.get().literal("1", "Fiji");
106+
o.get().literal("1", "HongKong");
107+
o.get().endRecord();
108+
}
109+
);
110+
}
111+
93112
@Test
94113
public void shouldWhitelistValuesInFileBasedMap() {
95114
assertMorph(receiver, buildMorph("whitelist map", ""),
@@ -308,6 +327,16 @@ public void shouldLoadFileWithArbitraryExpectedColumns() {
308327
});
309328
}
310329

330+
@Test
331+
public void shouldLoadFileWithIgnorePattern() {
332+
assertMap(369, i -> {
333+
i.setIgnorePattern(".*New.*");
334+
335+
Assert.assertNull(i.get("pp"));
336+
Assert.assertEquals("Puerto Rico", i.get("pr"));
337+
});
338+
}
339+
311340
@Test
312341
public void shouldNotLoadFileWithOutOfRangeKeyColumn() {
313342
assertMap(0, i -> {

0 commit comments

Comments
 (0)