All Downloads are FREE. Search and download functionalities are using the official Maven repository.

au.csiro.pathling.fhirpath.literal.CodingLiteral Maven / Gradle / Ivy

There is a newer version: 7.0.1
Show newest version
/*
 * Copyright 2023 Commonwealth Scientific and Industrial Research
 * Organisation (CSIRO) ABN 41 687 119 230.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package au.csiro.pathling.fhirpath.literal;

import static au.csiro.pathling.fhirpath.literal.StringLiteral.escapeFhirPathString;
import static au.csiro.pathling.fhirpath.literal.StringLiteral.unescapeFhirPathString;
import static au.csiro.pathling.utilities.Strings.unSingleQuote;

import java.util.Arrays;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.hl7.fhir.r4.model.Coding;

/**
 * Represents a FHIRPath Coding literal.
 *
 * @author John Grimes
 */
public abstract class CodingLiteral {

  /**
   * Special characters that require quoting within a Coding literal component.
   */
  private static final String SPECIAL_CHARACTERS = "\\s'|\\r\\n\\t(),";

  private static final String COMPONENT_REGEX = String
      .format("('.*?(?|[|][|[|]]].");
      }
    }
  }

  /**
   * Returns a FHIRPath literal representation of a coding.
   *
   * @param coding A coding to represent as a literal.
   * @return The FHIRPath representation of the coding.
   * @throws IllegalArgumentException if the literal is malformed
   */
  @Nonnull
  public static String toLiteral(@Nonnull final Coding coding) {
    final String[] components = new String[]{
        coding.getSystem(),
        coding.getCode(),
        coding.getVersion(),
        coding.getDisplay(),
        coding.hasUserSelected()
        ? String.valueOf(coding.getUserSelected())
        : null
    };

    // Drop the null components other than system and code from the tail.
    int nonNullHead = components.length;
    while (nonNullHead > 2 && components[nonNullHead - 1] == null) {
      nonNullHead--;
    }
    return Arrays.stream(components)
        .limit(nonNullHead)
        .map(CodingLiteral::encodeComponent).collect(Collectors.joining("|"));
  }

  @Nonnull
  private static String decodeComponent(@Nonnull final String component) {
    final Matcher matcher = NEEDS_UNQUOTING.matcher(component);
    if (matcher.matches()) {
      final String result = unSingleQuote(component);
      return unescapeFhirPathString(result);
    } else {
      return component;
    }
  }

  @Nonnull
  private static String encodeComponent(@Nullable final String component) {
    if (component == null) {
      return "";
    } else {
      final Matcher matcher = NEEDS_QUOTING.matcher(component);
      if (matcher.find()) {
        final String result = escapeFhirPathString(component);
        return "'" + result + "'";
      } else {
        return component;
      }
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy