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                if (path.startsWith("()")) {
034                        path = path.substring(2);
035                }
036
037                // remove trailing .
038                while (path.endsWith(".")) {
039                        path = path.substring(0, path.length() - 1);
040                }
041
042                // remove preceding .
043                while (path.startsWith(".")) {
044                        path = path.substring(1);
045                }
046
047                return path;
048        }
049
050        /**
051         * Determines if the node is a subsetting node
052         * as described by http://hl7.org/fhirpath/N1/#subsetting
053         */
054        public static boolean isSubsettingNode(ParsedFhirPath.FhirPathNode theNode) {
055                if (theNode.getListIndex() >= 0) {
056                        return true;
057                }
058                if (theNode.isFunction()) {
059                        String funName = theNode.getValue();
060                        switch (funName) {
061                                case "first", "last", "single", "tail", "skip", "take", "exclude", "intersect" -> {
062                                        return true;
063                                }
064                        }
065                }
066                return false;
067        }
068}