FnBoolean.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.metapath.function.library;

  27. import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
  28. import gov.nist.secauto.metaschema.core.metapath.ISequence;
  29. import gov.nist.secauto.metaschema.core.metapath.MetapathConstants;
  30. import gov.nist.secauto.metaschema.core.metapath.function.IArgument;
  31. import gov.nist.secauto.metaschema.core.metapath.function.IFunction;
  32. import gov.nist.secauto.metaschema.core.metapath.function.InvalidArgumentFunctionException;
  33. import gov.nist.secauto.metaschema.core.metapath.item.IItem;
  34. import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyUriItem;
  35. import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem;
  36. import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem;
  37. import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;
  38. import gov.nist.secauto.metaschema.core.metapath.item.atomic.IUntypedAtomicItem;
  39. import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
  40. import gov.nist.secauto.metaschema.core.util.ObjectUtils;

  41. import java.util.List;

  42. import edu.umd.cs.findbugs.annotations.NonNull;
  43. import edu.umd.cs.findbugs.annotations.Nullable;

  44. public final class FnBoolean {
  45.   @NonNull
  46.   static final IFunction SIGNATURE = IFunction.builder()
  47.       .name("boolean")
  48.       .namespace(MetapathConstants.NS_XPATH_FUNCTIONS)
  49.       .deterministic()
  50.       .contextIndependent()
  51.       .focusIndependent()
  52.       .argument(IArgument.newBuilder()
  53.           .name("arg")
  54.           .type(IItem.class)
  55.           .zeroOrMore()
  56.           .build())
  57.       .returnType(IBooleanItem.class)
  58.       .returnOne()
  59.       .functionHandler(FnBoolean::execute)
  60.       .build();

  61.   private FnBoolean() {
  62.     // disable construction
  63.   }

  64.   @SuppressWarnings("unused")
  65.   @NonNull
  66.   private static ISequence<IBooleanItem> execute(@NonNull IFunction function,
  67.       @NonNull List<ISequence<?>> arguments,
  68.       @NonNull DynamicContext dynamicContext,
  69.       IItem focus) {

  70.     ISequence<?> items = ObjectUtils.requireNonNull(arguments.iterator().next());

  71.     IBooleanItem result = fnBoolean(items);
  72.     return ISequence.of(result);
  73.   }

  74.   /**
  75.    * Get the effective boolean value of the provided sequence.
  76.    * <p>
  77.    * Based on the XPath 3.1 <a href=
  78.    * "https://www.w3.org/TR/xpath-functions-31/#func-boolean">fn:boolean</a>
  79.    * function.
  80.    *
  81.    * @param sequence
  82.    *          the sequence to evaluate
  83.    * @return the effective boolean value of the sequence
  84.    */
  85.   @NonNull
  86.   public static IBooleanItem fnBoolean(@Nullable ISequence<?> sequence) {
  87.     IBooleanItem retval;
  88.     if (sequence == null) {
  89.       retval = IBooleanItem.FALSE;
  90.     } else {
  91.       retval = IBooleanItem.valueOf(fnBooleanAsPrimitive(sequence));
  92.     }
  93.     return retval;
  94.   }

  95.   /**
  96.    * A helper method that gets the effective boolean value of the provided
  97.    * sequence based on <a href="https://www.w3.org/TR/xpath-31/#id-ebv">XPath
  98.    * 3.1</a>.
  99.    *
  100.    * @param sequence
  101.    *          the sequence to evaluate
  102.    * @return the effective boolean value
  103.    */
  104.   public static boolean fnBooleanAsPrimitive(@NonNull ISequence<?> sequence) {
  105.     boolean retval = false;
  106.     if (!sequence.isEmpty()) {
  107.       List<? extends IItem> items = sequence.asList();
  108.       IItem first = ObjectUtils.notNull(items.iterator().next());
  109.       if (first instanceof INodeItem) {
  110.         retval = true;
  111.       } else if (items.size() == 1) {
  112.         retval = fnBooleanAsPrimitive(first);
  113.       }
  114.     }
  115.     return retval;
  116.   }

  117.   /**
  118.    * A helper method that gets the effective boolean value of the provided item
  119.    * based on <a href="https://www.w3.org/TR/xpath-31/#id-ebv">XPath 3.1</a>.
  120.    *
  121.    * @param item
  122.    *          the item to evaluate
  123.    * @return the effective boolean value
  124.    */
  125.   public static boolean fnBooleanAsPrimitive(@NonNull IItem item) {
  126.     boolean retval;
  127.     if (item instanceof IBooleanItem) {
  128.       retval = ((IBooleanItem) item).toBoolean();
  129.     } else if (item instanceof IStringItem) {
  130.       String string = ((IStringItem) item).asString();
  131.       retval = !string.isBlank();
  132.     } else if (item instanceof INumericItem) {
  133.       retval = ((INumericItem) item).toEffectiveBoolean();
  134.     } else if (item instanceof IUntypedAtomicItem) {
  135.       String string = ((IUntypedAtomicItem) item).asString();
  136.       retval = !string.isBlank();
  137.     } else if (item instanceof IAnyUriItem) {
  138.       String string = ((IAnyUriItem) item).asString();
  139.       retval = !string.isBlank();
  140.     } else {
  141.       throw new InvalidArgumentFunctionException(InvalidArgumentFunctionException.INVALID_ARGUMENT_TYPE,
  142.           String.format("Invalid argument type '%s'", item.getClass().getName()));
  143.     }
  144.     return retval;
  145.   }
  146. }