Debian Codegraph @

net/sf/saxon/trans/packages/PackageLibrary.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.trans.packages;
9
10
11 import net.sf.saxon.Configuration;
12 import net.sf.saxon.om.GroundedValue;
13 import net.sf.saxon.om.StructuredQName;
14 import net.sf.saxon.style.*;
15 import net.sf.saxon.trans.CompilerInfo;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.tree.util.FastStringBuffer;
18
19 import javax.xml.transform.Source;
20 import java.io.File;
21 import java.util.*;
22
23 /**
24 * A PackageLibrary represents a collection of packages that can be searched, typically to satisfy
25 * an xsl:use-package declaration. Packages are indexed by their package name and version. It is
26 * possible to index a package under a different name and/or version than appears in the source code;
27 * this allows the same source package to be compiled into two different variants with different
28 * settings for static parameters, so that the xsl:use-package declaration can pick up one or the other
29 * by a suitable choice of package name or version. A package can also have a short-form alias,
30 * which is ignored by xsl:use-package, but can be used to identify the package in the -xsl option
31 * of the Transform command line (this is only useful when the package is set up from a configuration
32 * file).
33 */
34
35 public class PackageLibrary {
36
37 private final Configuration config;
38 private final CompilerInfo compilerInfo;
39
40 private Map<String, List<PackageVersion>> packageVersions =
41 new HashMap<>();
42
43 private Map<VersionedPackageName, PackageDetails> packages =
44 new HashMap<>();
45
46
47 /**
48 * Create an empty package library
49 *
50 * @param compilerInfo the compiler options to be used if any package from the library
51 * needs to be compiled. The package library retains a reference
52 * to the CompilerInfo, so any subsequent changes to the options in the
53 * CompilerInfo will take effect.
54 */
55
56 public PackageLibrary(CompilerInfo compilerInfo) {
57 this.compilerInfo = compilerInfo;
58 this.config = compilerInfo.getConfiguration();
59 }
60
61 /**
62 * Create a package library as a copy of an existing package library
63 *
64 * @param library the existing package library to be copied. The new package
65 * library will contain a reference to the same CompilerInfo
66 * as the supplied package library, and any subsequent changes
67 * to this CompilerInfo will take effect.
68 */
69
70 public PackageLibrary(PackageLibrary library) {
71 packageVersions =
72 new HashMap<>(library.packageVersions);
73 packages = new HashMap<>(library.packages);
74 compilerInfo = library.compilerInfo;
75 config = library.config;
76 }
77
78 /**
79 * Create a package library from a set of source files (being either the source XSLT of the
80 * top-level modules of the packages in question, or the exported SEF file representing the
81 * compiled package)
82 *
83 * @param info the compiler to which this library belongs
84 * @param files the files making up the package library
85 * @throws XPathException if any of the files doesn't have a top-level xsl:package element
86 * containing the package name and package-version attributes. Note that validation
87 * of the content of the file is minimal; if the name and version can be successfully
88 * extracted, then full validation will be carried out if and when the package is actually
89 * used.
90 */
91
92 public PackageLibrary(CompilerInfo info, Set<File> files) throws XPathException {
93 compilerInfo = info;
94 config = info.getConfiguration();
95 for (File file : files) {
96 PackageDetails details = PackageInspector.getPackageDetails(file, config);
97 if (details == null) {
98 throw new XPathException("Unable to get package name and version for file " + file.getName());
99 }
100 addPackage(details);
101 }
102 }
103
104 /**
105 * Add a compiled and loaded package to this package library. This will replace
106 * any existing package with the same name and version.
107 *
108 * @param packageIn The stylesheet package to be added
109 */
110 public synchronized void addPackage(StylesheetPackage packageIn) {
111 String name = packageIn.getPackageName();
112 PackageVersion version = packageIn.getPackageVersion();
113 VersionedPackageName vp = new VersionedPackageName(name, version);
114 PackageDetails details = new PackageDetails();
115 details.nameAndVersion = vp;
116 details.loadedPackage = packageIn;
117 packages.put(vp, details);
118 addPackage(details);
119 }
120
121 /**
122 * Add an entry for a package to the library
123 *
124 * @param details details of the package to be added. The package
125 * referenced in these details may exist in either
126 * source or compiled form. No static processing
127 * or validation takes place at this stage.
128 */
129
130 public synchronized void addPackage(PackageDetails details) {
131 VersionedPackageName vp = details.nameAndVersion;
132 String name = vp.packageName;
133 PackageVersion version = vp.packageVersion;
134 List<PackageVersion> versions = packageVersions.computeIfAbsent(name, k -> new ArrayList<>());
135 versions.add(version);
136 packages.put(vp, details);
137 }
138
139 /**
140 * Add a package supplied in the form of a file: either a source XSLT package, or
141 * an exported package
142 *
143 * @param file a file to be added to the package library
144 */
145
146 public void addPackage(File file) throws XPathException {
147 PackageDetails details = PackageInspector.getPackageDetails(file, config);
148 if (details == null) {
149 throw new XPathException("Unable to get package name and version for file " + file.getName());
150 }
151 addPackage(details);
152 }
153
154
155 /**
156 * Find a package from the library that has the given name and whose version lies in the given ranges.
157 * If multiple versions exist, select the one with highest priority or version number.
158 *
159 * @param name The name of the package. This must match the name under which the
160 * package is registered in the library, which is not necessarily the same
161 * as the package name appearing in the source code.
162 * @param ranges The ranges of versions of that package that are acceptable
163 * @return Details of the package that best meets the criteria, or null if none can be found.
164 * The name of the package must match; if there are multiple versions, then the
165 * version chosen is based first on the priority attached to this package/version
166 * in the library, and if the priorities are equal (or there are no explicit priorities)
167 * then the one with highest version number is taken.
168 */
169
170 public synchronized PackageDetails findPackage(String name, PackageVersionRanges ranges) {
171 Set<PackageDetails> candidates = new HashSet<>();
172 List<PackageVersion> available = packageVersions.get(name);
173 if (available == null) {
174 return null;
175 }
176 int maxPriority = Integer.MIN_VALUE;
177 for (PackageVersion pv : available) {
178 PackageDetails details = packages.get(new VersionedPackageName(name, pv));
179 if (ranges.contains(pv)) {
180 candidates.add(details);
181 Integer priority = details.priority;
182 if (priority != null && priority > maxPriority) {
183 maxPriority = priority;
184 }
185 }
186 }
187 if (candidates.isEmpty()) {
188 return null;
189 } else if (candidates.size() == 1) {
190 return candidates.iterator().next();
191 } else {
192 // more than one candidate
193 Set<PackageVersion> shortList = new HashSet<>();
194 PackageDetails highest = null;
195 if (maxPriority == Integer.MIN_VALUE) {
196 for (PackageDetails details : candidates) {
197 if (highest == null || details.nameAndVersion.packageVersion.compareTo(highest.nameAndVersion.packageVersion) > 0) {
198 highest = details;
199 }
200 }
201 } else {
202 for (PackageDetails details : candidates) {
203 Integer priority = details.priority;
204 PackageVersion pv = details.nameAndVersion.packageVersion;
205 if (priority != null && priority == maxPriority && (highest == null || pv.compareTo(highest.nameAndVersion.packageVersion) > 0)) {
206 highest = details;
207 }
208 }
209 }
210 return highest;
211 }
212 }
213
214 /**
215 * Find the entry with a given shortName.
216 *
217 * @param shortName the shortName of the entry
218 * @return the PackageDetails for this shortName if present, or null otherwise.
219 * @throws IllegalStateException if there is more than one entry with the requested shortName
220 */
221
222 public synchronized PackageDetails findDetailsForAlias(String shortName) {
223 assert shortName != null;
224 PackageDetails selected = null;
225 for (PackageDetails details : packages.values()) {
226 if (shortName.equals(details.shortName)) {
227 if (selected == null) {
228 selected = details;
229 } else {
230 throw new IllegalStateException("Non-unique shortName in package library: " + shortName);
231 }
232 }
233 }
234 return selected;
235 }
236
237 /**
238 * Obtain a loaded package, given details of the package.
239 * This will return the package if it is already loaded; otherwise it will attempt to
240 * compile and/or load the package from XSLT source or from an export file.
241 * It will also load all the packages on which it depends, recursively, and will report
242 * an error if any cycles are found.
243 *
244 * @param details the package details for the required package, as previously returned
245 * (perhaps) using {@link #findPackage(String, PackageVersionRanges)}
246 * @param disallowed the names/versions of packages that reference this package, and that
247 * therefore cannot be referenced by this package without creating a cycle of dependencies.
248 * @return the loaded package
249 * @throws XPathException if loading the package fails, typically either because (a) a package
250 * contains static XSLT errors, or (b) because there is a cycle of package dependencies
251 * @since 9.8
252 */
253
254 public StylesheetPackage obtainLoadedPackage(PackageDetails details, List<VersionedPackageName> disallowed) throws XPathException {
255 if (details.loadedPackage != null) {
256 return details.loadedPackage;
257 } else if (details.exportLocation != null) {
258 testForCycles(details, disallowed);
259 details.beingProcessed = Thread.currentThread();
260 Source input = details.exportLocation;
261 IPackageLoader loader = config.makePackageLoader();
262 StylesheetPackage pack = loader.loadPackage(input);
263 checkNameAndVersion(pack, details);
264 details.loadedPackage = pack;
265 details.beingProcessed = null;
266 return pack;
267 } else if (details.sourceLocation != null) {
268 testForCycles(details, disallowed);
269 details.beingProcessed = Thread.currentThread();
270 Compilation compilation = new Compilation(config, compilerInfo);
271 compilation.setUsingPackages(disallowed);
272 compilation.setExpectedNameAndVersion(details.nameAndVersion);
273 compilation.clearParameters();
274 compilation.setLibraryPackage(true);
275 if (details.staticParams != null) {
276 for (Map.Entry<StructuredQName, GroundedValue<?>> entry : details.staticParams.entrySet()) {
277 compilation.setParameter(entry.getKey(), entry.getValue());
278 }
279 }
280 PrincipalStylesheetModule psm = compilation.compilePackage(details.sourceLocation);
281 details.beingProcessed = null;
282 if (compilation.getErrorCount() > 0) {
283 throw new XPathException("Errors found in package " + details.nameAndVersion.packageName);
284 }
285 StylesheetPackage styPack = psm.getStylesheetPackage();
286 checkNameAndVersion(styPack, details);
287 details.loadedPackage = styPack;
288 return styPack;
289 } else {
290 return null;
291 }
292 }
293
294 private void testForCycles(PackageDetails details, List<VersionedPackageName> disallowed) throws XPathException {
295 if (details.beingProcessed == Thread.currentThread()) {
296 // Report a cycle of package dependencies
297 FastStringBuffer buffer = new FastStringBuffer(1024);
298 for (VersionedPackageName n : disallowed) {
299 buffer.append(n.packageName);
300 buffer.append(", ");
301 }
302 buffer.append("and ");
303 buffer.append(details.nameAndVersion.packageName);
304 throw new XPathException("There is a cycle of package dependencies involving " + buffer, "XTSE3005");
305 }
306 }
307
308 private void checkNameAndVersion(StylesheetPackage pack, PackageDetails details) throws XPathException {
309 String storedName = pack.getPackageName();
310 if (details.baseName != null) {
311 if (!details.baseName.equals(storedName)) {
312 throw new XPathException("Base name of package (" + details.baseName +
313 ") does not match the value in the XSLT source (" +
314 storedName + ")");
315 }
316 } else {
317 if (!details.nameAndVersion.packageName.equals(storedName)) {
318 throw new XPathException("Registered name of package (" + details.nameAndVersion.packageName +
319 ") does not match the value in the XSLT source (" +
320 storedName + ")");
321 }
322 }
323 PackageVersion actualVersion = pack.getPackageVersion();
324 if (!actualVersion.equals(details.nameAndVersion.packageVersion)) {
325 throw new XPathException("Registered version number of package (" + details.nameAndVersion.packageVersion +
326 ") does not match the value in the XSLT source (" +
327 actualVersion + ")");
328 }
329 }
330
331 /**
332 * Supply all the packages that currently exist in this library
333 *
334 * @return every package indexed
335 */
336 public synchronized List<StylesheetPackage> getPackages() {
337 List<StylesheetPackage> result = new ArrayList<>();
338 for (PackageDetails details : packages.values()) {
339 if (details.loadedPackage != null) {
340 result.add(details.loadedPackage);
341 }
342 }
343 return result;
344 }
345
346 }