FnDoc.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.DocumentFunctionException;
  31. import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils;
  32. import gov.nist.secauto.metaschema.core.metapath.function.IArgument;
  33. import gov.nist.secauto.metaschema.core.metapath.function.IFunction;
  34. import gov.nist.secauto.metaschema.core.metapath.item.IItem;
  35. import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;
  36. import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem;
  37. import gov.nist.secauto.metaschema.core.util.ObjectUtils;

  38. import java.io.IOException;
  39. import java.net.URI;
  40. import java.util.List;

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

  42. public final class FnDoc {
  43.   // private static final Logger logger = LogManager.getLogger(FnDoc.class);

  44.   @NonNull
  45.   static final IFunction SIGNATURE = IFunction.builder()
  46.       .name("doc")
  47.       .namespace(MetapathConstants.NS_XPATH_FUNCTIONS)
  48.       .deterministic()
  49.       .contextDependent()
  50.       .focusIndependent()
  51.       .argument(IArgument.newBuilder()
  52.           .name("arg1")
  53.           .type(IStringItem.class)
  54.           .zeroOrOne()
  55.           .build())
  56.       .returnType(IDocumentNodeItem.class)
  57.       .returnOne()
  58.       .functionHandler(FnDoc::execute)
  59.       .build();

  60.   private FnDoc() {
  61.     // disable construction
  62.   }

  63.   @SuppressWarnings("unused")
  64.   @NonNull
  65.   private static ISequence<IDocumentNodeItem> execute(@NonNull IFunction function,
  66.       @NonNull List<ISequence<?>> arguments, @NonNull DynamicContext dynamicContext,
  67.       IItem focus) {
  68.     ISequence<? extends IStringItem> arg = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0)));

  69.     IStringItem item = FunctionUtils.getFirstItem(arg, true);

  70.     return item == null ? ISequence.empty() : ISequence.of(fnDoc(item, dynamicContext));
  71.   }

  72.   /**
  73.    * Dynamically load the document associated with the URI, and return a
  74.    * {@link IDocumentNodeItem} containing the result.
  75.    * <p>
  76.    * Based on the XPath 3.1
  77.    * <a href="https://www.w3.org/TR/xpath-functions-31/#func-doc">fn:doc</a>
  78.    * function.
  79.    *
  80.    * @param uri
  81.    *          the resource to load the data from
  82.    * @param context
  83.    *          the Metapath dynamic context
  84.    * @return the loaded document node item
  85.    */
  86.   public static IDocumentNodeItem fnDoc(@NonNull IStringItem uri, @NonNull DynamicContext context) {
  87.     URI documentUri;
  88.     try {
  89.       documentUri = URI.create(uri.asString());
  90.     } catch (IllegalArgumentException ex) {
  91.       throw new DocumentFunctionException(DocumentFunctionException.INVALID_ARGUMENT,
  92.           String.format("Invalid URI argument '%s' to fn:doc or fn:doc-available.", uri.asString()), ex);
  93.     }

  94.     URI baseUri = context.getStaticContext().getBaseUri();
  95.     if (baseUri != null) {
  96.       // resolve if possible
  97.       documentUri = baseUri.resolve(documentUri);
  98.     } else {
  99.       if (!documentUri.isAbsolute() && !documentUri.isOpaque()) {
  100.         throw new DocumentFunctionException(DocumentFunctionException.ERROR_RETRIEVING_RESOURCE, String
  101.             .format("No base-uri is available in the static context to resolve the URI '%s'.", documentUri.toString()));
  102.       }
  103.     }

  104.     try {
  105.       return context.getDocumentLoader().loadAsNodeItem(ObjectUtils.notNull(documentUri));
  106.     } catch (IOException ex) {
  107.       throw new DocumentFunctionException(DocumentFunctionException.ERROR_RETRIEVING_RESOURCE, String
  108.           .format("Unable to retrieve the resource identified by the URI '%s'.", documentUri.toString()), ex);
  109.     }
  110.   }
  111. }