DefaultExpectConstraint.java

  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. package gov.nist.secauto.metaschema.core.model.constraint;

  27. import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine;
  28. import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
  29. import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
  30. import gov.nist.secauto.metaschema.core.metapath.MetapathExpression;
  31. import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem;
  32. import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
  33. import gov.nist.secauto.metaschema.core.util.ObjectUtils;
  34. import gov.nist.secauto.metaschema.core.util.ReplacementScanner;

  35. import java.util.Map;
  36. import java.util.Objects;
  37. import java.util.Set;
  38. import java.util.regex.Pattern;

  39. import javax.xml.namespace.QName;

  40. import edu.umd.cs.findbugs.annotations.NonNull;
  41. import edu.umd.cs.findbugs.annotations.Nullable;

  42. public final class DefaultExpectConstraint
  43.     extends AbstractConstraint
  44.     implements IExpectConstraint {
  45.   @SuppressWarnings("null")
  46.   @NonNull
  47.   private static final Pattern METAPATH_VALUE_TEMPLATE_PATTERN
  48.       = Pattern.compile("(?<!\\\\)(\\{\\s*((?:(?:\\\\})|[^}])*)\\s*\\})");
  49.   @NonNull
  50.   private final MetapathExpression test;
  51.   private final String message;

  52.   /**
  53.    * Construct a new expect constraint which requires that the associated test
  54.    * evaluates to {@link IBooleanItem#TRUE} against the target.
  55.    *
  56.    * @param id
  57.    *          the optional identifier for the constraint
  58.    * @param formalName
  59.    *          the constraint's formal name or {@code null} if not provided
  60.    * @param description
  61.    *          the constraint's semantic description or {@code null} if not
  62.    *          provided
  63.    * @param source
  64.    *          information about the constraint source
  65.    * @param level
  66.    *          the significance of a violation of this constraint
  67.    * @param target
  68.    *          the Metapath expression identifying the nodes the constraint targets
  69.    * @param properties
  70.    *          a collection of associated properties
  71.    * @param test
  72.    *          a Metapath expression that is evaluated against the target node to
  73.    *          determine if the constraint passes
  74.    * @param message
  75.    *          an optional message to emit when the constraint is violated
  76.    * @param remarks
  77.    *          optional remarks describing the intent of the constraint
  78.    */
  79.   private DefaultExpectConstraint(
  80.       @Nullable String id,
  81.       @Nullable String formalName,
  82.       @Nullable MarkupLine description,
  83.       @NonNull ISource source,
  84.       @NonNull Level level,
  85.       @NonNull MetapathExpression target,
  86.       @NonNull Map<QName, Set<String>> properties,
  87.       @NonNull MetapathExpression test,
  88.       @Nullable String message,
  89.       MarkupMultiline remarks) {
  90.     super(id, formalName, description, source, level, target, properties, remarks);
  91.     this.test = Objects.requireNonNull(test);
  92.     this.message = message;
  93.   }

  94.   @Override
  95.   public MetapathExpression getTest() {
  96.     return test;
  97.   }

  98.   @Override
  99.   public String getMessage() {
  100.     return message;
  101.   }

  102.   @Override
  103.   public CharSequence generateMessage(@NonNull INodeItem item, @NonNull DynamicContext context) {
  104.     String message = getMessage();

  105.     return message == null ? null
  106.         : ReplacementScanner.replaceTokens(message, METAPATH_VALUE_TEMPLATE_PATTERN, match -> {
  107.           @SuppressWarnings("null")
  108.           @NonNull String metapath = match.group(2);
  109.           MetapathExpression expr = MetapathExpression.compile(metapath);
  110.           return expr.evaluateAs(item, MetapathExpression.ResultType.STRING, context);
  111.         });
  112.   }

  113.   @Override
  114.   public <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
  115.     return visitor.visitExpectConstraint(this, state);
  116.   }

  117.   @NonNull
  118.   public static Builder builder() {
  119.     return new Builder();
  120.   }

  121.   public static final class Builder
  122.       extends AbstractConstraintBuilder<Builder, DefaultExpectConstraint> {
  123.     private MetapathExpression test;
  124.     private String message;

  125.     private Builder() {
  126.       // disable construction
  127.     }

  128.     public Builder test(@NonNull MetapathExpression test) {
  129.       this.test = test;
  130.       return this;
  131.     }

  132.     public Builder message(@NonNull String message) {
  133.       this.message = message;
  134.       return this;
  135.     }

  136.     @Override
  137.     protected Builder getThis() {
  138.       return this;
  139.     }

  140.     @Override
  141.     protected void validate() {
  142.       super.validate();

  143.       ObjectUtils.requireNonNull(getTest());
  144.     }

  145.     protected MetapathExpression getTest() {
  146.       return test;
  147.     }

  148.     protected String getMessage() {
  149.       return message;
  150.     }

  151.     @Override
  152.     protected DefaultExpectConstraint newInstance() {
  153.       return new DefaultExpectConstraint(
  154.           getId(),
  155.           getFormalName(),
  156.           getDescription(),
  157.           ObjectUtils.notNull(getSource()),
  158.           getLevel(),
  159.           getTarget(),
  160.           getProperties(),
  161.           ObjectUtils.requireNonNull(getTest()),
  162.           getMessage(),
  163.           getRemarks());
  164.     }
  165.   }
  166. }