View Javadoc
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  
27  package gov.nist.secauto.metaschema.core.metapath;
28  
29  import gov.nist.secauto.metaschema.core.metapath.antlr.metapath10Lexer;
30  import gov.nist.secauto.metaschema.core.metapath.item.ItemUtils;
31  import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
32  import gov.nist.secauto.metaschema.core.util.CollectionUtil;
33  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
34  
35  import java.util.List;
36  import java.util.function.Function;
37  import java.util.stream.Stream;
38  
39  import edu.umd.cs.findbugs.annotations.NonNull;
40  
41  @SuppressWarnings("PMD.ShortClassName") // intentional
42  public enum Axis implements IExpression {
43    SELF(metapath10Lexer.KW_SELF, focus -> Stream.of(focus)),
44    PARENT(metapath10Lexer.KW_PARENT, focus -> Stream.ofNullable(focus.getParentNodeItem())),
45    ANCESTOR(metapath10Lexer.KW_ANCESTOR, INodeItem::ancestor),
46    ANCESTOR_OR_SELF(metapath10Lexer.KW_ANCESTOR_OR_SELF, INodeItem::ancestorOrSelf),
47    CHILDREN(metapath10Lexer.KW_CHILD, INodeItem::modelItems),
48    DESCENDANT(metapath10Lexer.KW_DESCENDANT, INodeItem::descendant),
49    DESCENDANT_OR_SELF(metapath10Lexer.KW_DESCENDANT_OR_SELF, INodeItem::descendantOrSelf);
50  
51    private final int keywordIndex;
52    @NonNull
53    private final Function<INodeItem, Stream<? extends INodeItem>> action;
54  
55    Axis(int keywordIndex, @NonNull Function<INodeItem, Stream<? extends INodeItem>> action) {
56      this.keywordIndex = keywordIndex;
57      this.action = action;
58    }
59  
60    /**
61     * The ANTLR keyword for this axis type.
62     *
63     * @return the keyword
64     */
65    public int getKeywordIndex() {
66      return keywordIndex;
67    }
68  
69    /**
70     * Execute the axis operation on the provided {@code focus}.
71     *
72     * @param focus
73     *          the node to operate on
74     * @return the result of the axis operation
75     */
76    @NonNull
77    public Stream<? extends INodeItem> execute(@NonNull INodeItem focus) {
78      return ObjectUtils.notNull(action.apply(focus));
79    }
80  
81    @Override
82    public List<? extends IExpression> getChildren() {
83      return CollectionUtil.emptyList();
84    }
85  
86    @Override
87    public Class<INodeItem> getBaseResultType() {
88      return INodeItem.class;
89    }
90  
91    @Override
92    public Class<INodeItem> getStaticResultType() {
93      return getBaseResultType();
94    }
95  
96    @Override
97    public <RESULT, CONTEXT> RESULT accept(IExpressionVisitor<RESULT, CONTEXT> visitor, CONTEXT context) {
98      return visitor.visitAxis(this, context);
99    }
100 
101   @Override
102   public ISequence<? extends INodeItem> accept(
103       DynamicContext dynamicContext,
104       ISequence<?> outerFocus) {
105     ISequence<? extends INodeItem> retval;
106     if (outerFocus.isEmpty()) {
107       retval = ISequence.empty();
108     } else {
109       retval = ISequence.of(outerFocus.asStream()
110           .map(item -> ItemUtils.checkItemIsNodeItemForStep(item))
111           .flatMap(item -> {
112             assert item != null;
113             return execute(item);
114           }).distinct());
115     }
116     return retval;
117   }
118 }