From 548284fcda95a44f0d1bc1810785ee6612ab9c98 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Sun, 2 Apr 2023 23:08:17 +1000 Subject: [PATCH] rework language handling again --- .../r5/utils/ResourceLanguageFileBuilder.java | 4 ++- .../utilities/i18n/LanguageFileProducer.java | 33 +++++++++++++++++-- .../utilities/i18n/PoGetTextProducer.java | 11 +++---- .../fhir/utilities/i18n/XLIFFProducer.java | 15 +++++---- .../org/hl7/fhir/validation/IgLoader.java | 2 +- .../fhir/validation/cli/model/CliContext.java | 13 ++++++++ .../cli/services/ValidationService.java | 4 +-- .../hl7/fhir/validation/cli/utils/Common.java | 24 +++++++++----- .../fhir/validation/cli/utils/Display.java | 2 +- .../fhir/validation/cli/utils/EngineMode.java | 1 + .../hl7/fhir/validation/cli/utils/Params.java | 4 +++ 11 files changed, 83 insertions(+), 30 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/ResourceLanguageFileBuilder.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/ResourceLanguageFileBuilder.java index 46b883289..8943beb01 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/ResourceLanguageFileBuilder.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/ResourceLanguageFileBuilder.java @@ -15,6 +15,8 @@ import org.hl7.fhir.r5.model.StructureDefinition; import org.hl7.fhir.utilities.i18n.LanguageFileProducer; import org.hl7.fhir.utilities.i18n.LanguageFileProducer.LanguageProducerLanguageSession; import org.hl7.fhir.utilities.i18n.LanguageFileProducer.LanguageProducerSession; +import org.hl7.fhir.utilities.i18n.LanguageFileProducer.TextUnit; + public class ResourceLanguageFileBuilder { @@ -75,7 +77,7 @@ public class ResourceLanguageFileBuilder { String ppath = path+"."+p.getName()+(p.isList() ? "["+i+"]" : ""); i++; if (isTranslatable(p, b, pid)) { - sess.entry(ppath, b.primitiveValue(), getTranslation(b, target)); + sess.entry(new TextUnit(ppath, b.primitiveValue(), getTranslation(b, target))); } for (Property pp : b.children()) { process(sess, pp, pid, ppath); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/LanguageFileProducer.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/LanguageFileProducer.java index dc1c77c8d..e2bd8641f 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/LanguageFileProducer.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/LanguageFileProducer.java @@ -3,11 +3,38 @@ package org.hl7.fhir.utilities.i18n; import java.io.IOException; import java.util.List; import java.util.Map; + +import org.hl7.fhir.utilities.i18n.LanguageFileProducer.TextUnit; + import java.util.HashMap; public abstract class LanguageFileProducer { + public static class TextUnit { + private String context; + private String srcText; + private String tgtText; + public TextUnit(String context, String srcText, String tgtText) { + super(); + this.context = context; + this.srcText = srcText; + this.tgtText = tgtText; + } + + public String getContext() { + return context; + } + + public String getSrcText() { + return srcText; + } + public String getTgtText() { + return tgtText; + } + + } + public class Translations { private String id; private Map translations = new HashMap<>(); @@ -47,7 +74,7 @@ public abstract class LanguageFileProducer { public abstract void finish() throws IOException; - public abstract void entry(String id, String src, String dst); + public abstract void entry(TextUnit unit); } public abstract class LanguageProducerSession { @@ -55,7 +82,7 @@ public abstract class LanguageFileProducer { protected String id; protected String baseLang; - protected LanguageProducerSession( String id, String baseLang) { + protected LanguageProducerSession(String id, String baseLang) { super(); this.id = id; this.baseLang = baseLang; @@ -78,5 +105,5 @@ public abstract class LanguageFileProducer { public abstract LanguageProducerSession startSession(String id, String baseLang) throws IOException; public abstract void finish(); - public abstract List loadTranslations(String id); + public abstract List loadTranslations(String baseLang, String targetLang); } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/PoGetTextProducer.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/PoGetTextProducer.java index 210f47e7b..4eab7755c 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/PoGetTextProducer.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/PoGetTextProducer.java @@ -71,13 +71,13 @@ public class PoGetTextProducer extends LanguageFileProducer { } @Override - public void entry(String id, String src, String dst) { - ln("#: "+id); + public void entry(TextUnit unit) { + ln("#: "+unit.getContext()); // if (context != null) { // ln("#. "+context); // } - ln("msgid \""+src+"\""); - ln("msgstr \""+dst+"\""); + ln("msgid \""+unit.getSrcText()+"\""); + ln("msgstr \""+unit.getTgtText()+"\""); ln(""); } @@ -85,8 +85,7 @@ public class PoGetTextProducer extends LanguageFileProducer { @Override - public List loadTranslations(String id) { - // TODO Auto-generated method stub + public List loadTranslations(String baseLang, String tgtLang) { return null; } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/XLIFFProducer.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/XLIFFProducer.java index c8fd7b8ee..548583596 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/XLIFFProducer.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/XLIFFProducer.java @@ -5,6 +5,7 @@ import java.util.List; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; +import org.hl7.fhir.utilities.i18n.LanguageFileProducer.TextUnit; public class XLIFFProducer extends LanguageFileProducer { @@ -35,16 +36,16 @@ public class XLIFFProducer extends LanguageFileProducer { } @Override - public void entry(String id, String src, String dst) { + public void entry(TextUnit unit) { i++; - ln(" "); + ln(" "); // if (context != null) { // ln(" "); // ln(" "+Utilities.escapeXml(context)+""); // ln(" "); // } - ln(" "+Utilities.escapeXml(src)+""); - ln(" "+Utilities.escapeXml(dst)+""); + ln(" "+Utilities.escapeXml(unit.getSrcText())+""); + ln(" "+Utilities.escapeXml(unit.getTgtText())+""); ln(" "); } @@ -82,10 +83,10 @@ public class XLIFFProducer extends LanguageFileProducer { public void finish() { // nothing } - + @Override - public List loadTranslations(String id) { - // TODO Auto-generated method stub + public List loadTranslations(String baseLang, String tgtLang) { return null; } + } diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/IgLoader.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/IgLoader.java index 489da4564..403b9dada 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/IgLoader.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/IgLoader.java @@ -829,7 +829,7 @@ public class IgLoader { else throw new FHIRException("Unsupported format for " + fn); r = VersionConvertorFactory_10_50.convertResource(res, new org.hl7.fhir.convertors.misc.IGR2ConvertorAdvisor5()); - } else if (fhirVersion.startsWith("5.0") || "current".equals(fhirVersion)) { + } else if (fhirVersion.startsWith("5.0")) { if (fn.endsWith(".xml") && !fn.endsWith("template.xml")) r = new XmlParser().parse(new ByteArrayInputStream(content)); else if (fn.endsWith(".json") && !fn.endsWith("template.json")) diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/model/CliContext.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/model/CliContext.java index cbaa0e19d..7f26aea48 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/model/CliContext.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/model/CliContext.java @@ -50,6 +50,8 @@ public class CliContext { @JsonProperty("htmlInMarkdownCheck") private HtmlInMarkdownCheck htmlInMarkdownCheck = HtmlInMarkdownCheck.WARNING; + @JsonProperty("langTransform") + private String langTransform = null; @JsonProperty("map") private String map = null; @JsonProperty("output") @@ -136,6 +138,17 @@ public class CliContext { return this; } + + @JsonProperty("langTransform") + public String getLangTransform() { + return langTransform; + } + + @JsonProperty("langTransform") + public CliContext setLangTransform(String langTransform) { + this.langTransform = langTransform; + return this; + } @JsonProperty("igs") public List getIgs() { return igs; diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java index 8c7163f70..230993027 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java @@ -107,7 +107,7 @@ public class ValidationService { public VersionSourceInformation scanForVersions(CliContext cliContext) throws Exception { VersionSourceInformation versions = new VersionSourceInformation(); IgLoader igLoader = new IgLoader( - new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION), + new FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager.FilesystemPackageCacheMode.USER), new SimpleWorkerContext.SimpleWorkerContextBuilder().fromNothing(), null); for (String src : cliContext.getIgs()) { @@ -454,7 +454,7 @@ public class ValidationService { public String determineVersion(CliContext cliContext, String sessionId) throws Exception { if (cliContext.getMode() != EngineMode.VALIDATION) { - return "current"; + return "5.0"; } System.out.println("Scanning for versions (no -version parameter):"); VersionSourceInformation versions = scanForVersions(cliContext); diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Common.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Common.java index 0d129ec54..c7e2c2880 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Common.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Common.java @@ -13,7 +13,7 @@ public class Common { public static String getVersion(String[] args) { String v = Params.getParam(args, "-version"); if (v == null) { - v = "current"; + v = "5.0"; for (int i = 0; i < args.length; i++) { if ("-ig".equals(args[i])) { if (i + 1 == args.length) @@ -24,16 +24,20 @@ public class Common { } } } - } else if ("1.0".equals(v)) { + } else if (VersionUtilities.isR2Ver(v)) { v = "1.0"; - } else if ("1.4".equals(v)) { + } else if (VersionUtilities.isR2BVer(v)) { v = "1.4"; - } else if ("3.0".equals(v)) { + } else if (VersionUtilities.isR3Ver(v)) { v = "3.0"; - } else if ("4.0".equals(v)) { + } else if (VersionUtilities.isR4Ver(v)) { v = "4.0"; - } else if (v.startsWith(Constants.VERSION)) { - v = "current"; + } else if (VersionUtilities.isR4BVer(v)) { + v = "4.3"; + } else if (VersionUtilities.isR5Ver(v)) { + v = "5.0"; + } else if (VersionUtilities.isR6Ver(v)) { + v = "6.0"; } return v; } @@ -49,7 +53,7 @@ public class Common { */ public static String getVersionFromIGName(String defaultValue, String igFileName) { if (igFileName.equals("hl7.fhir.core")) { - defaultValue = "current"; + defaultValue = "5.0"; } else if (igFileName.startsWith("hl7.fhir.core#")) { defaultValue = VersionUtilities.getCurrentPackageVersion(igFileName.substring(14)); } else if (igFileName.startsWith("hl7.fhir.r2.core#") || igFileName.equals("hl7.fhir.r2.core")) { @@ -61,7 +65,9 @@ public class Common { } else if (igFileName.startsWith("hl7.fhir.r4.core#") || igFileName.equals("hl7.fhir.r4.core")) { defaultValue = "4.0"; } else if (igFileName.startsWith("hl7.fhir.r5.core#") || igFileName.equals("hl7.fhir.r5.core")) { - defaultValue = "current"; + defaultValue = "5.0"; + } else if (igFileName.startsWith("hl7.fhir.r6.core#") || igFileName.equals("hl7.fhir.r6.core")) { + defaultValue = "6.0"; } return defaultValue; } diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Display.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Display.java index a4a200469..14a988487 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Display.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Display.java @@ -22,7 +22,7 @@ public class Display { } public static void printCliArgumentsAndInfo(String[] args) throws IOException { - System.out.println(" Paths: Current = " + System.getProperty("user.dir") + ", Package Cache = " + new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION).getFolder()); + System.out.println(" Paths: Current = " + System.getProperty("user.dir") + ", Package Cache = " + new FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager.FilesystemPackageCacheMode.USER).getFolder()); System.out.print(" Params:"); for (String s : args) { System.out.print(s.contains(" ") ? " \"" + s + "\"" : " " + s); diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/EngineMode.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/EngineMode.java index 7c4e8a83b..73c76b03e 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/EngineMode.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/EngineMode.java @@ -4,6 +4,7 @@ public enum EngineMode { VALIDATION, COMPILE, TRANSFORM, + LANG_TRANSFORM, NARRATIVE, SNAPSHOT, SCAN, diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Params.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Params.java index d58561552..d907783df 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Params.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Params.java @@ -41,6 +41,7 @@ public class Params { public static final String NO_NATIVE = "-no-native"; public static final String COMPILE = "-compile"; public static final String TRANSFORM = "-transform"; + public static final String LANG_TRANSFORM = "-lang-transform"; public static final String NARRATIVE = "-narrative"; public static final String SNAPSHOT = "-snapshot"; public static final String SCAN = "-scan"; @@ -237,6 +238,9 @@ public class Params { } else if (args[i].equals(TRANSFORM)) { cliContext.setMap(args[++i]); cliContext.setMode(EngineMode.TRANSFORM); + } else if (args[i].equals(LANG_TRANSFORM)) { + cliContext.setLangTransform(args[++i]); + cliContext.setMode(EngineMode.LANG_TRANSFORM); } else if (args[i].equals(COMPILE)) { cliContext.setMap(args[++i]); cliContext.setMode(EngineMode.COMPILE);