@@ -14,12 +14,19 @@ import org.apache.jena.riot.RDFParser
1414import org .apache .jena .riot .system .StreamRDFLib
1515import org .apache .jena .sparql .core .Quad
1616
17- import scala .collection .BufferedIterator
1817import scala .util .Using
1918
2019object RdfValidatePrint extends RdfCommandPrintUtil [RdfFormat .Jena ]:
2120 override val defaultFormat : RdfFormat = RdfFormat .NQuads
2221
22+ @ HelpMessage (
23+ " Validates the input Jelly-RDF stream.\n If no additional options are specified, " +
24+ " only basic validations are performed. You can also validate the stream against " +
25+ " a reference RDF file, check the stream options, and its delimiting.\n " +
26+ " If an error is detected, the program will exit with a non-zero code.\n " +
27+ " Otherwise, the program will exit with code 0." ,
28+ )
29+ @ ArgsName (" <file-to-validate>" )
2330case class RdfValidateOptions (
2431 @ Recurse
2532 common : JellyCommandOptions = JellyCommandOptions (),
@@ -85,10 +92,10 @@ object RdfValidate extends JellyCommand[RdfValidateOptions]:
8592 // Step 1: Validate delimiting
8693 validateDelimiting(delimiting, delimited)
8794 // Step 2: Validate basic stream structure & the stream options
88- val framesBuffered = frameIterator.buffered
89- validateOptions(framesBuffered )
95+ val framesSeq = frameIterator.toSeq
96+ validateOptions(framesSeq )
9097 // Step 3: Validate the content
91- validateContent(framesBuffered , frameIndices, rdfComparison)
98+ validateContent(framesSeq , frameIndices, rdfComparison)
9299
93100 private def validateDelimiting (
94101 expected : Delimiting ,
@@ -102,7 +109,7 @@ object RdfValidate extends JellyCommand[RdfValidateOptions]:
102109 if delimited then
103110 throw CriticalException (" Expected undelimited input, but the file was delimited" )
104111
105- private def validateOptions (frames : BufferedIterator [RdfStreamFrame ]): Unit =
112+ private def validateOptions (frames : Seq [RdfStreamFrame ]): Unit =
106113 // Validate basic stream structure
107114 if frames.isEmpty then throw CriticalException (" Empty input stream" )
108115 if frames.head.rows.isEmpty then
@@ -117,7 +124,9 @@ object RdfValidate extends JellyCommand[RdfValidateOptions]:
117124 }
118125 if streamOptions != o then
119126 throw CriticalException (
120- s " Stream options do not match the expected options in $optionsFileName" ,
127+ s " Stream options do not match the expected options in $optionsFileName\n " +
128+ s " Expected: $o\n " +
129+ s " Actual: $streamOptions" ,
121130 )
122131 o
123132 }
@@ -127,7 +136,7 @@ object RdfValidate extends JellyCommand[RdfValidateOptions]:
127136 )
128137
129138 private def validateContent (
130- frames : BufferedIterator [RdfStreamFrame ],
139+ frames : Seq [RdfStreamFrame ],
131140 frameIndices : IndexRange ,
132141 maybeRdfComparison : Option [StreamRdfCollector ],
133142 ): Unit =
@@ -140,12 +149,13 @@ object RdfValidate extends JellyCommand[RdfValidateOptions]:
140149 None ,
141150 (prefix, iri) => jellyStreamConsumer.prefix(prefix, iri.getURI),
142151 )
143- for (frame, i) <- frameIndices.slice(frames).zipWithIndex do
152+ val x = frameIndices.slice(frames).zipWithIndex
153+ for (frame, i) <- x do
144154 val frameIndex = frameIndices.start.getOrElse(0 ) + i
145155 for row <- frame.rows do
146156 if row.row.isOptions && row.row.options != opt then
147157 throw CriticalException (
148- s " Later occurrence of stream options in frame $frameIndex do not match the first " ,
158+ s " Later occurrence of stream options in frame $frameIndex does not match the first " ,
149159 )
150160 // Push the stream frames through the decoder
151161 // This will catch most of the errors
@@ -156,23 +166,23 @@ object RdfValidate extends JellyCommand[RdfValidateOptions]:
156166 // because it's too performance-costly.
157167 case t : Triple =>
158168 if ! opt.generalizedStatements && StatementUtils .isGeneralized(t) then
159- throw CriticalException (s " Generalized triple in frame $frameIndex: $t" )
169+ throw CriticalException (s " Unexpected generalized triple in frame $frameIndex: $t" )
160170 if ! opt.rdfStar && StatementUtils .isRdfStar(t) then
161- throw CriticalException (s " RDF-star triple in frame $frameIndex: $t" )
171+ throw CriticalException (s " Unexpected RDF-star triple in frame $frameIndex: $t" )
162172 jellyStreamConsumer.triple(t)
163173 case q : Quad =>
164174 if ! opt.generalizedStatements && StatementUtils .isGeneralized(q) then
165- throw CriticalException (s " Generalized quad in frame $frameIndex: $q" )
175+ throw CriticalException (s " Unexpected generalized quad in frame $frameIndex: $q" )
166176 if ! opt.rdfStar && StatementUtils .isRdfStar(q) then
167- throw CriticalException (s " RDF-star quad in frame $frameIndex: $q" )
177+ throw CriticalException (s " Unexpected RDF-star quad in frame $frameIndex: $q" )
168178 jellyStreamConsumer.quad(q)
169179 // Compare the Jelly data with the reference RDF data, if specified
170180 maybeRdfComparison.foreach { rdfComparison =>
171181 val actual = jellyStreamConsumer.asInstanceOf [StreamRdfCollector ]
172182 val comparator =
173183 if getOptions.compareOrdered then OrderedRdfCompare
174184 else UnorderedRdfCompare
175- comparator.compare(actual, rdfComparison )
185+ comparator.compare(rdfComparison, actual )
176186 }
177187
178188 /** Reads the RDF file for comparison and returns a StreamRdfCollector
0 commit comments