001/*
002 * Portions of this software was developed by employees of the National Institute
003 * of Standards and Technology (NIST), an agency of the Federal Government and is
004 * being made available as a public service. Pursuant to title 17 United States
005 * Code Section 105, works of NIST employees are not subject to copyright
006 * protection in the United States. This software may be subject to foreign
007 * copyright. Permission in the United States and in foreign countries, to the
008 * extent that NIST may hold copyright, to use, copy, modify, create derivative
009 * works, and distribute this software and its documentation without fee is hereby
010 * granted on a non-exclusive basis, provided that this notice and disclaimer
011 * of warranty appears in all copies.
012 *
013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
025 */
026
027package gov.nist.secauto.metaschema.core.metapath.function.library;
028
029import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
030import gov.nist.secauto.metaschema.core.metapath.ISequence;
031import gov.nist.secauto.metaschema.core.metapath.InvalidTypeMetapathException;
032import gov.nist.secauto.metaschema.core.metapath.MetapathConstants;
033import gov.nist.secauto.metaschema.core.metapath.format.IPathFormatter;
034import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils;
035import gov.nist.secauto.metaschema.core.metapath.function.IArgument;
036import gov.nist.secauto.metaschema.core.metapath.function.IFunction;
037import gov.nist.secauto.metaschema.core.metapath.item.IItem;
038import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;
039import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
040import gov.nist.secauto.metaschema.core.util.ObjectUtils;
041
042import java.util.List;
043
044import edu.umd.cs.findbugs.annotations.NonNull;
045import edu.umd.cs.findbugs.annotations.Nullable;
046
047/**
048 * Since a node doesn't have a base URI in Metaschema, this is an alias for the
049 * document-uri function.
050 */
051public final class FnPath {
052
053  @NonNull
054  static final IFunction SIGNATURE_NO_ARG = IFunction.builder()
055      .name("path")
056      .namespace(MetapathConstants.NS_XPATH_FUNCTIONS)
057      .deterministic()
058      .contextDependent()
059      .focusDependent()
060      .returnType(IStringItem.class)
061      .returnZeroOrOne()
062      .functionHandler(FnPath::executeNoArg)
063      .build();
064
065  @NonNull
066  static final IFunction SIGNATURE_ONE_ARG = IFunction.builder()
067      .name("path")
068      .namespace(MetapathConstants.NS_XPATH_FUNCTIONS)
069      .deterministic()
070      .contextIndependent()
071      .focusIndependent()
072      .argument(IArgument.newBuilder()
073          .name("arg1")
074          .type(INodeItem.class)
075          .zeroOrOne()
076          .build())
077      .returnType(IStringItem.class)
078      .returnZeroOrOne()
079      .functionHandler(FnPath::executeOneArg)
080      .build();
081
082  private FnPath() {
083    // disable construction
084  }
085
086  @SuppressWarnings("unused")
087  @NonNull
088  private static ISequence<IStringItem> executeNoArg(@NonNull IFunction function,
089      @NonNull List<ISequence<?>> arguments,
090      @NonNull DynamicContext dynamicContext,
091      IItem focus) {
092
093    INodeItem item = FunctionUtils.requireTypeOrNull(INodeItem.class, focus);
094
095    ISequence<IStringItem> retval;
096    if (item == null) {
097      retval = ISequence.empty();
098    } else {
099      retval = ISequence.of(IStringItem.valueOf(item.toPath(IPathFormatter.METAPATH_PATH_FORMATER)));
100    }
101    return retval;
102  }
103
104  @SuppressWarnings("unused")
105  @NonNull
106  private static ISequence<IStringItem> executeOneArg(@NonNull IFunction function,
107      @NonNull List<ISequence<?>> arguments,
108      @NonNull DynamicContext dynamicContext,
109      IItem focus) {
110
111    return fnPath(ObjectUtils.requireNonNull(arguments.get(0)));
112  }
113
114  /**
115   * An implementation of XPath 3.1
116   * <a href="https://www.w3.org/TR/xpath-functions-31/#func-data">fn:data</a>
117   * supporting <a href="https://www.w3.org/TR/xpath-31/#id-atomization">item
118   * atomization</a>.
119   *
120   * @param sequence
121   *          the sequence of items to atomize
122   * @return the atomized result
123   */
124  @NonNull
125  public static ISequence<IStringItem> fnPath(@NonNull ISequence<?> sequence) {
126    IItem item = FunctionUtils.getFirstItem(sequence, true);
127
128    ISequence<IStringItem> retval;
129    if (item == null) {
130      retval = ISequence.empty();
131    } else {
132      try {
133        retval = ISequence.of(fnPath((INodeItem) item));
134      } catch (ClassCastException ex) {
135        throw new InvalidTypeMetapathException(
136            item,
137            String.format("Expected a '%s', but received a '%s'",
138                INodeItem.class.getName(),
139                item.getClass().getName()),
140            ex);
141      }
142    }
143    return retval;
144  }
145
146  /**
147   * An implementation of
148   * <a href="https://www.w3.org/TR/xpath-31/#id-atomization">item
149   * atomization</a>.
150   *
151   * @param item
152   *          the item to atomize
153   * @return the atomized result
154   */
155  @Nullable
156  public static IStringItem fnPath(@Nullable INodeItem item) {
157    return item == null ? null : IStringItem.valueOf(item.toPath(IPathFormatter.METAPATH_PATH_FORMATER));
158  }
159}