Skip to content

Commit

Permalink
#6. Enables JSON-LD and Turtle reading and writing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yev Bronshteyn committed Jan 30, 2017
1 parent 3e44047 commit d9cd231
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 23 deletions.
23 changes: 16 additions & 7 deletions src/main/java/spdxedit/IoFileTypeSelectionDialog.java
@@ -1,22 +1,31 @@
package spdxedit;

import javafx.scene.control.ChoiceDialog;
import javafx.scene.image.ImageView;
import spdxedit.io.FileDataType;
import spdxedit.util.UiUtils;

import java.util.Optional;

/**
* A dialog that obtains a data type
*/
public final class IoFileTypeSelectionDialog{
public final class IoFileTypeSelectionDialog {

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

return dialog.showAndWait();
static {
fileTypeChoiceDialog = new ChoiceDialog<>();
fileTypeChoiceDialog.setTitle(Main.APP_TITLE);
fileTypeChoiceDialog.setHeaderText("Select data file type:");
fileTypeChoiceDialog.getItems().addAll(FileDataType.values());
fileTypeChoiceDialog.setGraphic(UiUtils.ICON_IMAGE_VIEW_SMALL);
fileTypeChoiceDialog.setSelectedItem(fileTypeChoiceDialog.getItems().get(0));
}


public static Optional<FileDataType> getDataType(String title) {
return fileTypeChoiceDialog.showAndWait();

}

Expand Down
12 changes: 8 additions & 4 deletions src/main/java/spdxedit/io/FileDataType.java
Expand Up @@ -16,7 +16,9 @@
public enum FileDataType {
RDF_XML("RDF/XML", FileIoLogic::writeRdfXml, FileIoLogic::loadRdfXml, "rdf", "spdx"),
TAG("Tag:Value", FileIoLogic::writeTagValue, FileIoLogic::loadTagValue, "spdx"),
/* TURTLE("RDF-Turtle", "turtle"),
TURTLE("RDF-Turtle", FileIoLogic::writeTurtle, FileIoLogic::readTurtle, "turtle", "spdx"),
JSON_LD("JSON-LD", FileIoLogic::writeJsonLd, FileIoLogic::readJsonLd, "json");
/*
JSONLD("JSON-LD", "json"),
RDF_JSON("RDF/JSON", "json")*/;

Expand All @@ -32,17 +34,19 @@ public enum FileDataType {
this.fileOutputLogic = fileOutputLogic;
}

public List<String> getExtensions(){return extensions;}
public List<String> getExtensions() {
return extensions;
}

public String getDisplayName() {
return displayName;
}

public void writeToFile(File file, SpdxDocument document) throws IOException{
public void writeToFile(File file, SpdxDocument document) throws IOException {
fileOutputLogic.write(file, document);
}

public SpdxDocument readFromFile(File file) throws IOException, InvalidSPDXAnalysisException{
public SpdxDocument readFromFile(File file) throws IOException, InvalidSPDXAnalysisException {
return fileInputLogic.read(file);
}

Expand Down
81 changes: 69 additions & 12 deletions src/main/java/spdxedit/io/FileIoLogic.java
Expand Up @@ -4,6 +4,14 @@
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TextArea;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.riot.RDFFormat;
import org.apache.jena.riot.system.PrefixMap;
import org.apache.jena.riot.system.PrefixMapFactory;
import org.apache.jena.riot.writer.JsonLDWriter;
import org.apache.jena.riot.writer.TurtleWriter;
import org.apache.jena.sparql.core.DatasetGraphFactory;
import org.spdx.rdfparser.InvalidSPDXAnalysisException;
import org.spdx.rdfparser.SPDXDocumentFactory;
import org.spdx.rdfparser.SpdxDocumentContainer;
Expand All @@ -12,6 +20,7 @@
import org.spdx.tools.TagToRDF;

import java.io.*;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
Expand All @@ -20,29 +29,32 @@
* Created by ybronshteyn on 1/29/17.
*/
public class FileIoLogic {
public static void writeRdfXml(File file, SpdxDocument document) throws IOException{
try(FileWriter writer = new FileWriter(file)){
document.getDocumentContainer().getModel().write(writer);
}

private static final RDFFormat JSON_LD_FORMAT = RDFFormat.JSONLD_COMPACT_PRETTY;

public static void writeRdfXml(File file, SpdxDocument document) throws IOException {
try (FileWriter writer = new FileWriter(file)) {
document.getDocumentContainer().getModel().write(writer);
}
}

public static SpdxDocument loadRdfXml(File file) throws IOException, InvalidSPDXAnalysisException{
public static SpdxDocument loadRdfXml(File file) throws IOException, InvalidSPDXAnalysisException {
return SPDXDocumentFactory.createSpdxDocument(file.getAbsolutePath());
}


public static void writeTagValue(File file, SpdxDocument document) throws IOException{
public static void writeTagValue(File file, SpdxDocument document) throws IOException {
Properties constants = CommonCode
.getTextFromProperties("org/spdx/tag/SpdxTagValueConstants.properties");
try (FileOutputStream os = new FileOutputStream(file); PrintWriter out = new PrintWriter(os);) {
// print document to a file using tag-value format
CommonCode.printDoc(document, out, constants);
} catch (InvalidSPDXAnalysisException e){
} catch (InvalidSPDXAnalysisException e) {
throw new RuntimeException(("Illegal SPDX - unable to convert to tag/value"), e);
}
}

public static SpdxDocument loadTagValue(File file) throws IOException, InvalidSPDXAnalysisException{
public static SpdxDocument loadTagValue(File file) throws IOException, InvalidSPDXAnalysisException {
try (FileInputStream in = new FileInputStream(file)) {
List<String> warnings = new LinkedList<>();
SpdxDocumentContainer container = TagToRDF.convertTagFileToRdf(in, "RDF/XML", warnings);
Expand All @@ -54,11 +66,56 @@ public static SpdxDocument loadTagValue(File file) throws IOException, InvalidSP
warningsAlert.showAndWait();
}
return container.getSpdxDocument();
} catch (Exception e){
if (e instanceof InvalidSPDXAnalysisException) throw (InvalidSPDXAnalysisException)e;
if (e instanceof IOException) throw (IOException)e;
throw new IOException("Unable to read/parse tag-value file "+file.getAbsolutePath(), e);
} catch (Exception e) {
if (e instanceof InvalidSPDXAnalysisException) throw (InvalidSPDXAnalysisException) e;
if (e instanceof IOException) throw (IOException) e;
throw new IOException("Unable to read/parse tag-value file " + file.getAbsolutePath(), e);
}
}

public static void writeTurtle(File file, SpdxDocument document) throws IOException {
try (FileWriter writer = new FileWriter(file)) {
TurtleWriter turtleWriter = new TurtleWriter();
Model model = document.getDocumentContainer().getModel();
PrefixMap prefixMap = PrefixMapFactory.create(model.getNsPrefixMap());
turtleWriter.write(writer, model.getGraph(), prefixMap, document.getDocumentUri(), null);
} catch (InvalidSPDXAnalysisException e) {
throw new RuntimeException("Document namespace missing. The document is not complete");
}
}

public static SpdxDocument readTurtle(File file) throws IOException, InvalidSPDXAnalysisException {
try (FileReader reader = new FileReader(file)) {
Model model = ModelFactory.createDefaultModel();
model.getReader("TURTLE").read(model, reader, getBaseUrl(file));
SpdxDocumentContainer container = new SpdxDocumentContainer(model);
return container.getSpdxDocument();
}
}

public static void writeJsonLd(File file, SpdxDocument document) throws IOException {
try (FileWriter writer = new FileWriter(file)) {
Model model = document.getDocumentContainer().getModel();
JsonLDWriter jsonLDWriter = new JsonLDWriter(JSON_LD_FORMAT);
PrefixMap prefixMap = PrefixMapFactory.create(model.getNsPrefixMap());
jsonLDWriter.write(writer, DatasetGraphFactory.create(model.getGraph()), prefixMap, document.getDocumentUri(), null);
} catch (InvalidSPDXAnalysisException e) {
throw new RuntimeException("Document namespace missing. The document is not complete");
}
}

public static SpdxDocument readJsonLd(File file) throws IOException, InvalidSPDXAnalysisException {
try (FileReader reader = new FileReader(file)) {
Model model = ModelFactory.createDefaultModel();
model.getReader(JSON_LD_FORMAT.getLang().getName()).read(model, reader, getBaseUrl(file));
SpdxDocumentContainer container = new SpdxDocumentContainer(model);
return container.getSpdxDocument();

}
}

private static String getBaseUrl(File file) throws IOException {
return Paths.get(file.getAbsolutePath()).toUri().toString();
}

}
1 change: 1 addition & 0 deletions src/main/java/spdxedit/util/UiUtils.java
Expand Up @@ -14,6 +14,7 @@
*/
public class UiUtils {
public static final ImageView ICON_IMAGE_VIEW = new ImageView(MainSceneController.class.getResource("/img/document-8x.png").toString());
public static final ImageView ICON_IMAGE_VIEW_SMALL = new ImageView(MainSceneController.class.getResource("/img/document-2x.png").toString());

/**
* Get a modal dialog with the application icon
Expand Down

0 comments on commit d9cd231

Please sign in to comment.