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.metapath.MetapathException;
9   import gov.nist.secauto.metaschema.core.metapath.MetapathExpression;
10  import gov.nist.secauto.metaschema.core.model.constraint.impl.DefaultLet;
11  
12  import javax.xml.namespace.QName;
13  
14  import edu.umd.cs.findbugs.annotations.NonNull;
15  
16  @SuppressWarnings("PMD.ShortClassName")
17  public interface ILet {
18    /**
19     * Create a new Let expression by compiling the provided Metapath expression
20     * string.
21     *
22     * @param name
23     *          the let expression variable name
24     * @param valueExpression
25     *          a Metapath expression string representing the variable value
26     * @param source
27     *          the source descriptor for the resource containing the constraint
28     * @return the original let statement with the same name or {@code null}
29     */
30    @SuppressWarnings("PMD.ShortMethodName")
31    @NonNull
32    static ILet of(
33        @NonNull QName name,
34        @NonNull String valueExpression,
35        @NonNull ISource source) {
36      try {
37        return of(name, MetapathExpression.compile(valueExpression, source.getStaticContext()), source);
38      } catch (MetapathException ex) {
39        throw new MetapathException(
40            String.format("Unable to compile the let expression '%s=%s'%s. %s",
41                name,
42                valueExpression,
43                source.getSource() == null ? "" : " in " + source.getSource(),
44                ex.getMessage()),
45            ex);
46      }
47    }
48  
49    /**
50     * Create a new Let expression.
51     *
52     * @param name
53     *          the let expression variable name
54     * @param valueExpression
55     *          a Metapath expression representing the variable value
56     * @param source
57     *          the source descriptor for the resource containing the constraint
58     * @return the original let statement with the same name or {@code null}
59     */
60    @SuppressWarnings("PMD.ShortMethodName")
61    @NonNull
62    static ILet of(
63        @NonNull QName name,
64        @NonNull MetapathExpression valueExpression,
65        @NonNull ISource source) {
66      return new DefaultLet(name, valueExpression, source);
67    }
68  
69    /**
70     * Get the name of the let variable.
71     *
72     * @return the name
73     */
74    @NonNull
75    QName getName();
76  
77    /**
78     * Get the Metapath expression to use to query the value.
79     *
80     * @return the Metapath expression to use to query the value
81     */
82    @NonNull
83    MetapathExpression getValueExpression();
84  
85    /**
86     * Information about the source resource containing the let statement.
87     *
88     * @return the source information
89     */
90    @NonNull
91    ISource getSource();
92  }