View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.metaschema.core.metapath.format;
28  
29  import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem;
30  import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem;
31  import gov.nist.secauto.metaschema.core.metapath.item.node.IFieldNodeItem;
32  import gov.nist.secauto.metaschema.core.metapath.item.node.IFlagNodeItem;
33  import gov.nist.secauto.metaschema.core.metapath.item.node.IModuleNodeItem;
34  import gov.nist.secauto.metaschema.core.metapath.item.node.IRootAssemblyNodeItem;
35  
36  import java.util.stream.Collectors;
37  
38  import edu.umd.cs.findbugs.annotations.NonNull;
39  
40  /**
41   * This interface provides an implementation contract for all path formatters.
42   * When {@link #format(IPathSegment)} is called on a formatter implementation,
43   * the formatter will render the path segments based on the implemented path
44   * syntax. This allows a collection of path segments to be rendered in different
45   * forms by swapping out the formatter used.
46   *
47   * A path formatter is expected to be stateless and thus thread safe.
48   */
49  public interface IPathFormatter {
50    /**
51     * A path formatter that produces Metapath-based paths.
52     */
53    @NonNull
54    IPathFormatter METAPATH_PATH_FORMATER = new MetapathFormatter();
55  
56    /**
57     * Format the path represented by the provided path segment. The provided
58     * segment is expected to be the last node in this path. A call to
59     * {@link IPathSegment#getPathStream()} or {@link IPathSegment#getPath()} can be
60     * used to walk the path tree in descending order.
61     *
62     * @param segment
63     *          The last segment in a sequence of path segments
64     * @return a formatted path
65     * @see IPathSegment#getPathStream()
66     * @see IPathSegment#getPath()
67     */
68    @SuppressWarnings("null")
69    @NonNull
70    default String format(@NonNull IPathSegment segment) {
71      return segment.getPathStream().map(pathSegment -> {
72        return pathSegment.format(this);
73      }).collect(Collectors.joining("/"));
74    }
75  
76    /**
77     * This visitor callback is used to format an individual flag path segment.
78     *
79     * @param flag
80     *          the node to format
81     * @return the formatted text for the segment
82     */
83    @NonNull
84    String formatFlag(@NonNull IFlagNodeItem flag);
85  
86    /**
87     * This visitor callback is used to format an individual field path segment.
88     *
89     * @param field
90     *          the node to format
91     * @return the formatted text for the segment
92     */
93    @NonNull
94    String formatField(@NonNull IFieldNodeItem field);
95  
96    /**
97     * This visitor callback is used to format an individual assembly path segment.
98     *
99     * @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 }