Skip to content

Commit 358d5f0

Browse files
authored
Merge pull request #167 from itsallcode/story/#143-refactor-tag-importer
Story/#143 unify short & long tag importer
2 parents cac7595 + 59dd2a2 commit 358d5f0

26 files changed

Lines changed: 415 additions & 251 deletions

src/main/java/org/itsallcode/openfasttrace/Converter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
import java.util.List;
2828

2929
import org.itsallcode.openfasttrace.core.Newline;
30-
import org.itsallcode.openfasttrace.FilterSettings;
31-
import org.itsallcode.openfasttrace.importer.legacytag.LegacyTagImporterFactory;
32-
import org.itsallcode.openfasttrace.importer.legacytag.config.LegacyTagImporterConfig;
30+
import org.itsallcode.openfasttrace.importer.tag.config.TagImporterConfig;
3331

3432
/**
3533
* Convert between different requirements formats (e.g. from ReqM2 to Markdown)
@@ -72,14 +70,14 @@ public interface Converter
7270
public Converter setNewline(Newline newline);
7371

7472
/**
75-
* Set the {@link LegacyTagImporterConfig} for the
73+
* Set the {@link TagImporterConfig} for the
7674
* {@link LegacyTagImporterFactory}.
7775
*
7876
* @param config
79-
* the {@link LegacyTagImporterConfig} to set.
77+
* the {@link TagImporterConfig} to set.
8078
* @return a <code>Converter</code> instance for fluent programming
8179
*/
82-
public Converter setLegacyTagImporterPathConfig(final LegacyTagImporterConfig config);
80+
public Converter setLegacyTagImporterPathConfig(final TagImporterConfig config);
8381

8482
/**
8583
* Convert the collected requirements into target requirement format

src/main/java/org/itsallcode/openfasttrace/Reporter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828

2929
import org.itsallcode.openfasttrace.core.Newline;
3030
import org.itsallcode.openfasttrace.core.Trace;
31-
import org.itsallcode.openfasttrace.FilterSettings;
32-
import org.itsallcode.openfasttrace.importer.legacytag.LegacyTagImporterFactory;
33-
import org.itsallcode.openfasttrace.importer.legacytag.config.LegacyTagImporterConfig;
31+
import org.itsallcode.openfasttrace.importer.tag.config.TagImporterConfig;
3432
import org.itsallcode.openfasttrace.report.ReportVerbosity;
3533

3634
/**
@@ -83,14 +81,14 @@ public interface Reporter
8381
public Reporter setReportVerbosity(final ReportVerbosity verbosity);
8482

8583
/**
86-
* Set the {@link LegacyTagImporterConfig} for the
84+
* Set the {@link TagImporterConfig} for the
8785
* {@link LegacyTagImporterFactory}.
8886
*
8987
* @param config
90-
* the {@link LegacyTagImporterConfig} to set.
88+
* the {@link TagImporterConfig} to set.
9189
* @return a <code>Reporter</code> instance for fluent programming
9290
*/
93-
public Reporter setLegacyTagImporterPathConfig(final LegacyTagImporterConfig config);
91+
public Reporter setLegacyTagImporterPathConfig(final TagImporterConfig config);
9492

9593
/**
9694
* Run a trace on the input files

src/main/java/org/itsallcode/openfasttrace/importer/ImporterContext.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
*/
2424
import java.util.Objects;
2525

26-
import org.itsallcode.openfasttrace.importer.legacytag.config.LegacyTagImporterConfig;
26+
import org.itsallcode.openfasttrace.importer.tag.config.TagImporterConfig;
2727

