| 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.s9api.streams; |
| 9 | |
| 10 | import net.sf.saxon.expr.sort.AtomicMatchKey; |
| 11 | import net.sf.saxon.s9api.*; |
| 12 | |
| 13 | import java.util.function.Predicate; |
| 14 | import java.util.regex.Pattern; |
| 15 | |
| 16 | /** |
| 17 | * This non-instantiable class provides a number of useful implementations of the {@link Predicate} |
| 18 | * interface, designed for use when navigating streams of XDM items. |
| 19 | */ |
| 20 | |
| 21 | public class Predicates { |
| 22 | /** |
| 23 | * A predicate to test whether an item is a node |
| 24 | * @return a predicate that returns true if given an item that is a node |
| 25 | */ |
| 26 | |
| 27 | public static Predicate<XdmItem> isNode() { |
| 28 | return item -> item instanceof XdmNode; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * A predicate to test whether an item is an element node |
| 33 | * @return a predicate that returns true if given an item that is an element node |
| 34 | */ |
| 35 | |
| 36 | public static Predicate<XdmItem> isElement() { |
| 37 | return nodeKindPredicate(XdmNodeKind.ELEMENT); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * A predicate to test whether an item is an attribute node |
| 42 | * @return a predicate that returns true if given an item that is an attribute node |
| 43 | */ |
| 44 | |
| 45 | public static Predicate<XdmItem> isAttribute() { |
| 46 | return nodeKindPredicate(XdmNodeKind.ATTRIBUTE); |
| 47 | } |
| 48 | /** |
| 49 | * A predicate to test whether an item is a text node |
| 50 | * @return a predicate that returns true if given an item that is a text node |
| 51 | */ |
| 52 | |
| 53 | public static Predicate<XdmItem> isText() { |
| 54 | return nodeKindPredicate(XdmNodeKind.TEXT); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * A predicate to test whether an item is a comment node |
| 59 | * @return a predicate that returns true if given an item that is a comment node |
| 60 | */ |
| 61 | |
| 62 | public static Predicate<XdmItem> isComment() { |
| 63 | return nodeKindPredicate(XdmNodeKind.COMMENT); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * A predicate to test whether an item is a processing instruction node |
| 68 | * @return a predicate that returns true if given an item that is a processing instruction node |
| 69 | */ |
| 70 | |
| 71 | public static Predicate<XdmItem> isProcessingInstruction() { |
| 72 | return nodeKindPredicate(XdmNodeKind.PROCESSING_INSTRUCTION); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * A predicate to test whether an item is a document node |
| 77 | * @return a predicate that returns true if given an item that is a document node |
| 78 | */ |
| 79 | |
| 80 | public static Predicate<XdmItem> isDocument() { |
| 81 | return nodeKindPredicate(XdmNodeKind.DOCUMENT); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * A predicate to test whether an item is a namespace node |
| 86 | * @return a predicate that returns true if given an item that is a namespace node |
| 87 | */ |
| 88 | |
| 89 | public static Predicate<XdmItem> isNamespace() { |
| 90 | return nodeKindPredicate(XdmNodeKind.NAMESPACE); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * A predicate to test whether an item is an atomic value |
| 95 | * <p>Note: to test for a specific type of atomic value, use a predicate such as |
| 96 | * {@code hasType(ItemType.XS_INTEGER)}</p> |
| 97 | * @return a predicate that returns true if given an item that is an atomic value. |
| 98 | */ |
| 99 | |
| 100 | public static Predicate<XdmItem> isAtomic() { |
| 101 | return item -> item instanceof XdmAtomicValue; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * A predicate to test whether an item is a function value (this includes maps and arrays) |
| 106 | * @return a predicate that returns true if given an item that is a function, including |
| 107 | * maps and arrays |
| 108 | */ |
| 109 | |
| 110 | public static Predicate<XdmItem> isFunction() { |
| 111 | return item -> item instanceof XdmFunctionItem; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * A predicate to test whether an item is an XDM map |
| 116 | * @return a predicate that returns true if given an item that is a map |
| 117 | */ |
| 118 | |
| 119 | public static Predicate<XdmItem> isMap() { |
| 120 | return item -> item instanceof XdmMap; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * A predicate to test whether an item is an XDM array |
| 125 | * @return a predicate that returns true if given an item that is an array |
| 126 | */ |
| 127 | |
| 128 | public static Predicate<XdmItem> isArray() { |
| 129 | return item -> item instanceof XdmArray; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Obtain a predicate that tests whether a supplied {@link Step} delivers an empty result |
| 134 | * |
| 135 | * @param step a step to be applied to the item being tested |
| 136 | * @return a predicate that returns true if the supplied step returns an empty result. |
| 137 | * For example {@code empty(attribute("id")} is a predicate that returns true for a node that |
| 138 | * has no "id" attribute. Similarly {@code CHILD.where(empty(child(IS_ELEMENT)))} is a step |
| 139 | * that selects child elements having no element children. |
| 140 | */ |
| 141 | |
| 142 | public static <T extends XdmItem> Predicate<XdmItem> empty(Step<T> step) { |
| 143 | return item -> !step.apply(item).findFirst().isPresent(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Return a {@link Predicate} that is the negation of a supplied {@link Predicate} |
| 148 | * |
| 149 | * @param condition the supplied predicate |
| 150 | * @param <T> The type of object to which the predicate is applicable |
| 151 | * @return a Predicate that matches an item if and only if the supplied Predicate does |
| 152 | * not match the item. |
| 153 | */ |
| 154 | |
| 155 | public static <T> Predicate<T> not(Predicate<T> condition) { |
| 156 | return condition.negate(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Obtain a predicate that tests whether a supplied Step delivers a non-empty result |
| 161 | * |
| 162 | * @param step a step to be applied to the item being tested |
| 163 | * @return a predicate that returns true if the |
| 164 | * step returns a non-empty result. For example {@code exists(attribute("id")} is a |
| 165 | * predicate that returns true for a node that has an "id" attribute. So |
| 166 | * {@code CHILD.where(exists(attribute("id"))} is a step that selects child elements |
| 167 | * having an "id" attribute. |
| 168 | */ |
| 169 | |
| 170 | public static <T extends XdmItem> Predicate<XdmItem> exists(Step<T> step) { |
| 171 | return item -> step.apply(item).findFirst().isPresent(); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Obtain a predicate that tests whether an item is a node with a given namespace URI and local name |
| 176 | * |
| 177 | * @param uri the required namespace URI: supply a zero-length string to indicate the null namespace |
| 178 | * @param localName the required local name |
| 179 | * @return a predicate that returns true if and only if the supplied item is a node with the given |
| 180 | * namespace URI and local name |
| 181 | */ |
| 182 | |
| 183 | public static Predicate<? super XdmNode> hasName(String uri, String localName) { |
| 184 | return item -> { |
| 185 | QName name = item.getNodeName(); |
| 186 | return name != null && |
| 187 | name.getLocalName().equals(localName) && |
| 188 | name.getNamespaceURI().equals(uri); |
| 189 | }; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Obtain a predicate that tests whether an item is a node with a given local name, |
| 194 | * irrespective of the namespace |
| 195 | * |
| 196 | * @param localName the required local name. If a zero-length string is supplied, the predicate |
| 197 | * will match nodes having no name (for example, comments and text nodes) |
| 198 | * @return a predicate that returns true if and only if the supplied item is a node with the given |
| 199 | * local name, irrespective of the namespace |
| 200 | */ |
| 201 | |
| 202 | public static Predicate<XdmNode> hasLocalName(String localName) { |
| 203 | return item -> { |
| 204 | QName name = item.getNodeName(); |
| 205 | return name != null && |
| 206 | name.getLocalName().equals(localName); |
| 207 | }; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Obtain a predicate that tests whether an item is a node with a given namespace URI |
| 212 | * |
| 213 | * @param uri the required namespace URI: supply a zero-length string to identify the null namespace |
| 214 | * @return a predicate that returns true if and only if the supplied item is a node with the given |
| 215 | * namespace URI. If a zero-length string is supplied, the predicate will also match nodes having no name, |
| 216 | * such as text and comment nodes, and nodes having a local name only, such as namespace and processing-instruction |
| 217 | * nodes. |
| 218 | */ |
| 219 | |
| 220 | public static Predicate<XdmNode> hasNamespace(String uri) { |
| 221 | return item -> { |
| 222 | QName name = item.getNodeName(); |
| 223 | return name != null && |
| 224 | name.getNamespaceURI().equals(uri); |
| 225 | }; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Obtain a predicate that tests whether an item is an element node with a given attribute (whose |
| 230 | * name is in no namespace) |
| 231 | * |
| 232 | * @param local the required attribute name |
| 233 | * @return a predicate that returns true if and only if the supplied item is an element having an attribute |
| 234 | * with the given local name, in no namespace |
| 235 | */ |
| 236 | |
| 237 | public static Predicate<XdmNode> hasAttribute(String local) { |
| 238 | return item -> item.attribute(local) != null; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Obtain a predicate that tests whether an item is an element node with a given attribute (whose |
| 243 | * name is in no namespace) whose string value is equal to a given value |
| 244 | * |
| 245 | * @param local the required attribute name |
| 246 | * @param value the required attribute value |
| 247 | * @return a predicate that returns true if and only if the supplied item is an element having an attribute |
| 248 | * with the given local name, in no namespace, whose string value is equal to the given value |
| 249 | */ |
| 250 | |
| 251 | public static Predicate<XdmNode> attributeEq(String local, String value) { |
| 252 | return item -> value.equals(item.attribute(local)); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Obtain a predicate that tests whether an item matches a given item type |
| 257 | * |
| 258 | * @param type the required item type |
| 259 | * @return a predicate that returns true if and only if the supplied item matches the supplied |
| 260 | * item type. For example, {@code hasType(ItemType.DATE_TIME)} matches atomic values of type |
| 261 | * <code>xs:dateTime</code>. |
| 262 | */ |
| 263 | |
| 264 | public static Predicate<XdmItem> hasType(ItemType type) { |
| 265 | return type::matches; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Obtain a predicate that tests whether there is some item in the result of applying a step that |
| 270 | * satisfies the supplied condition. |
| 271 | * <p>For example, {@code some(CHILD, exists(attribute("foo"))} matches an element if it has a child |
| 272 | * element with an attribute whose local name is "foo".</p> |
| 273 | * <p>If the step returns an empty sequence the result will always be false.</p> |
| 274 | * |
| 275 | * @param step the step to be evaluated |
| 276 | * @param condition the predicate to be applied to the items returned by the step |
| 277 | */ |
| 278 | |
| 279 | public static <T extends XdmItem> Predicate<XdmItem> some(Step<T> step, Predicate<? super T> condition) { |
| 280 | return item -> step.apply(item).anyMatch(condition); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Obtain a predicate that tests whether every item in the result of applying a step |
| 285 | * satisfies the supplied condition. |
| 286 | * <p>For example, {@code every(CHILD, exists(attribute("foo"))} matches an element if each of its child |
| 287 | * elements has an attribute whose local name is "foo".</p> |
| 288 | * <p>If the step returns an empty sequence the result will always be true.</p> |
| 289 | * |
| 290 | * @param step the step to be evaluated |
| 291 | * @param condition the predicate to be applied to the items returned by the step |
| 292 | */ |
| 293 | |
| 294 | public static <T extends XdmItem> Predicate<XdmItem> every(Step<T> step, Predicate<? super XdmItem> condition) { |
| 295 | return item -> step.apply(item).allMatch(condition); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Obtain a predicate that tests whether an atomic value compares equal to a supplied atomic value of |
| 300 | * a comparable type |
| 301 | * |
| 302 | * @param value the atomic value to be compared with |
| 303 | * @return a Predicate which returns true when applied to a value that is equal to the supplied |
| 304 | * value under the "is-same-key" comparison rules. (These are the rules used to compare key values |
| 305 | * in an XDM map. The rules are chosen to be context-free, error-free, and transitive.) |
| 306 | */ |
| 307 | |
| 308 | public static Predicate<XdmAtomicValue> eq(XdmAtomicValue value) { |
| 309 | AtomicMatchKey k2 = value.getUnderlyingValue().asMapKey(); |
| 310 | return item -> item.getUnderlyingValue().asMapKey().equals(k2); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Obtain a predicate that tests whether the result of applying the XPath string() function to an item |
| 315 | * is equal to a given string |
| 316 | * |
| 317 | * @param value the string being tested |
| 318 | * @return a Predicate which returns true if the string value of the item being tested |
| 319 | * is equal to the given string under Java comparison rules for comparing strings. |
| 320 | */ |
| 321 | |
| 322 | public static Predicate<XdmItem> eq(String value) { |
| 323 | return item -> item.getStringValue().equals(value); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Obtain a predicate that tests whether the result of applying the XPath string() function to an item |
| 328 | * matches a given regular expression |
| 329 | * |
| 330 | * @param regex the regular expression (this is a Java regular expression, not an XPath regular expression) |
| 331 | * @return a Predicate which returns true if the string value of the item being tested |
| 332 | * contains a substring that matches the given regular expression. To test the string in its entirety, |
| 333 | * use anchors "^" and "$" in the regular expression. |
| 334 | */ |
| 335 | |
| 336 | public static Predicate<XdmItem> matchesRegex(String regex) { |
| 337 | Pattern re = Pattern.compile(regex); |
| 338 | return item -> re.matcher(item.getStringValue()).find(); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Obtain a predicate that tests whether there is some item in the result of applying a step, |
| 343 | * whose string value is equal to a given string. |
| 344 | * <p>For example, {@code eq(attribute("id"), "foo")} matches an element if it has an "id" |
| 345 | * attribute whose value is "foo".</p> |
| 346 | * |
| 347 | * @param step the step to be evaluated |
| 348 | * @param value the string to be compared against the items returned by the step |
| 349 | * @return a Predicate which returns true if some item selected by the step has as string value |
| 350 | * equal to the given string |
| 351 | */ |
| 352 | |
| 353 | public static <T extends XdmItem> Predicate<XdmItem> eq(Step<T> step, String value) { |
| 354 | return some(step, eq(value)); |
| 355 | } |
| 356 | |
| 357 | |
| 358 | |
| 359 | private static Predicate<XdmItem> nodeKindPredicate(XdmNodeKind kind) { |
| 360 | return item -> item instanceof XdmNode && ((XdmNode) item).getNodeKind() == kind; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | } |
| 365 |