| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.expr; |
| 9 | |
| 10 | import net.sf.saxon.expr.parser.ExpressionTool; |
| 11 | import net.sf.saxon.expr.parser.RebindingMap; |
| 12 | import net.sf.saxon.om.Item; |
| 13 | import net.sf.saxon.om.NodeInfo; |
| 14 | import net.sf.saxon.om.SequenceIterator; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | import net.sf.saxon.type.ItemType; |
| 17 | import net.sf.saxon.type.Type; |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * This class performs part of the processing in "constructing simple content": |
| 22 | * it takes an input sequence and eliminates empty text nodes |
| 23 | * into one. |
| 24 | * |
| 25 | * @since 9.3 |
| 26 | */ |
| 27 | public class EmptyTextNodeRemover extends UnaryExpression |
| 28 | implements ItemMappingFunction { |
| 29 | |
| 30 | public EmptyTextNodeRemover(Expression p0) { |
| 31 | super(p0); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Determine the data type of the expression, if possible. The default |
| 36 | * implementation for unary expressions returns the item type of the operand |
| 37 | * |
| 38 | * @return the item type of the items in the result sequence, insofar as this |
| 39 | * is known statically. |
| 40 | */ |
| 41 | |
| 42 | /*@NotNull*/ |
| 43 | @Override |
| 44 | public ItemType getItemType() { |
| 45 | return getBaseExpression().getItemType(); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public int computeCardinality() { |
| 50 | return getBaseExpression().getCardinality() | StaticProperty.ALLOWS_ZERO; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected OperandRole getOperandRole() { |
| 55 | return OperandRole.SAME_FOCUS_ACTION; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Copy an expression. This makes a deep copy. |
| 60 | * |
| 61 | * @return the copy of the original expression |
| 62 | * @param rebindings |
| 63 | */ |
| 64 | |
| 65 | /*@NotNull*/ |
| 66 | public Expression copy(RebindingMap rebindings) { |
| 67 | EmptyTextNodeRemover e2 = new EmptyTextNodeRemover(getBaseExpression().copy(rebindings)); |
| 68 | ExpressionTool.copyLocationInfo(this, e2); |
| 69 | return e2; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process(). |
| 74 | * This method indicates which of these methods is prefered. |
| 75 | */ |
| 76 | |
| 77 | public int getImplementationMethod() { |
| 78 | return Expression.ITERATE_METHOD | ITEM_FEED_METHOD | WATCH_METHOD; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Return an Iterator to iterate over the values of a sequence. The value of every |
| 83 | * expression can be regarded as a sequence, so this method is supported for all |
| 84 | * expressions. This default implementation handles iteration for expressions that |
| 85 | * return singleton values: for non-singleton expressions, the subclass must |
| 86 | * provide its own implementation. |
| 87 | * |
| 88 | * @param context supplies the context for evaluation |
| 89 | * @return a SequenceIterator that can be used to iterate over the result |
| 90 | * of the expression |
| 91 | * @throws net.sf.saxon.trans.XPathException |
| 92 | * if any dynamic error occurs evaluating the |
| 93 | * expression |
| 94 | */ |
| 95 | |
| 96 | /*@NotNull*/ |
| 97 | @Override |
| 98 | public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 99 | return new ItemMappingIterator(getBaseExpression().iterate(context), this); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Map an item to another item |
| 104 | * |
| 105 | * @param item The input item to be mapped. |
| 106 | * @return the result of the mapping: maybe null |
| 107 | * @throws XPathException |
| 108 | */ |
| 109 | |
| 110 | /*@Nullable*/ |
| 111 | public Item mapItem(Item item) throws XPathException { |
| 112 | if (item instanceof NodeInfo && |
| 113 | ((NodeInfo) item).getNodeKind() == Type.TEXT && |
| 114 | item.getStringValueCS().length() == 0) { |
| 115 | return null; |
| 116 | } else { |
| 117 | return item; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get the (partial) name of a class that supports streaming of this kind of expression |
| 123 | * |
| 124 | * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE, |
| 125 | * or null if there is no such class |
| 126 | */ |
| 127 | @Override |
| 128 | public String getStreamerName() { |
| 129 | return "EmptyTextNodeRemover"; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String getExpressionName() { |
| 134 | return "emptyTextNodeRemover"; |
| 135 | } |
| 136 | |
| 137 | } |
| 138 |