Skip to content

Make ContinuationLineTransformation stateless to simplify DI #24 #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2020
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 @@ -98,7 +98,7 @@ private ResultWithErrors<List<CobolLine>> readLines(

private ResultWithErrors<List<CobolLine>> transformLines(
List<CobolLine> lines, String documentURI) {
return createContinuationLineProcessor(documentURI).transformLines(lines);
return createContinuationLineProcessor().transformLines(documentURI, lines);
}

/**
Expand Down Expand Up @@ -133,8 +133,8 @@ private CobolLineIndicatorProcessor createLineIndicatorProcessor() {
return new CobolLineIndicatorProcessorImpl();
}

private CobolLinesTransformation createContinuationLineProcessor(String documentURI) {
return new ContinuationLineTransformation(documentURI);
private CobolLinesTransformation createContinuationLineProcessor() {
return new ContinuationLineTransformation();
}

private CobolLineReader createLineReader(String documentURI) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
*/
public interface CobolLinesTransformation {

ResultWithErrors<List<CobolLine>> transformLines(List<CobolLine> lines);
ResultWithErrors<List<CobolLine>> transformLines(String documentURI, List<CobolLine> lines);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,17 @@ public class ContinuationLineTransformation implements CobolLinesTransformation
private static final Pattern CONTINUATION_LINE_PATTERN =
Pattern.compile(CONT_LINE_NO_AREA_A_REGEX);

private String documentURI;

public ContinuationLineTransformation(String documentURI) {
this.documentURI = documentURI;
}

@Override
public ResultWithErrors<List<CobolLine>> transformLines(List<CobolLine> lines) {
public ResultWithErrors<List<CobolLine>> transformLines(
String documentURI, List<CobolLine> lines) {
List<CobolLine> result = new ArrayList<>();
List<SyntaxError> errors = new ArrayList<>();
CobolLine previousLine = null;
for (int i = 0; i < lines.size(); i++) {
CobolLine cobolLine = lines.get(i);

Optional.ofNullable(checkContinuationLine(i, cobolLine)).ifPresent(errors::add);
Optional.ofNullable(checkIfStringClosedCorrectly(previousLine, i, cobolLine))
Optional.ofNullable(checkContinuationLine(documentURI, i, cobolLine)).ifPresent(errors::add);
Optional.ofNullable(checkIfStringClosedCorrectly(previousLine, documentURI, i, cobolLine))
.ifPresent(errors::add);

previousLine = cobolLine;
Expand All @@ -71,13 +66,13 @@ public ResultWithErrors<List<CobolLine>> transformLines(List<CobolLine> lines) {
/**
* Check if line is a continuation one and performs pattern check
*
* @return
* @return a SyntaxError if there is a continuation line error or null if not
*/
private SyntaxError checkContinuationLine(int lineNumber, CobolLine cobolLine) {
private SyntaxError checkContinuationLine(String uri, int lineNumber, CobolLine cobolLine) {
if (CobolLineTypeEnum.CONTINUATION.equals(cobolLine.getType())) {

// invoke method for noContentInAreaA
return checkContentAreaAWithContinuationLine(cobolLine, lineNumber);
return checkContentAreaAWithContinuationLine(cobolLine, uri, lineNumber);
}
return null;
}
Expand All @@ -87,14 +82,16 @@ private SyntaxError checkContinuationLine(int lineNumber, CobolLine cobolLine) {
* the content area A (7-11) should be blank.
*
* @param cobolLine line - a line to be processed
* @param lineNumber lineNumber
* @param uri - URI of the processing document
* @param lineNumber - number of the currently processing line
* @return error if found or null
*/
private SyntaxError checkContentAreaAWithContinuationLine(CobolLine cobolLine, int lineNumber) {
private SyntaxError checkContentAreaAWithContinuationLine(
CobolLine cobolLine, String uri, int lineNumber) {
String line = cobolLine.toString();
Matcher continuationLineMatcher = CONTINUATION_LINE_PATTERN.matcher(line);
if (!continuationLineMatcher.matches()) {
return registerContinuationLineError(lineNumber, countAreaASpaces(line));
return registerContinuationLineError(uri, lineNumber, countAreaASpaces(line));
}
return null;
}
Expand All @@ -111,11 +108,12 @@ private int countAreaASpaces(String line) {
* doesn't end correctly.
*/
private SyntaxError checkIfStringClosedCorrectly(
CobolLine lastCobolLine, int lineNumber, CobolLine currentCobolLine) {
if (checkIfLineHasUnclosedString(lastCobolLine)
CobolLine previousCobolLine, String uri, int lineNumber, CobolLine currentCobolLine) {
if (checkIfLineHasUnclosedString(previousCobolLine)
&& !CobolLineTypeEnum.CONTINUATION.equals(currentCobolLine.getType())) {
// there is a string not closed correctly - I'll raise an error
return registerStringClosingError(lineNumber, getCobolLineTrimmedLength(lastCobolLine));
return registerStringClosingError(
uri, lineNumber, getCobolLineTrimmedLength(previousCobolLine));
}
return null;
}
Expand Down Expand Up @@ -152,7 +150,7 @@ private boolean isStringMatchOdd(String cobolLineToCheck, String substring) {
* The syntax error should be thrown in the content area B, so it is necessary to remove the
* comment area from the Cobol line
*
* @param lastCobolLine
* @param lastCobolLine - CobolLine that was processed before the current one
* @return content length without spaces
*/
private int getCobolLineTrimmedLength(CobolLine lastCobolLine) {
Expand All @@ -165,11 +163,12 @@ private int getCobolLineTrimmedLength(CobolLine lastCobolLine) {
- 1;
}

private SyntaxError registerStringClosingError(int lineNumber, int cobolLineTrimmedLength) {
private SyntaxError registerStringClosingError(
String uri, int lineNumber, int cobolLineTrimmedLength) {
return SyntaxError.syntaxError()
.position(
new Position(
documentURI,
uri,
cobolLineTrimmedLength,
cobolLineTrimmedLength,
lineNumber,
Expand All @@ -179,13 +178,11 @@ private SyntaxError registerStringClosingError(int lineNumber, int cobolLineTrim
.build();
}

private SyntaxError registerContinuationLineError(int lineNumber, int countingSpace) {

private SyntaxError registerContinuationLineError(String uri, int lineNumber, int countingSpace) {
int startPosition = END_INDEX_CONTENT_AREA_A - (START_INDEX_AREA_A - countingSpace) + 1;
return SyntaxError.syntaxError()
.position(
new Position(
documentURI, startPosition, END_INDEX_CONTENT_AREA_A, lineNumber, startPosition))
new Position(uri, startPosition, END_INDEX_CONTENT_AREA_A, lineNumber, startPosition))
.suggestion("A continuation line cannot contain values in the Content Area A")
.severity(1)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public void testQuotesInsideStringNotCauseError() {
private List<SyntaxError> runTransformation(String text) {
List<CobolLine> lines = convertToCobolLines(text);

ContinuationLineTransformation transformation = new ContinuationLineTransformation(null);
return transformation.transformLines(lines).getErrors();
ContinuationLineTransformation transformation = new ContinuationLineTransformation();
return transformation.transformLines(null, lines).getErrors();
}

private List<CobolLine> convertToCobolLines(String text) {
Expand Down