001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.oscal.lib;
007
008import gov.nist.secauto.metaschema.core.metapath.StaticContext;
009import gov.nist.secauto.metaschema.core.util.ObjectUtils;
010import gov.nist.secauto.metaschema.databind.DefaultBindingContext;
011import gov.nist.secauto.metaschema.databind.IBindingContext;
012import gov.nist.secauto.metaschema.databind.SimpleModuleLoaderStrategy;
013import gov.nist.secauto.oscal.lib.model.AssessmentPlan;
014import gov.nist.secauto.oscal.lib.model.AssessmentResults;
015import gov.nist.secauto.oscal.lib.model.Catalog;
016import gov.nist.secauto.oscal.lib.model.ComponentDefinition;
017import gov.nist.secauto.oscal.lib.model.OscalCompleteModule;
018import gov.nist.secauto.oscal.lib.model.PlanOfActionAndMilestones;
019import gov.nist.secauto.oscal.lib.model.Profile;
020import gov.nist.secauto.oscal.lib.model.SystemSecurityPlan;
021
022import java.io.File;
023import java.io.IOException;
024import java.net.URISyntaxException;
025import java.net.URL;
026import java.nio.file.Path;
027
028import edu.umd.cs.findbugs.annotations.NonNull;
029import nl.talsmasoftware.lazy4j.Lazy;
030
031public class OscalBindingContext
032    extends DefaultBindingContext {
033
034  @NonNull
035  public static final StaticContext OSCAL_STATIC_METAPATH_CONTEXT = StaticContext.builder()
036      .defaultModelNamespace(OscalModelConstants.NS_OSCAL)
037      .build();
038  private static final Lazy<OscalBindingContext> SINGLETON = Lazy.lazy(OscalBindingContext::new);
039
040  @NonNull
041  public static OscalBindingContext instance() {
042    return ObjectUtils.notNull(SINGLETON.get());
043  }
044
045  /**
046   * Get a new builder that can produce a new, configured OSCAL-flavored binding
047   * context.
048   *
049   * @return the builder
050   * @since 2.0.0
051   */
052  public static IBindingContext.BindingContextBuilder builder() {
053    return new IBindingContext.BindingContextBuilder(OscalBindingContext::new);
054  }
055
056  /**
057   * Get a new OSCAL-flavored {@link IBindingContext} instance, which can be used
058   * to load information that binds a model to a set of Java classes.
059   *
060   * @return a new binding context
061   * @since 2.0.0
062   */
063  @NonNull
064  public static OscalBindingContext newInstance() {
065    return new OscalBindingContext();
066  }
067
068  /**
069   * Get a new OSCAL-flavored {@link IBindingContext} instance, which can be used
070   * to load information that binds a model to a set of Java classes.
071   *
072   * @param strategy
073   *          the loader strategy to use when loading Metaschema modules
074   * @return a new binding context
075   * @since 2.0.0
076   */
077  @NonNull
078  public static OscalBindingContext newInstance(@NonNull IBindingContext.IModuleLoaderStrategy strategy) {
079    return new OscalBindingContext(strategy);
080  }
081
082  /**
083   * Construct a new OSCAL-flavored binding context.
084   */
085  protected OscalBindingContext() {
086    this(new SimpleModuleLoaderStrategy());
087  }
088
089  /**
090   * Construct a new OSCAL-flavored binding context.
091   *
092   * @param strategy
093   *          the behavior class to use for loading Metaschema modules
094   * @since 2.0.0
095   */
096  @SuppressWarnings("PMD.ConstructorCallsOverridableMethod") // false positive
097  public OscalBindingContext(@NonNull IBindingContext.IModuleLoaderStrategy strategy) {
098    super(strategy);
099    registerModule(OscalCompleteModule.class);
100  }
101
102  @NonNull
103  public Catalog loadCatalog(@NonNull URL url) throws IOException, URISyntaxException {
104    return newBoundLoader().load(Catalog.class, url);
105  }
106
107  @NonNull
108  public Catalog loadCatalog(@NonNull Path path) throws IOException {
109    return newBoundLoader().load(Catalog.class, path);
110  }
111
112  @NonNull
113  public Catalog loadCatalog(@NonNull File file) throws IOException {
114    return newBoundLoader().load(Catalog.class, file);
115  }
116
117  @NonNull
118  public Profile loadProfile(@NonNull URL url) throws IOException, URISyntaxException {
119    return newBoundLoader().load(Profile.class, url);
120  }
121
122  @NonNull
123  public Profile loadProfile(@NonNull Path path) throws IOException {
124    return newBoundLoader().load(Profile.class, path);
125  }
126
127  @NonNull
128  public Profile loadProfile(@NonNull File file) throws IOException {
129    return newBoundLoader().load(Profile.class, file);
130  }
131
132  @NonNull
133  public SystemSecurityPlan loadSystemSecurityPlan(@NonNull URL url) throws IOException, URISyntaxException {
134    return newBoundLoader().load(SystemSecurityPlan.class, url);
135  }
136
137  @NonNull
138  public SystemSecurityPlan loadSystemSecurityPlan(@NonNull Path path) throws IOException {
139    return newBoundLoader().load(SystemSecurityPlan.class, path);
140  }
141
142  @NonNull
143  public SystemSecurityPlan loadSystemSecurityPlan(@NonNull File file) throws IOException {
144    return newBoundLoader().load(SystemSecurityPlan.class, file);
145  }
146
147  @NonNull
148  public ComponentDefinition loadComponentDefinition(@NonNull URL url) throws IOException, URISyntaxException {
149    return newBoundLoader().load(ComponentDefinition.class, url);
150  }
151
152  @NonNull
153  public ComponentDefinition loadComponentDefinition(@NonNull Path path) throws IOException {
154    return newBoundLoader().load(ComponentDefinition.class, path);
155  }
156
157  @NonNull
158  public ComponentDefinition loadComponentDefinition(@NonNull File file) throws IOException {
159    return newBoundLoader().load(ComponentDefinition.class, file);
160  }
161
162  @NonNull
163  public AssessmentPlan loadAssessmentPlan(@NonNull URL url) throws IOException, URISyntaxException {
164    return newBoundLoader().load(AssessmentPlan.class, url);
165  }
166
167  @NonNull
168  public AssessmentPlan loadAssessmentPlan(@NonNull Path path) throws IOException {
169    return newBoundLoader().load(AssessmentPlan.class, path);
170  }
171
172  @NonNull
173  public AssessmentPlan loadAssessmentPlan(@NonNull File file) throws IOException {
174    return newBoundLoader().load(AssessmentPlan.class, file);
175  }
176
177  @NonNull
178  public AssessmentResults loadAssessmentResults(@NonNull URL url) throws IOException, URISyntaxException {
179    return newBoundLoader().load(AssessmentResults.class, url);
180  }
181
182  @NonNull
183  public AssessmentResults loadAssessmentResults(@NonNull Path path) throws IOException {
184    return newBoundLoader().load(AssessmentResults.class, path);
185  }
186
187  @NonNull
188  public AssessmentResults loadAssessmentResults(@NonNull File file) throws IOException {
189    return newBoundLoader().load(AssessmentResults.class, file);
190  }
191
192  @NonNull
193  public PlanOfActionAndMilestones loadPlanOfActionAndMilestones(@NonNull URL url)
194      throws IOException, URISyntaxException {
195    return newBoundLoader().load(PlanOfActionAndMilestones.class, url);
196  }
197
198  @NonNull
199  public PlanOfActionAndMilestones loadPlanOfActionAndMilestones(@NonNull Path path) throws IOException {
200    return newBoundLoader().load(PlanOfActionAndMilestones.class, path);
201  }
202
203  @NonNull
204  public PlanOfActionAndMilestones loadPlanOfActionAndMilestones(@NonNull File file) throws IOException {
205    return newBoundLoader().load(PlanOfActionAndMilestones.class, file);
206  }
207}