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.IMetapathExpression;
10  import gov.nist.secauto.metaschema.core.metapath.MetapathException;
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     * <p>
27     * This method is deprecated. Callers should use
28     * {@link #of(IEnhancedQName, IMetapathExpression, ISource, MarkupMultiline)}
29     * instead.
30     *
31     * @param name
32     *          the let expression variable name
33     * @param valueExpression
34     *          a Metapath expression string representing the variable value
35     * @param source
36     *          the source descriptor for the resource containing the constraint
37     * @param remarks
38     *          remarks about the let statement
39     * @return the original let statement with the same name or {@code null}
40     */
41    @SuppressWarnings("PMD.ShortMethodName")
42    @NonNull
43    @Deprecated(since = "2.2.0", forRemoval = true)
44    static ILet of(
45        @NonNull IEnhancedQName name,
46        @NonNull String valueExpression,
47        @NonNull ISource source,
48        @Nullable MarkupMultiline remarks) {
49      try {
50        return of(
51            name,
52            IMetapathExpression.lazyCompile(valueExpression, source.getStaticContext()),
53            source,
54            remarks);
55      } catch (MetapathException ex) {
56        throw new MetapathException(
57            String.format("Unable to compile the let expression '%s=%s'%s. %s",
58                name,
59                valueExpression,
60                source.getSource() == null ? "" : " in " + source.getSource(),
61                ex.getMessage()),
62            ex);
63      }
64    }
65  
66    /**
67     * Create a new Let expression.
68     *
69     * @param name
70     *          the let expression variable name
71     * @param valueExpression
72     *          a Metapath expression representing the variable value
73     * @param source
74     *          the source descriptor for the resource containing the constraint
75     * @param remarks
76     *          remarks about the let statement
77     * @return the original let statement with the same name or {@code null}
78     */
79    @SuppressWarnings("PMD.ShortMethodName")
80    @NonNull
81    static ILet of(
82        @NonNull IEnhancedQName name,
83        @NonNull IMetapathExpression valueExpression,
84        @NonNull ISource source,
85        @Nullable MarkupMultiline remarks) {
86      return new DefaultLet(name, valueExpression, source, remarks);
87    }
88  
89    /**
90     * Get the name of the let variable.
91     *
92     * @return the name
93     */
94    @NonNull
95    IEnhancedQName getName();
96  
97    /**
98     * Get the Metapath expression to use to query the value.
99     *
100    * @return the Metapath expression to use to query the value
101    */
102   @NonNull
103   IMetapathExpression getValueExpression();
104 
105   /**
106    * Information about the source resource containing the let statement.
107    *
108    * @return the source information
109    */
110   @NonNull
111   ISource getSource();
112 
113   /**
114    * Get the remarks associated with the let statement.
115    *
116    * @return the remark or {@code null} if no remarks are defined
117    */
118   @Nullable
119   MarkupMultiline getRemarks();
120 }