
001/*- 002 * #%L 003 * HAPI FHIR Storage api 004 * %% 005 * Copyright (C) 2014 - 2025 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.jpa.util; 021 022import ca.uhn.fhir.jpa.patch.ParsedFhirPath; 023 024public class FhirPathUtils { 025 /** 026 * Turns an invalid FhirPath into a valid one 027 * - 028 * Do not use this for user input; but it can be used for internally parsed paths 029 */ 030 public static String cleansePath(String thePath) { 031 String path = thePath; 032 033 // remove trailing . 034 while (path.endsWith(".")) { 035 path = path.substring(0, path.length() - 1); 036 } 037 038 // remove preceding . 039 while (path.startsWith(".")) { 040 path = path.substring(1); 041 } 042 043 // balance brackets 044 int openBrace = path.indexOf("("); 045 String remainder = path; 046 int endingIndex = openBrace == -1 ? path.length() : 0; 047 while (openBrace != -1) { 048 int closing = RandomTextUtils.findMatchingClosingBrace(openBrace, remainder); 049 endingIndex += closing + 1; // +1 because substring ending is exclusive 050 remainder = remainder.substring(closing + 1); 051 openBrace = remainder.indexOf("("); 052 } 053 path = path.substring(0, endingIndex); 054 055 return path; 056 } 057 058 /** 059 * Determines if the node is a subsetting node 060 * as described by http://hl7.org/fhirpath/N1/#subsetting 061 */ 062 public static boolean isSubsettingNode(ParsedFhirPath.FhirPathNode theNode) { 063 if (theNode.getListIndex() >= 0) { 064 return true; 065 } 066 if (theNode.isFunction()) { 067 String funName = theNode.getValue(); 068 switch (funName) { 069 case "first", "last", "single", "tail", "skip", "take", "exclude", "intersect" -> { 070 return true; 071 } 072 } 073 } 074 return false; 075 } 076}