Skip to content

Commit 1db1ace

Browse files
RDF output format (#25)
* Add option to specify output format * Remove unnecessary variable
1 parent 9c4d7ad commit 1db1ace

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ Where `inputFile` is the location of the NetCDF file to convert,
7777
and `outputFile` is the location of the file in which to output the RDF graph.
7878
If you don't specify an `outputFile`, the graph will be printed on the command line.
7979

80+
By default, the RDF graph output will be in [Turtle](https://www.w3.org/TR/turtle/) format.
81+
You can use the `--output` or `-o` option to specify the RDF format to emit.
82+
This option can accept any of the RDF formats supported by Apache Jena, eg. JSON-LD and RDFXML.
83+
8084
You can also supply various options.
8185
Use the `-h` or `--help` option to emit full documentation for the available options.
8286
```

binary-array-ld-cli/src/main/kotlin/net/bald/BinaryArrayConvertCli.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import org.apache.commons.cli.DefaultParser
99
import org.apache.commons.cli.HelpFormatter
1010
import org.apache.commons.cli.Options
1111
import org.apache.jena.rdf.model.ModelFactory
12-
import java.io.File
13-
import java.io.OutputStream
12+
import java.io.*
1413
import kotlin.system.exitProcess
1514

1615
/**
@@ -21,6 +20,7 @@ class BinaryArrayConvertCli {
2120
addOption("u", "uri", true, "The URI which identifies the dataset.")
2221
addOption("a", "alias", true, "Comma-delimited list of RDF alias files.")
2322
addOption("c", "context", true, "Comma-delimited list of JSON-LD context files.")
23+
addOption("o", "output", true, "Output format. eg. ttl, json-ld, rdfxml.")
2424
addOption("h", "help", false, "Show help.")
2525
}
2626

@@ -44,9 +44,10 @@ class BinaryArrayConvertCli {
4444
.withContext(opts.contextLocs)
4545
.withAlias(opts.aliasLocs)
4646
val model = ba.use(ModelBinaryArrayConverter::convert)
47+
val outputFormat = opts.outputFormat ?: "ttl"
4748

4849
modelOutput(opts.outputLoc).use { output ->
49-
model.write(output, "ttl")
50+
model.write(output, outputFormat)
5051
}
5152
}
5253

@@ -77,7 +78,11 @@ class BinaryArrayConvertCli {
7778
}
7879

7980
private fun modelOutput(outputLoc: String?): OutputStream {
80-
return outputLoc?.let(::File)?.outputStream() ?: System.out
81+
return outputLoc?.let(::File)?.outputStream() ?: object: FilterOutputStream(System.out) {
82+
override fun close() {
83+
// do nothing, leave System.out open
84+
}
85+
}
8186
}
8287
}
8388

binary-array-ld-cli/src/main/kotlin/net/bald/CommandLineOptions.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ class CommandLineOptions(
1212
val uri: String? get() = cmd.getOptionValue("uri")
1313
val aliasLocs: List<String> get() = cmd.getOptionValue("alias")?.split(",") ?: emptyList()
1414
val contextLocs: List<String> get() = cmd.getOptionValue("context")?.split(",") ?: emptyList()
15+
val outputFormat: String? get() = cmd.getOptionValue("output")
1516
val help: Boolean get() = cmd.hasOption("help")
1617
}

binary-array-ld-cli/src/test/kotlin/net/bald/BinaryArrayConvertCliTest.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import bald.netcdf.CdlConverter.writeToNetCdf
66
import net.bald.vocab.BALD
77
import org.apache.jena.rdf.model.ModelFactory.createDefaultModel
88
import org.apache.jena.rdf.model.ResourceFactory.*
9+
import org.apache.jena.riot.RiotException
910
import org.apache.jena.vocabulary.*
1011
import org.junit.jupiter.api.Test
1112
import org.junit.jupiter.api.assertThrows
@@ -93,6 +94,36 @@ class BinaryArrayConvertCliTest {
9394
}
9495
}
9596

97+
@Test
98+
fun run_withOutputFormat_outputsToFile() {
99+
val inputFile = writeToNetCdf("/netcdf/identity.cdl")
100+
val outputFile = createTempFile()
101+
run(
102+
"--uri", "http://test.binary-array-ld.net/example",
103+
"--output", "json-ld",
104+
inputFile.absolutePath,
105+
outputFile.absolutePath
106+
)
107+
val result = outputFile.readText()
108+
val expected = javaClass.getResource("/jsonld/identity.json").readText()
109+
assertEquals(expected, result)
110+
}
111+
112+
@Test
113+
fun run_withInvalidFormat_throwsException() {
114+
val inputFile = writeToNetCdf("/netcdf/identity.cdl")
115+
val outputFile = createTempFile()
116+
val re = assertThrows<RiotException> {
117+
run(
118+
"--uri", "http://test.binary-array-ld.net/example",
119+
"--output", "foo",
120+
inputFile.absolutePath,
121+
outputFile.absolutePath
122+
)
123+
}
124+
assertEquals("No graph writer for 'foo'", re.message)
125+
}
126+
96127
private fun run_withPrefixMapping_outputsPrefixMapping(cdlLoc: String) {
97128
val inputFile = writeToNetCdf(cdlLoc)
98129
val outputFile = createTempFile()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"@graph" : [ {
3+
"@id" : "http://test.binary-array-ld.net/example",
4+
"@type" : "https://www.opengis.net/def/binary-array-ld/Container",
5+
"contains" : "http://test.binary-array-ld.net/example/"
6+
}, {
7+
"@id" : "http://test.binary-array-ld.net/example/",
8+
"@type" : "https://www.opengis.net/def/binary-array-ld/Container",
9+
"contains" : [ "http://test.binary-array-ld.net/example/var1", "http://test.binary-array-ld.net/example/var0" ]
10+
}, {
11+
"@id" : "http://test.binary-array-ld.net/example/var0",
12+
"@type" : "https://www.opengis.net/def/binary-array-ld/Resource"
13+
}, {
14+
"@id" : "http://test.binary-array-ld.net/example/var1",
15+
"@type" : "https://www.opengis.net/def/binary-array-ld/Resource"
16+
} ],
17+
"@context" : {
18+
"contains" : {
19+
"@id" : "https://www.opengis.net/def/binary-array-ld/contains",
20+
"@type" : "@id"
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)