Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main/java/org/spdx/tools/SpdxConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
* Converts between various SPDX file types
* arg[0] from file path
* arg[1] to file path
* arg[2] from file type [RDFXML|JSON|XLS|XLSX|YAML|TAG] - if not present, file type of the from file will be used
* arg[3] to file type [RDFXML|JSON|XLS|XLSX|YAML|TAG] - if not present, file type of the to file will be used
* arg[2] from file type [RDFXML|RDFTTL|JSON|XLS|XLSX|YAML|TAG] - if not present, file type of the from file will be used
* arg[3] to file type [RDFXML|RDFTTL|JSON|XLS|XLSX|YAML|TAG] - if not present, file type of the to file will be used
*
* the <code>covert(...)</code> methods can be called programmatically to convert files
* @author Gary O'Neall
Expand Down Expand Up @@ -200,8 +200,8 @@ private static void usage() {
System.out.println("SpdxConverter fromFilePath toFilePath [fromFileType] [toFileType]");
System.out.println("\tfromFilePath - File path of the file to convert from");
System.out.println("\ttoFilePath - output file");
System.out.println("\t[fromFileType] - optional file type of the input file. One of JSON, XLS, XLSX, TAG, RDFXML, YAML or XML. If not provided the file type will be determined by the file extension");
System.out.println("\t[toFileType] - optional file type of the output file. One of JSON, XLS, XLSX, TAG, RDFXML, YAML or XML. If not provided the file type will be determined by the file extension");
System.out.println("\t[fromFileType] - optional file type of the input file. One of JSON, XLS, XLSX, TAG, RDFXML, RDFTTL, YAML or XML. If not provided the file type will be determined by the file extension");
System.out.println("\t[toFileType] - optional file type of the output file. One of JSON, XLS, XLSX, TAG, RDFXML, RDFTTL, YAML or XML. If not provided the file type will be determined by the file extension");
}

}
91 changes: 67 additions & 24 deletions src/main/java/org/spdx/tools/SpdxToolsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.spdx.jacksonstore.MultiFormatStore.Verbose;
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.model.SpdxDocument;
import org.spdx.spdxRdfStore.OutputFormat;
import org.spdx.spdxRdfStore.RdfStore;
import org.spdx.spreadsheetstore.SpreadsheetStore;
import org.spdx.spreadsheetstore.SpreadsheetStore.SpreadsheetFormatType;
Expand All @@ -49,7 +50,7 @@
public class SpdxToolsHelper {

public enum SerFileType {
JSON, RDFXML, XML, XLS, XLSX, YAML, TAG
JSON, RDFXML, XML, XLS, XLSX, YAML, TAG, RDFTTL
}

static Map<String, SerFileType> EXT_TO_FILETYPE;
Expand All @@ -65,52 +66,85 @@ public enum SerFileType {
temp.put("tag", SerFileType.TAG);
temp.put("spdx", SerFileType.TAG);
temp.put("yml", SerFileType.YAML);
temp.put("rdf.ttl", SerFileType.RDFTTL);
EXT_TO_FILETYPE = Collections.unmodifiableMap(temp);
}

/**
* @param fileType file type for the store
* @return the appropriate in memory based model store which supports serialization for the fileType
* @param fileType
* file type for the store
* @return the appropriate in memory based model store which supports
* serialization for the fileType
* @throws InvalidSPDXAnalysisException
*/
public static ISerializableModelStore fileTypeToStore(SerFileType fileType) throws InvalidSPDXAnalysisException {
switch(fileType) {
case JSON: return new MultiFormatStore(new InMemSpdxStore(), Format.JSON_PRETTY, Verbose.COMPACT);
case RDFXML: return new RdfStore();
case TAG: return new TagValueStore(new InMemSpdxStore());
case XLS: return new SpreadsheetStore(new InMemSpdxStore(), SpreadsheetFormatType.XLS);
case XLSX: return new SpreadsheetStore(new InMemSpdxStore(), SpreadsheetFormatType.XLSX);
case XML: return new MultiFormatStore(new InMemSpdxStore(), Format.XML, Verbose.COMPACT);
case YAML: return new MultiFormatStore(new InMemSpdxStore(), Format.YAML, Verbose.COMPACT);
default: throw new InvalidSPDXAnalysisException("Unsupported file type: "+fileType+". Check back later.");
public static ISerializableModelStore fileTypeToStore(SerFileType fileType)
throws InvalidSPDXAnalysisException {
switch (fileType) {
case JSON :
return new MultiFormatStore(new InMemSpdxStore(),
Format.JSON_PRETTY, Verbose.COMPACT);
case RDFXML : {
RdfStore rdfStore = new RdfStore();
rdfStore.setOutputFormat(OutputFormat.XML);
return rdfStore;
}
case RDFTTL : {
RdfStore rdfStore = new RdfStore();
rdfStore.setOutputFormat(OutputFormat.TURTLE);
return rdfStore;
}
case TAG :
return new TagValueStore(new InMemSpdxStore());
case XLS :
return new SpreadsheetStore(new InMemSpdxStore(),
SpreadsheetFormatType.XLS);
case XLSX :
return new SpreadsheetStore(new InMemSpdxStore(),
SpreadsheetFormatType.XLSX);
case XML :
return new MultiFormatStore(new InMemSpdxStore(), Format.XML,
Verbose.COMPACT);
case YAML :
return new MultiFormatStore(new InMemSpdxStore(), Format.YAML,
Verbose.COMPACT);
default :
throw new InvalidSPDXAnalysisException("Unsupported file type: "
+ fileType + ". Check back later.");
}
}


/**
* @param file
* @return the file type based on the file name and file extension
* @throws InvalidFileNameException
*/
public static SerFileType fileToFileType(File file) throws InvalidFileNameException {
public static SerFileType fileToFileType(File file)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that these and subsequent changes are just reformatting

throws InvalidFileNameException {
String fileName = file.getName();
if (!fileName.contains(".")) {
throw new InvalidFileNameException("Can not convert file to file type - no file extension");
throw new InvalidFileNameException(
"Can not convert file to file type - no file extension");
}
String ext = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
String ext = fileName.substring(fileName.lastIndexOf(".") + 1)
.toLowerCase();
if ("xml".equals(ext)) {
if (fileName.endsWith("rdf.xml")) {
ext = "rdf.xml";
}
}
if ("ttl".equals(ext)) {
if (fileName.endsWith("rdf.ttl")) {
ext = "rdf.ttl";
}
}
SerFileType retval = EXT_TO_FILETYPE.get(ext);
if (Objects.isNull(retval)) {
throw new InvalidFileNameException("Unrecognized file extension: "+ext);
throw new InvalidFileNameException(
"Unrecognized file extension: " + ext);
}
return retval;
}


/**
* @param str
* @return the file type based on the file extension or string
Expand All @@ -121,27 +155,36 @@ public static SerFileType strToFileType(String str) {
}

/**
* @param file file containing an SPDX document with the standard file extension for the serialization formats
* @param file
* file containing an SPDX document with the standard file
* extension for the serialization formats
* @return the SPDX document stored in the file
* @throws InvalidSPDXAnalysisException
* @throws IOException
* @throws InvalidFileNameException
*/
public static SpdxDocument deserializeDocument(File file) throws InvalidSPDXAnalysisException, IOException, InvalidFileNameException {
public static SpdxDocument deserializeDocument(File file)
throws InvalidSPDXAnalysisException, IOException,
InvalidFileNameException {
ISerializableModelStore store = fileTypeToStore(fileToFileType(file));
try (InputStream is = new FileInputStream(file)) {
String documentUri = store.deSerialize(is, false);
return new SpdxDocument(store, documentUri, null, false);
}
}
/**
* @param file file containing an SPDX document in one of the supported SerFileTypes
* @param fileType serialization file type
* @param file
* file containing an SPDX document in one of the supported
* SerFileTypes
* @param fileType
* serialization file type
* @return the SPDX document stored in the file
* @throws InvalidSPDXAnalysisException
* @throws IOException
*/
public static SpdxDocument deserializeDocument(File file, SerFileType fileType) throws InvalidSPDXAnalysisException, IOException {
public static SpdxDocument deserializeDocument(File file,
SerFileType fileType)
throws InvalidSPDXAnalysisException, IOException {
ISerializableModelStore store = fileTypeToStore(fileType);
try (InputStream is = new FileInputStream(file)) {
String documentUri = store.deSerialize(is, false);
Expand Down