Debian Codegraph @

net/sf/saxon/expr/AdjacentTextNodeMerger.java

raw symbols SCIP
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.event.Receiver;
11 import net.sf.saxon.expr.instruct.Block;
12 import net.sf.saxon.expr.instruct.Choose;
13 import net.sf.saxon.expr.instruct.ValueOf;
14 import net.sf.saxon.expr.parser.*;
15 import net.sf.saxon.om.AtomicSequence;
16 import net.sf.saxon.om.Item;
17 import net.sf.saxon.om.NodeInfo;
18 import net.sf.saxon.om.SequenceIterator;
19 import net.sf.saxon.pattern.NodeKindTest;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.tree.iter.AdjacentTextNodeMergingIterator;
22 import net.sf.saxon.tree.util.FastStringBuffer;
23 import net.sf.saxon.type.ItemType;
24 import net.sf.saxon.type.Type;
25 import net.sf.saxon.type.TypeHierarchy;
26 import net.sf.saxon.type.UType;
27 import net.sf.saxon.value.AtomicValue;
28 import net.sf.saxon.value.Cardinality;
29
30
31 /**
32 * This class performs the first phase of processing in "constructing simple content":
33 * it takes an input sequence, eliminates empty text nodes, and combines adjacent text nodes
34 * into one.
35 *
36 * @since 9.3
37 */
38 public class AdjacentTextNodeMerger extends UnaryExpression {
39
40 public AdjacentTextNodeMerger(Expression p0) {
41 super(p0);
42 }
43
44 /**
45 * Make an AdjacentTextNodeMerger expression with a given operand, or a simpler equivalent expression if appropriate
46 *
47 * @param base the operand expression
48 * @return an AdjacentTextNodeMerger or equivalent expression
49 */
50
51 public static Expression makeAdjacentTextNodeMerger(Expression base) {
52 if (base instanceof Literal && ((Literal) base).getValue() instanceof AtomicSequence) {
53 return base;
54 } else {
55 return new AdjacentTextNodeMerger(base);
56 }
57 }
58
59 protected OperandRole getOperandRole() {
60 return OperandRole.SAME_FOCUS_ACTION;
61 }
62
63 @Override
64 public Expression simplify() throws XPathException {
65 Expression operand = getBaseExpression();
66 if (operand instanceof Literal && ((Literal) operand).getValue() instanceof AtomicValue) {
67 return operand;
68 } else {
69 return super.simplify();
70 }
71 }
72
73 /*@NotNull*/
74 @Override
75 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
76 getOperand().typeCheck(visitor, contextInfo);
77
78 // This wrapper expression is unnecessary if the base expression cannot return text nodes,
79 // or if it can return at most one item
80 TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
81 if (th.relationship(getBaseExpression().getItemType(), NodeKindTest.TEXT) == TypeHierarchy.DISJOINT) {
82 Expression base = getBaseExpression();
83 base.setParentExpression(getParentExpression());
84 return base;
85 }
86 if (!Cardinality.allowsMany(getBaseExpression().getCardinality())) {
87 Expression base = getBaseExpression();
88 base.setParentExpression(getParentExpression());
89 return base;
90 }
91 // In a choose expression, we can push the wrapper down to the action branches (whence it may disappear)
92 if (getBaseExpression() instanceof Choose) {
93 Choose choose = (Choose) getBaseExpression();
94 for (int i = 0; i < choose.size(); i++) {
95 AdjacentTextNodeMerger atm2 = new AdjacentTextNodeMerger(choose.getAction(i));
96 choose.setAction(i, atm2.typeCheck(visitor, contextInfo));
97 }
98 return choose;
99 }
100 // In a Block expression, check whether adjacent text nodes can occur (used in test strmode089)
101 // Code deleted:
102 if (getBaseExpression() instanceof Block) {
103 Block block = (Block) getBaseExpression();
104 Operand[] actions = block.getOperanda();
105 boolean prevtext = false;
106 boolean needed = false;
107 boolean maybeEmpty = false;
108 for (Operand o : actions) {
109 Expression action = o.getChildExpression();
110 boolean maybetext;
111 if (action instanceof ValueOf) {
112 maybetext = true;
113 Expression content = ((ValueOf) action).getSelect();
114 if (content instanceof StringLiteral) {
115 // if it's empty, we could remove it now, but that's awkward and probably doesn't happen
116 maybeEmpty |= ((StringLiteral) content).getStringValue().isEmpty();
117 } else {
118 maybeEmpty = true;
119 }
120 } else {
121 maybetext = th.relationship(action.getItemType(), NodeKindTest.TEXT) != TypeHierarchy.DISJOINT;
122 maybeEmpty |= maybetext;
123 }
124 if (prevtext && maybetext) {
125 needed = true;
126 break; // may contain adjacent text nodes
127 }
128 if (maybetext && Cardinality.allowsMany(action.getCardinality())) {
129 needed = true;
130 break; // may contain adjacent text nodes
131 }
132 prevtext = maybetext;
133 }
134 if (!needed) {
135 // We don't need to merge adjacent text nodes, we only need to remove empty ones.
136 if (maybeEmpty) {
137 return new EmptyTextNodeRemover(block);
138 } else {
139 return block;
140 }
141 }
142 }
143 return this;
144 }
145
146 /**
147 * Determine the data type of the expression, if possible. The default
148 * implementation for unary expressions returns the item type of the operand
149 *
150 * @return the item type of the items in the result sequence, insofar as this
151 * is known statically.
152 */
153
154 /*@NotNull*/
155 @Override
156 public ItemType getItemType() {
157 return getBaseExpression().getItemType();
158 }
159
160 /**
161 * Get the static type of the expression as a UType, following precisely the type
162 * inference rules defined in the XSLT 3.0 specification.
163 *
164 * @param contextItemType the static type of the context item
165 * @return the static item type of the expression according to the XSLT 3.0 defined rules
166 */
167 @Override
168 public UType getStaticUType(UType contextItemType) {
169 return getBaseExpression().getStaticUType(contextItemType);
170 }
171
172 @Override
173 public int computeCardinality() {
174 return getBaseExpression().getCardinality() | StaticProperty.ALLOWS_ZERO;
175 }
176
177 /**
178 * Copy an expression. This makes a deep copy.
179 *
180 * @return the copy of the original expression
181 * @param rebindings
182 */
183
184 /*@NotNull*/
185 public Expression copy(RebindingMap rebindings) {
186 AdjacentTextNodeMerger a2 = new AdjacentTextNodeMerger(getBaseExpression().copy(rebindings));
187 ExpressionTool.copyLocationInfo(this, a2);
188 return a2;
189 }
190
191 /**
192 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
193 * This method indicates which of these methods is prefered.
194 */
195
196 public int getImplementationMethod() {
197 return Expression.PROCESS_METHOD | Expression.ITERATE_METHOD | ITEM_FEED_METHOD | WATCH_METHOD;
198 }
199
200 /**
201 * Get the (partial) name of a class that supports streaming of this kind of expression
202 *
203 * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE,
204 * or null if there is no such class
205 */
206 @Override
207 public String getStreamerName() {
208 return "AdjacentTextNodeMerger";
209 }
210
211 /**
212 * Return an Iterator to iterate over the values of a sequence. The value of every
213 * expression can be regarded as a sequence, so this method is supported for all
214 * expressions. This default implementation handles iteration for expressions that
215 * return singleton values: for non-singleton expressions, the subclass must
216 * provide its own implementation.
217 *
218 * @param context supplies the context for evaluation
219 * @return a SequenceIterator that can be used to iterate over the result
220 * of the expression
221 * @throws net.sf.saxon.trans.XPathException
222 * if any dynamic error occurs evaluating the
223 * expression
224 */
225
226 /*@NotNull*/
227 @Override
228 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
229 return new AdjacentTextNodeMergingIterator(getBaseExpression().iterate(context));
230 }
231
232 /**
233 * Process the instruction, without returning any tail calls
234 *
235 * @param context The dynamic context, giving access to the current node,
236 * the current variables, etc.
237 */
238
239 public void process(XPathContext context, Location locationId, int options) throws XPathException {
240 Receiver out = context.getReceiver();
241 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C256);
242 SequenceIterator iter = getBaseExpression().iterate(context);
243 boolean prevText = false;
244 Item item;
245 while ((item = iter.next()) != null) {
246 if (isTextNode(item)) {
247 CharSequence s = item.getStringValueCS();
248 if (s.length() > 0) {
249 fsb.append(s);
250 prevText = true;
251 }
252
253 } else {
254 if (prevText) {
255 out.characters(fsb, locationId, options);
256 }
257 prevText = false;
258 fsb.setLength(0);
259 out.append(item, locationId, options);
260 }
261 }
262 if (prevText) {
263 out.characters(fsb, locationId, options);
264 }
265 }
266
267
268 @Override
269 public String getExpressionName() {
270 return "mergeAdj";
271 }
272
273 /**
274 * Ask whether an item is a text node
275 *
276 * @param item the item in question
277 * @return true if the item is a node of kind text
278 */
279
280 public static boolean isTextNode(Item item) {
281 return item instanceof NodeInfo && ((NodeInfo) item).getNodeKind() == Type.TEXT;
282 }
283
284 }
285