Debian Codegraph @

net/sf/saxon/query/AnnotationList.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.query;
9
10
11 import net.sf.saxon.Configuration;
12 import net.sf.saxon.lib.FunctionAnnotationHandler;
13 import net.sf.saxon.om.StructuredQName;
14 import net.sf.saxon.trans.XPathException;
15
16 import java.util.*;
17
18 /**
19 * An immutable list of function or variable annotations, or of annotation assertions
20 */
21
22 public class AnnotationList implements Iterable<Annotation> {
23
24
25 private List<Annotation> list;
26
27 /**
28 * An empty annotation list
29 */
30 public static AnnotationList EMPTY = new AnnotationList(Collections.emptyList());
31
32 public AnnotationList(List<Annotation> list) {
33 this.list = list;
34 }
35
36 /**
37 * Construct an annotation list containing a single annotation
38 * @param ann the single annotation in the annotation list
39 * @return a singleton annotation list
40 */
41
42 public static AnnotationList singleton(Annotation ann) {
43 return new AnnotationList(Collections.singletonList(ann));
44 }
45
46
47 /**
48 * Check an annotation list for internal consistency (e.g. rules that %public and %private cannot coexist)
49 * @param where the context where the list appears: one of "DF" (declare function), "DV" (declare variable),
50 * "IF" (inline function declaration), "FT" (function test)
51 * @throws XPathException if the annotation list is not internally consistent
52 */
53
54 public void check(Configuration config, String where) throws XPathException {
55 Map<String, List<Annotation>> map = groupByNamespace();
56 for (Map.Entry<String, List<Annotation>> entry : map.entrySet()) {
57 FunctionAnnotationHandler handler = config.getFunctionAnnotationHandler(entry.getKey());
58 if (handler != null) {
59 handler.check(new AnnotationList(entry.getValue()), where);
60 }
61 }
62 }
63
64 private Map<String, List<Annotation>> groupByNamespace() {
65 Map<String, List<Annotation>> result = new HashMap<>();
66 for (Annotation ann : list) {
67 String ns = ann.getAnnotationQName().getURI();
68 if (result.containsKey(ns)) {
69 result.get(ns).add(ann);
70 } else {
71 List<Annotation> list = new ArrayList<>();
72 list.add(ann);
73 result.put(ns, list);
74 }
75 }
76 return result;
77 }
78
79 /**
80 * Filter the annotation list by the namespace URI part of the annotation name
81 * @param ns the namespace URI required
82 * @return an annotation list containing the subset of this annotation list where the
83 * annotation names have the required namespace
84 */
85
86 public AnnotationList filterByNamespace(String ns) {
87 List<Annotation> out = new ArrayList<>();
88 for (Annotation ann : list) {
89 if (ann.getAnnotationQName().hasURI(ns)) {
90 out.add(ann);
91 }
92 }
93 return new AnnotationList(out);
94 }
95
96
97 /**
98 * Returns an iterator over a set of elements of type T.
99 *
100 * @return an Iterator.
101 */
102 @Override
103 public Iterator<Annotation> iterator() {
104 return list.iterator();
105 }
106
107 /**
108 * Ask whether the list of annotations is empty
109 * @return true if the list of annotations is empty
110 */
111
112 public boolean isEmpty() {
113 return list.isEmpty();
114 }
115
116 /**
117 * The number of annotations in the list
118 * @return the number of annotations in the list of annotations
119 */
120
121 public int size() {
122 return list.size();
123 }
124
125 /**
126 * Get the i'th annotation in the list (counting from zero)
127 * @param i the index of the required annotation (counting from zero)
128 * @return the annotation at the specified position
129 */
130
131 public Annotation get(int i) {
132 return list.get(i);
133 }
134
135 /**
136 * Ask whether a list of annotations contains an annotation with a given name
137 *
138 * @param name the given name
139 * @return true if one or more annotations with the given name are present in the list
140 */
141
142 public boolean includes(StructuredQName name) {
143 for (Annotation a : list) {
144 if (a.getAnnotationQName().equals(name)) {
145 return true;
146 }
147 }
148 return false;
149 }
150
151 /**
152 * Ask whether a list of annotations contains an annotation with a given local name
153 *
154 * @param localName the given local name
155 * @return true if one or more annotations with the given name are present in the list
156 */
157
158 public boolean includes(String localName) {
159 for (Annotation a : list) {
160 if (a.getAnnotationQName().getLocalPart().equals(localName)) {
161 return true;
162 }
163 }
164 return false;
165 }
166
167 public boolean equals(Object other) {
168 // treat the annotation list as ordered
169 return other instanceof AnnotationList && list.equals(((AnnotationList)other).list);
170 }
171
172 public int hashCode() {
173 return list.hashCode();
174 }
175
176
177 }