Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
53 changes: 46 additions & 7 deletions daffodil-cli/src/main/scala/org/apache/daffodil/cli/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import org.apache.daffodil.lib.util.Timer
import org.apache.daffodil.lib.validation.Validators
import org.apache.daffodil.lib.xml.QName
import org.apache.daffodil.lib.xml.RefQName
import org.apache.daffodil.lib.xml.XMLUtils
import org.apache.daffodil.runtime1.api.DFDL
import org.apache.daffodil.runtime1.debugger.DebuggerExitException
import org.apache.daffodil.runtime1.debugger.InteractiveDebugger
Expand Down Expand Up @@ -226,14 +227,52 @@ class CLIConf(arguments: Array[String], stdout: PrintStream, stderr: PrintStream
implicit def rootNSConverter = org.rogach.scallop.singleArgConverter[RefQName](qnameConvert _)

implicit def fileResourceURIConverter = singleArgConverter[URI]((s: String) => {
val file = new File(s)
val uri =
if (file.isFile()) {
Some(file.toURI)
} else {
Misc.getResourceRelativeOption(s, None)
val optResolved =
try {
val uri =
if (File.separatorChar == '/' || s.startsWith("/")) {
// This is either a non-Windows system or a resource on the classpath. Either way we
// assume it is a valid URI except for things like spaces, which this URI
// constructor converts. We do not specify a schema since this might be a relative
// path
new URI(null, s, null)
} else {
// This is a Windows system, which has complex path resolution and paths that are
// not valid URIs. Try to convert it to a relative URI where possible, otherwise we
// settle for an absolute URI
val p = Paths.get(s)
if (p.isAbsolute() || s.startsWith("\\")) {
// if the Windows path is absolute (i.e. starts with a drive letter and colon) or
// starts with a backslash (which resolves relative to the current drive instead
// of the current working directory), then there is no valid relative URI
// representation, so we just convert it to an absolute URI
p.toUri
} else {
// this Windows path is relative to the current working directory. We can convert
// it to a relative URI by just switching all the path separators. We do not
// specify a schema since this is a relative path
new URI(null, s.replace('\\', '/'), null)
}
}
// At this point we have a valid URI, which could be absolute or relative, with relative
// URIs resolved from the current working directory. We can convert this to a string and
// pass it to resolveSchemaLocation to find the actual file or resource
val cwd = Paths.get("").toUri
XMLUtils.resolveSchemaLocation(uri.toString, Some(cwd))
} catch {
case _: Exception => throw new Exception(s"Could not find file or resource $s")
}
optResolved match {
case Some((uri, relToAbs)) => {
if (relToAbs) {
Logger.log.warn(s"Found relative path on classpath absolutely, did you mean /$s")
}
uri
}
uri.getOrElse(throw new Exception("Could not find file or resource %s".format(s)))
case None => {
throw new Exception(s"Could not find file or resource $s")
}
}
})

printedName = "Apache Daffodil"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
xmlns:ibmSchExtn="http://www.ibm.com/schema/extensions"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://www.ibm.com/dfdl/GeneralPurposeFormat"
schemaLocation="IBMdefined/GeneralPurposeFormat.xsd" />
schemaLocation="/IBMdefined/GeneralPurposeFormat.xsd" />
<xsd:element dfdl:terminator="%NL;" name="ABC">
<xsd:complexType>
<xsd:sequence dfdl:separator="">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
xmlns:dfdlx="http://www.ogf.org/dfdl/dfdl-1.0/extensions"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormatPortable.dfdl.xsd"/>
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormatPortable.dfdl.xsd"/>

<annotation>
<appinfo source="http://www.ogf.org/dfdl/">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>

<annotation>
<appinfo source="http://www.ogf.org/dfdl/">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" targetNamespace="http://example.com"
xmlns:ex="http://example.com">

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>

<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/" >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" targetNamespace="http://example.com"
xmlns:ex="http://example.com">

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>

<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/" >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" targetNamespace="http://example.com"
xmlns:ex="http://example.com">

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>

<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/" >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" xmlns:tns="http://complexSchema.com"
targetNamespace="http://complexSchema.com">

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd" />
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd" />
<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/">
<dfdl:format ref="tns:GeneralFormat" separator=""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" xmlns:tns="http://baseSchema.com"
targetNamespace="http://baseSchema.com">

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>

<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
xmlns:dfdlx="http://www.ogf.org/dfdl/dfdl-1.0/extensions"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormatPortable.dfdl.xsd"/>
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormatPortable.dfdl.xsd"/>

<annotation>
<appinfo source="http://www.ogf.org/dfdl/">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/">

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>

<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/" >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" xmlns:tns="http://example.com"
targetNamespace="http://example.com">

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd" />
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd" />
<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/">
<dfdl:format ref="ex:GeneralFormat"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
xmlns:ex="http://example.com"
elementFormDefault="unqualified">

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>

<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/" >
Expand All @@ -32,7 +32,7 @@
</xs:appinfo>
</xs:annotation>

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd" />
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd" />

<!-- Generates a warning unless it is suppressed by way of the tunable -->
<xs:element name="e1" dfdl:lengthKind="implicit">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<tdml:defineSchema name="s1">
<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<dfdl:format ref="ex:GeneralFormat" representation="binary"/>
<xs:element name="e1" type="xs:int"/>
</tdml:defineSchema>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
xmlns:fn="http://www.w3.org/2005/xpath-functions"
elementFormDefault="unqualified">

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>

<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/" >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
xmlns="http://example.com"
elementFormDefault="unqualified">

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>

<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/" >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
xmlns:tns="http://example.com"
targetNamespace="http://example.com">

<include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd" />
<include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd" />

<import
namespace="urn:org.apache.daffodil.layers.buggy"
schemaLocation="org/apache/daffodil/layers/xsd/buggyLayer.dfdl.xsd"/>
schemaLocation="/org/apache/daffodil/layers/xsd/buggyLayer.dfdl.xsd"/>

<annotation>
<appinfo source="http://www.ogf.org/dfdl/">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
targetNamespace="urn:org.apache.daffodil.layers.buggy"
elementFormDefault="unqualified">

<include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd" />
<include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd" />

<annotation>
<appinfo source="http://www.ogf.org/dfdl/">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
xmlns:tns="http://www.example.org/example1/"
targetNamespace="http://www.example.org/example1/">

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>

<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,53 @@ class TestCLIParsing {
}

@Test def test_DFDL_1203_schema_from_jar(): Unit = {
// This test makes sure a schema can be compiled from a jar on the classpath--the schema
// val should not use the path() function, and the value should be an absolute resource
// path. Note that this requires the daffodil-cli src/test/resources directory to be on the
// classpath. If this test changes so that runCLI forks, that might not be the case and this
// test might fail
val schema = "/org/apache/daffodil/cli/global_element.dfdl.xsd"

val input = path(
"daffodil-cli/src/test/resources/org/apache/daffodil/cli/input/test_DFDL-714.txt",
)

runCLI(args"parse -s $schema $input") { cli =>
cli.expect("<tns:elem xmlns:tns=\"http://baseSchema.com\">")
cli.expect("<content")
cli.expect("Hello World")
cli.expect("</tns:elem>")
}(ExitCode.Success)
}

@Test def test_DFDL_1203_schema_from_jar_relative(): Unit = {
// This is the same as test_DFDL_1203_schema_from_jar, but its schema is relative. This
// behavior is deprecated and should lead to a CLI warning. Future versions of Daffodil may
// disallow this and it may become a CLI error
val schema = "org/apache/daffodil/cli/global_element.dfdl.xsd"

val input = path(
"daffodil-cli/src/test/resources/org/apache/daffodil/cli/input/test_DFDL-714.txt",
)

runCLI(args"parse -s $schema $input") { cli =>
cli.expect("<tns:elem xmlns:tns=\"http://baseSchema.com\">")
cli.expect("<content")
cli.expect("Hello World")
cli.expect("</tns:elem>")
cli.expectErr("Found relative path on classpath absolutely")
cli.expectErr("/org/apache/daffodil/cli/global_element.dfdl.xsd")
}(ExitCode.Success)
}

@Test def test_DFDL_1203_schema_from_absolute_path(): Unit = {
// This test makes sure a schema can be compiled from an absolute path on the filesystem.
// Note that this will result in the CLI seeing OS-dependent path separators, and on Windows
// will have a drive letter and colon prefixed
val schema = path(
"daffodil-cli/src/test/resources/org/apache/daffodil/cli/global_element.dfdl.xsd",
)
).toAbsolutePath
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this going to be a MS-Windows style path when this test is run on Windows?
I honestly did not know that. I assumed Java paths were somehow portable things at that point.
If so, please add comment like // on MS Windows this will be a windows-style path with backslash separators.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the path method just does Paths.get(...) which supports unix-style separators even on windows systems. And when call toString on that Path to get a string to pass in to the CLI, the string contains windows style paths.

So Paths.get("/path/containing/unix/separators").toString will work on Linux or Windows, and return the right thing depending on the OS.


val input = path(
"daffodil-cli/src/test/resources/org/apache/daffodil/cli/input/test_DFDL-714.txt",
)
Expand Down Expand Up @@ -672,10 +716,9 @@ class TestCLIParsing {
val input = path("daffodil-cli/src/test/resources/org/apache/daffodil/cli/input/input6.txt")

runCLI(args"parse -s $schema -r e $input") { cli =>
cli.expectErr("Schema Definition Warning")
cli.expectErr("Schema Definition Error")
cli.expectErr("edu/illinois/ncsa/daffodil/xsd/built-in-formats.xsd")
cli.expectErr("org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd")
}(ExitCode.Success)
}(ExitCode.UnableToCreateProcessor)
}

@Test def test_CLI_Parsing_JavaDefaults(): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<!-- Representation property bindings -->

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/">
<dfdl:format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
-->

<tdml:defineSchema name="length">
<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<dfdl:format ref="GeneralFormat" representation="binary"/>
<xs:element name="e1">
<xs:complexType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<!-- Representation property bindings -->

<xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/">
<dfdl:format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<!-- Binary representation properties -->

<include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<annotation>
<appinfo source="http://www.ogf.org/dfdl/">
<dfdl:format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<!-- Binary representation properties -->

<include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<annotation>
<appinfo source="http://www.ogf.org/dfdl/">
<dfdl:format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<!-- Binary representation properties -->

<include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<annotation>
<appinfo source="http://www.ogf.org/dfdl/">
<dfdl:format
Expand Down
Loading