Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the XMLInputFactory property for RDF files #91

Merged
merged 1 commit into from
Nov 1, 2022
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
21 changes: 21 additions & 0 deletions src/main/java/org/spdx/tools/SpdxConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.ModelCopyManager;
import org.spdx.library.SpdxConstants;
import org.spdx.spdxRdfStore.RdfStore;
import org.spdx.storage.ISerializableModelStore;
import org.spdx.tools.SpdxToolsHelper.SerFileType;

Expand Down Expand Up @@ -143,9 +144,22 @@ public static void convert(String fromFilePath, String toFilePath, SerFileType f
}
FileInputStream input = null;
FileOutputStream output = null;
String oldXmlInputFactory = null;
boolean propertySet = false;
try {
ISerializableModelStore fromStore = SpdxToolsHelper.fileTypeToStore(fromFileType);
ISerializableModelStore toStore = SpdxToolsHelper.fileTypeToStore(toFileType);
if (fromStore instanceof RdfStore || toStore instanceof RdfStore) {
// Setting the property value will avoid the error message
// See issue #90 for more information
try {
oldXmlInputFactory = System.setProperty(SpdxToolsHelper.XML_INPUT_FACTORY_PROPERTY_KEY,
"com.sun.xml.internal.stream.XMLInputFactoryImpl");
propertySet = true;
} catch (SecurityException e) {
propertySet = false; // we'll just deal with the extra error message
}
}
input = new FileInputStream(fromFile);
output = new FileOutputStream(toFile);
String documentUri = fromStore.deSerialize(input, false);
Expand Down Expand Up @@ -177,6 +191,13 @@ public static void convert(String fromFilePath, String toFilePath, SerFileType f
}
throw new SpdxConverterException(msg, ex);
} finally {
if (propertySet) {
if (Objects.isNull(oldXmlInputFactory)) {
System.clearProperty(SpdxToolsHelper.XML_INPUT_FACTORY_PROPERTY_KEY);
} else {
System.setProperty(SpdxToolsHelper.XML_INPUT_FACTORY_PROPERTY_KEY, oldXmlInputFactory);
}
}
if (Objects.nonNull(input)) {
try {
input.close();
Expand Down
42 changes: 38 additions & 4 deletions src/main/java/org/spdx/tools/SpdxToolsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
Expand Down Expand Up @@ -53,6 +54,8 @@ public enum SerFileType {
JSON, RDFXML, XML, XLS, XLSX, YAML, TAG, RDFTTL
}

static final String XML_INPUT_FACTORY_PROPERTY_KEY = "javax.xml.stream.XMLInputFactory";

static Map<String, SerFileType> EXT_TO_FILETYPE;
static {
HashMap<String, SerFileType> temp = new HashMap<>();
Expand Down Expand Up @@ -167,10 +170,7 @@ 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);
}
return readDocumentFromFile(store, file);
}
/**
* @param file
Expand All @@ -186,9 +186,43 @@ public static SpdxDocument deserializeDocument(File file,
SerFileType fileType)
throws InvalidSPDXAnalysisException, IOException {
ISerializableModelStore store = fileTypeToStore(fileType);
return readDocumentFromFile(store, file);
}

/**
* Reads an SPDX Document from a file
* @param store Store where the document is to be stored
* @param file File to read the store from
* @return SPDX Document from the store
* @throws FileNotFoundException If the file is not found
* @throws IOException If there is an error reading the file
* @throws InvalidSPDXAnalysisException If there is a problem in the SPDX document structure
*/
public static SpdxDocument readDocumentFromFile(ISerializableModelStore store, File file) throws FileNotFoundException, IOException, InvalidSPDXAnalysisException {
String oldXmlInputFactory = null;
boolean propertySet = false;
try (InputStream is = new FileInputStream(file)) {
if (store instanceof RdfStore) {
// Setting the property value will avoid the error message
// See issue #90 for more information
try {
oldXmlInputFactory = System.setProperty(XML_INPUT_FACTORY_PROPERTY_KEY,
"com.sun.xml.internal.stream.XMLInputFactoryImpl");
propertySet = true;
} catch (SecurityException e) {
propertySet = false; // we'll just deal with the extra error message
}
}
String documentUri = store.deSerialize(is, false);
return new SpdxDocument(store, documentUri, null, false);
} finally {
if (propertySet) {
if (Objects.isNull(oldXmlInputFactory)) {
System.clearProperty(XML_INPUT_FACTORY_PROPERTY_KEY);
} else {
System.setProperty(XML_INPUT_FACTORY_PROPERTY_KEY, oldXmlInputFactory);
}
}
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/spdx/tools/Verify.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.Version;
import org.spdx.library.model.SpdxDocument;
import org.spdx.spdxRdfStore.RdfStore;
import org.spdx.storage.ISerializableModelStore;
import org.spdx.tagvaluestore.TagValueStore;
import org.spdx.tools.SpdxToolsHelper.SerFileType;
Expand Down Expand Up @@ -124,9 +125,8 @@ public static List<String> verify(String filePath, SerFileType fileType) throws
throw new SpdxVerificationException("Error converting fileType to store",e);
}
SpdxDocument doc = null;
try (InputStream is = new FileInputStream(file)) {
String documentUri = store.deSerialize(is, false);
doc = new SpdxDocument(store, documentUri, null, false);
try {
doc = SpdxToolsHelper.readDocumentFromFile(store, file);
} catch (FileNotFoundException e) {
throw new SpdxVerificationException("File "+filePath+ " not found.",e);
} catch (IOException e) {
Expand Down