Skip to content

Commit

Permalink
Add helper method to build platform independent file paths
Browse files Browse the repository at this point in the history
-This uses FilenameUtils.concat for underlying path concatenation
  • Loading branch information
Chris0296 committed Oct 6, 2023
1 parent bd9ac88 commit d0704c0
Show file tree
Hide file tree
Showing 27 changed files with 117 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.hl7.fhir.r4.model.UsageContext;
import org.opencds.cqf.tooling.Operation;
import org.opencds.cqf.tooling.terminology.SpreadsheetHelper;
import org.opencds.cqf.tooling.utilities.IOUtils;

import ca.uhn.fhir.context.FhirContext;

Expand Down Expand Up @@ -608,7 +609,7 @@ private void writeLibraryHeader(StringBuilder cql, Library library) {

private void writeLibraries(String outputPath) {
if (libraries != null && libraries.size() > 0) {
String outputFilePath = outputPath + File.separator + "input" + File.separator + "resources" + File.separator + "library";
String outputFilePath = IOUtils.concatFilePath(outputPath, "input", "resources", "library");
ensurePath(outputFilePath);

for (Library library : libraries.values()) {
Expand All @@ -620,8 +621,9 @@ private void writeLibraries(String outputPath) {
private void writeLibraryCQL(String outputPath) {
if (libraryCQL != null && libraryCQL.size() > 0) {
for (Map.Entry<String, StringBuilder> entry : libraryCQL.entrySet()) {
String outputDirectoryPath = outputPath + File.separator + "input" + File.separator + "cql";
String outputFilePath = outputDirectoryPath + File.separator + entry.getKey() + ".cql";
String outputDirectoryPath = IOUtils.concatFilePath(outputPath, "input", "cql");
String outputFilePath = IOUtils.concatFilePath(outputDirectoryPath,
entry.getKey() + ".cql");
ensurePath(outputDirectoryPath);

try (FileOutputStream writer = new FileOutputStream(outputFilePath)) {
Expand All @@ -639,7 +641,8 @@ private void writeLibraryCQL(String outputPath) {
private void writePlanDefinitions(String outputPath) {
if (planDefinitions != null && planDefinitions.size() > 0) {
for (PlanDefinition planDefinition : planDefinitions.values()) {
String outputFilePath = outputPath + File.separator + "input" + File.separator + "resources" + File.separator + "plandefinition";
String outputFilePath = IOUtils.concatFilePath(outputPath,
"input", "resources", "plandefinition");
ensurePath(outputFilePath);
writeResource(outputFilePath, planDefinition);
}
Expand All @@ -648,7 +651,8 @@ private void writePlanDefinitions(String outputPath) {

/* Write Methods */
public void writeResource(String path, Resource resource) {
String outputFilePath = path + File.separator + resource.getResourceType().toString().toLowerCase() + "-" + resource.getIdElement().getIdPart() + "." + encoding;
String outputFilePath = IOUtils.concatFilePath(path,
resource.getResourceType().toString().toLowerCase() + "-" + resource.getIdElement().getIdPart() + "." + encoding);
try (FileOutputStream writer = new FileOutputStream(outputFilePath)) {
writer.write(
encoding.equals("json")
Expand Down Expand Up @@ -681,7 +685,8 @@ private String buildPlanDefinitionIndex() {
}

public void writePlanDefinitionIndex(String outputPath) {
String outputFilePath = outputPath + File.separator + "input" + File.separator + "pagecontent"+ File.separator + "PlanDefinitionIndex.md";
String outputFilePath = IOUtils.concatFilePath(outputPath,
"input", "pagecontent", "PlanDefinitionIndex.md");
ensurePath(outputFilePath);

try (FileOutputStream writer = new FileOutputStream(outputFilePath)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.hl7.fhir.r4.model.ValueSet;
import org.opencds.cqf.tooling.Operation;
import org.opencds.cqf.tooling.terminology.SpreadsheetHelper;
import org.opencds.cqf.tooling.utilities.IOUtils;

import ca.uhn.fhir.context.FhirContext;

Expand Down Expand Up @@ -2574,7 +2575,8 @@ public void processExamples() {

/* Write Methods */
public void writeResource(String path, Resource resource) {
String outputFilePath = path + "/" + resource.getResourceType().toString().toLowerCase() + "-" + resource.getIdElement().getIdPart() + "." + encoding;
String outputFilePath = IOUtils.concatFilePath(path,
resource.getResourceType().toString().toLowerCase() + "-" + resource.getIdElement().getIdPart() + "." + encoding);
try (FileOutputStream writer = new FileOutputStream(outputFilePath)) {
writer.write(
encoding.equals("json")
Expand Down Expand Up @@ -2976,7 +2978,8 @@ public void writeConcepts(String scope, String scopePath) {
}

ensureCqlPath(scopePath);
try (FileOutputStream writer = new FileOutputStream(getCqlPath(scopePath) + "/" + scope + "Concepts.cql")) {
try (FileOutputStream writer = new FileOutputStream(
IOUtils.concatFilePath(getCqlPath(scopePath),scope + "Concepts.cql"))) {
writer.write(sb.toString().getBytes());
writer.flush();
}
Expand Down Expand Up @@ -3420,7 +3423,8 @@ public void writeDataElements(String scope, String scopePath, String context) {
}

ensureCqlPath(scopePath);
try (FileOutputStream writer = new FileOutputStream(getCqlPath(scopePath) + "/" + scope + (context.equals("Encounter") ? "Contact" : "") + "DataElements.cql")) {
try (FileOutputStream writer = new FileOutputStream(IOUtils.concatFilePath(getCqlPath(scopePath),
scope + (context.equals("Encounter") ? "Contact" : "") + "DataElements.cql"))) {
writer.write(sb.toString().getBytes());
writer.flush();
}
Expand All @@ -3429,7 +3433,8 @@ public void writeDataElements(String scope, String scopePath, String context) {
throw new IllegalArgumentException("Error writing concepts library source");
}

try (FileOutputStream writer = new FileOutputStream(getCqlPath(scopePath) + "/" + scope + "DataElementsByActivity.md")) {
try (FileOutputStream writer = new FileOutputStream(
IOUtils.concatFilePath(getCqlPath(scopePath), scope + "DataElementsByActivity.md"))) {
writer.write(activityIndex.toString().getBytes());
writer.flush();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import org.opencds.cqf.tooling.utilities.IOUtils;
import org.opencds.cqf.tooling.utilities.LogUtils;

import java.io.File;
import java.io.IOException;
import java.util.Objects;


public abstract class BaseCqfmSoftwareSystemHelper {
public static final String separator = System.getProperty("file.separator");
private String rootDir;
protected String getRootDir() {
return this.rootDir;
Expand All @@ -18,7 +18,8 @@ protected String getRootDir() {
private static String cqfToolingDeviceName = "cqf-tooling";
// private static String cqfToolingDeviceReferenceID = "#" + cqfToolingDeviceName;
private static String cqfmSoftwareSystemExtensionUrl = "http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-softwaresystem";
protected static String devicePath = separator + "input" + separator + "resources" + separator + "device";
protected static String devicePath = IOUtils.concatFilePath(File.separator,
"input", "resources", "device");

protected String getCqfToolingDeviceName() { return cqfToolingDeviceName; }
// protected String getCqfToolingDeviceReferenceID() { return cqfToolingDeviceReferenceID; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static String loadResourceAlsoFromJar(String name) throws IOException {
String cpName = name.substring("classpath:".length());
try (InputStream resource = Thread.currentThread().getContextClassLoader().getResourceAsStream(cpName)) {
if (resource == null) {
try (InputStream resource2 =Thread.currentThread().getContextClassLoader().getResourceAsStream("/" + cpName)) {
try (InputStream resource2 =Thread.currentThread().getContextClassLoader().getResourceAsStream(File.separator + cpName)) {
if (resource2 == null) {
throw new IOException("Can not find '" + cpName + "' on classpath");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.opencds.cqf.tooling.cql_generation.drool.visitor.DroolToElmVisitor.CQLTYPES;
import org.opencds.cqf.tooling.cql_generation.drool.visitor.ElmToCqlVisitor;
import org.opencds.cqf.tooling.cql_generation.drool.visitor.Visitor;
import org.opencds.cqf.tooling.utilities.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -115,7 +116,8 @@ private void writeCql(ElmContext context, File outpuDirectory) {
VersionedIdentifier vi = new VersionedIdentifier();
vi.setId(getIdFromSource(cql));
vi.setVersion(getVersionFromSource(cql));
File outputFile = new File(outpuDirectory.getAbsolutePath() + "/" + vi.getId() + "-" + vi.getVersion() + ".cql");
File outputFile = new File(IOUtils.concatFilePath(outpuDirectory.getAbsolutePath(),
vi.getId() + "-" + vi.getVersion() + ".cql"));
IOUtil.writeToFile(outputFile, cql);
} else {
throw new IllegalArgumentException("Output directory is not a directory: " + outpuDirectory.getAbsolutePath());
Expand All @@ -130,7 +132,8 @@ private void writeElm(ElmContext context, VmrToModelElmBuilder modelBuilder, Fil
try {
String elm = serializer.convertToXml(modelBuilder.of.createLibrary(entry.getValue()), serializer.getJaxbContext());
if (outpuDirectory.isDirectory()) {
File outputFile = new File(outpuDirectory.getAbsolutePath() + "/" + entry.getKey() + ".xml");
File outputFile = new File(IOUtils.concatFilePath(outpuDirectory.getAbsolutePath(),
entry.getKey() + ".xml"));
IOUtil.writeToFile(outputFile, elm);
} else {
throw new IllegalArgumentException("Output directory is not a directory: " + outpuDirectory.getAbsolutePath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.cdsframework.dto.OpenCdsConceptDTO;
import org.opencds.cqf.tooling.cql_generation.IOUtil;
import org.opencds.cqf.tooling.cql_generation.context.ElmContext;
import org.opencds.cqf.tooling.utilities.IOUtils;

/**
* Generates html files from an RCKMS Drool Object graph.
Expand Down Expand Up @@ -114,8 +115,7 @@ public void visit(ConditionDTO conditionDTO) {
@Override
public ElmContext visit(List<ConditionDTO> rootNode) {
htmlStrings.entrySet().stream().forEach(entry -> {
// Dont like this concat
String filePath = outputDirectoryPath.concat("/" + entry.getKey() + ".html");
String filePath = IOUtils.concatFilePath(outputDirectoryPath, entry.getKey() + ".html");
File file = new File(filePath);
String content = "<html><head><title>" + entry.getKey() + "</title></head><body><p>" + entry.getValue()
+ "</p></body></html>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

public class DataDateRollerOperation extends Operation {
private FhirContext fhirContext;
public static final String separator = System.getProperty("file.separator");
public String fhirVersion;
private IOUtils.Encoding fileEncoding;
private Logger logger = LoggerFactory.getLogger(this.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import org.opencds.cqf.tooling.parameter.FileFhirPlatformParameters;
import org.opencds.cqf.tooling.utilities.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -46,7 +47,7 @@ public void create(IBaseResource resource){
if (resourceTypeDefined(resource)){

//ensure resource type directory exists
String typePath = this.resourceDir + "/" + resource.getIdElement().getResourceType();
String typePath = IOUtils.concatFilePath(this.resourceDir, resource.getIdElement().getResourceType());
String path = getPath(resource);

File typeDir = new File(typePath);
Expand Down Expand Up @@ -129,11 +130,13 @@ private void writeResource(String path, IBaseResource resource){
}

private String getPath(IBaseResource resource){
return this.resourceDir + "/" + resource.getIdElement().getResourceType() + "/" + resource.getIdElement().getIdPart() + "." + this.encoding.toString();
return IOUtils.concatFilePath(this.resourceDir, resource.getIdElement().getResourceType(),
resource.getIdElement().getIdPart() + "." + this.encoding.toString());
}

private String getPath(IIdType id){
return this.resourceDir + "/" + id.getResourceType() + "/" + id.getIdPart() + "." + this.encoding.toString();
return IOUtils.concatFilePath(this.resourceDir, id.getResourceType(),
id.getIdPart() + "." + this.encoding.toString());
}

private boolean resourceTypeDefined(IBaseResource resource){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.opencds.cqf.tooling.modelinfo.quick.QuickModelInfoBuilder;
import org.opencds.cqf.tooling.modelinfo.uscore.USCoreClassInfoBuilder;
import org.opencds.cqf.tooling.modelinfo.uscore.USCoreModelInfoBuilder;
import org.opencds.cqf.tooling.utilities.IOUtils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
Expand Down Expand Up @@ -162,7 +163,8 @@ public void execute(String[] args) {
Map<String, TypeInfo> typeInfos = ciBuilder.build();
ciBuilder.afterBuild();

String fhirHelpersPath = this.getOutputPath() + "/" + modelName + "Helpers-" + modelVersion + ".cql";
String fhirHelpersPath = IOUtils.concatFilePath(this.getOutputPath(),
modelName + "Helpers-" + modelVersion + ".cql");
miBuilder = new FHIRModelInfoBuilder(modelVersion, typeInfos, atlas, fhirHelpersPath);
mi = miBuilder.build();
}
Expand All @@ -173,7 +175,8 @@ else if (modelName.equals("USCore")) {
Map<String, TypeInfo> typeInfos = ciBuilder.build();
ciBuilder.afterBuild();

String helpersPath = this.getOutputPath() + "/" + modelName + "Helpers-" + modelVersion + ".cql";
String helpersPath = IOUtils.concatFilePath(this.getOutputPath(),
modelName + "Helpers-" + modelVersion + ".cql");
miBuilder = new USCoreModelInfoBuilder(modelVersion, typeInfos, atlas, helpersPath);
mi = miBuilder.build();
}
Expand All @@ -184,7 +187,8 @@ else if (modelName.equals("QICore")) {
Map<String, TypeInfo> typeInfos = ciBuilder.build();
ciBuilder.afterBuild();

String helpersPath = this.getOutputPath() + "/" + modelName + "Helpers-" + modelVersion + ".cql";
String helpersPath = IOUtils.concatFilePath(this.getOutputPath(),
modelName + "Helpers-" + modelVersion + ".cql");
miBuilder = new QICoreModelInfoBuilder(modelVersion, typeInfos, atlas, helpersPath);
mi = miBuilder.build();
}
Expand Down Expand Up @@ -235,7 +239,7 @@ else if (modelName.equals("QUICK")) {
}

private void writeOutput(String fileName, String content) throws IOException {
try (FileOutputStream writer = new FileOutputStream(getOutputPath() + "/" + fileName)) {
try (FileOutputStream writer = new FileOutputStream(IOUtils.concatFilePath(getOutputPath(), fileName))) {
writer.write(content.getBytes());
writer.flush();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.hl7.fhir.instance.model.api.IBaseResource;
import org.opencds.cqf.tooling.Operation;
import org.opencds.cqf.tooling.utilities.IOUtils;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
Expand Down Expand Up @@ -192,7 +193,7 @@ else if (resource.getPath().endsWith(".json")) {
public void output(IBaseResource resource, FhirContext context) {
String fileNameBase = getOutputPath() + getOutputPath().substring(getOutputPath().lastIndexOf(File.separator));
if (bundleId != null && !bundleId.isEmpty()) {
fileNameBase = getOutputPath() + File.separator + bundleId;
fileNameBase = IOUtils.concatFilePath(getOutputPath(), bundleId);
}

try (FileOutputStream writer = new FileOutputStream(fileNameBase + "-bundle." + encoding)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.hl7.fhir.instance.model.api.IBaseResource;
import org.opencds.cqf.tooling.Operation;
import org.opencds.cqf.tooling.utilities.IOUtils;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
Expand Down Expand Up @@ -163,7 +164,9 @@ else if (resource.getPath().endsWith(".json")) {

// Output
public void output(IBaseResource resource, FhirContext context) {
try (FileOutputStream writer = new FileOutputStream(getOutputPath() + "/" + resource.getIdElement().getResourceType() + "-" + resource.getIdElement().getIdPart() + "." + encoding)) {
try (FileOutputStream writer = new FileOutputStream(
IOUtils.concatFilePath(getOutputPath(),
resource.getIdElement().getResourceType() + "-" + resource.getIdElement().getIdPart() + "." + encoding))) {
writer.write(
encoding.equals("json")
? context.newJsonParser().setPrettyPrint(true).encodeResourceToString(resource).getBytes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.hl7.fhir.dstu3.model.Resource;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.opencds.cqf.tooling.Operation;
import org.opencds.cqf.tooling.utilities.IOUtils;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.IParser;
Expand Down Expand Up @@ -157,12 +158,12 @@ private void setResourcePaths(final JsonObject igControl) {
if (resources.isJsonArray()) {
for (final JsonElement path : resources.getAsJsonArray()) {
if (path.isJsonPrimitive()) {
resourcePaths.add(pathToIg + "/" + path.getAsString());
resourcePaths.add(IOUtils.concatFilePath(pathToIg, path.getAsString()));
outputBundles.put(path.getAsString(), null);
}
}
} else {
resourcePaths.add(pathToIg + "/" + resources.getAsString());
resourcePaths.add(IOUtils.concatFilePath(pathToIg, resources.getAsString()));
}
}
}
Expand Down Expand Up @@ -247,7 +248,7 @@ private void addArtifactToBundle(final Path path, final Bundle bundle) {
private void outputBundles() {
for (final Map.Entry<String, IBaseResource> set : outputBundles.entrySet()) {
try (FileOutputStream writer = new FileOutputStream(
getOutputPath() + "/" + set.getKey() + "." + encoding)) {
IOUtils.concatFilePath(getOutputPath(), set.getKey() + "." + encoding))) {
writer.write(encoding.equals("json")
? jsonParser.setPrettyPrint(true).encodeResourceToString(set.getValue()).getBytes()
: xmlParser.setPrettyPrint(true).encodeResourceToString(set.getValue()).getBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.opencds.cqf.tooling.acceleratorkit.StructureDefinitionBindingObject;
import org.opencds.cqf.tooling.acceleratorkit.StructureDefinitionElementBindingVisitor;
import org.opencds.cqf.tooling.terminology.SpreadsheetCreatorHelper;
import org.opencds.cqf.tooling.utilities.IOUtils;
import org.opencds.cqf.tooling.utilities.ModelCanonicalAtlasCreator;


Expand Down Expand Up @@ -83,7 +84,8 @@ private void createOutput(List<StructureDefinitionBindingObject> bindingObjects)
bindingObjects.forEach((bindingObject) -> {
addBindingObjectRowDataToCurrentSheet(firstSheet, rowCount.getAndAccumulate(1, ibo), bindingObject);
});
SpreadsheetCreatorHelper.writeSpreadSheet(workBook, getOutputPath() + separator + modelName + modelVersion + ".xlsx");
SpreadsheetCreatorHelper.writeSpreadSheet(workBook,
IOUtils.concatFilePath(getOutputPath(), modelName + modelVersion + ".xlsx"));
}

private List<String> createHeaderNameList() {
Expand Down
Loading

0 comments on commit d0704c0

Please sign in to comment.