001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.metaschema.cli.processor.command;
007
008import gov.nist.secauto.metaschema.core.util.ObjectUtils;
009import gov.nist.secauto.metaschema.core.util.UriUtils;
010
011import java.net.URI;
012import java.net.URISyntaxException;
013import java.nio.file.Path;
014import java.nio.file.Paths;
015
016import edu.umd.cs.findbugs.annotations.NonNull;
017import nl.talsmasoftware.lazy4j.Lazy;
018
019/**
020 * A base class for terminal commands in the command processing hierarchy.
021 * Terminal commands represent leaf nodes that perform actual operations and
022 * cannot have child commands.
023 */
024public abstract class AbstractTerminalCommand implements ICommand {
025  private static Lazy<Path> currentWorkingDirectory = Lazy.lazy(() -> Paths.get(System.getProperty("user.dir")));
026
027  /**
028   * A utility method that can be used to get the current working directory.
029   * <p>
030   * This method is thread-safe due to lazy initialization.
031   *
032   * @return the current working directory
033   */
034  @NonNull
035  protected static Path getCurrentWorkingDirectory() {
036    return ObjectUtils.notNull(currentWorkingDirectory.get());
037  }
038
039  /**
040   * A utility method that can be used to resolve a path against the current
041   * working directory.
042   * <p>
043   * If the path is already absolute, then the provided path is returned.
044   *
045   * @param path
046   *          the path to resolve
047   *
048   * @return the resolved path
049   */
050  @NonNull
051  protected static Path resolveAgainstCWD(@NonNull Path path) {
052
053    return path.isAbsolute()
054        ? path
055        : ObjectUtils.notNull(getCurrentWorkingDirectory().resolve(path).normalize());
056  }
057
058  /**
059   * A utility method that can be used to resolve a URI against the URI for the
060   * current working directory.
061   * <p>
062   * If the URI is already absolute, then the provided URI is returned.
063   * <p>
064   * The path is normalized after resolution to remove any redundant name elements
065   * (like "." or "..").
066   *
067   *
068   * @param uri
069   *          the uri to resolve
070   *
071   * @return the resolved URI
072   */
073  @NonNull
074  protected static URI resolveAgainstCWD(@NonNull URI uri) {
075    return uri.isAbsolute()
076        ? uri
077        : ObjectUtils.notNull(getCurrentWorkingDirectory().toUri().resolve(uri.normalize()));
078  }
079
080  /**
081   * A utility method that can be used to resolve a URI (as a string) against the
082   * URI for the current working directory.
083   *
084   * @param uri
085   *          the uri to resolve
086   * @return the resolved URI
087   * @throws URISyntaxException
088   *           if the provided URI is not a valid URI
089   */
090  @NonNull
091  protected static URI resolveAgainstCWD(@NonNull String uri) throws URISyntaxException {
092    return UriUtils.toUri(uri, ObjectUtils.notNull(getCurrentWorkingDirectory().toUri()));
093  }
094}