Skip to content

Commit d9cd231

Browse files
author
Yev Bronshteyn
committed
#6. Enables JSON-LD and Turtle reading and writing.
1 parent 3e44047 commit d9cd231

File tree

4 files changed

+94
-23
lines changed

4 files changed

+94
-23
lines changed

src/main/java/spdxedit/IoFileTypeSelectionDialog.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
package spdxedit;
22

33
import javafx.scene.control.ChoiceDialog;
4+
import javafx.scene.image.ImageView;
45
import spdxedit.io.FileDataType;
6+
import spdxedit.util.UiUtils;
57

68
import java.util.Optional;
79

810
/**
911
* A dialog that obtains a data type
1012
*/
11-
public final class IoFileTypeSelectionDialog{
13+
public final class IoFileTypeSelectionDialog {
1214

13-
public static Optional<FileDataType> getDataType(String title){
14-
ChoiceDialog<FileDataType> dialog = new ChoiceDialog<>();
15-
dialog.setTitle(title);
16-
dialog.setHeaderText("Select data file type:");
17-
dialog.getItems().addAll(FileDataType.values());
15+
private static final ChoiceDialog<FileDataType> fileTypeChoiceDialog;
1816

19-
return dialog.showAndWait();
17+
static {
18+
fileTypeChoiceDialog = new ChoiceDialog<>();
19+
fileTypeChoiceDialog.setTitle(Main.APP_TITLE);
20+
fileTypeChoiceDialog.setHeaderText("Select data file type:");
21+
fileTypeChoiceDialog.getItems().addAll(FileDataType.values());
22+
fileTypeChoiceDialog.setGraphic(UiUtils.ICON_IMAGE_VIEW_SMALL);
23+
fileTypeChoiceDialog.setSelectedItem(fileTypeChoiceDialog.getItems().get(0));
24+
}
25+
26+
27+
public static Optional<FileDataType> getDataType(String title) {
28+
return fileTypeChoiceDialog.showAndWait();
2029

2130
}
2231

src/main/java/spdxedit/io/FileDataType.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
public enum FileDataType {
1717
RDF_XML("RDF/XML", FileIoLogic::writeRdfXml, FileIoLogic::loadRdfXml, "rdf", "spdx"),
1818
TAG("Tag:Value", FileIoLogic::writeTagValue, FileIoLogic::loadTagValue, "spdx"),
19-
/* TURTLE("RDF-Turtle", "turtle"),
19+
TURTLE("RDF-Turtle", FileIoLogic::writeTurtle, FileIoLogic::readTurtle, "turtle", "spdx"),
20+
JSON_LD("JSON-LD", FileIoLogic::writeJsonLd, FileIoLogic::readJsonLd, "json");
21+
/*
2022
JSONLD("JSON-LD", "json"),
2123
RDF_JSON("RDF/JSON", "json")*/;
2224

@@ -32,17 +34,19 @@ public enum FileDataType {
3234
this.fileOutputLogic = fileOutputLogic;
3335
}
3436

35-
public List<String> getExtensions(){return extensions;}
37+
public List<String> getExtensions() {
38+
return extensions;
39+
}
3640

3741
public String getDisplayName() {
3842
return displayName;
3943
}
4044

41-
public void writeToFile(File file, SpdxDocument document) throws IOException{
45+
public void writeToFile(File file, SpdxDocument document) throws IOException {
4246
fileOutputLogic.write(file, document);
4347
}
4448

45-
public SpdxDocument readFromFile(File file) throws IOException, InvalidSPDXAnalysisException{
49+
public SpdxDocument readFromFile(File file) throws IOException, InvalidSPDXAnalysisException {
4650
return fileInputLogic.read(file);
4751
}
4852

src/main/java/spdxedit/io/FileIoLogic.java

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
import javafx.scene.control.Alert;
55
import javafx.scene.control.ButtonType;
66
import javafx.scene.control.TextArea;
7+
import org.apache.jena.rdf.model.Model;
8+
import org.apache.jena.rdf.model.ModelFactory;
9+
import org.apache.jena.riot.RDFFormat;
10+
import org.apache.jena.riot.system.PrefixMap;
11+
import org.apache.jena.riot.system.PrefixMapFactory;
12+
import org.apache.jena.riot.writer.JsonLDWriter;
13+
import org.apache.jena.riot.writer.TurtleWriter;
14+
import org.apache.jena.sparql.core.DatasetGraphFactory;
715
import org.spdx.rdfparser.InvalidSPDXAnalysisException;
816
import org.spdx.rdfparser.SPDXDocumentFactory;
917
import org.spdx.rdfparser.SpdxDocumentContainer;
@@ -12,6 +20,7 @@
1220
import org.spdx.tools.TagToRDF;
1321

1422
import java.io.*;
23+
import java.nio.file.Paths;
1524
import java.util.LinkedList;
1625
import java.util.List;
1726
import java.util.Properties;
@@ -20,29 +29,32 @@
2029
* Created by ybronshteyn on 1/29/17.
2130
*/
2231
public class FileIoLogic {
23-
public static void writeRdfXml(File file, SpdxDocument document) throws IOException{
24-
try(FileWriter writer = new FileWriter(file)){
25-
document.getDocumentContainer().getModel().write(writer);
26-
}
32+
33+
private static final RDFFormat JSON_LD_FORMAT = RDFFormat.JSONLD_COMPACT_PRETTY;
34+
35+
public static void writeRdfXml(File file, SpdxDocument document) throws IOException {
36+
try (FileWriter writer = new FileWriter(file)) {
37+
document.getDocumentContainer().getModel().write(writer);
38+
}
2739
}
2840

29-
public static SpdxDocument loadRdfXml(File file) throws IOException, InvalidSPDXAnalysisException{
41+
public static SpdxDocument loadRdfXml(File file) throws IOException, InvalidSPDXAnalysisException {
3042
return SPDXDocumentFactory.createSpdxDocument(file.getAbsolutePath());
3143
}
3244

3345

34-
public static void writeTagValue(File file, SpdxDocument document) throws IOException{
46+
public static void writeTagValue(File file, SpdxDocument document) throws IOException {
3547
Properties constants = CommonCode
3648
.getTextFromProperties("org/spdx/tag/SpdxTagValueConstants.properties");
3749
try (FileOutputStream os = new FileOutputStream(file); PrintWriter out = new PrintWriter(os);) {
3850
// print document to a file using tag-value format
3951
CommonCode.printDoc(document, out, constants);
40-
} catch (InvalidSPDXAnalysisException e){
52+
} catch (InvalidSPDXAnalysisException e) {
4153
throw new RuntimeException(("Illegal SPDX - unable to convert to tag/value"), e);
4254
}
4355
}
4456

45-
public static SpdxDocument loadTagValue(File file) throws IOException, InvalidSPDXAnalysisException{
57+
public static SpdxDocument loadTagValue(File file) throws IOException, InvalidSPDXAnalysisException {
4658
try (FileInputStream in = new FileInputStream(file)) {
4759
List<String> warnings = new LinkedList<>();
4860
SpdxDocumentContainer container = TagToRDF.convertTagFileToRdf(in, "RDF/XML", warnings);
@@ -54,11 +66,56 @@ public static SpdxDocument loadTagValue(File file) throws IOException, InvalidSP
5466
warningsAlert.showAndWait();
5567
}
5668
return container.getSpdxDocument();
57-
} catch (Exception e){
58-
if (e instanceof InvalidSPDXAnalysisException) throw (InvalidSPDXAnalysisException)e;
59-
if (e instanceof IOException) throw (IOException)e;
60-
throw new IOException("Unable to read/parse tag-value file "+file.getAbsolutePath(), e);
69+
} catch (Exception e) {
70+
if (e instanceof InvalidSPDXAnalysisException) throw (InvalidSPDXAnalysisException) e;
71+
if (e instanceof IOException) throw (IOException) e;
72+
throw new IOException("Unable to read/parse tag-value file " + file.getAbsolutePath(), e);
73+
}
74+
}
75+
76+
public static void writeTurtle(File file, SpdxDocument document) throws IOException {
77+
try (FileWriter writer = new FileWriter(file)) {
78+
TurtleWriter turtleWriter = new TurtleWriter();
79+
Model model = document.getDocumentContainer().getModel();
80+
PrefixMap prefixMap = PrefixMapFactory.create(model.getNsPrefixMap());
81+
turtleWriter.write(writer, model.getGraph(), prefixMap, document.getDocumentUri(), null);
82+
} catch (InvalidSPDXAnalysisException e) {
83+
throw new RuntimeException("Document namespace missing. The document is not complete");
84+
}
85+
}
86+
87+
public static SpdxDocument readTurtle(File file) throws IOException, InvalidSPDXAnalysisException {
88+
try (FileReader reader = new FileReader(file)) {
89+
Model model = ModelFactory.createDefaultModel();
90+
model.getReader("TURTLE").read(model, reader, getBaseUrl(file));
91+
SpdxDocumentContainer container = new SpdxDocumentContainer(model);
92+
return container.getSpdxDocument();
6193
}
6294
}
6395

96+
public static void writeJsonLd(File file, SpdxDocument document) throws IOException {
97+
try (FileWriter writer = new FileWriter(file)) {
98+
Model model = document.getDocumentContainer().getModel();
99+
JsonLDWriter jsonLDWriter = new JsonLDWriter(JSON_LD_FORMAT);
100+
PrefixMap prefixMap = PrefixMapFactory.create(model.getNsPrefixMap());
101+
jsonLDWriter.write(writer, DatasetGraphFactory.create(model.getGraph()), prefixMap, document.getDocumentUri(), null);
102+
} catch (InvalidSPDXAnalysisException e) {
103+
throw new RuntimeException("Document namespace missing. The document is not complete");
104+
}
105+
}
106+
107+
public static SpdxDocument readJsonLd(File file) throws IOException, InvalidSPDXAnalysisException {
108+
try (FileReader reader = new FileReader(file)) {
109+
Model model = ModelFactory.createDefaultModel();
110+
model.getReader(JSON_LD_FORMAT.getLang().getName()).read(model, reader, getBaseUrl(file));
111+
SpdxDocumentContainer container = new SpdxDocumentContainer(model);
112+
return container.getSpdxDocument();
113+
114+
}
115+
}
116+
117+
private static String getBaseUrl(File file) throws IOException {
118+
return Paths.get(file.getAbsolutePath()).toUri().toString();
119+
}
120+
64121
}

src/main/java/spdxedit/util/UiUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
public class UiUtils {
1616
public static final ImageView ICON_IMAGE_VIEW = new ImageView(MainSceneController.class.getResource("/img/document-8x.png").toString());
17+
public static final ImageView ICON_IMAGE_VIEW_SMALL = new ImageView(MainSceneController.class.getResource("/img/document-2x.png").toString());
1718

1819
/**
1920
* Get a modal dialog with the application icon

0 commit comments

Comments
 (0)