1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.core.model;
7   
8   import gov.nist.secauto.metaschema.core.metapath.StaticContext;
9   import gov.nist.secauto.metaschema.core.model.constraint.impl.InternalModelSource;
10  import gov.nist.secauto.metaschema.core.model.constraint.impl.StaticContextSource;
11  
12  import java.net.URI;
13  
14  import edu.umd.cs.findbugs.annotations.NonNull;
15  import edu.umd.cs.findbugs.annotations.Nullable;
16  
17  /**
18   * A descriptor that identifies where a given constraint was defined.
19   */
20  public interface ISource {
21    enum SourceType {
22      /**
23       * A constraint embedded in a model.
24       */
25      MODEL,
26      /**
27       * A constraint defined externally from a model.
28       */
29      EXTERNAL;
30    }
31  
32    /**
33     * Get the descriptor for a
34     * {@link gov.nist.secauto.metaschema.core.model.ISource.SourceType#MODEL}
35     * source with as associated resource.
36     *
37     * @param module
38     *          the Metaschema module the constraint was defined in
39     * @return the source descriptor
40     * @since 2.0.0
41     */
42    @NonNull
43    static ISource moduleSource(@NonNull IModule module) {
44      return InternalModelSource.instance(module);
45    }
46  
47    /**
48     * Get the descriptor for a
49     * {@link gov.nist.secauto.metaschema.core.model.ISource.SourceType#EXTERNAL}
50     * source with as associated resource.
51     * <p>
52     * The provided static context idenfies the location of this source based on the
53     * {@link StaticContext#getBaseUri()} method.
54     *
55     * @param staticContext
56     *          the static Metapath context to use for compiling Metapath
57     *          expressions in this source
58     *
59     * @return the source descriptor
60     */
61    @NonNull
62    static ISource externalSource(@NonNull StaticContext staticContext) {
63      if (staticContext.getBaseUri() == null) {
64        throw new IllegalArgumentException("The static content must define a baseUri identifing the source resource.");
65      }
66      return StaticContextSource.instance(staticContext);
67    }
68  
69    /**
70     * Get the type of source.
71     *
72     * @return the type
73     */
74    @NonNull
75    ISource.SourceType getSourceType();
76  
77    /**
78     * Get the resource where the constraint was defined, if known.
79     *
80     * @return the resource or {@code null} if the resource is not known
81     */
82    @Nullable
83    URI getSource();
84  
85    /**
86     * Get the static Metapath context to use when compiling Metapath expressions.
87     *
88     * @return the static Metapath context
89     */
90    @NonNull
91    StaticContext getStaticContext();
92  }