|
| 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.cst; |
| 28 | + |
| 29 | +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; |
| 30 | +import gov.nist.secauto.metaschema.core.metapath.ISequence; |
| 31 | +import gov.nist.secauto.metaschema.core.metapath.InvalidTypeMetapathException; |
| 32 | +import gov.nist.secauto.metaschema.core.metapath.function.library.FnData; |
| 33 | +import gov.nist.secauto.metaschema.core.metapath.item.IItem; |
| 34 | +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; |
| 35 | +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem; |
| 36 | +import gov.nist.secauto.metaschema.core.metapath.item.function.ArrayException; |
| 37 | +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; |
| 38 | + |
| 39 | +import java.util.stream.Stream; |
| 40 | + |
| 41 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 42 | + |
| 43 | +public abstract class AbstractLookup implements IExpression { |
| 44 | + @NonNull |
| 45 | + private final IKeySpecifier keySpecifier; |
| 46 | + |
| 47 | + protected AbstractLookup(@NonNull IKeySpecifier keySpecifier) { |
| 48 | + this.keySpecifier = keySpecifier; |
| 49 | + } |
| 50 | + |
| 51 | + @NonNull |
| 52 | + public IKeySpecifier getKeySpecifier() { |
| 53 | + return keySpecifier; |
| 54 | + } |
| 55 | + |
| 56 | + protected interface IKeySpecifier { |
| 57 | + |
| 58 | + default Stream<? extends IItem> lookup( |
| 59 | + @NonNull IItem item, |
| 60 | + @NonNull DynamicContext dynamicContext, |
| 61 | + @NonNull ISequence<?> focus) { |
| 62 | + Stream<? extends IItem> result; |
| 63 | + if (item instanceof IArrayItem) { |
| 64 | + result = lookupInArray((IArrayItem<?>) item, dynamicContext, focus); |
| 65 | + } else { |
| 66 | + throw new InvalidTypeMetapathException(item, |
| 67 | + String.format("Item type '%s' is not an array or map.", item.getClass().getName())); |
| 68 | + } |
| 69 | + return result; |
| 70 | + } |
| 71 | + |
| 72 | + Stream<? extends IItem> lookupInArray( |
| 73 | + @NonNull IArrayItem<?> item, |
| 74 | + @NonNull DynamicContext dynamicContext, |
| 75 | + @NonNull ISequence<?> focus); |
| 76 | + } |
| 77 | + |
| 78 | + protected static class NCNameKeySpecifier implements IKeySpecifier { |
| 79 | + @NonNull |
| 80 | + private final String name; |
| 81 | + |
| 82 | + public NCNameKeySpecifier(String name) { |
| 83 | + this.name = name; |
| 84 | + } |
| 85 | + |
| 86 | + public String getName() { |
| 87 | + return name; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Stream<? extends IItem> lookupInArray( |
| 92 | + IArrayItem<?> item, |
| 93 | + DynamicContext dynamicContext, |
| 94 | + ISequence<?> focus) { |
| 95 | + throw new InvalidTypeMetapathException(item, |
| 96 | + String.format("The key name-based lookup '%s' is not appropriate for an array.", getName())); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + protected static class IntegerLiteralKeySpecifier implements IKeySpecifier { |
| 101 | + private final int index; |
| 102 | + |
| 103 | + public IntegerLiteralKeySpecifier(IIntegerItem literal) { |
| 104 | + index = literal.asInteger().intValueExact() - 1; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public Stream<? extends IItem> lookupInArray( |
| 109 | + IArrayItem<?> item, |
| 110 | + DynamicContext dynamicContext, |
| 111 | + ISequence<?> focus) { |
| 112 | + try { |
| 113 | + return Stream.of(item.get(index)); |
| 114 | + } catch (IndexOutOfBoundsException ex) { |
| 115 | + throw new ArrayException( |
| 116 | + ArrayException.INDEX_OUT_OF_BOUNDS, |
| 117 | + String.format("The index %d is outside the range of values for the array size '%d'.", |
| 118 | + index + 1, |
| 119 | + item.size()), |
| 120 | + ex); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + protected static class WildcardKeySpecifier implements IKeySpecifier { |
| 126 | + |
| 127 | + @Override |
| 128 | + public Stream<? extends IItem> lookupInArray( |
| 129 | + IArrayItem<?> item, |
| 130 | + DynamicContext dynamicContext, |
| 131 | + ISequence<?> focus) { |
| 132 | + return item.stream(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public static class ParenthesizedExprKeySpecifier implements IKeySpecifier { |
| 137 | + @NonNull |
| 138 | + private final IExpression keyExpression; |
| 139 | + |
| 140 | + public ParenthesizedExprKeySpecifier(@NonNull IExpression keyExpression) { |
| 141 | + this.keyExpression = keyExpression; |
| 142 | + } |
| 143 | + |
| 144 | + public IExpression getKeyExpression() { |
| 145 | + return keyExpression; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public Stream<? extends IItem> lookupInArray( |
| 150 | + IArrayItem<?> item, |
| 151 | + DynamicContext dynamicContext, |
| 152 | + ISequence<?> focus) { |
| 153 | + ISequence<IAnyAtomicItem> keys = FnData.fnData(getKeyExpression().accept(dynamicContext, focus)); |
| 154 | + |
| 155 | + return keys.stream() |
| 156 | + .map(key -> { |
| 157 | + if (key instanceof IIntegerItem) { |
| 158 | + int index = ((IIntegerItem) key).asInteger().intValueExact() - 1; |
| 159 | + try { |
| 160 | + return item.get(index); |
| 161 | + } catch (IndexOutOfBoundsException ex) { |
| 162 | + throw new ArrayException( |
| 163 | + ArrayException.INDEX_OUT_OF_BOUNDS, |
| 164 | + String.format("The index %d is outside the range of values for the array size '%d'.", |
| 165 | + index + 1, |
| 166 | + item.size()), |
| 167 | + ex); |
| 168 | + } |
| 169 | + } |
| 170 | + throw new InvalidTypeMetapathException(item, |
| 171 | + String.format("The key '%s' of type '%s' is not appropriate for an array lookup.", |
| 172 | + key.asString(), |
| 173 | + key.getClass().getName())); |
| 174 | + |
| 175 | + }); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | +} |
0 commit comments