4
4
import javafx .scene .control .Alert ;
5
5
import javafx .scene .control .ButtonType ;
6
6
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 ;
7
15
import org .spdx .rdfparser .InvalidSPDXAnalysisException ;
8
16
import org .spdx .rdfparser .SPDXDocumentFactory ;
9
17
import org .spdx .rdfparser .SpdxDocumentContainer ;
12
20
import org .spdx .tools .TagToRDF ;
13
21
14
22
import java .io .*;
23
+ import java .nio .file .Paths ;
15
24
import java .util .LinkedList ;
16
25
import java .util .List ;
17
26
import java .util .Properties ;
20
29
* Created by ybronshteyn on 1/29/17.
21
30
*/
22
31
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
+ }
27
39
}
28
40
29
- public static SpdxDocument loadRdfXml (File file ) throws IOException , InvalidSPDXAnalysisException {
41
+ public static SpdxDocument loadRdfXml (File file ) throws IOException , InvalidSPDXAnalysisException {
30
42
return SPDXDocumentFactory .createSpdxDocument (file .getAbsolutePath ());
31
43
}
32
44
33
45
34
- public static void writeTagValue (File file , SpdxDocument document ) throws IOException {
46
+ public static void writeTagValue (File file , SpdxDocument document ) throws IOException {
35
47
Properties constants = CommonCode
36
48
.getTextFromProperties ("org/spdx/tag/SpdxTagValueConstants.properties" );
37
49
try (FileOutputStream os = new FileOutputStream (file ); PrintWriter out = new PrintWriter (os );) {
38
50
// print document to a file using tag-value format
39
51
CommonCode .printDoc (document , out , constants );
40
- } catch (InvalidSPDXAnalysisException e ){
52
+ } catch (InvalidSPDXAnalysisException e ) {
41
53
throw new RuntimeException (("Illegal SPDX - unable to convert to tag/value" ), e );
42
54
}
43
55
}
44
56
45
- public static SpdxDocument loadTagValue (File file ) throws IOException , InvalidSPDXAnalysisException {
57
+ public static SpdxDocument loadTagValue (File file ) throws IOException , InvalidSPDXAnalysisException {
46
58
try (FileInputStream in = new FileInputStream (file )) {
47
59
List <String > warnings = new LinkedList <>();
48
60
SpdxDocumentContainer container = TagToRDF .convertTagFileToRdf (in , "RDF/XML" , warnings );
@@ -54,11 +66,56 @@ public static SpdxDocument loadTagValue(File file) throws IOException, InvalidSP
54
66
warningsAlert .showAndWait ();
55
67
}
56
68
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 ();
61
93
}
62
94
}
63
95
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
+
64
121
}
0 commit comments