Debian Codegraph @

net/sf/saxon/functions/URIQueryParameters.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.functions;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.lib.Validation;
12 import net.sf.saxon.om.AllElementsSpaceStrippingRule;
13 import net.sf.saxon.om.IgnorableSpaceStrippingRule;
14 import net.sf.saxon.om.NoElementsSpaceStrippingRule;
15 import net.sf.saxon.om.SpaceStrippingRule;
16 import net.sf.saxon.regex.ARegularExpression;
17 import net.sf.saxon.regex.JavaRegularExpression;
18 import net.sf.saxon.regex.RegularExpression;
19 import net.sf.saxon.trans.Instantiator;
20 import net.sf.saxon.trans.Maker;
21 import net.sf.saxon.trans.XPathException;
22 import net.sf.saxon.tree.util.FastStringBuffer;
23 import org.xml.sax.XMLReader;
24
25 import java.io.File;
26 import java.io.FilenameFilter;
27 import java.util.ArrayList;
28 import java.util.StringTokenizer;
29
30 /**
31 * A set of query parameters on a URI passed to the collection() or document() function
32 */
33
34 public class URIQueryParameters {
35
36 /*@Nullable*/ FilenameFilter filter = null;
37 Boolean recurse = null;
38 Integer validation = null;
39 SpaceStrippingRule strippingRule = null;
40 Integer onError = null;
41 Maker<XMLReader> parserMaker = null;
42 Boolean xinclude = null;
43 boolean unparsed;
44 Boolean stable = null;
45 Boolean metadata = null;
46
47 public static final int ON_ERROR_FAIL = 1;
48 public static final int ON_ERROR_WARNING = 2;
49 public static final int ON_ERROR_IGNORE = 3;
50
51 /**
52 * Create an object representing the query part of a URI
53 *
54 * @param query the part of the URI after the "?" symbol
55 * @param config the Saxon configuration
56 */
57
58 public URIQueryParameters(String query, Configuration config) throws XPathException {
59 if (query != null) {
60 StringTokenizer t = new StringTokenizer(query, ";&");
61 while (t.hasMoreTokens()) {
62 String tok = t.nextToken();
63 int eq = tok.indexOf('=');
64 if (eq > 0 && eq < (tok.length() - 1)) {
65 String keyword = tok.substring(0, eq);
66 String value = tok.substring(eq + 1);
67 processParameter(config, keyword, value);
68 }
69 }
70 }
71 }
72
73 private void processParameter(Configuration config, String keyword, String value) throws XPathException {
74 if (keyword.equals("select")) {
75 filter = makeGlobFilter(value);
76 } else if (keyword.equals("match")) {
77 ARegularExpression regex = new ARegularExpression(value, "", "XP", new ArrayList<>(), config);
78 filter = new RegexFilter(regex);
79 } else if (keyword.equals("recurse")) {
80 recurse = "yes".equals(value);
81 } else if (keyword.equals("validation")) {
82 int v = Validation.getCode(value);
83 if (v != Validation.INVALID) {
84 validation = v;
85 }
86 } else if (keyword.equals("strip-space")) {
87 switch (value) {
88 case "yes":
89 strippingRule = AllElementsSpaceStrippingRule.getInstance();
90 break;
91 case "ignorable":
92 strippingRule = IgnorableSpaceStrippingRule.getInstance();
93 break;
94 case "no":
95 strippingRule = NoElementsSpaceStrippingRule.getInstance();
96 break;
97 }
98 } else if (keyword.equals("stable")) {
99 if (value.equals("yes")) {
100 stable = Boolean.TRUE;
101 } else if (value.equals("no")) {
102 stable = Boolean.FALSE;
103 }
104 } else if (keyword.equals("metadata")) {
105 if (value.equals("yes")) {
106 metadata = Boolean.TRUE;
107 } else if (value.equals("no")) {
108 metadata = Boolean.FALSE;
109 }
110 } else if (keyword.equals("xinclude")) {
111 if (value.equals("yes")) {
112 xinclude = Boolean.TRUE;
113 } else if (value.equals("no")) {
114 xinclude = Boolean.FALSE;
115 }
116 } else if (keyword.equals("unparsed")) {
117 if (value.equals("yes")) {
118 unparsed = true;
119 } else if (value.equals("no")) {
120 unparsed = false;
121 }
122 } else if (keyword.equals("on-error")) {
123 switch (value) {
124 case "warning":
125 onError = ON_ERROR_WARNING;
126 break;
127 case "ignore":
128 onError = ON_ERROR_IGNORE;
129 break;
130 case "fail":
131 onError = ON_ERROR_FAIL;
132 break;
133 }
134 } else if (keyword.equals("parser") && config != null) {
135 parserMaker = new Instantiator<>(value, config);
136 }
137 }
138
139 public static FilenameFilter makeGlobFilter(String value) throws XPathException {
140 FastStringBuffer sb = new FastStringBuffer(value.length() + 6);
141 sb.append('^');
142 for (int i = 0; i < value.length(); i++) {
143 char c = value.charAt(i);
144 if (c == '.') {
145 // replace "." with "\."
146 sb.append("\\.");
147 } else if (c == '*') {
148 // replace "*" with ".*"
149 sb.append(".*");
150 } else if (c == '?') {
151 // replace "?" with ".?"
152 sb.append(".?");
153 } else {
154 sb.append(c);
155 }
156 }
157 sb.append('$');
158 try {
159 return new RegexFilter(new JavaRegularExpression(sb, ""));
160 } catch (XPathException e) {
161 throw new XPathException("Invalid glob " + value + " in collection URI", "FODC0004");
162 }
163 }
164
165 /**
166 * Get the value of the strip-space=yes|no parameter.
167 *
168 * @return an instance of {@link AllElementsSpaceStrippingRule}, {@link IgnorableSpaceStrippingRule},
169 * or {@link NoElementsSpaceStrippingRule}, or null
170 */
171
172 public SpaceStrippingRule getSpaceStrippingRule() {
173 return strippingRule;
174 }
175
176 /**
177 * Get the value of the validation=strict|lax|preserve|strip parameter, or null if unspecified
178 */
179
180 public Integer getValidationMode() {
181 return validation;
182 }
183
184 /**
185 * Get the file name filter (select=pattern), or null if unspecified
186 */
187
188 public FilenameFilter getFilenameFilter() {
189 return filter;
190 }
191
192 /**
193 * Get the value of the recurse=yes|no parameter, or null if unspecified
194 */
195
196 public Boolean getRecurse() {
197 return recurse;
198 }
199
200 /**
201 * Get the value of the on-error=fail|warning|ignore parameter, or null if unspecified
202 */
203
204 public Integer getOnError() {
205 return onError;
206 }
207
208 /**
209 * Get the value of xinclude=yes|no, or null if unspecified
210 */
211
212 public Boolean getXInclude() {
213 return xinclude;
214 }
215
216 /**
217 * Get the value of metadata=yes|no, or null if unspecified
218 */
219
220 public Boolean getMetaData() {
221 return metadata;
222 }
223
224 /**
225 * Get the value of unparsed=yes|no, or false if unspecified
226 */
227
228 public boolean isUnparsed() {
229 return unparsed;
230 }
231
232 /**
233 * Get the value of stable=yes|no, or null if unspecified
234 */
235
236 public Boolean getStable() {
237 return stable;
238 }
239
240 /**
241 * Get a factory for the selected XML parser class, or null if unspecified
242 */
243
244 public Maker<XMLReader> getXMLReaderMaker() {
245 return parserMaker;
246 }
247
248 /**
249 * A FilenameFilter that tests file names against a regular expression
250 */
251
252 public static class RegexFilter implements FilenameFilter {
253
254 private RegularExpression pattern;
255
256
257 public RegexFilter(RegularExpression regex) {
258 this.pattern = regex;
259 }
260
261 /**
262 * Tests if a specified file should be included in a file list.
263 *
264 * @param dir the directory in which the file was found.
265 * @param name the name (last component) of the file.
266 * @return <code>true</code> if and only if the name should be
267 * included in the file list; <code>false</code> otherwise.
268 * Returns true if the file is a directory or if it matches the glob pattern.
269 */
270
271 public boolean accept(File dir, String name) {
272 return new File(dir, name).isDirectory() || pattern.matches(name);
273 }
274
275 /**
276 * Test whether a name matches the pattern (regardless whether it is a directory or not)
277 *
278 * @param name the name (last component) of the file
279 * @return true if the name matches the pattern.
280 */
281 public boolean matches(String name) {
282 return pattern.matches(name);
283 }
284 }
285
286
287 }
288