Debian Codegraph @

net/sf/saxon/lib/XQueryFunctionAnnotationHandler.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
9 package net.sf.saxon.lib;
10
11 import net.sf.saxon.om.StructuredQName;
12 import net.sf.saxon.query.Annotation;
13 import net.sf.saxon.query.AnnotationList;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.type.TypeHierarchy;
16
17 import java.util.Collections;
18 import java.util.HashSet;
19 import java.util.Set;
20
21 /**
22 * Function annotation handler for annotations in the built-in namespace
23 * http://www.w3.org/2012/xquery. This defines the annotations %public and %private,
24 * and the (XQuery Update 3.0) annotations %updating and %simple
25 */
26
27 public class XQueryFunctionAnnotationHandler implements FunctionAnnotationHandler {
28
29 private static class DisallowedCombination {
30 public DisallowedCombination(StructuredQName one, StructuredQName two, String errorCode, String... where) {
31 this.one = one;
32 this.two = two;
33 this.errorCode = errorCode;
34 this.where = new HashSet<String>(where.length);
35 Collections.addAll(this.where, where);
36 }
37
38 public StructuredQName one;
39 public StructuredQName two;
40 public String errorCode;
41 public Set<String> where;
42 }
43
44 private static DisallowedCombination[] blackList = {
45 new DisallowedCombination(Annotation.SIMPLE, null, "XUST0032", "DV"),
46 new DisallowedCombination(Annotation.UPDATING, null, "XUST0032", "DV"),
47 new DisallowedCombination(Annotation.PUBLIC, null, "XQST0125", "IF"),
48 new DisallowedCombination(Annotation.PRIVATE, null, "XQST0125", "IF"),
49 new DisallowedCombination(Annotation.PRIVATE, Annotation.PRIVATE, "XQST0106", "DF"),
50 new DisallowedCombination(Annotation.PRIVATE, Annotation.PUBLIC, "XQST0106", "DF"),
51 new DisallowedCombination(Annotation.PUBLIC, Annotation.PUBLIC, "XQST0106", "DF"),
52 new DisallowedCombination(Annotation.PUBLIC, Annotation.PRIVATE, "XQST0106", "DF"),
53 new DisallowedCombination(Annotation.PRIVATE, Annotation.PRIVATE, "XQST0116", "DV"),
54 new DisallowedCombination(Annotation.PRIVATE, Annotation.PUBLIC, "XQST0116", "DV"),
55 new DisallowedCombination(Annotation.PUBLIC, Annotation.PUBLIC, "XQST0116", "DV"),
56 new DisallowedCombination(Annotation.PUBLIC, Annotation.PRIVATE, "XQST0116", "DV"),
57 new DisallowedCombination(Annotation.UPDATING, Annotation.UPDATING, "XUST0033", "DF", "IF"),
58 new DisallowedCombination(Annotation.UPDATING, Annotation.SIMPLE, "XUST0033", "DF", "IF"),
59 new DisallowedCombination(Annotation.SIMPLE, Annotation.SIMPLE, "XUST0033", "DF", "IF"),
60 new DisallowedCombination(Annotation.SIMPLE, Annotation.UPDATING, "XUST0033", "DF", "IF"),
61 };
62
63 public XQueryFunctionAnnotationHandler() {
64
65 }
66
67 @Override
68 public void check(AnnotationList annotations, String construct) throws XPathException {
69 for (int i = 0; i < annotations.size(); i++) {
70 Annotation ann = annotations.get(i);
71 for (DisallowedCombination dc : blackList) {
72 if (dc.one.equals(ann.getAnnotationQName()) && dc.where.contains(construct)) {
73 if (dc.two == null) {
74 throw new XPathException("Annotation %" + ann.getAnnotationQName().getLocalPart() + " is not allowed here",
75 dc.errorCode);
76 } else {
77 for (int j = 0; j < i; j++) {
78 Annotation other = annotations.get(j);
79 if (dc.two.equals(other.getAnnotationQName())) {
80 if (dc.two.equals(ann.getAnnotationQName())) {
81 throw new XPathException("Annotation %" + ann.getAnnotationQName().getLocalPart() +
82 " cannot appear more than once", dc.errorCode);
83 } else {
84 throw new XPathException("Annotations %" + ann.getAnnotationQName().getLocalPart() +
85 " and " + other.getAnnotationQName().getLocalPart() + " cannot appear together",
86 dc.errorCode);
87 }
88 }
89 }
90 }
91 }
92 }
93 }
94 }
95
96 /**
97 * Get the namespace handled by this function annotation handler. This handler will only be called in respect
98 * of annotation assertions that are in this namespace. Use the empty string to refer to the null namespace
99 *
100 * @return the namespace handled by this function annotation handler, namely "http://www.w3.org/2012/xquery"
101 */
102
103 public String getAssertionNamespace() {
104 return "http://www.w3.org/2012/xquery";
105 };
106
107 /**
108 * Test whether a function with a given list of annotations satisfies an annotation assertion present
109 * on a function item test.
110 *
111 * @param assertion the annotation assertion present in the function item test
112 * @param annotationList the annotations present on the function being tested
113 * @return true if the assertion is satisfied, false if not
114 */
115
116 public boolean satisfiesAssertion(Annotation assertion, AnnotationList annotationList) {
117 // annotation assertions are not defined for this namespace (surprisingly)
118 return true;
119 }
120
121 /**
122 * Test the relationship of one list of annotation assertions to another list of annotation assertions,
123 * all from this namespace
124 * <p>The lists will always be non-empty, because it is assumed that a test with an empty list of
125 * assertions always subsumes a test with a non-empty list.</p>
126 *
127 * @param firstList the first list of annotation assertions
128 * @param secondList the second list of annotation assertions
129 * @return the relationship between the two lists, as one of the values {@link TypeHierarchy#SAME_TYPE},
130 * {@link TypeHierarchy#DISJOINT}, {@link TypeHierarchy#OVERLAPS},
131 * {@link TypeHierarchy#SUBSUMES}, {@link TypeHierarchy#SUBSUMED_BY}.
132 * For example, if the first list is <code>%colour("blue")</code> and the second list is <code>%colour("green")</code>,
133 * and no function can be both blue and green, then return <code>DISJOINT</code>. But if a function can have more than
134 * one colour, return <code>OVERLAPS</code> because the set of functions conforming to the two assertions has a non-empty
135 * intersection. If the first list is <code>%colour("any")</code> and the second list is <code>%colour("blue")</code>,
136 * then return <code>SUBSUMES</code>, because the set of functions satisfying the first assertion is a superset of those
137 * satisfying the second assertion.
138 * <p>The result of this method must be consistent with the {@link #satisfiesAssertion(Annotation, AnnotationList)} method.
139 * For example, if this method indicates that <code>%big</code> subsumes <code>%huge</code>, then it must indeed
140 * be the case that the set of functions that satisfy the assertion <code>%big</code> is a superset of those that
141 * satisfy the assertion <code>%huge</code>.</p>
142 * <p>If in doubt, it is always safe to return <code>OVERLAPS</code>: the worst that can happen is that type-checking is deferred
143 * until run-time.</p>
144 */
145
146 public int relationship(AnnotationList firstList, AnnotationList secondList) {
147 return TypeHierarchy.OVERLAPS;
148 }
149
150
151 }
152