Add compact rendering

This commit is contained in:
Grahame Grieve 2023-03-06 20:45:21 +11:00
parent 6559c4b245
commit e449bf9cfe
7 changed files with 173 additions and 8 deletions

View File

@ -1,5 +1,7 @@
package org.hl7.fhir.validation.cli.renderers;
import java.io.File;
import org.hl7.fhir.r5.model.OperationOutcome;
import org.hl7.fhir.r5.utils.ToolingExtensions;
@ -21,4 +23,21 @@ public class CSVRenderer extends ValidationOutputRenderer {
}
}
@Override
public boolean isSingleFile() {
return true;
}
@Override
public String getStyleCode() {
return "csv";
}
@Override
public void setFolder(File dir) {
throw new Error("Not supported");
}
}

View File

@ -0,0 +1,71 @@
package org.hl7.fhir.validation.cli.renderers;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.hl7.fhir.r5.model.OperationOutcome;
import org.hl7.fhir.r5.utils.ToolingExtensions;
import org.hl7.fhir.utilities.Utilities;
public class CompactRenderer extends ValidationOutputRenderer {
private boolean split;
private File dir;
public CompactRenderer(boolean split) {
super();
this.split = split;
}
@Override
public void render(OperationOutcome op) throws IOException {
if (split) {
String file = Utilities.changeFileExt(tail(ToolingExtensions.readStringExtension(op, ToolingExtensions.EXT_OO_FILE)), ".txt");
PrintStream dstF = new PrintStream(new FileOutputStream(Utilities.path(dir.getAbsolutePath(), file)));
render(dstF, op);
dstF.close();
} else {
render(dst, op);
}
}
private void render(PrintStream d, OperationOutcome op) {
d.println(ToolingExtensions.readStringExtension(op, ToolingExtensions.EXT_OO_FILE));
List<String> lines = new ArrayList<>();
for (OperationOutcome.OperationOutcomeIssueComponent issue : op.getIssue()) {
String path = issue.hasExpression() ? issue.getExpression().get(0).asStringValue() : "n/a";
int line = ToolingExtensions.readIntegerExtension(issue, ToolingExtensions.EXT_ISSUE_LINE, -1);
int col = ToolingExtensions.readIntegerExtension(issue, ToolingExtensions.EXT_ISSUE_COL, -1);
lines.add("["+Integer.toString(line) + ", " + Integer.toString(col)+"] "+path+": "+issue.getSeverity().getDisplay()+" - "+issue.getDetails().getText());
}
Collections.sort(lines);
for (String s : lines) {
d.println(s);
}
}
private String tail(String n) {
return n.contains(File.separator) ? n.substring(n.lastIndexOf(File.separator)+1) : n;
}
@Override
public boolean isSingleFile() {
return !split;
}
@Override
public String getStyleCode() {
return split ? "compact-split" : "compact";
}
@Override
public void setFolder(File dir) {
this.dir = dir;
}
}

View File

@ -1,5 +1,7 @@
package org.hl7.fhir.validation.cli.renderers;
import java.io.File;
import org.hl7.fhir.r5.model.OperationOutcome;
import org.hl7.fhir.r5.model.OperationOutcome.IssueSeverity;
import org.hl7.fhir.r5.utils.ToolingExtensions;
@ -69,4 +71,21 @@ public class DefaultRenderer extends ValidationOutputRenderer {
return " " + issue.getSeverity().getDisplay() + loc + ": " + issue.getDetails().getText();
}
@Override
public boolean isSingleFile() {
return true;
}
@Override
public String getStyleCode() {
return "(default)";
}
@Override
public void setFolder(File dir) {
throw new Error("Not supported");
}
}

View File

@ -1,5 +1,7 @@
package org.hl7.fhir.validation.cli.renderers;
import java.io.File;
import org.hl7.fhir.r5.model.OperationOutcome;
import org.hl7.fhir.r5.utils.ToolingExtensions;
@ -15,4 +17,21 @@ public class ESLintCompactRenderer extends ValidationOutputRenderer {
}
}
@Override
public boolean isSingleFile() {
return true;
}
@Override
public String getStyleCode() {
return "eslint-compact";
}
@Override
public void setFolder(File dir) {
throw new Error("Not supported");
}
}

View File

@ -1,5 +1,6 @@
package org.hl7.fhir.validation.cli.renderers;
import java.io.File;
import java.io.IOException;
import org.hl7.fhir.r5.elementmodel.Manager.FhirFormat;
@ -45,4 +46,20 @@ public class NativeRenderer extends ValidationOutputRenderer {
x.compose(dst, bundle);
}
@Override
public boolean isSingleFile() {
return true;
}
@Override
public String getStyleCode() {
return format.toString().toLowerCase();
}
@Override
public void setFolder(File dir) {
throw new Error("Not supported");
}
}

View File

@ -1,5 +1,6 @@
package org.hl7.fhir.validation.cli.renderers;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
@ -40,4 +41,10 @@ public abstract class ValidationOutputRenderer {
public boolean handlesBundleDirectly() {
return false;
}
public abstract boolean isSingleFile();
public abstract String getStyleCode();
public abstract void setFolder(File dir);
}

View File

@ -1,5 +1,6 @@
package org.hl7.fhir.validation.cli.services;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.management.ManagementFactory;
@ -47,6 +48,7 @@ import org.hl7.fhir.validation.cli.model.ValidationOutcome;
import org.hl7.fhir.validation.cli.model.ValidationRequest;
import org.hl7.fhir.validation.cli.model.ValidationResponse;
import org.hl7.fhir.validation.cli.renderers.CSVRenderer;
import org.hl7.fhir.validation.cli.renderers.CompactRenderer;
import org.hl7.fhir.validation.cli.renderers.DefaultRenderer;
import org.hl7.fhir.validation.cli.renderers.ESLintCompactRenderer;
import org.hl7.fhir.validation.cli.renderers.NativeRenderer;
@ -119,15 +121,22 @@ public class ValidationService {
System.out.println();
PrintStream dst = null;
if (cliContext.getOutput() == null) {
dst = System.out;
} else {
dst = new PrintStream(new FileOutputStream(cliContext.getOutput()));
}
ValidationOutputRenderer renderer = makeValidationOutputRenderer(cliContext);
renderer.setOutput(dst);
renderer.setCrumbTrails(validator.isCrumbTrails());
if (renderer.isSingleFile()) {
if (cliContext.getOutput() == null) {
dst = System.out;
} else {
dst = new PrintStream(new FileOutputStream(cliContext.getOutput()));
}
renderer.setOutput(dst);
} else {
File dir = new File(cliContext.getOutput());
if (!dir.isDirectory()) {
throw new Error("THe output location "+dir.getAbsolutePath()+" must be a directory for the output style "+renderer.getStyleCode());
}
renderer.setFolder(dir);
}
int ec = 0;
@ -154,7 +163,7 @@ public class ValidationService {
renderer.finish();
}
if (cliContext.getOutput() != null) {
if (cliContext.getOutput() != null && dst != null) {
dst.close();
}
@ -190,6 +199,10 @@ public class ValidationService {
}
} else if (Utilities.existsInList(style, "eslint-compact")) {
return new ESLintCompactRenderer();
} else if (Utilities.existsInList(style, "compact-split")) {
return new CompactRenderer(true);
} else if (Utilities.existsInList(style, "compact")) {
return new CompactRenderer(false);
} else if (Utilities.existsInList(style, "csv")) {
return new CSVRenderer();
} else if (Utilities.existsInList(style, "xml")) {