-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Capture current file being converted and used if logRecord.cursor is …
…null
- Loading branch information
1 parent
2bf959a
commit 7c801c0
Showing
7 changed files
with
226 additions
and
54 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
asciidoctor-maven-commons/src/main/java/org/asciidoctor/maven/log/CapturedLogRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.asciidoctor.maven.log; | ||
|
||
import java.io.File; | ||
import java.util.Optional; | ||
|
||
import org.asciidoctor.ast.Cursor; | ||
import org.asciidoctor.log.LogRecord; | ||
|
||
/** | ||
* {@link LogRecord} proxy that allows capturing the source file being | ||
* processed. | ||
* Important: the {@link #sourceFile} and the actual source where an error is present | ||
* may not be the same. For example if the source is being included. | ||
* | ||
* @since 3.1.2 | ||
*/ | ||
final class CapturedLogRecord extends LogRecord { | ||
|
||
private final File sourceFile; | ||
|
||
CapturedLogRecord(LogRecord record, File sourceFile) { | ||
super(record.getSeverity(), record.getCursor(), record.getMessage(), record.getSourceFileName(), record.getSourceMethodName()); | ||
this.sourceFile = sourceFile; | ||
} | ||
|
||
public Cursor getCursor() { | ||
if (sourceFile == null) | ||
return null; | ||
|
||
return Optional.ofNullable(super.getCursor()) | ||
.orElse(new FileCursor(sourceFile)); | ||
} | ||
|
||
public File getSourceFile() { | ||
return sourceFile; | ||
} | ||
|
||
class FileCursor implements Cursor { | ||
|
||
private final File file; | ||
|
||
public FileCursor(File file) { | ||
this.file = file; | ||
} | ||
|
||
@Override | ||
public int getLineNumber() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public String getPath() { | ||
return file.getName(); | ||
} | ||
|
||
@Override | ||
public String getDir() { | ||
return file.getParent(); | ||
} | ||
|
||
@Override | ||
public String getFile() { | ||
return file.getAbsolutePath(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
asciidoctor-maven-commons/src/test/java/org/asciidoctor/maven/log/TestLogRecords.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.asciidoctor.maven.log; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.asciidoctor.log.Severity.*; | ||
|
||
import org.asciidoctor.log.LogRecord; | ||
|
||
class TestLogRecords { | ||
|
||
static LogRecord errorMessage() { | ||
return errorMessage(null); | ||
} | ||
|
||
static LogRecord errorMessage(Integer index) { | ||
LogRecord logRecord = new LogRecord(ERROR, buildMessage("error", index)); | ||
return new CapturedLogRecord(logRecord, null); | ||
} | ||
|
||
static LogRecord getInfoMessage() { | ||
return getInfoMessage(null); | ||
} | ||
|
||
static LogRecord getInfoMessage(Integer index) { | ||
LogRecord logRecord = new LogRecord(INFO, buildMessage("info", index)); | ||
return new CapturedLogRecord(logRecord, null); | ||
} | ||
|
||
static LogRecord warningMessage() { | ||
return warningMessage(null); | ||
} | ||
|
||
static LogRecord warningMessage(Integer index) { | ||
LogRecord logRecord = new LogRecord(WARN, buildMessage("warning", index)); | ||
return new CapturedLogRecord(logRecord, null); | ||
} | ||
|
||
private static String buildMessage(String type, Integer index) { | ||
return Optional.ofNullable(index) | ||
.map(i -> type + " message " + index) | ||
.orElse(type + " message"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.