001/*
002 * Portions of this software was developed by employees of the National Institute
003 * of Standards and Technology (NIST), an agency of the Federal Government and is
004 * being made available as a public service. Pursuant to title 17 United States
005 * Code Section 105, works of NIST employees are not subject to copyright
006 * protection in the United States. This software may be subject to foreign
007 * copyright. Permission in the United States and in foreign countries, to the
008 * extent that NIST may hold copyright, to use, copy, modify, create derivative
009 * works, and distribute this software and its documentation without fee is hereby
010 * granted on a non-exclusive basis, provided that this notice and disclaimer
011 * of warranty appears in all copies.
012 *
013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
025 */
026
027package gov.nist.secauto.metaschema.core.metapath.format;
028
029import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem;
030import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem;
031import gov.nist.secauto.metaschema.core.metapath.item.node.IFieldNodeItem;
032import gov.nist.secauto.metaschema.core.metapath.item.node.IFlagNodeItem;
033import gov.nist.secauto.metaschema.core.metapath.item.node.IModuleNodeItem;
034import gov.nist.secauto.metaschema.core.metapath.item.node.IRootAssemblyNodeItem;
035
036import java.util.stream.Collectors;
037
038import edu.umd.cs.findbugs.annotations.NonNull;
039
040/**
041 * This interface provides an implementation contract for all path formatters.
042 * When {@link #format(IPathSegment)} is called on a formatter implementation,
043 * the formatter will render the path segments based on the implemented path
044 * syntax. This allows a collection of path segments to be rendered in different
045 * forms by swapping out the formatter used.
046 *
047 * A path formatter is expected to be stateless and thus thread safe.
048 */
049public interface IPathFormatter {
050  /**
051   * A path formatter that produces Metapath-based paths.
052   */
053  @NonNull
054  IPathFormatter METAPATH_PATH_FORMATER = new MetapathFormatter();
055
056  /**
057   * Format the path represented by the provided path segment. The provided
058   * segment is expected to be the last node in this path. A call to
059   * {@link IPathSegment#getPathStream()} or {@link IPathSegment#getPath()} can be
060   * used to walk the path tree in descending order.
061   *
062   * @param segment
063   *          The last segment in a sequence of path segments
064   * @return a formatted path
065   * @see IPathSegment#getPathStream()
066   * @see IPathSegment#getPath()
067   */
068  @SuppressWarnings("null")
069  @NonNull
070  default String format(@NonNull IPathSegment segment) {
071    return segment.getPathStream().map(pathSegment -> {
072      return pathSegment.format(this);
073    }).collect(Collectors.joining("/"));
074  }
075
076  /**
077   * This visitor callback is used to format an individual flag path segment.
078   *
079   * @param flag
080   *          the node to format
081   * @return the formatted text for the segment
082   */
083  @NonNull
084  String formatFlag(@NonNull IFlagNodeItem flag);
085
086  /**
087   * This visitor callback is used to format an individual field path segment.
088   *
089   * @param field
090   *          the node to format
091   * @return the formatted text for the segment
092   */
093  @NonNull
094  String formatField(@NonNull IFieldNodeItem field);
095
096  /**
097   * This visitor callback is used to format an individual assembly path segment.
098   *
099   * @param assembly
100   *          the node to format
101   * @return the formatted text for the segment
102   */
103  @NonNull
104  String formatAssembly(@NonNull IAssemblyNodeItem assembly);
105
106  /**
107   * This visitor callback is used to format a root assembly path segment.
108   *
109   * @param root
110   *          the node to format
111   * @return the formatted text for the segment
112   */
113  @NonNull
114  String formatRootAssembly(@NonNull IRootAssemblyNodeItem root);
115
116  /**
117   * This visitor callback is used to format an individual document path segment.
118   *
119   * @param document
120   *          the node to format
121   * @return the formatted text for the segment
122   */
123  @NonNull
124  String formatDocument(@NonNull IDocumentNodeItem document);
125
126  /**
127   * This visitor callback is used to format an individual metaschema path
128   * segment.
129   *
130   * @param metaschema
131   *          the node to format
132   * @return the formatted text for the segment
133   */
134  @NonNull
135  String formatMetaschema(@NonNull IModuleNodeItem metaschema);
136}