diff --git a/pom.xml b/pom.xml index e7e8aeb..3d76e7c 100644 --- a/pom.xml +++ b/pom.xml @@ -1,102 +1,138 @@ - 4.0.0 - - - jitpack.io - https://jitpack.io - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + + + jena-releases + Apache Jena Releases + https://repository.apache.org/content/repositories/releases + + + + false + + bintray-yevster-spdx-tools-fork + bintray + https://dl.bintray.com/yevster/spdx-tools-fork + + + rdfa-bintray + https://dl.bintray.com/yevster/maven + + + - src/main/java - test/main/java - - - test/main/resources - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.5.1 - - 1.8 - 1.8 - - - - org.apache.maven.plugins - maven-resources-plugin - 2.7 - - ${project.build.sourceEncoding} - - - - maven-assembly-plugin - - - - spdxedit.Main - - - - jar-with-dependencies - - - - - make-assembly - - package - - - single - - - - - - - groupId - spdx-edit - 1.0.1 - - UTF-8 - - - - org.slf4j - slf4j-api - 1.7.12 - - - org.slf4j - slf4j-simple - 1.7.12 - - - com.hp.hpl.jena - arq - 2.8.8 - - + src/main/java + test/main/java + + + test/main/resources + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-resources-plugin + 2.7 + + ${project.build.sourceEncoding} + + + + maven-assembly-plugin + + + + spdxedit.Main + + + + jar-with-dependencies + + + + + make-assembly + + package + + + single + + + + + + + groupId + spdx-edit + 1.0.1 + + UTF-8 + + + + org.slf4j + slf4j-api + 1.7.12 + + + org.slf4j + slf4j-simple + 1.7.12 + + + org.spdx + spdx-tools + 2.2.0 + + + org.antlr + antlr + 3.4 + + + org.controlsfx + controlsfx + 8.40.12 + + + org.apache.commons + commons-lang3 + 3.5 + + + com.google.guava + guava + 16.0.1 + + + org.apache.jena + apache-jena-libs + 3.1.1 + pom + + + junit + junit + 4.12 + test + + - - com.github.spdx - tools - 01f8d0d4da - - - org.controlsfx - controlsfx - 8.40.12 - - + + \ No newline at end of file diff --git a/src/main/java/spdxedit/IoFileTypeSelectionDialog.java b/src/main/java/spdxedit/IoFileTypeSelectionDialog.java new file mode 100644 index 0000000..4dd440d --- /dev/null +++ b/src/main/java/spdxedit/IoFileTypeSelectionDialog.java @@ -0,0 +1,23 @@ +package spdxedit; + +import javafx.scene.control.ChoiceDialog; +import spdxedit.io.FileDataType; + +import java.util.Optional; + +/** + * A dialog that obtains a data type + */ +public final class IoFileTypeSelectionDialog{ + + public static Optional getDataType(String title){ + ChoiceDialog dialog = new ChoiceDialog<>(); + dialog.setTitle(title); + dialog.setHeaderText("Select data file type:"); + dialog.getItems().addAll(FileDataType.values()); + + return dialog.showAndWait(); + + } + +} diff --git a/src/main/java/spdxedit/MainSceneController.java b/src/main/java/spdxedit/MainSceneController.java index 3ce3370..629984b 100644 --- a/src/main/java/spdxedit/MainSceneController.java +++ b/src/main/java/spdxedit/MainSceneController.java @@ -20,6 +20,7 @@ import org.spdx.rdfparser.model.SpdxPackage; import org.spdx.tag.CommonCode; import org.spdx.tools.TagToRDF; +import spdxedit.io.FileDataType; import spdxedit.util.UiUtils; import java.io.*; @@ -46,9 +47,6 @@ public class MainSceneController { @FXML private Button saveSpdx; - @FXML - private Button saveSpdxTag; - @FXML private Button validateSpdx; @@ -86,16 +84,20 @@ protected void updateItem(SpdxPackage item, boolean empty) { } - private FileChooser getSpdxFileChooser(String... extensions) { + private FileChooser getSpdxFileChooser(Iterable extenions) { FileChooser chooser = new FileChooser(); - for (int i = 0; i < extensions.length; ++i) { - FileChooser.ExtensionFilter spdxExtensionFilter = new FileChooser.ExtensionFilter(extensions[i], "*." + extensions[i]); + boolean first = true; + for (String extension : extenions) { + FileChooser.ExtensionFilter spdxExtensionFilter = new FileChooser.ExtensionFilter(extension, "*." + extension); chooser.getExtensionFilters().add(spdxExtensionFilter); - if (i == 0) { + if (first){ chooser.setSelectedExtensionFilter(spdxExtensionFilter); + first = false; } + } + return chooser; } @@ -106,7 +108,6 @@ void initialize() { assert btnAddPackage != null : "fx:id=\"btnAddPackage\" was not injected: check your FXML file 'MainScene.fxml'."; assert btnNewDocument != null : "fx:id=\"btnNewDocument\" was not injected: check your FXML file 'MainScene.fxml'."; assert saveSpdx != null : "fx:id=\"saveSpdx\" was not injected: check your FXML file 'MainScene.fxml'."; - assert saveSpdxTag != null : "fx:id=\"saveSpdxTag\" was not injected: check your FXML file 'MainScene.fxml'."; assert validateSpdx != null : "fx:id=\"validateSpdx\" was not injected: check your FXML file 'MainScene.fxml'."; assert txtDocumentName != null : "fx:id=\"txtDocumentName\" was not injected: check your FXML file 'MainScene.fxml'."; assert addedPackagesUiList != null : "fx:id=\"addedPackagesUiList\" was not injected: check your FXML file 'MainScene.fxml'."; @@ -122,7 +123,6 @@ private void enableAllButtons(){ this.chooseDir.setDisable(false); this.txtDocumentName.setDisable(false); this.saveSpdx.setDisable(false); - this.saveSpdxTag.setDisable(false); } private static Optional selectDirectory(Window parentWindow) { @@ -183,33 +183,21 @@ public void handleChooseDirectoryClicked(MouseEvent event) { } + public void handleSaveSpdxClicked(MouseEvent event) { - File targetFile = getSpdxFileChooser("rdf", "spdx").showSaveDialog(saveSpdx.getScene().getWindow()); + Optional outputType = IoFileTypeSelectionDialog.getDataType("Save SPDX"); + if (!outputType.isPresent()) return; + + File targetFile = getSpdxFileChooser( outputType.get().getExtensions()).showSaveDialog(saveSpdx.getScene().getWindow()); if (targetFile == null) //Dialog cancelled return; - try (FileWriter writer = new FileWriter(targetFile)) { - this.documentToEdit.getDocumentContainer().getModel().write(writer); + try { + outputType.get().writeToFile(targetFile, documentToEdit); } catch (IOException e) { logger.error("Unable to write SPDX file", e); } } - public void handleSaveTagClicked(MouseEvent event) { - File targetFile = getSpdxFileChooser("spdx", "tag").showSaveDialog(saveSpdx.getScene().getWindow()); - if (targetFile == null) //Dialog cancelled - return; - try (PrintWriter out = new PrintWriter(targetFile)) { -// read the tag-value constants from a file - Properties constants = CommonCode - .getTextFromProperties("org/spdx/tag/SpdxTagValueConstants.properties"); - // print document to a file using tag-value format - CommonCode.printDoc(documentToEdit, out, constants); - } catch (IOException | InvalidSPDXAnalysisException e) { - new Alert(Alert.AlertType.ERROR, "Unable to write tag file: " + e.getMessage(), ButtonType.OK); - } - - } - public void handleAddPackageClicked(MouseEvent event) { List> selectedNodes = dirTree.getSelectionModel().getSelectedItems(); assert (selectedNodes.size() <= 1); @@ -231,10 +219,13 @@ private void loadSpdxDocument(SpdxDocument loadedDocument) { } public void handleLoadSpdxClicked(MouseEvent event) { - File targetFile = getSpdxFileChooser("rdf", "spdx").showOpenDialog(saveSpdx.getScene().getWindow()); + Optional inputType = IoFileTypeSelectionDialog.getDataType("Load SPDX"); + if (!inputType.isPresent())return; + + File targetFile = getSpdxFileChooser(inputType.get().getExtensions()).showOpenDialog(saveSpdx.getScene().getWindow()); if (targetFile == null) return; //Cancelled try { - SpdxDocument loadedDocument = SPDXDocumentFactory.createSpdxDocument(targetFile.getPath()); + SpdxDocument loadedDocument = inputType.get().readFromFile(targetFile); loadSpdxDocument(loadedDocument); } catch (InvalidSPDXAnalysisException isae) { logger.error("Invalid SPDX load attempt", isae); @@ -248,24 +239,7 @@ public void handleLoadSpdxClicked(MouseEvent event) { } } - public void handleLoadSpdxTagClicked(MouseEvent event) { - File targetFile = getSpdxFileChooser("spdx", "tag").showOpenDialog(saveSpdx.getScene().getWindow()); - try (FileInputStream in = new FileInputStream(targetFile)) { - List warnings = new LinkedList<>(); - SpdxDocumentContainer container = TagToRDF.convertTagFileToRdf(in, "RDF/XML", warnings); - if (warnings.size() > 0) { - Alert warningsAlert = new Alert(Alert.AlertType.WARNING, "Warnings occured in parsing Tag document", ButtonType.OK); - TextArea warningList = new TextArea(); - warningList.setText(Joiner.on("\n").join(warnings)); - warningsAlert.getDialogPane().setExpandableContent(warningList); - warningsAlert.showAndWait(); - } - loadSpdxDocument(container.getSpdxDocument()); - } catch (Exception e) { - new Alert(Alert.AlertType.ERROR, "Error loading SPDX file " + targetFile.getAbsolutePath()).showAndWait(); - } - } public void handlePackageListClicked(MouseEvent event) { if (event.getClickCount() == 2) { diff --git a/src/main/java/spdxedit/SpdxLogic.java b/src/main/java/spdxedit/SpdxLogic.java index 333f728..576cc16 100644 --- a/src/main/java/spdxedit/SpdxLogic.java +++ b/src/main/java/spdxedit/SpdxLogic.java @@ -3,14 +3,15 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; import com.google.common.net.MediaType; -import com.hp.hpl.jena.rdf.model.Property; -import com.hp.hpl.jena.rdf.model.ResIterator; -import com.hp.hpl.jena.rdf.model.Resource; -import com.hp.hpl.jena.rdf.model.impl.PropertyImpl; + import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.text.WordUtils; +import org.apache.jena.rdf.model.Property; +import org.apache.jena.rdf.model.ResIterator; +import org.apache.jena.rdf.model.Resource; +import org.apache.jena.rdf.model.impl.PropertyImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spdx.rdfparser.InvalidSPDXAnalysisException; diff --git a/src/main/java/spdxedit/io/FileDataType.java b/src/main/java/spdxedit/io/FileDataType.java new file mode 100644 index 0000000..915944a --- /dev/null +++ b/src/main/java/spdxedit/io/FileDataType.java @@ -0,0 +1,53 @@ +package spdxedit.io; + +import com.google.common.collect.ImmutableList; +import org.spdx.rdfparser.InvalidSPDXAnalysisException; +import org.spdx.rdfparser.model.SpdxDocument; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.function.BiConsumer; +import java.util.function.Function; + +/** + * Created by ybronshteyn on 1/29/17. + */ +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"), + JSONLD("JSON-LD", "json"), + RDF_JSON("RDF/JSON", "json")*/; + + private final String displayName; + private final List extensions; + private final FileOutputStrategy fileOutputLogic; + private final FileInputStrategy fileInputLogic; + + FileDataType(String displayName, FileOutputStrategy fileOutputLogic, FileInputStrategy fileInputLogic, String... extensions) { + this.displayName = displayName; + this.extensions = ImmutableList.copyOf(extensions); + this.fileInputLogic = fileInputLogic; + this.fileOutputLogic = fileOutputLogic; + } + + public List getExtensions(){return extensions;} + + public String getDisplayName() { + return displayName; + } + + public void writeToFile(File file, SpdxDocument document) throws IOException{ + fileOutputLogic.write(file, document); + } + + public SpdxDocument readFromFile(File file) throws IOException, InvalidSPDXAnalysisException{ + return fileInputLogic.read(file); + } + + @Override + public String toString() { + return getDisplayName(); + } +} diff --git a/src/main/java/spdxedit/io/FileInputStrategy.java b/src/main/java/spdxedit/io/FileInputStrategy.java new file mode 100644 index 0000000..3c15dfb --- /dev/null +++ b/src/main/java/spdxedit/io/FileInputStrategy.java @@ -0,0 +1,12 @@ +package spdxedit.io; + +import org.spdx.rdfparser.InvalidSPDXAnalysisException; +import org.spdx.rdfparser.model.SpdxDocument; + +import java.io.File; +import java.io.IOException; + +@FunctionalInterface +public interface FileInputStrategy { + SpdxDocument read(File file) throws IOException, InvalidSPDXAnalysisException; +} diff --git a/src/main/java/spdxedit/io/FileIoLogic.java b/src/main/java/spdxedit/io/FileIoLogic.java new file mode 100644 index 0000000..06b3138 --- /dev/null +++ b/src/main/java/spdxedit/io/FileIoLogic.java @@ -0,0 +1,64 @@ +package spdxedit.io; + +import com.google.common.base.Joiner; +import javafx.scene.control.Alert; +import javafx.scene.control.ButtonType; +import javafx.scene.control.TextArea; +import org.spdx.rdfparser.InvalidSPDXAnalysisException; +import org.spdx.rdfparser.SPDXDocumentFactory; +import org.spdx.rdfparser.SpdxDocumentContainer; +import org.spdx.rdfparser.model.SpdxDocument; +import org.spdx.tag.CommonCode; +import org.spdx.tools.TagToRDF; + +import java.io.*; +import java.util.LinkedList; +import java.util.List; +import java.util.Properties; + +/** + * 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); + } + } + + public static SpdxDocument loadRdfXml(File file) throws IOException, InvalidSPDXAnalysisException{ + return SPDXDocumentFactory.createSpdxDocument(file.getAbsolutePath()); + } + + + 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){ + throw new RuntimeException(("Illegal SPDX - unable to convert to tag/value"), e); + } + } + + public static SpdxDocument loadTagValue(File file) throws IOException, InvalidSPDXAnalysisException{ + try (FileInputStream in = new FileInputStream(file)) { + List warnings = new LinkedList<>(); + SpdxDocumentContainer container = TagToRDF.convertTagFileToRdf(in, "RDF/XML", warnings); + if (warnings.size() > 0) { + Alert warningsAlert = new Alert(Alert.AlertType.WARNING, "Warnings occured in parsing Tag document", ButtonType.OK); + TextArea warningList = new TextArea(); + warningList.setText(Joiner.on("\n").join(warnings)); + warningsAlert.getDialogPane().setExpandableContent(warningList); + 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); + } + } + +} diff --git a/src/main/java/spdxedit/io/FileOutputStrategy.java b/src/main/java/spdxedit/io/FileOutputStrategy.java new file mode 100644 index 0000000..a027e40 --- /dev/null +++ b/src/main/java/spdxedit/io/FileOutputStrategy.java @@ -0,0 +1,11 @@ +package spdxedit.io; + +import org.spdx.rdfparser.model.SpdxDocument; + +import java.io.File; +import java.io.IOException; + +@FunctionalInterface +public interface FileOutputStrategy { + void write(File file, SpdxDocument document) throws IOException; +} \ No newline at end of file diff --git a/src/main/resources/MainScene.fxml b/src/main/resources/MainScene.fxml index 93ccb92..20167d4 100644 --- a/src/main/resources/MainScene.fxml +++ b/src/main/resources/MainScene.fxml @@ -21,10 +21,8 @@