Skip to content

Commit 10c7c22

Browse files
Merge branch 'master' of github.com:binary-array-ld/net.binary_array_ld.bald
2 parents 5791830 + 9b79dc1 commit 10c7c22

File tree

20 files changed

+155
-25
lines changed

20 files changed

+155
-25
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,3 @@ the `ncgen` command line tool must be available on your system.
2222

2323
You can use Maven to build this project and each of its modules with `mvn clean package`.
2424
After building, the JAR for the command line application is located at `binary-array-ld-cli/target/bald-cli.jar`.
25-
26-
27-

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class BinaryArrayConvertCli {
2121
addOption("a", "alias", true, "Comma-delimited list of RDF alias files.")
2222
addOption("c", "context", true, "Comma-delimited list of JSON-LD context files.")
2323
addOption("o", "output", true, "Output format. eg. ttl, json-ld, rdfxml.")
24+
addOption("d", "download", true, "The URL from which the original file can be downloaded.")
2425
addOption("h", "help", false, "Show help.")
2526
}
2627

@@ -42,7 +43,7 @@ class BinaryArrayConvertCli {
4243
val context = context(opts.contextLocs)
4344
val alias = alias(opts.aliasLocs)
4445
val inputLoc = opts.inputLoc ?: throw IllegalArgumentException("First argument is required: NetCDF file to convert.")
45-
val ba = NetCdfBinaryArray.create(inputLoc, opts.uri, context, alias)
46+
val ba = NetCdfBinaryArray.create(inputLoc, opts.uri, context, alias, opts.downloadUrl)
4647
val model = ba.use(ModelBinaryArrayConverter::convert)
4748
val outputFormat = opts.outputFormat ?: "ttl"
4849

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
@@ -13,5 +13,6 @@ class CommandLineOptions(
1313
val aliasLocs: List<String> get() = cmd.getOptionValue("alias")?.split(",") ?: emptyList()
1414
val contextLocs: List<String> get() = cmd.getOptionValue("context")?.split(",") ?: emptyList()
1515
val outputFormat: String? get() = cmd.getOptionValue("output")
16+
val downloadUrl: String? get() = cmd.getOptionValue("download")
1617
val help: Boolean get() = cmd.hasOption("help")
1718
}

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,33 @@ class BinaryArrayConvertCliTest {
9797
}
9898
}
9999

100+
@Test
101+
fun run_withDownloadUrl_outputsDownloadUrl() {
102+
val inputFile = writeToNetCdf("/netcdf/identity.cdl")
103+
val outputFile = createTempFile()
104+
run(
105+
"--uri", "http://test.binary-array-ld.net/example",
106+
"--download", "http://test.binary-array-ld.net/download/example.nc",
107+
inputFile.absolutePath,
108+
outputFile.absolutePath
109+
)
110+
111+
val model = createDefaultModel().read(outputFile.toURI().toString(), "ttl")
112+
ModelVerifier(model).apply {
113+
resource("http://test.binary-array-ld.net/example/") {
114+
format()
115+
statement(RDF.type, BALD.Container)
116+
distribution("http://test.binary-array-ld.net/download/example.nc")
117+
statement(BALD.contains, model.createResource("http://test.binary-array-ld.net/example/var0")) {
118+
statement(RDF.type, BALD.Resource)
119+
}
120+
statement(BALD.contains, model.createResource("http://test.binary-array-ld.net/example/var1")) {
121+
statement(RDF.type, BALD.Resource)
122+
}
123+
}
124+
}
125+
}
126+
100127
@Test
101128
fun run_withOutputFormat_outputsToFile() {
102129
val inputFile = writeToNetCdf("/netcdf/identity.cdl")
@@ -569,9 +596,10 @@ class BinaryArrayConvertCliTest {
569596
}
570597
}
571598

