| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public PackageLibrary(CompilerInfo compilerInfo) { |
| 57 |
this.compilerInfo = compilerInfo; |
| 58 |
this.config = compilerInfo.getConfiguration(); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 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 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 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 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 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 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 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 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 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 |
|
| 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 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 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 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 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 |
|
| 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 |
|
| 333 |
|
| 334 |
|
| 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 |
} |