FnPath.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.InvalidTypeMetapathException;
  30. import gov.nist.secauto.metaschema.core.metapath.MetapathConstants;
  31. import gov.nist.secauto.metaschema.core.metapath.format.IPathFormatter;
  32. import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils;
  33. import gov.nist.secauto.metaschema.core.metapath.function.IArgument;
  34. import gov.nist.secauto.metaschema.core.metapath.function.IFunction;
  35. import gov.nist.secauto.metaschema.core.metapath.item.IItem;
  36. import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;
  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 edu.umd.cs.findbugs.annotations.NonNull;
  41. import edu.umd.cs.findbugs.annotations.Nullable;

  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 FnPath {

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

  58.   @NonNull
  59.   static final IFunction SIGNATURE_ONE_ARG = IFunction.builder()
  60.       .name("path")
  61.       .namespace(MetapathConstants.NS_XPATH_FUNCTIONS)
  62.       .deterministic()
  63.       .contextIndependent()
  64.       .focusIndependent()
  65.       .argument(IArgument.newBuilder()
  66.           .name("arg1")
  67.           .type(INodeItem.class)
  68.           .zeroOrOne()
  69.           .build())
  70.       .returnType(IStringItem.class)
  71.       .returnZeroOrOne()
  72.       .functionHandler(FnPath::executeOneArg)
  73.       .build();

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

  77.   @SuppressWarnings("unused")
  78.   @NonNull
  79.   private static ISequence<IStringItem> 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<IStringItem> retval;
  85.     if (item == null) {
  86.       retval = ISequence.empty();
  87.     } else {
  88.       retval = ISequence.of(IStringItem.valueOf(item.toPath(IPathFormatter.METAPATH_PATH_FORMATER)));
  89.     }
  90.     return retval;
  91.   }

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

  98.     return fnPath(ObjectUtils.requireNonNull(arguments.get(0)));
  99.   }

  100.   /**
  101.    * An implementation of XPath 3.1
  102.    * <a href="https://www.w3.org/TR/xpath-functions-31/#func-data">fn:data</a>
  103.    * supporting <a href="https://www.w3.org/TR/xpath-31/#id-atomization">item
  104.    * atomization</a>.
  105.    *
  106.    * @param sequence
  107.    *          the sequence of items to atomize
  108.    * @return the atomized result
  109.    */
  110.   @NonNull
  111.   public static ISequence<IStringItem> fnPath(@NonNull ISequence<?> sequence) {
  112.     IItem item = FunctionUtils.getFirstItem(sequence, true);

  113.     ISequence<IStringItem> retval;
  114.     if (item == null) {
  115.       retval = ISequence.empty();
  116.     } else {
  117.       try {
  118.         retval = ISequence.of(fnPath((INodeItem) item));
  119.       } catch (ClassCastException ex) {
  120.         throw new InvalidTypeMetapathException(
  121.             item,
  122.             String.format("Expected a '%s', but received a '%s'",
  123.                 INodeItem.class.getName(),
  124.                 item.getClass().getName()),
  125.             ex);
  126.       }
  127.     }
  128.     return retval;
  129.   }

  130.   /**
  131.    * An implementation of
  132.    * <a href="https://www.w3.org/TR/xpath-31/#id-atomization">item
  133.    * atomization</a>.
  134.    *
  135.    * @param item
  136.    *          the item to atomize
  137.    * @return the atomized result
  138.    */
  139.   @Nullable
  140.   public static IStringItem fnPath(@Nullable INodeItem item) {
  141.     return item == null ? null : IStringItem.valueOf(item.toPath(IPathFormatter.METAPATH_PATH_FORMATER));
  142.   }
  143. }