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.MetapathConstants;
032import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils;
033import gov.nist.secauto.metaschema.core.metapath.function.IArgument;
034import gov.nist.secauto.metaschema.core.metapath.function.IFunction;
035import gov.nist.secauto.metaschema.core.metapath.function.InvalidTypeFunctionException;
036import gov.nist.secauto.metaschema.core.metapath.item.IAtomicValuedItem;
037import gov.nist.secauto.metaschema.core.metapath.item.IItem;
038import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
039import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
040import gov.nist.secauto.metaschema.core.util.ObjectUtils;
041
042import java.util.List;
043import java.util.stream.Stream;
044
045import edu.umd.cs.findbugs.annotations.NonNull;
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 FnData {
052
053  @NonNull
054  static final IFunction SIGNATURE_NO_ARG = IFunction.builder()
055      .name("data")
056      .namespace(MetapathConstants.NS_XPATH_FUNCTIONS)
057      .deterministic()
058      .contextDependent()
059      .focusDependent()
060      .returnType(IAnyAtomicItem.class)
061      .returnOne()
062      .functionHandler(FnData::executeNoArg)
063      .build();
064
065  @NonNull
066  static final IFunction SIGNATURE_ONE_ARG = IFunction.builder()
067      .name("data")
068      .namespace(MetapathConstants.NS_XPATH_FUNCTIONS)
069      .deterministic()
070      .contextIndependent()
071      .focusIndependent()
072      .argument(IArgument.newBuilder()
073          .name("arg1")
074          .type(IItem.class)
075          .zeroOrMore()
076          .build())
077      .returnType(IAnyAtomicItem.class)
078      .returnOne()
079      .functionHandler(FnData::executeOneArg)
080      .build();
081
082  private FnData() {
083    // disable construction
084  }
085
086  @SuppressWarnings("unused")
087  @NonNull
088  private static ISequence<IAnyAtomicItem> 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<IAnyAtomicItem> retval;
096    if (item == null) {
097      retval = ISequence.empty();
098    } else {
099      IAnyAtomicItem data = fnDataItem(item);
100      retval = ISequence.of(data);
101    }
102    return retval;
103  }
104
105  @SuppressWarnings("unused")
106  @NonNull
107  private static ISequence<IAnyAtomicItem> executeOneArg(@NonNull IFunction function,
108      @NonNull List<ISequence<?>> arguments,
109      @NonNull DynamicContext dynamicContext,
110      IItem focus) {
111
112    ISequence<?> sequence = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0)));
113    return fnData(sequence);
114  }
115
116  /**
117   * An implementation of XPath 3.1
118   * <a href="https://www.w3.org/TR/xpath-functions-31/#func-data">fn:data</a>
119   * supporting <a href="https://www.w3.org/TR/xpath-31/#id-atomization">item
120   * atomization</a>.
121   *
122   * @param sequence
123   *          the sequence of items to atomize
124   * @return the atomized result
125   */
126  @SuppressWarnings("null")
127  @NonNull
128  public static ISequence<IAnyAtomicItem> fnData(@NonNull ISequence<?> sequence) {
129    @NonNull Stream<? extends IItem> stream = sequence.asStream();
130    return ISequence.of(stream.flatMap(x -> {
131      return Stream.of(fnDataItem(x));
132    }));
133  }
134
135  /**
136   * An implementation of
137   * <a href="https://www.w3.org/TR/xpath-31/#id-atomization">item
138   * atomization</a>.
139   *
140   * @param item
141   *          the item to atomize
142   * @return the atomized result
143   */
144  @NonNull
145  public static IAnyAtomicItem fnDataItem(@NonNull IItem item) {
146    IAnyAtomicItem retval = null;
147    if (item instanceof IAnyAtomicItem) {
148      retval = (IAnyAtomicItem) item;
149    } else if (item instanceof IAtomicValuedItem) {
150      retval = ((IAtomicValuedItem) item).toAtomicItem();
151    }
152
153    if (retval == null) {
154      throw new InvalidTypeFunctionException(InvalidTypeFunctionException.NODE_HAS_NO_TYPED_VALUE, item);
155    }
156    return retval;
157  }
158}