FnDocumentUri.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.IDocumentNodeItem;
  36. import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
  37. import gov.nist.secauto.metaschema.core.util.ObjectUtils;

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

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

  42. public final class FnDocumentUri {

  43.   @NonNull
  44.   static final IFunction SIGNATURE_NO_ARG = IFunction.builder()
  45.       .name("document-uri")
  46.       .namespace(MetapathConstants.NS_XPATH_FUNCTIONS)
  47.       .deterministic()
  48.       .contextDependent()
  49.       .focusDependent()
  50.       .returnType(IAnyUriItem.class)
  51.       .returnOne()
  52.       .functionHandler(FnDocumentUri::executeNoArg)
  53.       .build();

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

  70.   private FnDocumentUri() {
  71.     // disable construction
  72.   }

  73.   @SuppressWarnings("unused")
  74.   @NonNull
  75.   private static ISequence<IAnyUriItem> executeNoArg(@NonNull IFunction function,
  76.       @NonNull List<ISequence<?>> arguments,
  77.       @NonNull DynamicContext dynamicContext,
  78.       IItem focus) {

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

  80.     ISequence<IAnyUriItem> retval;
  81.     if (item instanceof IDocumentNodeItem) {
  82.       IAnyUriItem uri = fnDocumentUri((IDocumentNodeItem) item);
  83.       retval = ISequence.of(uri);
  84.     } else {
  85.       retval = ISequence.empty();
  86.     }
  87.     return retval;
  88.   }

  89.   @SuppressWarnings("unused")
  90.   @NonNull
  91.   private static ISequence<IAnyUriItem> executeOneArg(@NonNull IFunction function,
  92.       @NonNull List<ISequence<?>> arguments,
  93.       @NonNull DynamicContext dynamicContext,
  94.       IItem focus) {

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

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

  97.     ISequence<IAnyUriItem> retval;
  98.     if (item == null) {
  99.       retval = ISequence.empty();
  100.     } else if (item instanceof IDocumentNodeItem) {
  101.       IAnyUriItem uri = fnDocumentUri((IDocumentNodeItem) item);
  102.       retval = ISequence.of(uri);
  103.     } else {
  104.       retval = ISequence.empty();
  105.     }
  106.     return retval;
  107.   }

  108.   /**
  109.    * Get the URI of the document.
  110.    * <p>
  111.    * Based on the XPath 3.1 <a href=
  112.    * "https://www.w3.org/TR/xpath-functions-31/#func-document-uri">fn:document-uri</a>
  113.    * function.
  114.    *
  115.    * @param document
  116.    *          the document to get the URI for
  117.    * @return the URI of the document or {@code null} if not available
  118.    */
  119.   @Nullable
  120.   public static IAnyUriItem fnDocumentUri(@NonNull IDocumentNodeItem document) {
  121.     URI documentUri = document.getDocumentUri();
  122.     return documentUri == null ? null : IAnyUriItem.valueOf(documentUri);
  123.   }
  124. }