572-
private fun StatementsVerifier.distribution() {
599+
private fun StatementsVerifier.distribution(downloadUrl: String? = null) {
573600
statement(DCAT.distribution) {
574601
statement(RDF.type, DCAT.Distribution)
602+
if (downloadUrl != null) statement(DCAT.downloadURL, createResource(downloadUrl))
575603
statement(DCAT.mediaType) {
576604
statement(DCTerms.identifier, createStringLiteral("application/x-netcdf"))
577605
statement(RDF.type, DCTerms.MediaType)

binary-array-ld-demo/src/main/java/net/bald/NetCdfConvertJava.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void convert() throws Exception {
2727
public static void convertWithExternalPrefixes() throws Exception {
2828
PrefixMapping prefix = ModelFactory.createDefaultModel().read("/path/to/context.json", "json-ld");
2929
ModelContext context = ModelContext.create(prefix);
30-
BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.nc", "http://test.binary-array-ld.net/example", context, null);
30+
BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.nc", "http://test.binary-array-ld.net/example", context, null, null);
3131
Model model = ModelBinaryArrayConverter.convert(ba);
3232

3333
try (OutputStream output = new FileOutputStream("/path/to/output.ttl")) {
@@ -38,7 +38,7 @@ public static void convertWithExternalPrefixes() throws Exception {
3838
public static void convertWithAliases() throws Exception {
3939
Model aliasModel = ModelFactory.createDefaultModel().read("/path/to/alias.ttl", "ttl");
4040
AliasDefinition alias = ModelAliasDefinition.create(aliasModel);
41-
BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.nc", "http://test.binary-array-ld.net/example", null, alias);
41+
BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.nc", "http://test.binary-array-ld.net/example", null, alias, null);
4242
Model model = ModelBinaryArrayConverter.convert(ba);
4343

4444
try (OutputStream output = new FileOutputStream("/path/to/output.ttl")) {

binary-array-ld-lib/src/main/kotlin/net/bald/BinaryArray.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ interface BinaryArray: Closeable {
2828
* The distribution of the binary array, if it is available. Otherwise, null.
2929
*/
3030
val distribution: Distribution?
31-
3231
}

binary-array-ld-lib/src/main/kotlin/net/bald/Distribution.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.bald
22

3+
import org.apache.jena.rdf.model.Resource
4+
35
/**
46
* A distribution of a binary array file.
57
*/
@@ -8,4 +10,10 @@ interface Distribution {
810
* The media type of the binary array file.
911
*/
1012
val mediaType: String
13+
14+
/**
15+
* The URL from which this distribution of the file can be downloaded, if it has one.
16+
* Otherwise, null.
17+
*/
18+
val downloadUrl: Resource?
1119
}

binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelBinaryArrayBuilder.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class ModelBinaryArrayBuilder(
4242
val distribution = model.createResource()
4343
.addProperty(RDF.type, DCAT.Distribution)
4444
.addProperty(DCAT.mediaType, mediaType)
45+
.apply {
46+
dist.downloadUrl?.let { downloadUrl ->
47+
addProperty(DCAT.downloadURL, downloadUrl)
48+
}
49+
}
4550
root.addProperty(DCAT.distribution, distribution)
4651
}
4752
}

binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelBinaryArrayBuilderTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class ModelBinaryArrayBuilderTest {
3636
}
3737
private val distribution = mock<Distribution> {
3838
on { mediaType } doReturn "application/x-netcdf"
39+
on { downloadUrl } doReturn createResource("http://test.binary-array-ld.net/download/example.nc")
3940
}
4041
private val ba = mock<BinaryArray> {
4142
on { this.root } doReturn root
@@ -93,7 +94,7 @@ class ModelBinaryArrayBuilderTest {
9394
ModelVerifier(model).apply {
9495
resource("http://test.binary-array-ld.net/example/") {
9596
format()
96-
distribution()
97+
distribution("http://test.binary-array-ld.net/download/example.nc")
9798
}
9899
}
99100
}

binary-array-ld-lib/src/test/kotlin/net/bald/model/StatementsVerifierExt.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@ package net.bald.model
22

33
import bald.model.StatementsVerifier
44
import org.apache.jena.rdf.model.ResourceFactory
5+
import org.apache.jena.rdf.model.ResourceFactory.createResource
56
import org.apache.jena.vocabulary.DCAT
67
import org.apache.jena.vocabulary.DCTerms
78
import org.apache.jena.vocabulary.RDF
89

910
fun StatementsVerifier.format() {
1011
statement(DCTerms.format) {
11-
statement(DCTerms.identifier,
12-
ResourceFactory.createResource("http://vocab.nerc.ac.uk/collection/M01/current/NC/"))
12+
statement(DCTerms.identifier, createResource("http://vocab.nerc.ac.uk/collection/M01/current/NC/"))
1313
statement(RDF.type, DCTerms.MediaType)
1414
}
1515
}
1616

17-
fun StatementsVerifier.distribution() {
17+
fun StatementsVerifier.distribution(downloadUrl: String? = null) {
1818
statement(DCAT.distribution) {
1919
statement(RDF.type, DCAT.Distribution)
20+
if (downloadUrl != null) statement(DCAT.downloadURL, createResource(downloadUrl))
2021
statement(DCAT.mediaType) {
2122
statement(DCTerms.identifier, ResourceFactory.createStringLiteral("application/x-netcdf"))
2223
statement(RDF.type, DCTerms.MediaType)

binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfBinaryArray.kt

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import net.bald.Distribution
55
import net.bald.Format
66
import net.bald.alias.AliasDefinition
77
import net.bald.context.ModelContext
8+
import org.apache.jena.rdf.model.ResourceFactory.createResource
89
import org.apache.jena.shared.PrefixMapping
910
import ucar.nc2.AttributeContainer
1011
import ucar.nc2.Group
1112
import ucar.nc2.NetcdfFile
1213
import ucar.nc2.NetcdfFiles
1314
import java.io.File
15+
import java.net.URI
1416

1517
/**
1618
* NetCDF implementation of [BinaryArray].
@@ -20,7 +22,8 @@ class NetCdfBinaryArray(
2022
val uri: String,
2123
private val file: NetcdfFile,
2224
private val context: ModelContext,
23-
val alias: AliasDefinition
25+
val alias: AliasDefinition,
26+
private val downloadUrl: String?
2427
): BinaryArray {
2528
override val root: NetCdfContainer get() = container(file.rootGroup)
2629

@@ -32,7 +35,19 @@ class NetCdfBinaryArray(
3235
}
3336

3437
override val format: Format get() = NetCdfFormat
35-
override val distribution: Distribution get() = NetCdfDistribution()
38+
39+
override val distribution: Distribution get() {
40+
val downloadUrl = downloadUrl ?: file.location.takeIf(::isHttp)
41+
val res = downloadUrl?.let(::createResource)
42+
return NetCdfDistribution(res)
43+
}
44+
45+
private fun isHttp(loc: String): Boolean {
46+
return try {
47+
val scheme = URI(loc).scheme
48+
scheme == "http" || scheme == "https"
49+
} catch (e: Exception) { false }
50+
}
3651

3752
val prefixSrc: String? get() = prefixSourceName()
3853

@@ -75,18 +90,20 @@ class NetCdfBinaryArray(
7590
* @param uri The URI which identifies the dataset.
7691
* @param context The external context with which to resolve prefix mappings.
7792
* @param alias The alias definition with which to resolve resource and property references.
93+
* @param downloadUrl The URL from which the file can be downloaded, if it has one. Otherwise, null.
7894
* @return A [BinaryArray] representation of the NetCDF file.
7995
*/
8096
@JvmStatic
8197
fun create(
8298
fileLoc: String,
8399
uri: String? = null,
84100
context: ModelContext? = null,
85-
alias: AliasDefinition? = null
101+
alias: AliasDefinition? = null,
102+
downloadUrl: String? = null
86103
): NetCdfBinaryArray {
87104
val file = NetcdfFiles.open(fileLoc)
88105
val requiredUri = uri ?: uri(fileLoc)
89-
return create(file, requiredUri, context, alias)
106+
return create(file, requiredUri, context, alias, downloadUrl)
90107
}
91108

92109
/**
@@ -105,11 +122,12 @@ class NetCdfBinaryArray(
105122
file: NetcdfFile,
106123
uri: String,
107124
context: ModelContext? = null,
108-
alias: AliasDefinition? = null
125+
alias: AliasDefinition? = null,
126+
downloadUrl: String? = null
109127
): NetCdfBinaryArray {
110128
val requiredContext = context ?: ModelContext.Empty
111129
val requiredAlias = alias ?: AliasDefinition.Empty
112-
return NetCdfBinaryArray(uri, file, requiredContext, requiredAlias)
130+
return NetCdfBinaryArray(uri, file, requiredContext, requiredAlias, downloadUrl)
113131
}
114132

115133
private fun uri(fileLoc: String): String {
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package net.bald.netcdf
22

33
import net.bald.Distribution
4+
import org.apache.jena.rdf.model.Resource
45

56
/**
67
* NetCDF implementation of [Distribution].
78
*/
8-
class NetCdfDistribution: Distribution {
9+
class NetCdfDistribution(
10+
override val downloadUrl: Resource? = null
11+
): Distribution {
912
override val mediaType: String get() = "application/x-netcdf"
1013
}

binary-array-ld-netcdf/src/test/kotlin/net/bald/netcdf/NetCdfBinaryArrayTest.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ import org.apache.jena.vocabulary.SKOS
1818
import org.junit.jupiter.api.Test
1919
import org.junit.jupiter.api.assertThrows
2020
import kotlin.test.assertEquals
21+
import kotlin.test.assertNull
2122

2223
class NetCdfBinaryArrayTest {
2324

24-
private fun fromCdl(cdlLoc: String, uri: String? = null, context: ModelContext? = null, alias: AliasDefinition? = null): BinaryArray {
25+
private fun fromCdl(
26+
cdlLoc: String,
27+
uri: String? = null,
28+
context: ModelContext? = null,
29+
alias: AliasDefinition? = null,
30+
downloadUrl: String? = null
31+
): BinaryArray {
2532
val file = writeToNetCdf(cdlLoc)
26-
return NetCdfBinaryArray.create(file.absolutePath, uri, context, alias)
33+
return NetCdfBinaryArray.create(file.absolutePath, uri, context, alias, downloadUrl)
2734
}
2835

2936
/**
@@ -47,6 +54,24 @@ class NetCdfBinaryArrayTest {
4754
assertEquals(expectedUri, ba.uri)
4855
}
4956

57+
@Test
58+
fun distribution_withDownloadUrl_returnsDistribution() {
59+
val downloadUrl = "http://test.binary-array-ld.net/download/identity.nc"
60+
val ba = fromCdl("/netcdf/identity.cdl", "http://test.binary-array-ld.net/identity.nc", downloadUrl = downloadUrl)
61+
val distribution = ba.distribution
62+
assertEquals(downloadUrl, distribution?.downloadUrl?.uri)
63+
assertEquals("application/x-netcdf", distribution?.mediaType)
64+
}
65+
66+
@Test
67+
fun distribution_withoutDownloadUrl_returnsEmptyDistribution() {
68+
val ba = fromCdl("/netcdf/identity.cdl", "http://test.binary-array-ld.net/identity.nc")
69+
val distribution = ba.distribution
70+
assertNull(distribution?.downloadUrl?.uri)
71+
assertEquals("application/x-netcdf", distribution?.mediaType)
72+
}
73+
74+
5075
/**
5176
* Requirements class A-2
5277
*/

docs/CNAME

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kotlin.binary-array-ld.net

docs/alias.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ and an alias definition to create a `BinaryArray` with both.
4040
```java
4141
Model aliasModel = ModelFactory.createDefaultModel().read("/path/to/alias.ttl", "ttl");
4242
AliasDefinition alias = ModelAliasDefinition.create(aliasModel);
43-
BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.nc", "http://test.binary-array-ld.net/example", null, alias);
43+
BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.nc", "http://test.binary-array-ld.net/example", null, alias, null);
4444
Model model = ModelBinaryArrayConverter.convert(ba);
4545

4646
try (OutputStream output = new FileOutputStream("/path/to/output.ttl")) {

docs/cli.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ You can find the documentation for this feature [here](context.md#cli).
4646
The CLI supports [aliases](alias.md).
4747
You can find the documentation for this feature [here](alias.md#cli).
4848

49+
### Download URL
50+
51+
The CLI supports [download URLs](download.md).
52+
4953
### Quick Reference
5054

5155
You can supply command line options in long form with the `--` prefix or short form with `-`,
@@ -57,4 +61,5 @@ followed by their value.
5761
| --uri | -u | The URI which identifies the dataset. | Input file URI |
5862
| --output | -o | Output format, eg. ttl, json-ld, rdfxml. | ttl |
5963
| --context | -c | Comma-delimited list of JSON-LD context files. ||
60-
| --alias | -a | Comma-delimited list of RDF alias files. ||
64+
| --alias | -a | Comma-delimited list of RDF alias files. ||
65+
| --download | -d | The URL from which the original file can be downloaded, if available. ||

docs/context.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ to create a `BinaryArray` with both.
3030
```java
3131
PrefixMapping prefix = ModelFactory.createDefaultModel().read("/path/to/context.json", "json-ld");
3232
ModelContext context = ModelContext.create(prefix);
33-
BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.nc", "http://test.binary-array-ld.net/example", context, null);
33+
BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.nc", "http://test.binary-array-ld.net/example", context, null, null);
3434
Model model = ModelBinaryArrayConverter.convert(ba);
3535

3636
try (OutputStream output = new FileOutputStream("/path/to/output.ttl")) {

docs/download.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Download URL
2+
3+
The BALD CLI and library allow you to specify a download URL for the given NetCDF file,
4+
as described in the [draft specification](http://docs.opengeospatial.org/DRAFTS/19-002.html#_download_url).
5+
6+
If you don't specify a download URL, but the given location of the NetCDF file is a remote URL (ie. it has `http` or `https` scheme),
7+
then the download URL will be inferred to be the same.
8+
9+
## CLI
10+
11+
You can optionally provide the download URL as a command line argument.
12+
Use the `--download` or `-d` option to specify the download URL for the NetCDF file.
13+
14+
#### Example
15+
```
16+
java -jar bald-cli.jar --download http://test.binary-array-ld.net/download/netcdf.nc /path/to/netcdf.nc /path/to/graph.ttl
17+
```
18+
19+
## Library
20+
21+
You can optionally provide the download URL as a parameter to the `NetCdfBinaryArray.create` method.
22+
Otherwise, you can simply pass in a null value.
23+
24+
#### Example
25+
```java
26+
BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.ttl", "http://test.binary-array-ld.net/example", null, null, "http://test.binary-array-ld.net/download/netcdf.nc");
27+
Model model = ModelBinaryArrayConverter.convert(ba);
28+
29+
try (OutputStream output = new FileOutputStream("/path/to/output.ttl")) {
30+
model.write(output, "ttl");
31+
}
32+
```

0 commit comments

Comments
 (0)