Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

private ArrayList<String> filenames = new ArrayList<>();
private Matcher ignoreMatcher;
private Pattern split = Pattern.compile("\t", Pattern.LITERAL);
private boolean allowEmptyValues;
private boolean isUninitialized = true;
Expand Down Expand Up @@ -109,6 +111,15 @@ public void setExpectedColumns(final int expectedColumns) {
this.expectedColumns = expectedColumns;
}

/**
* Sets the pattern which determines whether a line should be ignored.
*
* @param ignorePattern a Java regular expression
*/
public void setIgnorePattern(final String ignorePattern) {
this.ignoreMatcher = Pattern.compile(ignorePattern).matcher("");
}

/**
* Sets a comma separated list of files which provides the {@link Map}.
*
Expand Down Expand Up @@ -168,25 +179,31 @@ private void loadFile(final String file) {

String line;
while ((line = br.readLine()) != null) {
if (line.isEmpty()) {
if (ignore(line)) {
continue;
}

final String[] parts = allowEmptyValues ? split.split(line, -1) : split.split(line);
if (parts.length < minColumns) {
if (ignore(parts.length, minColumns, expColumns)) {
continue;
}

if (expColumns < 0 || parts.length == expColumns) {
map.put(parts[keyColumn], parts[valueColumn]);
}
map.put(parts[keyColumn], parts[valueColumn]);
}
}
catch (final IOException | UncheckedIOException e) {
throw new MorphExecutionException("filemap: cannot read map file", e);
}
}

private boolean ignore(final String line) {
return line.isEmpty() || (ignoreMatcher != null && ignoreMatcher.reset(line).matches());
}

private boolean ignore(final int partsLength, final int minColumns, final int expColumns) {
return partsLength < minColumns || (expColumns > 0 && partsLength != expColumns);
}

private InputStream openStream(final String file) {
return openAsFile(file)
.orElseGet(() -> openAsResource(file)
Expand Down
6 changes: 6 additions & 0 deletions metamorph/src/main/resources/schemata/metamorph.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,12 @@
</restriction>
</simpleType>
</attribute>
<attribute name="ignorePattern" type="string" use="optional">
<annotation>
<documentation>Sets the pattern which determines whether a line should
be ignored.</documentation>
</annotation>
</attribute>
<attribute ref="xml:base" />
</complexType>
</element>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ public void shouldLookupValuesInFileBasedMapWithColumnOptions() {
);
}

@Test
public void shouldLookupValuesInFileBasedMapWithIgnorePattern() {
assertMorph(receiver, buildMorph("lookup in", "ignorePattern=\"g.*\""),
i -> {
i.startRecord("1");
i.literal("1", "gw");
i.literal("1", "fj");
i.literal("1", "hk");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().literal("1", "Fiji");
o.get().literal("1", "HongKong");
o.get().endRecord();
}
);
}

@Test
public void shouldWhitelistValuesInFileBasedMap() {
assertMorph(receiver, buildMorph("whitelist map", ""),
Expand Down Expand Up @@ -308,6 +327,16 @@ public void shouldLoadFileWithArbitraryExpectedColumns() {
});
}

@Test
public void shouldLoadFileWithIgnorePattern() {
assertMap(369, i -> {
i.setIgnorePattern(".*New.*");

Assert.assertNull(i.get("pp"));
Assert.assertEquals("Puerto Rico", i.get("pr"));
});
}

@Test
public void shouldNotLoadFileWithOutOfRangeKeyColumn() {
assertMap(0, i -> {
Expand Down