From ba505a800e1cbe95cba249db4cb0d8c7c3123e33 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Mon, 1 Jun 2020 16:51:49 +1000 Subject: [PATCH] Add toBase64 & fromBase64 to FHIRPath --- .../org/hl7/fhir/r5/model/ExpressionNode.java | 9 +++-- .../org/hl7/fhir/r5/utils/FHIRPathEngine.java | 33 +++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/ExpressionNode.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/ExpressionNode.java index 7c4e637e9..db70fb227 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/ExpressionNode.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/ExpressionNode.java @@ -72,8 +72,9 @@ public class ExpressionNode { Empty, Not, Exists, SubsetOf, SupersetOf, IsDistinct, Distinct, Count, Where, Select, All, Repeat, Aggregate, Item /*implicit from name[]*/, As, Is, Single, First, Last, Tail, Skip, Take, Union, Combine, Intersect, Exclude, Iif, Upper, Lower, ToChars, IndexOf, Substring, StartsWith, EndsWith, Matches, ReplaceMatches, Contains, Replace, Length, Children, Descendants, MemberOf, Trace, Check, Today, Now, Resolve, Extension, AllFalse, AnyFalse, AllTrue, AnyTrue, - HasValue, AliasAs, Alias, HtmlChecks, OfType, Type, - ConvertsToBoolean, ConvertsToInteger, ConvertsToString, ConvertsToDecimal, ConvertsToQuantity, ConvertsToDateTime, ConvertsToTime, ToBoolean, ToInteger, ToString, ToDecimal, ToQuantity, ToDateTime, ToTime, ConformsTo; + HasValue, OfType, Type, ConvertsToBoolean, ConvertsToInteger, ConvertsToString, ConvertsToDecimal, ConvertsToQuantity, ConvertsToDateTime, ConvertsToTime, ToBoolean, ToInteger, ToString, ToDecimal, ToQuantity, ToDateTime, ToTime, ConformsTo, + // Local extensions to FHIRPath + HtmlChecks, AliasAs, Alias, fromBase64, toBase64; public static Function fromCode(String name) { if (name.equals("empty")) return Function.Empty; @@ -133,6 +134,8 @@ public class ExpressionNode { if (name.equals("aliasAs")) return Function.AliasAs; if (name.equals("htmlChecks")) return Function.HtmlChecks; if (name.equals("htmlchecks")) return Function.HtmlChecks; // support change of care from R3 + if (name.equals("fromBase64")) return Function.fromBase64; + if (name.equals("toBase64")) return Function.toBase64; if (name.equals("ofType")) return Function.OfType; if (name.equals("type")) return Function.Type; if (name.equals("toInteger")) return Function.ToInteger; @@ -209,6 +212,8 @@ public class ExpressionNode { case HasValue : return "hasValue"; case Alias : return "alias"; case AliasAs : return "aliasAs"; + case fromBase64 : return "fromBase64"; + case toBase64 : return "toBase64"; case HtmlChecks : return "htmlChecks"; case OfType : return "ofType"; case Type : return "type"; diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/FHIRPathEngine.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/FHIRPathEngine.java index e78d91bfe..d53d2f412 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/FHIRPathEngine.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/FHIRPathEngine.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.NotImplementedException; import org.fhir.ucum.Decimal; import org.fhir.ucum.Pair; @@ -1146,6 +1147,8 @@ public class FHIRPathEngine { case HasValue: return checkParamCount(lexer, location, exp, 0); case Alias: return checkParamCount(lexer, location, exp, 1); case AliasAs: return checkParamCount(lexer, location, exp, 1); + case fromBase64: return checkParamCount(lexer, location, exp, 0); + case toBase64: return checkParamCount(lexer, location, exp, 0); case HtmlChecks: return checkParamCount(lexer, location, exp, 0); case ToInteger: return checkParamCount(lexer, location, exp, 0); case ToDecimal: return checkParamCount(lexer, location, exp, 0); @@ -2643,7 +2646,11 @@ public class FHIRPathEngine { return anything(CollectionStatus.SINGLETON); case AliasAs : checkParamTypes(exp.getFunction().toCode(), paramTypes, new TypeDetails(CollectionStatus.SINGLETON, TypeDetails.FP_String)); - return focus; + return focus; + case fromBase64: + return new TypeDetails(CollectionStatus.SINGLETON, TypeDetails.FP_String); + case toBase64: + return new TypeDetails(CollectionStatus.SINGLETON, TypeDetails.FP_String); case ToInteger : { checkContextPrimitive(focus, "toInteger", true); return new TypeDetails(CollectionStatus.SINGLETON, TypeDetails.FP_Integer); @@ -2818,6 +2825,8 @@ public class FHIRPathEngine { case AllTrue: return funcAllTrue(context, focus, exp); case HasValue : return funcHasValue(context, focus, exp); case AliasAs : return funcAliasAs(context, focus, exp); + case fromBase64 : return funcFromBase64(context, focus, exp); + case toBase64 : return funcToBase64(context, focus, exp); case Alias : return funcAlias(context, focus, exp); case HtmlChecks : return funcHtmlChecks(context, focus, exp); case ToInteger : return funcToInteger(context, focus, exp); @@ -2846,7 +2855,27 @@ public class FHIRPathEngine { } } - private List funcAliasAs(ExecutionContext context, List focus, ExpressionNode exp) throws FHIRException { + private List funcToBase64(ExecutionContext context, List focus, ExpressionNode exp) { + List result = new ArrayList(); + if (focus.size() == 1) { + String s = convertToString(focus.get(0)); + result.add(new StringType(Base64.encodeBase64String(s.getBytes()))); + } + return result; + } + + + private List funcFromBase64(ExecutionContext context, List focus, ExpressionNode exp) { + List result = new ArrayList(); + if (focus.size() == 1) { + String s = convertToString(focus.get(0)); + result.add(new StringType(new String(Base64.decodeBase64(s)))); + } + return result; + } + + + private List funcAliasAs(ExecutionContext context, List focus, ExpressionNode exp) throws FHIRException { List nl = execute(context, focus, exp.getParameters().get(0), true); String name = nl.get(0).primitiveValue(); context.addAlias(name, focus);