| 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.pattern; |
| 9 | |
| 10 | import net.sf.saxon.expr.XPathContext; |
| 11 | import net.sf.saxon.expr.parser.RebindingMap; |
| 12 | import net.sf.saxon.om.Item; |
| 13 | import net.sf.saxon.trace.ExpressionPresenter; |
| 14 | import net.sf.saxon.type.AnyItemType; |
| 15 | import net.sf.saxon.type.ItemType; |
| 16 | import net.sf.saxon.type.UType; |
| 17 | |
| 18 | /** |
| 19 | * The UniversalPattern matches everything |
| 20 | * |
| 21 | * @author Michael H. Kay |
| 22 | */ |
| 23 | |
| 24 | public class UniversalPattern extends Pattern { |
| 25 | |
| 26 | /** |
| 27 | * Create an UniversalPattern that matches all items |
| 28 | */ |
| 29 | |
| 30 | public UniversalPattern() { |
| 31 | setPriority(-1); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Determine whether this Pattern matches the given Node. This is the main external interface |
| 36 | * for matching patterns: it sets current() to the node being tested |
| 37 | * |
| 38 | * @param item The item to be tested against the Pattern |
| 39 | * @param context The context in which the match is to take place. |
| 40 | * @return true if the item matches the Pattern, false otherwise |
| 41 | */ |
| 42 | |
| 43 | public boolean matches(Item<?> item, XPathContext context) { |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get a UType indicating which kinds of items this Pattern can match. |
| 49 | * |
| 50 | * @return a UType indicating all the primitive types of item that the pattern can match. |
| 51 | */ |
| 52 | @Override |
| 53 | public UType getUType() { |
| 54 | return UType.ANY; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get a NodeTest that all the nodes matching this pattern must satisfy |
| 59 | */ |
| 60 | |
| 61 | public ItemType getItemType() { |
| 62 | return AnyItemType.getInstance(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Determine the name fingerprint of nodes to which this pattern applies. Used for |
| 67 | * optimisation. |
| 68 | * |
| 69 | * @return A fingerprint that the nodes must match, or -1 if it can match multiple fingerprints |
| 70 | */ |
| 71 | |
| 72 | public int getFingerprint() { |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Display the pattern for diagnostics |
| 78 | */ |
| 79 | |
| 80 | public String toString() { |
| 81 | return "."; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void export(ExpressionPresenter presenter) { |
| 86 | presenter.startElement("p.any"); |
| 87 | presenter.endElement(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Determine whether this pattern is the same as another pattern |
| 92 | * |
| 93 | * @param other the other object |
| 94 | */ |
| 95 | |
| 96 | public boolean equals(/*@NotNull*/ Object other) { |
| 97 | return other instanceof UniversalPattern; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Hashcode supporting equals() |
| 102 | */ |
| 103 | |
| 104 | public int computeHashCode() { |
| 105 | return 0x7aeccea8; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Copy a UniversalPattern. |
| 110 | * Since there is only one, return the same. |
| 111 | * |
| 112 | * @return the original nodeTest |
| 113 | * @param rebindings variables that need to be re-bound |
| 114 | */ |
| 115 | |
| 116 | /*@NotNull*/ |
| 117 | public Pattern copy(RebindingMap rebindings) { |
| 118 | return this; |
| 119 | } |
| 120 | } |
| 121 |