This is an automated email from the ASF dual-hosted git repository.
slawrence pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/daffodil-sbt.git
The following commit(s) were added to refs/heads/main by this push:
new 4d2ed18 Support compiling main schemas found in a jar
4d2ed18 is described below
commit 4d2ed18c577d44d188fd321178dc2ea0d9b59ed8
Author: Steve Lawrence <[email protected]>
AuthorDate: Tue Jul 9 15:16:45 2024 -0400
Support compiling main schemas found in a jar
It is possible for a schema resource path in daffodilPackageBinInfos to
resolve to a path inside jar, such as when the main schema is in a
dependency jar or when the exportJars setting is true. If we try to
convert the resolved resource to a File for the compileFile API, the jar
URIs fail with a confusing exception.
To fix this, this switches to the compileSource API, which supports
compiling schemas with either file:// or jar:// URIs.
Closes #3
---
.../scala/org/apache/daffodil/DaffodilSaver.scala | 20 ++++++-----
.../sbt-daffodil/saved-parsers-05/build.sbt | 38 +++++++++++++++++++++
.../saved-parsers-05/project/plugins.sbt | 20 +++++++++++
.../src/main/resources/test.dfdl.xsd | 39 ++++++++++++++++++++++
.../sbt-daffodil/saved-parsers-05/test.script | 23 +++++++++++++
5 files changed, 132 insertions(+), 8 deletions(-)
diff --git a/src/main/scala/org/apache/daffodil/DaffodilSaver.scala
b/src/main/scala/org/apache/daffodil/DaffodilSaver.scala
index 2a8f5fd..52488fc 100644
--- a/src/main/scala/org/apache/daffodil/DaffodilSaver.scala
+++ b/src/main/scala/org/apache/daffodil/DaffodilSaver.scala
@@ -17,7 +17,7 @@
package org.apache.daffodil
-import java.io.File
+import java.net.URI
import java.nio.channels.FileChannel
import java.nio.channels.WritableByteChannel
import java.nio.file.Paths
@@ -34,7 +34,7 @@ import scala.collection.JavaConverters._
object DaffodilSaver {
/**
- * Usage: daffodilReflectionSave <schemaFile> <outputFile> <root> <config>
+ * Usage: daffodilReflectionSave <schemaResource> <outputFile> <root>
<config>
*
* If <root> or <config> is unknown/not-provided, they must be the empty
string
*/
@@ -42,7 +42,11 @@ object DaffodilSaver {
if (args.length != 4) System.err.println(s"[error] four arguments are
required")
- val schemaFile = new File(this.getClass.getResource(args(0)).toURI)
+ val schemaUrl = this.getClass.getResource(args(0))
+ if (schemaUrl == null) {
+ System.err.println(s"failed to find schema resource: ${args(0)}")
+ System.exit(1)
+ }
val output = FileChannel.open(
Paths.get(args(1)),
StandardOpenOption.CREATE,
@@ -52,7 +56,7 @@ object DaffodilSaver {
val config = if (args(3) != "") args(3) else null
// parameter types
- val cFile = classOf[File]
+ val cURI = classOf[URI]
val cString = classOf[String]
val cWritableByteChannel = classOf[WritableByteChannel]
@@ -64,7 +68,7 @@ object DaffodilSaver {
val compilerClass = Class.forName("org.apache.daffodil.japi.Compiler")
val compilerWithTunable = compilerClass.getMethod("withTunable", cString,
cString)
- val compilerCompileFile = compilerClass.getMethod("compileFile", cFile,
cString, cString)
+ val compilerCompileSource = compilerClass.getMethod("compileSource", cURI,
cString, cString)
val processorFactoryClass =
Class.forName("org.apache.daffodil.japi.ProcessorFactory")
val processorFactoryIsError = processorFactoryClass.getMethod("isError")
@@ -104,9 +108,9 @@ object DaffodilSaver {
}
}
- // val processorFactory = compiler.compileFile(schemaFile, root, None)
- val processorFactory = compilerCompileFile
- .invoke(compiler, schemaFile, root, null)
+ // val processorFactory = compiler.compileSource(schemaUrl.toURI, root,
None)
+ val processorFactory = compilerCompileSource
+ .invoke(compiler, schemaUrl.toURI, root, null)
// val processorFactoryDiags = processorFactory.getDiagnostics()
val processorFactoryDiags = processorFactoryGetDiagnostics
diff --git a/src/sbt-test/sbt-daffodil/saved-parsers-05/build.sbt
b/src/sbt-test/sbt-daffodil/saved-parsers-05/build.sbt
new file mode 100644
index 0000000..d0f9e7d
--- /dev/null
+++ b/src/sbt-test/sbt-daffodil/saved-parsers-05/build.sbt
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+version := "0.1"
+
+name := "test"
+
+organization := "com.example"
+
+// tells SBT to build a jar and put it on the classpath instead of putting
src/main/resources on
+// the classpath. This means schema resource paths will resolve to a path
inside a jar instead
+// of to a file
+exportJars := true
+
+enablePlugins(DaffodilPlugin)
+
+daffodilPackageBinInfos := Seq(
+ DaffodilBinInfo("/test.dfdl.xsd"),
+ DaffodilBinInfo("/test.dfdl.xsd", Some("test02"), Some("two")),
+)
+
+daffodilPackageBinVersions := Seq("3.6.0", "3.5.0")
+
+daffodilVersion := daffodilPackageBinVersions.value.head
diff --git a/src/sbt-test/sbt-daffodil/saved-parsers-05/project/plugins.sbt
b/src/sbt-test/sbt-daffodil/saved-parsers-05/project/plugins.sbt
new file mode 100644
index 0000000..eaf249b
--- /dev/null
+++ b/src/sbt-test/sbt-daffodil/saved-parsers-05/project/plugins.sbt
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+addSbtPlugin("org.apache.daffodil" % "sbt-daffodil" %
sys.props("plugin.version"))
diff --git
a/src/sbt-test/sbt-daffodil/saved-parsers-05/src/main/resources/test.dfdl.xsd
b/src/sbt-test/sbt-daffodil/saved-parsers-05/src/main/resources/test.dfdl.xsd
new file mode 100644
index 0000000..ae0d6db
--- /dev/null
+++
b/src/sbt-test/sbt-daffodil/saved-parsers-05/src/main/resources/test.dfdl.xsd
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<schema
+ xmlns="http://www.w3.org/2001/XMLSchema"
+ xmlns:xs="http://www.w3.org/2001/XMLSchema"
+ xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/"
+ xmlns:ex="http://example.com"
+ targetNamespace="http://example.com"
+ elementFormDefault="unqualified">
+
+ <include
schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
+
+ <annotation>
+ <appinfo source="http://www.ogf.org/dfdl/">
+ <dfdl:format ref="ex:GeneralFormat" />
+ </appinfo>
+ </annotation>
+
+ <element name="test01" type="xs:string" dfdl:lengthKind="delimited" />
+
+ <element name="test02" type="xs:string" dfdl:lengthKind="delimited" />
+
+</schema>
diff --git a/src/sbt-test/sbt-daffodil/saved-parsers-05/test.script
b/src/sbt-test/sbt-daffodil/saved-parsers-05/test.script
new file mode 100644
index 0000000..76a79ef
--- /dev/null
+++ b/src/sbt-test/sbt-daffodil/saved-parsers-05/test.script
@@ -0,0 +1,23 @@
+## Licensed to the Apache Software Foundation (ASF) under one
+## or more contributor license agreements. See the NOTICE file
+## distributed with this work for additional information
+## regarding copyright ownership. The ASF licenses this file
+## to you under the Apache License, Version 2.0 (the
+## "License"); you may not use this file except in compliance
+## with the License. You may obtain a copy of the License at
+##
+## http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing,
+## software distributed under the License is distributed on an
+## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+## KIND, either express or implied. See the License for the
+## specific language governing permissions and limitations
+## under the License.
+##
+
+> packageDaffodilBin
+$ exists target/test-0.1-daffodil350.bin
+$ exists target/test-0.1-daffodil360.bin
+$ exists target/test-0.1-two-daffodil350.bin
+$ exists target/test-0.1-two-daffodil360.bin