FnBaseUri.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.item.IItem;
  34. import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyUriItem;
  35. import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
  36. import gov.nist.secauto.metaschema.core.util.ObjectUtils;

  37. import java.net.URI;
  38. import java.util.List;

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

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

  46.   @NonNull
  47.   static final IFunction SIGNATURE_NO_ARG = IFunction.builder()
  48.       .name("base-uri")
  49.       .namespace(MetapathConstants.NS_XPATH_FUNCTIONS)
  50.       .deterministic()
  51.       .contextDependent()
  52.       .focusDependent()
  53.       .returnType(IAnyUriItem.class)
  54.       .returnOne()
  55.       .functionHandler(FnBaseUri::executeNoArg)
  56.       .build();

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

  73.   private FnBaseUri() {
  74.     // disable construction
  75.   }

  76.   @SuppressWarnings("unused")
  77.   @NonNull
  78.   private static ISequence<IAnyUriItem> executeNoArg(@NonNull IFunction function,
  79.       @NonNull List<ISequence<?>> arguments,
  80.       @NonNull DynamicContext dynamicContext,
  81.       IItem focus) {
  82.     return ISequence.of(fnBaseUri(
  83.         FunctionUtils.requireTypeOrNull(INodeItem.class, focus)));
  84.   }

  85.   @SuppressWarnings("unused")
  86.   @NonNull
  87.   private static ISequence<IAnyUriItem> executeOneArg(@NonNull IFunction function,
  88.       @NonNull List<ISequence<?>> arguments,
  89.       @NonNull DynamicContext dynamicContext,
  90.       IItem focus) {

  91.     ISequence<? extends INodeItem> arg = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0)));

  92.     INodeItem item = FunctionUtils.getFirstItem(arg, true);

  93.     return ISequence.of(fnBaseUri(item));
  94.   }

  95.   /**
  96.    * Get the base URI for the provided {@code nodeItem}.
  97.    * <p>
  98.    * Based on the XPath 3.1 <a href=
  99.    * "https://www.w3.org/TR/xpath-functions-31/#func-base-uri">fn:base-uri</a>
  100.    * function.
  101.    *
  102.    * @param nodeItem
  103.    *          the node to get the base URI from
  104.    * @return the base URI, or {@code null} if the node is either null or doesn't
  105.    *         have a base URI
  106.    */
  107.   @SuppressWarnings("PMD.NullAssignment") // for readability
  108.   @Nullable
  109.   public static IAnyUriItem fnBaseUri(INodeItem nodeItem) {
  110.     IAnyUriItem retval;
  111.     if (nodeItem == null) {
  112.       retval = null; // NOPMD - intentional
  113.     } else {
  114.       URI baseUri = nodeItem.getBaseUri();
  115.       retval = baseUri == null ? null : IAnyUriItem.valueOf(baseUri);
  116.     }
  117.     return retval;
  118.   }
  119. }