FnData.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.FunctionUtils;
  31. import gov.nist.secauto.metaschema.core.metapath.function.IArgument;
  32. import gov.nist.secauto.metaschema.core.metapath.function.IFunction;
  33. import gov.nist.secauto.metaschema.core.metapath.function.InvalidTypeFunctionException;
  34. import gov.nist.secauto.metaschema.core.metapath.item.IAtomicValuedItem;
  35. import gov.nist.secauto.metaschema.core.metapath.item.IItem;
  36. import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
  37. import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
  38. import gov.nist.secauto.metaschema.core.util.ObjectUtils;

  39. import java.util.List;
  40. import java.util.stream.Stream;

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

  42. /**
  43.  * Since a node doesn't have a base URI in Metaschema, this is an alias for the
  44.  * document-uri function.
  45.  */
  46. public final class FnData {

  47.   @NonNull
  48.   static final IFunction SIGNATURE_NO_ARG = IFunction.builder()
  49.       .name("data")
  50.       .namespace(MetapathConstants.NS_XPATH_FUNCTIONS)
  51.       .deterministic()
  52.       .contextDependent()
  53.       .focusDependent()
  54.       .returnType(IAnyAtomicItem.class)
  55.       .returnOne()
  56.       .functionHandler(FnData::executeNoArg)
  57.       .build();

  58.   @NonNull
  59.   static final IFunction SIGNATURE_ONE_ARG = IFunction.builder()
  60.       .name("data")
  61.       .namespace(MetapathConstants.NS_XPATH_FUNCTIONS)
  62.       .deterministic()
  63.       .contextIndependent()
  64.       .focusIndependent()
  65.       .argument(IArgument.newBuilder()
  66.           .name("arg1")
  67.           .type(IItem.class)
  68.           .zeroOrMore()
  69.           .build())
  70.       .returnType(IAnyAtomicItem.class)
  71.       .returnOne()
  72.       .functionHandler(FnData::executeOneArg)
  73.       .build();

  74.   private FnData() {
  75.     // disable construction
  76.   }

  77.   @SuppressWarnings("unused")
  78.   @NonNull
  79.   private static ISequence<IAnyAtomicItem> executeNoArg(@NonNull IFunction function,
  80.       @NonNull List<ISequence<?>> arguments,
  81.       @NonNull DynamicContext dynamicContext,
  82.       IItem focus) {

  83.     INodeItem item = FunctionUtils.requireTypeOrNull(INodeItem.class, focus);

  84.     ISequence<IAnyAtomicItem> retval;
  85.     if (item == null) {
  86.       retval = ISequence.empty();
  87.     } else {
  88.       IAnyAtomicItem data = fnDataItem(item);
  89.       retval = ISequence.of(data);
  90.     }
  91.     return retval;
  92.   }

  93.   @SuppressWarnings("unused")
  94.   @NonNull
  95.   private static ISequence<IAnyAtomicItem> executeOneArg(@NonNull IFunction function,
  96.       @NonNull List<ISequence<?>> arguments,
  97.       @NonNull DynamicContext dynamicContext,
  98.       IItem focus) {

  99.     ISequence<?> sequence = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0)));
  100.     return fnData(sequence);
  101.   }

  102.   /**
  103.    * An implementation of XPath 3.1
  104.    * <a href="https://www.w3.org/TR/xpath-functions-31/#func-data">fn:data</a>
  105.    * supporting <a href="https://www.w3.org/TR/xpath-31/#id-atomization">item
  106.    * atomization</a>.
  107.    *
  108.    * @param sequence
  109.    *          the sequence of items to atomize
  110.    * @return the atomized result
  111.    */
  112.   @SuppressWarnings("null")
  113.   @NonNull
  114.   public static ISequence<IAnyAtomicItem> fnData(@NonNull ISequence<?> sequence) {
  115.     @NonNull Stream<? extends IItem> stream = sequence.asStream();
  116.     return ISequence.of(stream.flatMap(x -> {
  117.       return Stream.of(fnDataItem(x));
  118.     }));
  119.   }

  120.   /**
  121.    * An implementation of
  122.    * <a href="https://www.w3.org/TR/xpath-31/#id-atomization">item
  123.    * atomization</a>.
  124.    *
  125.    * @param item
  126.    *          the item to atomize
  127.    * @return the atomized result
  128.    */
  129.   @NonNull
  130.   public static IAnyAtomicItem fnDataItem(@NonNull IItem item) {
  131.     IAnyAtomicItem retval = null;
  132.     if (item instanceof IAnyAtomicItem) {
  133.       retval = (IAnyAtomicItem) item;
  134.     } else if (item instanceof IAtomicValuedItem) {
  135.       retval = ((IAtomicValuedItem) item).toAtomicItem();
  136.     }

  137.     if (retval == null) {
  138.       throw new InvalidTypeFunctionException(InvalidTypeFunctionException.NODE_HAS_NO_TYPED_VALUE, item);
  139.     }
  140.     return retval;
  141.   }
  142. }