2828
/**
2929
* Common context shared by all {@link ImporterFactory}s. This allows importers
3030
* to access common infrastructure, e.g. the {@link ImporterService}.
3131
*/
3232
public class ImporterContext
3333
{
34-
private final LegacyTagImporterConfig tagImporterConfig;
34+
private final TagImporterConfig tagImporterConfig;
3535
private ImporterService importerService;
3636

3737
/**
@@ -40,7 +40,7 @@ public class ImporterContext
4040
* @param tagImporterConfig
4141
* importer specific configuration.
4242
*/
43-
public ImporterContext(final LegacyTagImporterConfig tagImporterConfig)
43+
public ImporterContext(final TagImporterConfig tagImporterConfig)
4444
{
4545
this.tagImporterConfig = tagImporterConfig;
4646
}
@@ -72,7 +72,7 @@ public ImporterService getImporterService()
7272
*
7373
* @return importer specific configuration.
7474
*/
75-
public LegacyTagImporterConfig getTagImporterConfig()
75+
public TagImporterConfig getTagImporterConfig()
7676
{
7777
return this.tagImporterConfig;
7878
}

src/main/java/org/itsallcode/openfasttrace/importer/LineReader.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void readLines(final LineConsumer consumer)
5050
while ((line = reader.readLine()) != null)
5151
{
5252
currentLineNumber = reader.getLineNumber();
53-
consumer.readLine(currentLineNumber, line);
53+
processLine(consumer, currentLineNumber, line);
5454
}
5555
}
5656
catch (final IOException exception)
@@ -60,6 +60,20 @@ public void readLines(final LineConsumer consumer)
6060
}
6161
}
6262

