revise BOM handling for stringToX routines

This commit is contained in:
Grahame Grieve 2024-01-15 17:29:42 +11:00
parent e6df130e3a
commit 56fc442db4
11 changed files with 36 additions and 41 deletions

View File

@ -27,7 +27,7 @@ public class BOMRemover {
String s = Utilities.stripBOM(src);
if (!s.equals(src)) {
System.out.println("Remove BOM from "+f.getAbsolutePath());
TextFile.stringToFile(s, f, false);
TextFile.stringToFile(s, f);
}
}
}

View File

@ -364,11 +364,11 @@ public class NPMPackageGenerator {
// also, for cache management on current builds, generate a little manifest
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(packageManifest);
TextFile.stringToFile(json, Utilities.changeFileExt(destFile, ".manifest.json"), false);
TextFile.stringToFile(json, Utilities.changeFileExt(destFile, ".manifest.json"));
}
private void buildIndexJson() throws IOException {
byte[] content = TextFile.stringToBytes(indexer.build(), false);
byte[] content = TextFile.stringToBytes(indexer.build());
addFile(Category.RESOURCE, ".index.json", content);
content = TextFile.fileToBytes(indexdb);
new File(indexdb).delete();

View File

@ -243,7 +243,7 @@ public class SHLParser extends ParserBase {
}
private void processContent(List<ValidatedFragment> res, List<ValidationMessage> errors, String path, String name, String jose, String ct) throws FHIRFormatError, DefinitionException, FHIRException, IOException {
ValidatedFragment bin = addNamedElement(res, "encrypted", "jose", TextFile.stringToBytes(jose, false));
ValidatedFragment bin = addNamedElement(res, "encrypted", "jose", TextFile.stringToBytes(jose));
byte[] cnt = null;
JWEObject jwe;
try {

View File

@ -1292,6 +1292,7 @@ public class ValueSetValidator extends ValueSetProcessBase {
for (ConceptReferenceComponent cc : vsi.getConcept()) {
if (cc.getCode().equals(code)) {
return true;
}
}
}
info.setErr(TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED);

View File

@ -380,11 +380,11 @@ public class NPMPackageGenerator {
TextFile.bytesToFile(OutputStream.toByteArray(), destFile);
// also, for cache management on current builds, generate a little manifest
String json = JsonParser.compose(packageManifest, true);
TextFile.stringToFile(json, Utilities.changeFileExt(destFile, ".manifest.json"), false);
TextFile.stringToFile(json, Utilities.changeFileExt(destFile, ".manifest.json"));
}
private void buildIndexJson() throws IOException {
byte[] content = TextFile.stringToBytes(indexer.build(), false);
byte[] content = TextFile.stringToBytes(indexer.build());
addFile(Category.RESOURCE, ".index.json", content);
content = TextFile.fileToBytes(indexdb);
new File(indexdb).delete();

View File

@ -88,35 +88,16 @@ public class TextFile {
s.close();
}
public static void stringToFile(String content, File file) throws IOException {
FileOutputStream fs = new FileOutputStream(file);
try {
OutputStreamWriter sw = new OutputStreamWriter(fs, "UTF-8");
sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
sw.write(content);
sw.flush();
sw.close();
} finally {
fs.close();
}
}
public static void stringToStream(String content, OutputStream stream, boolean bom) throws IOException {
public static void stringToStream(String content, OutputStream stream) throws IOException {
OutputStreamWriter sw = new OutputStreamWriter(stream, "UTF-8");
if (bom) {
sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
}
sw.write(content);
sw.flush();
sw.close();
}
public static byte[] stringToBytes(String content, boolean bom) throws IOException {
public static byte[] stringToBytes(String content) throws IOException {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
OutputStreamWriter sw = new OutputStreamWriter(bs, "UTF-8");
if (bom)
sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
sw.write(content);
sw.flush();
sw.close();
@ -128,20 +109,33 @@ public class TextFile {
stringToFile(content, file);
}
public static void stringToFile(String content, File file, boolean bom) throws IOException {
public static void stringToFile(String content, File file) throws IOException {
FileOutputStream fs = new FileOutputStream(file);
try {
OutputStreamWriter sw = new OutputStreamWriter(fs, "UTF-8");
sw.write(content);
sw.flush();
sw.close();
} finally {
fs.close();
}
}
public static void stringToFileWithBOM(String content, File file) throws IOException {
FileOutputStream fs = new FileOutputStream(file);
OutputStreamWriter sw = new OutputStreamWriter(fs, "UTF-8");
if (bom)
sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
sw.write(content);
sw.flush();
sw.close();
}
public static void stringToFile(String content, String path, boolean bom) throws IOException {
public static void stringToFileWithBom(String content, String path) throws IOException {
File file = new CSFile(path);
stringToFile(content, file, bom);
stringToFileWithBOM(content, file);
}
public static void stringToFileNoPrefix(String content, String path) throws IOException {
File file = new CSFile(path);

View File

@ -191,7 +191,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
File packagesIniFile = new File(packagesIniPath);
if (!(packagesIniFile.exists()))
packagesIniFile.createNewFile();
TextFile.stringToFile("[cache]\r\nversion=" + CACHE_VERSION + "\r\n\r\n[urls]\r\n\r\n[local]\r\n\r\n", packagesIniPath, false);
TextFile.stringToFile("[cache]\r\nversion=" + CACHE_VERSION + "\r\n\r\n[urls]\r\n\r\n[local]\r\n\r\n", packagesIniPath);
createIniFile();
for (File f : cacheFolder.listFiles()) {
if (f.isDirectory() && Utilities.isValidUUID(f.getName())) {
@ -541,7 +541,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
npm.getNpm().remove("version");
npm.getNpm().add("version", v);
}
TextFile.stringToFile(JsonParser.compose(npm.getNpm(), true), Utilities.path(cacheFolder, id + "#" + v, "package", "package.json"), false);
TextFile.stringToFile(JsonParser.compose(npm.getNpm(), true), Utilities.path(cacheFolder, id + "#" + v, "package", "package.json"));
}
pck = loadPackageInfo(packRoot);
} catch (Exception e) {

View File

@ -1125,7 +1125,7 @@ public class NpmPackage {
byte[] cnt = indexer.build().getBytes(StandardCharsets.UTF_8);
TextFile.bytesToFile(cnt, Utilities.path(dir.getAbsolutePath(), n, ".index.json"));
}
byte[] cnt = TextFile.stringToBytes(JsonParser.compose(npm, true), false);
byte[] cnt = TextFile.stringToBytes(JsonParser.compose(npm, true));
TextFile.bytesToFile(cnt, Utilities.path(dir.getAbsolutePath(), "package", "package.json"));
}
@ -1185,7 +1185,7 @@ public class NpmPackage {
tar.closeArchiveEntry();
}
}
byte[] cnt = TextFile.stringToBytes(JsonParser.compose(npm, true), false);
byte[] cnt = TextFile.stringToBytes(JsonParser.compose(npm, true));
TarArchiveEntry entry = new TarArchiveEntry("package/package.json");
entry.setSize(cnt.length);
tar.putArchiveEntry(entry);

View File

@ -126,7 +126,7 @@ public class XLSXmlNormaliser {
String s = TextFile.fileToString(dest);
s = s.replaceAll("\r\n","\n");
s = replaceSignificantEoln(s);
TextFile.stringToFile(s, dest, false);
TextFile.stringToFile(s, dest);
new File(dest).setLastModified(time);
} catch (Exception e) {
System.out.println("The file "+dest+" is still open in Excel, and you will have to run the build after closing Excel before committing");

View File

@ -554,7 +554,7 @@ public class IgLoader implements IValidationEngineLoader {
private String readInfoVersion(ByteProvider bs) throws IOException {
String is = TextFile.bytesToString(bs.getBytes());
is = is.trim();
IniFile ini = new IniFile(new ByteArrayInputStream(TextFile.stringToBytes(is, false)));
IniFile ini = new IniFile(new ByteArrayInputStream(TextFile.stringToBytes(is)));
return ini.getStringProperty("FHIR", "version");
}

View File

@ -927,7 +927,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
else if (fn.endsWith(".json") && !fn.endsWith("template.json"))
new org.hl7.fhir.dstu3.formats.JsonParser().setOutputStyle(org.hl7.fhir.dstu3.formats.IParser.OutputStyle.PRETTY).compose(s, res);
else if (fn.endsWith(".txt") || fn.endsWith(".map") || fn.endsWith(".fml"))
TextFile.stringToStream(org.hl7.fhir.dstu3.utils.StructureMapUtilities.render((org.hl7.fhir.dstu3.model.StructureMap) res), s, false);
TextFile.stringToStream(org.hl7.fhir.dstu3.utils.StructureMapUtilities.render((org.hl7.fhir.dstu3.model.StructureMap) res), s);
else
throw new FHIRException("Unsupported format for " + fn);
} else if (VersionUtilities.isR4Ver(version)) {
@ -937,7 +937,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
else if (fn.endsWith(".json") && !fn.endsWith("template.json"))
new org.hl7.fhir.r4.formats.JsonParser().setOutputStyle(org.hl7.fhir.r4.formats.IParser.OutputStyle.PRETTY).compose(s, res);
else if (fn.endsWith(".txt") || fn.endsWith(".map") || fn.endsWith(".fml"))
TextFile.stringToStream(org.hl7.fhir.r4.utils.StructureMapUtilities.render((org.hl7.fhir.r4.model.StructureMap) res), s, false);
TextFile.stringToStream(org.hl7.fhir.r4.utils.StructureMapUtilities.render((org.hl7.fhir.r4.model.StructureMap) res), s);
else
throw new FHIRException("Unsupported format for " + fn);
} else if (VersionUtilities.isR2BVer(version)) {
@ -962,7 +962,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
else if (fn.endsWith(".json") && !fn.endsWith("template.json"))
new JsonParser().setOutputStyle(org.hl7.fhir.r5.formats.IParser.OutputStyle.PRETTY).compose(s, r);
else if (fn.endsWith(".txt") || fn.endsWith(".map") || fn.endsWith(".fml"))
TextFile.stringToStream(StructureMapUtilities.render((org.hl7.fhir.r5.model.StructureMap) r), s, false);
TextFile.stringToStream(StructureMapUtilities.render((org.hl7.fhir.r5.model.StructureMap) r), s);
else
throw new FHIRException("Unsupported format for " + fn);
} else