Skip to content

Commit

Permalink
Add support for properties file encoding detection
Browse files Browse the repository at this point in the history
Bump up release version
  • Loading branch information
ndegwamartin committed Jul 29, 2024
1 parent 828fd2c commit f2d5404
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
3 changes: 2 additions & 1 deletion efsity/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repositories {

group = "org.smartregister"

version = "2.3.7-SNAPSHOT"
version = "2.3.8-SNAPSHOT"

description = "fhircore-tooling (efsity)"

Expand Down Expand Up @@ -82,6 +82,7 @@ dependencies {
implementation(deps.jsonschemafriend)
implementation(deps.picocli)
implementation(deps.xstream)
implementation(deps.icu4j)

testImplementation(kotlin("test"))
testImplementation("junit:junit:4.13.2")
Expand Down
2 changes: 2 additions & 0 deletions efsity/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ opencds-cql-version="2.4.0"
project-build-sourceEncoding="UTF-8"
spotless-version ="6.20.0"
xstream="1.4.20"
icu4j-version = "75.1"

[libraries]
caffeine = { module = "com.github.ben-manes.caffeine:caffeine", version.ref = "caffeine-version" }
Expand Down Expand Up @@ -44,6 +45,7 @@ jackson-core = { module = "com.fasterxml.jackson.core:jackson-core", version.ref
jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson-version" }
picocli = { module = "info.picocli:picocli", version.ref = "info-picocli-version" }
xstream = { module = "com.thoughtworks.xstream:xstream", version.ref = "xstream" }
icu4j = { module="com.ibm.icu:icu4j", version.ref = "icu4j-version" }

[bundles]
cqf-cql = ["cql-to-elm","elm","elm-jackson","model","model-jackson"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ public void run() {
long start = System.currentTimeMillis();
if (propertiesFile != null && !propertiesFile.isBlank()) {

Properties properties = FctUtils.readPropertiesFile(propertiesFile);
Properties properties = null;
try {
properties = FctUtils.readPropertiesFile(propertiesFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
setProperties(properties);
}
try {
Expand All @@ -121,18 +126,21 @@ public void run() {
}

void setProperties(Properties properties) {
if (properties == null)
throw new IllegalStateException("Properties file is missing or could not be parsed");

if (projectFolder == null || projectFolder.isBlank()) {
if (properties.getProperty("projectFolder") != null) {
projectFolder = properties.getProperty("projectFolder");
} else {
throw new NullPointerException("The projectFolder is missing");
throw new IllegalStateException("The projectFolder is missing");
}
}
if (fhirBaseUrl == null || fhirBaseUrl.isBlank()) {
if (properties.getProperty("fhirBaseUrl") != null) {
fhirBaseUrl = properties.getProperty("fhirBaseUrl");
} else {
throw new NullPointerException("The fhirBaseUrl is missing");
throw new IllegalStateException("The fhirBaseUrl is missing");
}
}
if (accessToken == null || accessToken.isBlank()) {
Expand Down
21 changes: 14 additions & 7 deletions efsity/src/main/java/org/smartregister/util/FctUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.parser.IParser;
import com.ibm.icu.text.CharsetDetector;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
Expand Down Expand Up @@ -108,14 +110,19 @@ public static void writeJsonFile(String outputPath, String fhirResourceAsString)
}
}

public static Properties readPropertiesFile(String propertiesFilePath) {
public static Properties readPropertiesFile(String propertiesFilePath) throws IOException {

CharsetDetector detector = new CharsetDetector();
byte[] fileBytes = Files.readAllBytes(Path.of(propertiesFilePath));
detector.setText(fileBytes);

Properties properties = new Properties();
try (FileInputStream fileInputStream = new FileInputStream(propertiesFilePath);
InputStreamReader reader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8)) {
try (InputStreamReader reader =
new InputStreamReader(
new ByteArrayInputStream(fileBytes), Charset.forName(detector.detect().getName()))) {
properties.load(reader);
} catch (IOException ex) {
ex.printStackTrace();
}

return properties;
}

Expand All @@ -136,7 +143,7 @@ public static Map<String, Map<String, String>> indexConfigurationFiles(
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (!Files.isDirectory(file)
&& (fileExtensions.length == 1 && fileExtensions[0] == "*"
&& (fileExtensions.length == 1 && "*".equals(fileExtensions[0])
|| Arrays.asList(fileExtensions)
.contains(FilenameUtils.getExtension(file.getFileName().toString())))) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public void testRunMerge() throws IOException {
Path tempRawQuestionnaire = Files.createTempFile("temp_raw_questionnaire", ".json");
Files.copy(rawQuestionnairePath, tempRawQuestionnaire, StandardCopyOption.REPLACE_EXISTING);

Path mergedQuestionnairePath = Paths.get("src/test/resources/merged_questionnaire.json");
Path expectedMergedQuestionnairePath =
Paths.get("src/test/resources/merged_questionnaire.json");

Path frPropertiesPath = Paths.get("src/test/resources/strings_fr.properties");

Expand All @@ -121,15 +122,15 @@ public void testRunMerge() throws IOException {
assertDoesNotThrow(() -> translateCommand.run());

ObjectMapper objectMapper = new ObjectMapper();
JsonNode rawQuestionnaire =
JsonNode processedRawQuestionnaire =
objectMapper.readTree(
Files.newBufferedReader(tempRawQuestionnaire, StandardCharsets.UTF_8));
JsonNode mergedQuestionnaire =
objectMapper.readTree(
Files.newBufferedReader(mergedQuestionnairePath, StandardCharsets.UTF_8));
Files.newBufferedReader(expectedMergedQuestionnairePath, StandardCharsets.UTF_8));

// Compare the contents of the two nodes
assertEquals(rawQuestionnaire, mergedQuestionnaire, "File merged as expected");
assertEquals(mergedQuestionnaire, processedRawQuestionnaire, "File merged as expected");
tempRawQuestionnaire.toFile().delete();
}
}

0 comments on commit f2d5404

Please sign in to comment.