| 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 | |
| 9 | package net.sf.saxon.lib; |
| 10 | |
| 11 | import net.sf.saxon.query.Annotation; |
| 12 | import net.sf.saxon.query.AnnotationList; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.type.TypeHierarchy; |
| 15 | |
| 16 | /** |
| 17 | * A simple function annotation handler that handles an annotation namespace where |
| 18 | * the annotations are simple named properties that are either present or absent; |
| 19 | * if a property name is used in an annotation assertion then it only matches a |
| 20 | * function where the property is present; property names can be prefixed "not-" |
| 21 | * in an annotation assertion to indicate that the relevant property must be |
| 22 | * absent |
| 23 | */ |
| 24 | |
| 25 | public class SimpleFunctionAnnotationHandler implements FunctionAnnotationHandler { |
| 26 | |
| 27 | private String namespace; |
| 28 | |
| 29 | public SimpleFunctionAnnotationHandler(String namespace) { |
| 30 | this.namespace = namespace; |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public void check(AnnotationList existingAnnotations, String construct) throws XPathException { |
| 35 | // no action |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Get the namespace handled by this function annotation handler. This handler will only be called in respect |
| 40 | * of annotation assertions that are in this namespace. Use the empty string to refer to the null namespace |
| 41 | * |
| 42 | * @return the namespace handled by this function annotation handler. |
| 43 | */ |
| 44 | |
| 45 | public String getAssertionNamespace() { |
| 46 | return namespace; |
| 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * Test whether a function with a given list of annotations satisfies an annotation assertion present |
| 51 | * on a function item test. |
| 52 | * |
| 53 | * @param assertion the annotation assertion present in the function item test |
| 54 | * @param annotationList the annotations present on the function being tested |
| 55 | * @return true if the assertion is satisfied, false if not |
| 56 | */ |
| 57 | |
| 58 | public boolean satisfiesAssertion(Annotation assertion, AnnotationList annotationList) { |
| 59 | String local = assertion.getAnnotationQName().getLocalPart(); |
| 60 | if (local.startsWith("not-")) { |
| 61 | return !annotationList.includes(local.substring(4)); |
| 62 | } else { |
| 63 | return annotationList.includes(local); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Test the relationship of one list of annotation assertions to another list of annotation assertions, |
| 69 | * all from this namespace. |
| 70 | * <p>The lists will always be non-empty, because it is assumed that a test with an empty list of |
| 71 | * assertions always subsumes a test with a non-empty list.</p> |
| 72 | * |
| 73 | * @param firstList the first list of annotation assertions |
| 74 | * @param secondList the second list of annotation assertions |
| 75 | * @return the relationship between the two lists, as one of the values {@link net.sf.saxon.type.TypeHierarchy#SAME_TYPE}, |
| 76 | * {@link net.sf.saxon.type.TypeHierarchy#DISJOINT}, {@link net.sf.saxon.type.TypeHierarchy#OVERLAPS}, |
| 77 | * {@link net.sf.saxon.type.TypeHierarchy#SUBSUMES}, {@link net.sf.saxon.type.TypeHierarchy#SUBSUMED_BY}. |
| 78 | * For example, if the first list is <code>%colour("blue")</code> and the second list is <code>%colour("green")</code>, |
| 79 | * and no function can be both blue and green, then return <code>DISJOINT</code>. But if a function can have more than |
| 80 | * one colour, return <code>OVERLAPS</code> because the set of functions conforming to the two assertions has a non-empty |
| 81 | * intersection. If the first list is <code>%colour("any")</code> and the second list is <code>%colour("blue")</code>, |
| 82 | * then return <code>SUBSUMES</code>, because the set of functions satisfying the first assertion is a superset of those |
| 83 | * satisfying the second assertion. |
| 84 | * <p>The result of this method must be consistent with the {@link #satisfiesAssertion(Annotation, AnnotationList)} method. |
| 85 | * For example, if this method indicates that <code>%big</code> subsumes <code>%huge</code>, then it must indeed |
| 86 | * be the case that the set of functions that satisfy the assertion <code>%big</code> is a superset of those that |
| 87 | * satisfy the assertion <code>%huge</code>.</p> |
| 88 | * <p>If in doubt, it is always safe to return <code>OVERLAPS</code>: the worst that can happen is that type-checking is deferred |
| 89 | * until run-time.</p> |
| 90 | */ |
| 91 | |
| 92 | public int relationship(AnnotationList firstList, AnnotationList secondList) { |
| 93 | return TypeHierarchy.OVERLAPS; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | } |
| 98 |