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.xml;
28  
29  import gov.nist.secauto.metaschema.core.metapath.MetapathException;
30  import gov.nist.secauto.metaschema.core.model.constraint.DefaultAllowedValuesConstraint;
31  import gov.nist.secauto.metaschema.core.model.constraint.DefaultExpectConstraint;
32  import gov.nist.secauto.metaschema.core.model.constraint.DefaultIndexHasKeyConstraint;
33  import gov.nist.secauto.metaschema.core.model.constraint.DefaultMatchesConstraint;
34  import gov.nist.secauto.metaschema.core.model.constraint.IAllowedValuesConstraint;
35  import gov.nist.secauto.metaschema.core.model.constraint.IConstraint;
36  import gov.nist.secauto.metaschema.core.model.constraint.IConstraint.ISource;
37  import gov.nist.secauto.metaschema.core.model.constraint.IExpectConstraint;
38  import gov.nist.secauto.metaschema.core.model.constraint.IIndexHasKeyConstraint;
39  import gov.nist.secauto.metaschema.core.model.constraint.IMatchesConstraint;
40  import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained;
41  import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.AllowedValuesType;
42  import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.DefineFieldConstraintsType;
43  import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.DefineFlagConstraintsType;
44  import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.ExpectConstraintType;
45  import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.IndexHasKeyConstraintType;
46  import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.MatchesConstraintType;
47  
48  import org.apache.xmlbeans.XmlCursor;
49  import org.apache.xmlbeans.XmlObject;
50  import org.apache.xmlbeans.impl.values.XmlValueNotSupportedException;
51  
52  import java.util.LinkedList;
53  import java.util.List;
54  
55  import edu.umd.cs.findbugs.annotations.NonNull;
56  
57  class ValueConstraintSupport implements IValueConstrained { // NOPMD - intentional
58    private static final String PATH = "declare namespace m='http://csrc.nist.gov/ns/oscal/metaschema/1.0';"
59        + "$this/m:allowed-values|$this/m:matches|$this/m:index-has-key|$this/m:expect";
60  
61    @NonNull
62    private final List<IConstraint> constraints = new LinkedList<>();
63    @NonNull
64    private final List<IAllowedValuesConstraint> allowedValuesConstraints = new LinkedList<>();
65    @NonNull
66    private final List<IMatchesConstraint> matchesConstraints = new LinkedList<>();
67    @NonNull
68    private final List<IIndexHasKeyConstraint> indexHasKeyConstraints = new LinkedList<>();
69    @NonNull
70    private final List<IExpectConstraint> expectConstraints = new LinkedList<>();
71  
72    public ValueConstraintSupport() {
73      // empty constraints
74    }
75  
76    /**
77     * Generate a set of constraints from the provided XMLBeans instance.
78     *
79     * @param xmlConstraints
80     *          the XMLBeans instance
81     * @param source
82     *          information about the source of the constraints
83     */
84    @SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
85    public ValueConstraintSupport( // NOPMD - intentional
86        @NonNull DefineFlagConstraintsType xmlConstraints,
87        @NonNull ISource source) {
88      try (XmlCursor cursor = xmlConstraints.newCursor()) {
89        assert cursor != null;
90        parse(source, cursor, PATH);
91      }
92    }
93  
94    /**
95     * Generate a set of constraints from the provided XMLBeans instance.
96     *
97     * @param xmlConstraints
98     *          the XMLBeans instance
99     * @param source
100    *          information about the source of the constraints
101    */
102   @SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
103   public ValueConstraintSupport( // NOPMD - intentional
104       @NonNull DefineFieldConstraintsType xmlConstraints,
105       @NonNull ISource source) {
106     try (XmlCursor cursor = xmlConstraints.newCursor()) {
107       assert cursor != null;
108       parse(source, cursor, PATH);
109     }
110   }
111 
112   protected final void parse(
113       @NonNull ISource source,
114       @NonNull XmlCursor cursor,
115       @NonNull String path) {
116     try {
117       assert cursor != null;
118       cursor.selectPath(path);
119       while (cursor.toNextSelection()) {
120         XmlObject obj = cursor.getObject();
121         assert obj != null;
122         parseXmlObject(source, obj);
123       }
124     } catch (MetapathException | XmlValueNotSupportedException ex) {
125       if (ex.getCause() instanceof MetapathException) {
126         throw new MetapathException(
127             String.format("Unable to compile a Metapath in '%s'. %s",
128                 source.getSource(),
129                 ex.getLocalizedMessage()),
130             ex);
131       }
132       throw ex;
133     }
134   }
135 
136   protected boolean parseXmlObject(
137       @NonNull ISource source,
138       @NonNull XmlObject obj) {
139     boolean handled = false;
140     if (obj instanceof AllowedValuesType) {
141       DefaultAllowedValuesConstraint constraint
142           = ModelFactory.newAllowedValuesConstraint((AllowedValuesType) obj, source);
143       addConstraint(constraint);
144       handled = true;
145     } else if (obj instanceof MatchesConstraintType) {
146       DefaultMatchesConstraint constraint
147           = ModelFactory.newMatchesConstraint((MatchesConstraintType) obj, source);
148       addConstraint(constraint);
149       handled = true;
150     } else if (obj instanceof IndexHasKeyConstraintType) {
151       DefaultIndexHasKeyConstraint constraint
152           = ModelFactory.newIndexHasKeyConstraint((IndexHasKeyConstraintType) obj, source);
153       addConstraint(constraint);
154       handled = true;
155     } else if (obj instanceof ExpectConstraintType) {
156       DefaultExpectConstraint constraint = ModelFactory.newExpectConstraint((ExpectConstraintType) obj, source);
157       addConstraint(constraint);
158       handled = true;
159     }
160     return handled;
161   }
162 
163   @Override
164   public List<IConstraint> getConstraints() {
165     synchronized (this) {
166       return constraints;
167     }
168   }
169 
170   @Override
171   public List<IAllowedValuesConstraint> getAllowedValuesConstraints() {
172     synchronized (this) {
173       return allowedValuesConstraints;
174     }
175   }
176 
177   @Override
178   public List<IMatchesConstraint> getMatchesConstraints() {
179     synchronized (this) {
180       return matchesConstraints;
181     }
182   }
183 
184   @Override
185   public List<IIndexHasKeyConstraint> getIndexHasKeyConstraints() {
186     synchronized (this) {
187       return indexHasKeyConstraints;
188     }
189   }
190 
191   @Override
192   public List<IExpectConstraint> getExpectConstraints() {
193     synchronized (this) {
194       return expectConstraints;
195     }
196   }
197 
198   @Override
199   public final void addConstraint(@NonNull IAllowedValuesConstraint constraint) {
200     synchronized (this) {
201       constraints.add(constraint);
202       allowedValuesConstraints.add(constraint);
203     }
204   }
205 
206   @Override
207   public final void addConstraint(@NonNull IMatchesConstraint constraint) {
208     synchronized (this) {
209       constraints.add(constraint);
210       matchesConstraints.add(constraint);
211     }
212   }
213 
214   @Override
215   public final void addConstraint(@NonNull IIndexHasKeyConstraint constraint) {
216     synchronized (this) {
217       constraints.add(constraint);
218       indexHasKeyConstraints.add(constraint);
219     }
220   }
221 
222   @Override
223   public final void addConstraint(@NonNull IExpectConstraint constraint) {
224     synchronized (this) {
225       constraints.add(constraint);
226       expectConstraints.add(constraint);
227     }
228   }
229 }