Debian Codegraph @

net/sf/saxon/resource/StandardCollectionFinder.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.resource;
10
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.functions.ResolveURI;
13 import net.sf.saxon.functions.URIQueryParameters;
14 import net.sf.saxon.lib.CollectionFinder;
15 import net.sf.saxon.lib.ResourceCollection;
16 import net.sf.saxon.lib.StandardCollectionURIResolver;
17 import net.sf.saxon.trans.Err;
18 import net.sf.saxon.trans.XPathException;
19
20 import java.io.File;
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 /**
27 * Default implementation of the CollectionFinder interface. The standard CollectionFinder recognizes four
28 * types of collection:
29 * <p>
30 * <ol>
31 * <li>Any URI may be explicitly registered and associated with an instance of {@link ResourceCollection}</li>
32 * <li>If the file: URI scheme is used, and the relevant file identifies a directory, the directory
33 * is treated as a collection: it is returned as an instance of {@link DirectoryCollection}</li>
34 * <li>If the URI ends with ".jar" or ".zip", or more generally, if the method {@link #isJarFileURI(String)} returns
35 * true, the URI is treated as identifying a JAR or ZIP archive, whose contents form the
36 * resources in the collection: it is returned as an instance of {@link JarCollection}</li>
37 * <li>In all other cases, the URI is treated as the URI of an XML document listing the URIs
38 * of the resources in the collection, which are then retrieved using the {@link javax.xml.transform.URIResolver}</li>
39 * </ol>
40 */
41
42 public class StandardCollectionFinder implements CollectionFinder {
43
44 private Map<String, ResourceCollection> registeredCollections = new HashMap<>(2);
45
46 /**
47 * Register a specific URI and bind it to a specific ResourceCollection
48 * @param collectionURI the collection URI to be registered. Must not be null.
49 * @param collection the ResourceCollection to be associated with this URI. Must not be null.
50 */
51
52 public void registerCollection(String collectionURI, ResourceCollection collection) {
53 registeredCollections.put(collectionURI, collection);
54 }
55
56 /**
57 * Locate the collection of resources corresponding to a collection URI.
58 *
59 * @param context The XPath dynamic evaluation context
60 * @param collectionURI The collection URI: an absolute URI, formed by resolving the argument
61 * supplied to the fn:collection or fn:uri-collection against the static
62 * base URI
63 * @return a ResourceCollection object representing the resources in the collection identified
64 * by this collection URI
65 * @throws XPathException if the collection URI cannot be resolved to a collection
66 */
67
68 public ResourceCollection findCollection(XPathContext context, String collectionURI) throws XPathException {
69 checkNotNull(collectionURI, context);
70
71 ResourceCollection registeredCollection = registeredCollections.get(collectionURI);
72 if (registeredCollection != null) {
73 return registeredCollection;
74 }
75
76 URIQueryParameters params = null;
77 String query = null;
78
79 URI relativeURI;
80 try {
81 relativeURI = new URI(ResolveURI.escapeSpaces(collectionURI));
82 query = relativeURI.getQuery();
83 if (query != null) {
84 int q = collectionURI.indexOf('?');
85 params = new URIQueryParameters(query, context.getConfiguration());
86 collectionURI = ResolveURI.escapeSpaces(collectionURI.substring(0, q));
87 }
88
89 } catch (URISyntaxException e) {
90 XPathException err = new XPathException("Invalid relative URI " + Err.wrap(collectionURI, Err.VALUE) + " passed to collection() function");
91 err.setErrorCode("FODC0004");
92 err.setXPathContext(context);
93 throw err;
94 }
95
96 URI resolvedURI;
97 try {
98 resolvedURI = new URI(collectionURI);
99 } catch (URISyntaxException e) {
100 throw new XPathException(e);
101 }
102
103 if ("file".equals(resolvedURI.getScheme())) {
104 File file = new File(resolvedURI);
105 StandardCollectionURIResolver.checkFileExists(file, resolvedURI, context);
106 if (file.isDirectory()) {
107 return new DirectoryCollection(context.getConfiguration(), collectionURI, file, params);
108 }
109 }
110
111 // check if file is a zip file
112
113 if (isJarFileURI(collectionURI)) {
114 return new JarCollection(context, collectionURI, params);
115 }
116
117 // otherwise assume the URI identifies a collection catalog
118
119 return new CatalogCollection(context.getConfiguration(), collectionURI);
120 }
121
122 /**
123 * If the collectionURI is null, report that no default collection exists
124 * @param collectionURI the collection URI to be tested
125 * @param context XPath evaluation context
126 * @throws XPathException if the collectionURI is null
127 */
128
129 public static void checkNotNull(String collectionURI, XPathContext context) throws XPathException {
130 if (collectionURI == null) {
131 XPathException err = new XPathException("No default collection has been defined");
132 err.setErrorCode("FODC0002");
133 err.setXPathContext(context);
134 throw err;
135 }
136 }
137
138 /**
139 * Ask whether the collection URI should be interpreted as identifying a JAR (or ZIP) file.
140 * This method is provided so that it can be overridden in subclasses. The default implementation
141 * returns true if the collection URI ends with the extension ".jar" or ".zip", or if it
142 * is a URI using the "jar" scheme.
143 * @param collectionURI the requested absolute collection URI
144 * @return true if the collection URI should be interpreted as a JAR or ZIP file
145 */
146
147 protected boolean isJarFileURI(String collectionURI) {
148 return collectionURI.endsWith(".jar") ||
149 collectionURI.endsWith(".zip") ||
150 collectionURI.startsWith("jar:");
151 }
152 }