63+
private void processLine(final LineConsumer consumer, final int currentLineNumber,
64+
final String line)
65+
{
66+
try
67+
{
68+
consumer.readLine(currentLineNumber, line);
69+
}
70+
catch (final Exception e)
71+
{
72+
throw new ImporterException("Error processing line " + this.file.getPath() + ":"
73+
+ currentLineNumber + " (" + line + "): " + e.getMessage(), e);
74+
}
75+
}
76+
6377
@FunctionalInterface
6478
public interface LineConsumer
6579
{

src/main/java/org/itsallcode/openfasttrace/importer/legacytag/LegacyTagImporterFactory.java

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.itsallcode.openfasttrace.importer.tag;
2+
3+
/*-
4+
* #%L
5+
* OpenFastTrace
6+
* %%
7+
* Copyright (C) 2016 - 2018 itsallcode.org
8+
* %%
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public
20+
* License along with this program. If not, see
21+
* <http://www.gnu.org/licenses/gpl-3.0.html>.
22+
* #L%
23+
*/
24+
import java.util.List;
25+
26+
import org.itsallcode.openfasttrace.importer.LineReader.LineConsumer;
27+
28+
class DelegatingLineConsumer implements LineConsumer
29+
{
30+
private final List<LineConsumer> delegates;
31+
32+
DelegatingLineConsumer(final List<LineConsumer> delegates)
33+
{
34+
this.delegates = delegates;
35+
}
36+
37+
@Override
38+
public void readLine(final int lineNumber, final String line)
39+
{
40+
this.delegates.forEach(delegate -> delegate.readLine(lineNumber, line));
41+
}
42+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.itsallcode.openfasttrace.importer.tag;
2+
3+
/*-
4+
* #%L
5+
* OpenFastTrace
6+
* %%
7+
* Copyright (C) 2016 - 2018 itsallcode.org
8+
* %%
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public
20+
* License along with this program. If not, see
21+
* <http://www.gnu.org/licenses/gpl-3.0.html>.
22+
* #L%
23+
*/
24+
import java.util.logging.Logger;
25+
import java.util.regex.Matcher;
26+
27+
import org.itsallcode.openfasttrace.core.SpecificationItemId;
28+
import org.itsallcode.openfasttrace.importer.ChecksumCalculator;
29+
import org.itsallcode.openfasttrace.importer.ImportEventListener;
30+
import org.itsallcode.openfasttrace.importer.input.InputFile;
31+
32+
// [impl->dsn~import.full-coverage-tag~1]
33+
class LongTagImportingLineConsumer extends RegexLineConsumer
34+
{
35+
private static final Logger LOG = Logger
36+
.getLogger(LongTagImportingLineConsumer.class.getName());
37+
38+
private static final String COVERING_ARTIFACT_TYPE_PATTERN = "\\p{Alpha}+";
39+
private static final String TAG_PREFIX = "\\[";
40+
private static final String TAG_SUFFIX = "\\]";
41+
private static final String TAG_REGEX = TAG_PREFIX //
42+
+ "(" + COVERING_ARTIFACT_TYPE_PATTERN + ")" //
43+
+ "->" //
44+
+ "(" + SpecificationItemId.ID_PATTERN + ")" //
45+
+ TAG_SUFFIX;
46+
47+
private final InputFile file;
48+
private final ImportEventListener listener;
49+
50+
LongTagImportingLineConsumer(final InputFile file, final ImportEventListener listener)
51+
{
52+
super(TAG_REGEX);
53+
this.file = file;
54+
this.listener = listener;
55+
}
56+
57+
@Override
58+
public void processMatch(final Matcher matcher, final int lineNumber, final int lineMatchCount)
59+
{
60+
this.listener.beginSpecificationItem();
61+
this.listener.setLocation(this.file.getPath(), lineNumber);
62+
final SpecificationItemId coveredId = SpecificationItemId.parseId(matcher.group(2));
63+
final String generatedName = generateName(coveredId, lineNumber, lineMatchCount);
64+
final SpecificationItemId generatedId = SpecificationItemId.createId(matcher.group(1),
65+
generatedName, 0);
66+
67+
LOG.finest(() -> "File " + this.file + ":" + lineNumber + ": found '" + generatedId
68+
+ "' covering id '" + coveredId + "'");
69+
this.listener.setId(generatedId);
70+
this.listener.addCoveredId(coveredId);
71+
this.listener.endSpecificationItem();
72+
}
73+
74+
private String generateName(final SpecificationItemId coveredId, final int lineNumber,
75+
final int counter)
76+
{
77+
final String uniqueName = new StringBuilder() //
78+
.append(this.file.getPath()) //
79+
.append(lineNumber) //
80+
.append(counter) //
81+
.append(coveredId) //
82+
.toString();
83+
final String checksum = Long.toString(ChecksumCalculator.calculateCrc32(uniqueName));
84+
return coveredId.getName() + "-" + checksum;
85+
}
86+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.itsallcode.openfasttrace.importer.tag;
2+
3+
/*-
4+
* #%L
5+
* OpenFastTrace
6+
* %%
7+
* Copyright (C) 2016 - 2018 itsallcode.org
8+
* %%
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public
20+
* License along with this program. If not, see
21+
* <http://www.gnu.org/licenses/gpl-3.0.html>.
22+
* #L%
23+
*/
24+
import java.util.regex.Matcher;
25+
import java.util.regex.Pattern;
26+
27+
import org.itsallcode.openfasttrace.importer.LineReader.LineConsumer;
28+
29+
abstract class RegexLineConsumer implements LineConsumer
30+
{
31+
private final Pattern pattern;
32+
33+
RegexLineConsumer(final String patternRegex)
34+
{
35+
this(Pattern.compile(patternRegex));
36+
}
37+
38+
private RegexLineConsumer(final Pattern pattern)
39+
{
40+
this.pattern = pattern;
41+
}
42+
43+
@Override
44+
public void readLine(final int lineNumber, final String line)
45+
{
46+
final Matcher matcher = this.pattern.matcher(line);
47+
int counter = 0;
48+
while (matcher.find())
49+
{
50+
this.processMatch(matcher, lineNumber, counter);
51+
counter++;
52+
}
53+
}
54+
55+
abstract void processMatch(Matcher matcher, int lineNumber, int lineMatchCount);
56+
}

0 commit comments

Comments
 (0)