stevedlawrence commented on code in PR #183:
URL: https://github.com/apache/daffodil-sbt/pull/183#discussion_r3236299580
##########
src/main/scala/org/apache/daffodil/DaffodilPlugin.scala:
##########
@@ -840,6 +854,179 @@ object DaffodilPlugin extends AutoPlugin {
}
}
+ def daffodilFlattenSettings: Seq[Setting[_]] = Seq(
+ daffodilFlattenTarget := target.value /
s"${name.value}-${version.value}-flat.zip",
+
+ /**
+ * Whether or not to publish the flattened schemas zip. Defaults to false.
+ *
+ * If projects want to publish flattened schemas then they must explicitly
enable it by
+ * setting 'daffodilFlattenSchemas / publishArtifact := true'.
+ *
+ * If false, flattened schemas will not be created unless you explicitly
run the
+ * daffodilFlattenSchemas.
+ */
+ daffodilFlattenSchemas / publishArtifact := false,
+
+ daffodilFlattenSchemas / artifact := Artifact(
+ name.value,
+ "flat",
+ "zip",
+ Some("flat"),
+ Vector(),
+ None
+ ),
+
+ /**
+ * Only grab the file types that need to be flattened. These files are only
+ * grabbed from the root project. Any referenced files will be pulled in
+ * from the classpath. Note that XML files are commonly used in XSLT for
+ * settings.
+ */
+ daffodilFlattenSchemas / includeFilter := "*.xsd" | "*.xsl" | "*.xslt" |
"*.xml",
+ daffodilFlattenSchemas / excludeFilter := HiddenFileFilter,
+
+ daffodilFlattenResourceReferencePatterns := List(
+ """schemaLocation=\"([^\"]*)\"""".r,
+ "href=\"([^\"]*)\"".r,
+ "document[(]'([^']*)'[)]".r),
+
+ daffodilFlattenSchemas / products := {
+
+ val logger = streams.value.log
+ val filter = (daffodilFlattenSchemas / includeFilter).value --
(daffodilFlattenSchemas / excludeFilter).value
+
+ val flatDir = target.value / "flatDir"
+ if (flatDir.exists())
+ IO.delete(flatDir)
+ IO.createDirectory(flatDir)
+
+ val projectResources = (Compile / resourceDirectories).value
+ .map { root => root.toURI.toURL -> (root **
filter).get.map(_.toURI.toURL).toList }
+ .toMap
+
+ /**
+ * Create a URLClassLoader object with URLs to all resources used by the
+ * project. The class loader will be used to resolve references made
+ * within the flattened files.
+ */
+ val projectURLs = (Compile /
resourceDirectories).value.map(_.toURI.toURL)
+ val allClasspathURLs = (Test /
externalDependencyClasspath).value.map(_.data.toURI.toURL)
+ val classLoader = new URLClassLoader((projectURLs ++
allClasspathURLs).toArray, null)
+
+ val referenceRegexes = daffodilFlattenResourceReferencePatterns.value
+
+ val processed = scala.collection.mutable.Set[URI]()
+ projectResources foreach { case (rootURL, urls) =>
+ val unprocessed = scala.collection.mutable.Stack[URI]()
+ val rootURI = rootURL.toURI
+ val rootPath = Paths.get(rootURI)
+ unprocessed.pushAll(urls.map(_.toURI))
+ while (!unprocessed.isEmpty) {
+ val contextURI = unprocessed.pop
+ val bytes = contextURI.toURL.openStream().readAllBytes()
+ val contextRelPath = contextURI.getScheme match {
+ case "jar" => Paths.get(contextURI.toString.split("!")(1).tail)
+ case "file" => Paths.get(rootURI.relativize(contextURI).getPath)
+ case _ => throw new IllegalArgumentException(s"Unrecognized URI
scheme: $contextURI")
+ }
+ val contextFlatPath = Paths.get(
+ flatDir.toString,
+ contextRelPath.toString.replaceAll("/", "__"))
+ val bw = Files.newBufferedWriter(contextFlatPath)
+ val fileAsString = new String(bytes, Charset.defaultCharset())
+
+ val references = referenceRegexes.flatMap { re =>
+ re.findAllIn(fileAsString).matchData.map(_.group(1))
+ }
+
+ val resolvedRefs = references.map { ref =>
+ val origLocation = if (ref contains " ") ref.split(" ")(1) else ref
+ if (origLocation.startsWith("/")) {
+ // Dealing with an absolute path
+ val url = Option(classLoader.findResource(origLocation.tail))
+ if (url.isDefined)
+ Some(origLocation -> url.get.toURI)
+ else {
+ logger.warn(s"Unable to resolve absolute reference to $ref
from source file $contextURI")
+ None
+ }
+ } else {
+ // Relative path
+ val relPath =
contextRelPath.resolveSibling(origLocation).normalize()
+ if (Files.exists(rootPath.resolve(relPath))) {
+ // Found the file on the regular filesystem
+ Some(origLocation -> rootURI.resolve(relPath.toString))
+ } else {
+ // Check the classpath for the current reference relative to
the
+ // current contextURI
+ val url = Option(classLoader.findResource(relPath.toString))
Review Comment:
The URLClassLoader is needed to resolve absolute schemaLocations in the same
order that Daffodil does. But it's not used for relative schemaLocations. If a
schema happens to use all absolute paths (which jreap and link16 do) then the
URLClassLoader will be used to resolve things in the correct order and whatever
shows up on the classpath first wins. And many of our schemas use absolute
paths to override other schemas. But if a schema wants to disallow other jars
from overrideing paths they could use relative schemaLocations and Daffodil
will only look inside the same jar and ignore the classpath.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]