From 22c254d74c149353dd2504ccb647f2f311b58ebb Mon Sep 17 00:00:00 2001 From: Gary O'Neall Date: Mon, 31 Oct 2022 14:14:36 -0700 Subject: [PATCH] Set the XMLInputFactory property for RDF files Fixes #90 See Jena issue 2331 for more information: https://issues.apache.org/jira/browse/JENA-2331 Signed-off-by: Gary O'Neall --- .../java/org/spdx/tools/SpdxConverter.java | 21 ++++++++++ .../java/org/spdx/tools/SpdxToolsHelper.java | 42 +++++++++++++++++-- src/main/java/org/spdx/tools/Verify.java | 6 +-- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/spdx/tools/SpdxConverter.java b/src/main/java/org/spdx/tools/SpdxConverter.java index 3a43c66..25d229a 100644 --- a/src/main/java/org/spdx/tools/SpdxConverter.java +++ b/src/main/java/org/spdx/tools/SpdxConverter.java @@ -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; @@ -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); @@ -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(); diff --git a/src/main/java/org/spdx/tools/SpdxToolsHelper.java b/src/main/java/org/spdx/tools/SpdxToolsHelper.java index 73c0e35..7e43e58 100644 --- a/src/main/java/org/spdx/tools/SpdxToolsHelper.java +++ b/src/main/java/org/spdx/tools/SpdxToolsHelper.java @@ -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; @@ -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 EXT_TO_FILETYPE; static { HashMap temp = new HashMap<>(); @@ -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 @@ -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); + } + } } } } diff --git a/src/main/java/org/spdx/tools/Verify.java b/src/main/java/org/spdx/tools/Verify.java index 812cef9..e41899f 100644 --- a/src/main/java/org/spdx/tools/Verify.java +++ b/src/main/java/org/spdx/tools/Verify.java @@ -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; @@ -124,9 +125,8 @@ public static List 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) {