View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.metaschema.core.model.constraint;
28  
29  import gov.nist.secauto.metaschema.core.metapath.MetapathExpression;
30  import gov.nist.secauto.metaschema.core.metapath.MetapathExpression.ResultType;
31  import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem;
32  import gov.nist.secauto.metaschema.core.metapath.item.node.IModuleNodeItem;
33  import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
34  import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory;
35  import gov.nist.secauto.metaschema.core.model.IDefinition;
36  import gov.nist.secauto.metaschema.core.model.IModule;
37  import gov.nist.secauto.metaschema.core.model.MetaschemaException;
38  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
39  
40  import java.util.Collection;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.Set;
44  import java.util.stream.Collectors;
45  import java.util.stream.Stream;
46  
47  import javax.xml.namespace.QName;
48  
49  import edu.umd.cs.findbugs.annotations.NonNull;
50  
51  public interface IConstraintSet {
52  
53    @NonNull
54    static Set<IConstraintSet> resolveConstraintSets(@NonNull Set<IConstraintSet> constraintSets) {
55      return ObjectUtils.notNull(constraintSets.stream()
56          .flatMap(set -> resolveConstraintSet(ObjectUtils.notNull(set)))
57          .distinct()
58          .collect(Collectors.toUnmodifiableSet()));
59    }
60  
61    @NonNull
62    private static Stream<IConstraintSet> resolveConstraintSet(@NonNull IConstraintSet constraintSet) {
63      return ObjectUtils.notNull(Stream.concat(
64          Stream.of(constraintSet),
65          constraintSet.getImportedConstraintSets().stream()));
66    }
67  
68    @NonNull
69    static List<ITargetedConstaints> getTargetedConstraintsForMetaschema(
70        @NonNull Set<IConstraintSet> constraintSets,
71        @NonNull IModule module) {
72      return ObjectUtils.notNull(resolveConstraintSets(constraintSets).stream()
73          .flatMap(set -> set.getTargetedConstraintsForModule(module))
74          .collect(Collectors.toUnmodifiableList()));
75    }
76  
77    @NonNull
78    Stream<ITargetedConstaints> getTargetedConstraintsForModule(@NonNull IModule module);
79  
80    static void applyConstraintSetToModule(
81        @NonNull Set<IConstraintSet> constraintSets,
82        @NonNull IModule module) throws MetaschemaException {
83      Set<IConstraintSet> resolvedConstraintSets = resolveConstraintSets(constraintSets);
84  
85      ConstraintComposingVisitor visitor = new ConstraintComposingVisitor();
86      IModuleNodeItem item = INodeItemFactory.instance().newModuleNodeItem(module);
87  
88      for (ITargetedConstaints targeted : getTargetedConstraintsForMetaschema(resolvedConstraintSets, module)) {
89        MetapathExpression targetExpression = targeted.getTargetExpression();
90        INodeItem node = targetExpression.evaluateAs(item, ResultType.NODE);
91        if (node == null) {
92          throw new MetaschemaException(String.format("Target not found for expression '%s' on metaschema '%s'.",
93              targetExpression.getPath(),
94              module.getQName()));
95        } else if (node instanceof IDefinitionNodeItem) {
96          IDefinition nodeDefinition = ((IDefinitionNodeItem<?, ?>) node).getDefinition();
97          IModule nodeModule = nodeDefinition.getContainingModule();
98          if (!module.equals(nodeModule)) {
99            throw new MetaschemaException(
100               String.format("Target definition '%s' in metaschema '%s' is not in the scoped metaschema '%s'.",
101                   nodeDefinition.getName(),
102                   nodeModule.getQName(),
103                   module.getQName()));
104         }
105       }
106       node.accept(visitor, targeted);
107     }
108 
109   }
110 
111   Collection<IConstraintSet> getImportedConstraintSets();
112 
113   /**
114    * Get the set of Metaschema scoped constraints to apply by a {@link QName}
115    * formed from the Metaschema namespace and short name.
116    *
117    * @return the mapping of QName to scoped constraints
118    */
119   @NonNull
120   Map<QName, List<IScopedContraints>> getScopedContraints();
121 }