1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.core.model.constraint;
7   
8   import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
9   import gov.nist.secauto.metaschema.core.metapath.MetapathException;
10  import gov.nist.secauto.metaschema.core.metapath.MetapathExpression;
11  import gov.nist.secauto.metaschema.core.model.ISource;
12  import gov.nist.secauto.metaschema.core.model.constraint.impl.DefaultLet;
13  import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
14  
15  import edu.umd.cs.findbugs.annotations.NonNull;
16  import edu.umd.cs.findbugs.annotations.Nullable;
17  
18  /**
19   * Represents a variable assignment for use in Metaschema module constraints.
20   */
21  @SuppressWarnings("PMD.ShortClassName")
22  public interface ILet {
23    /**
24     * Create a new Let expression by compiling the provided Metapath expression
25     * string.
26     *
27     * @param name
28     *          the let expression variable name
29     * @param valueExpression
30     *          a Metapath expression string representing the variable value
31     * @param source
32     *          the source descriptor for the resource containing the constraint
33     * @param remarks
34     *          remarks about the let statement
35     * @return the original let statement with the same name or {@code null}
36     */
37    @SuppressWarnings("PMD.ShortMethodName")
38    @NonNull
39    static ILet of(
40        @NonNull IEnhancedQName name,
41        @NonNull String valueExpression,
42        @NonNull ISource source,
43        @Nullable MarkupMultiline remarks) {
44      try {
45        return of(
46            name,
47            MetapathExpression.compile(valueExpression, source.getStaticContext()),
48            source,
49            remarks);
50      } catch (MetapathException ex) {
51        throw new MetapathException(
52            String.format("Unable to compile the let expression '%s=%s'%s. %s",
53                name,
54                valueExpression,
55                source.getSource() == null ? "" : " in " + source.getSource(),
56                ex.getMessage()),
57            ex);
58      }
59    }
60  
61    /**
62     * Create a new Let expression.
63     *
64     * @param name
65     *          the let expression variable name
66     * @param valueExpression
67     *          a Metapath expression representing the variable value
68     * @param source
69     *          the source descriptor for the resource containing the constraint
70     * @param remarks
71     *          remarks about the let statement
72     * @return the original let statement with the same name or {@code null}
73     */
74    @SuppressWarnings("PMD.ShortMethodName")
75    @NonNull
76    static ILet of(
77        @NonNull IEnhancedQName name,
78        @NonNull MetapathExpression valueExpression,
79        @NonNull ISource source,
80        @Nullable MarkupMultiline remarks) {
81      return new DefaultLet(name, valueExpression, source, remarks);
82    }
83  
84    /**
85     * Get the name of the let variable.
86     *
87     * @return the name
88     */
89    @NonNull
90    IEnhancedQName getName();
91  
92    /**
93     * Get the Metapath expression to use to query the value.
94     *
95     * @return the Metapath expression to use to query the value
96     */
97    @NonNull
98    MetapathExpression getValueExpression();
99  
100   /**
101    * Information about the source resource containing the let statement.
102    *
103    * @return the source information
104    */
105   @NonNull
106   ISource getSource();
107 
108   /**
109    * Get the remarks associated with the let statement.
110    *
111    * @return the remark or {@code null} if no remarks are defined
112    */
113   @Nullable
114   MarkupMultiline getRemarks();
115 }