Skip to content

Commit

Permalink
Merge pull request #77 from spdx/rc1
Browse files Browse the repository at this point in the history
Update to SPDX Spec 3.0.1
  • Loading branch information
goneall authored Dec 15, 2024
2 parents 9b34e44 + b8e7e21 commit 135822c
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 244 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ This store supports serializing and deserializing files in JSON, YAML and XML fo
<dependency>
<groupId>org.spdx</groupId>
<artifactId>java-spdx-library</artifactId>
<version>2.0.0-Alpha</version>
<version>2.0.0-RC1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
199 changes: 81 additions & 118 deletions src/main/java/org/spdx/jacksonstore/JacksonDeSerializer.java

Large diffs are not rendered by default.

112 changes: 51 additions & 61 deletions src/main/java/org/spdx/jacksonstore/JacksonSerializer.java

Large diffs are not rendered by default.

46 changes: 22 additions & 24 deletions src/main/java/org/spdx/jacksonstore/MultiFormatStore.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* Copyright (c) 2020 Source Auditor Inc.
*
* <p>
* SPDX-License-Identifier: Apache-2.0
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -21,6 +21,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -57,8 +58,8 @@

/**
* Model store that supports multiple serialization formats (JSON, XML, YAML)
*
* Note that the serialization/deserlization methods are synchronized to prevent the format or verbose changing while serilizing
* <p>
* Note that the serialization/deserialization methods are synchronized to prevent the format or verbose changing while serializing
*
* @author Gary O'Neall
*
Expand All @@ -71,9 +72,9 @@ public enum Verbose {
COMPACT, // SPDX identifiers are used for any SPDX element references, license expressions as text
STANDARD, // Expand referenced SPDX element, license expressions as text
FULL // Expand all licenses to full objects and expand all SPDX elements
};
public enum Format {
}

public enum Format {
JSON,
JSON_PRETTY, // pretty printed JSON format
XML,
Expand All @@ -92,7 +93,7 @@ public enum Format {
private ObjectMapper inputMapper;

/**
* @param baseStore modelStore to store the results of the desearialization
* @param baseStore modelStore to store the results of the deserialization
* @param format Format - XML, JSON or YAML
* @param verbose How verbose to make the document
*/
Expand Down Expand Up @@ -120,7 +121,7 @@ private void setMapper() {

/**
* Default compact version of MultiFormatStore
* @param baseStore modelStore to store the results of the desearialization
* @param baseStore modelStore to store the results of the deserialization
* @param format Format - XML, JSON or YAML
*/
public MultiFormatStore(IModelStore baseStore, Format format) {
Expand Down Expand Up @@ -198,15 +199,12 @@ public synchronized void serialize(OutputStream stream, @Nullable CoreModelObjec
jgen = yamlFactory.createGenerator(stream);
break;
}
case XML: {
jgen = outputMapper.getFactory().createGenerator(stream).useDefaultPrettyPrinter();
break;
}
case JSON: {
jgen = outputMapper.getFactory().createGenerator(stream);
break;
}
case JSON_PRETTY:
case XML:
default: {
jgen = outputMapper.getFactory().createGenerator(stream).useDefaultPrettyPrinter();
break;
Expand All @@ -221,7 +219,7 @@ public synchronized void serialize(OutputStream stream, @Nullable CoreModelObjec
}

/**
* @param propertyName
* @param propertyName name of the singular property
* @return property name used for an array or collection of these values
*/
public static String propertyNameToCollectionPropertyName(String propertyName) {
Expand Down Expand Up @@ -256,7 +254,7 @@ public synchronized SpdxDocument deSerialize(InputStream stream, boolean overwri
JsonNode root;
if (Format.XML.equals(format)) {
// Jackson XML mapper does not support deserializing collections or arrays. Use Json-In-Java to convert to JSON
JSONObject jo = XML.toJSONObject(new InputStreamReader(stream, "UTF-8"));
JSONObject jo = XML.toJSONObject(new InputStreamReader(stream, StandardCharsets.UTF_8));
root = inputMapper.readTree(jo.toString()).get("Document");
} else {
root = inputMapper.readTree(stream);
Expand All @@ -266,7 +264,7 @@ public synchronized SpdxDocument deSerialize(InputStream stream, boolean overwri
}
List<String> documentNamespaces = new ArrayList<>();
if (root instanceof ArrayNode) {
for (JsonNode docNode:(ArrayNode)root) {
for (JsonNode docNode: root) {
documentNamespaces.add(getNamespaceFromDoc(docNode));
}
} else {
Expand Down Expand Up @@ -299,10 +297,10 @@ public synchronized SpdxDocument deSerialize(InputStream stream, boolean overwri
JacksonDeSerializer deSerializer = new JacksonDeSerializer(this, new ModelCopyManager(), format);
String docNamespace;
if (root instanceof ArrayNode) {
for (JsonNode doc:(ArrayNode)root) {
for (JsonNode doc: root) {
deSerializer.storeDocument(getNamespaceFromDoc(doc), doc);
}
docNamespace = getNamespaceFromDoc((ArrayNode)root.get(0));
docNamespace = getNamespaceFromDoc(root.get(0));
} else {
deSerializer.storeDocument(getNamespaceFromDoc(root), root);
docNamespace = getNamespaceFromDoc(root);
Expand Down Expand Up @@ -330,8 +328,8 @@ private String getNamespaceFromDoc(JsonNode docNode) throws InvalidSPDXAnalysisE
}

/**
* @param documentNamespace
* @throws InvalidSPDXAnalysisException
* @param documentNamespace Document namespace or Uri
* @throws InvalidSPDXAnalysisException on SPDX parsing errors
*/
public void clear(String documentNamespace) throws InvalidSPDXAnalysisException {
List<TypedValue> valuesToDelete = this.getAllItems(documentNamespace, null).collect(Collectors.toList());
Expand All @@ -341,8 +339,8 @@ public void clear(String documentNamespace) throws InvalidSPDXAnalysisException
}

/**
* @return list of SPDX V2 document URI's in this model store
* @throws InvalidSPDXAnalysisException
* @return set of SPDX V2 document URI's in this model store
* @throws InvalidSPDXAnalysisException on SPDX parsing errors
*/
public Set<String> getDocumentUris() throws InvalidSPDXAnalysisException {
Set<String> retval = new HashSet<>();
Expand Down
40 changes: 19 additions & 21 deletions src/main/java/org/spdx/jacksonstore/PropertyComparator.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* Copyright (c) 2020 Source Auditor Inc.
*
* <p>
* SPDX-License-Identifier: Apache-2.0
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -30,7 +30,7 @@

/**
* Comparator for property names for different SPDX class types
*
* <p>
* To change the order of properties, change the order in the arrays for the specific type
* If a property or type isn't in the propertyOrderMap, the alphaNumeric comparison of property names will be used
*
Expand All @@ -39,22 +39,20 @@
*/
public class PropertyComparator implements Comparator<String> {

static final List<String> DOCUMENT_PROPERTY_ORDER = Arrays.asList(new String[] {
SpdxConstantsCompatV2.PROP_DOCUMENT_NAMESPACE.getName(),
SpdxConstantsCompatV2.PROP_SPDX_SPEC_VERSION.getName(),
SpdxConstantsCompatV2.PROP_SPDX_CREATION_INFO.getName(),
SpdxConstantsCompatV2.PROP_NAME.getName(),
SpdxConstantsCompatV2.PROP_SPDX_DATA_LICENSE.getName(),
SpdxConstantsCompatV2.RDFS_PROP_COMMENT.getName(),
SpdxConstantsCompatV2.PROP_SPDX_EXTERNAL_DOC_REF.getName(),
SpdxConstantsCompatV2.PROP_DOCUMENT_DESCRIBES.getName(),
SpdxConstantsCompatV2.PROP_DOCUMENT_PACKAGES.getName(),
SpdxConstantsCompatV2.PROP_DOCUMENT_FILES.getName(),
SpdxConstantsCompatV2.PROP_DOCUMENT_SNIPPETS.getName(),
SpdxConstantsCompatV2.PROP_SPDX_EXTRACTED_LICENSES.getName(),
SpdxConstantsCompatV2.PROP_ANNOTATION.getName(),
SpdxConstantsCompatV2.PROP_DOCUMENT_RELATIONSHIPS.getName()
});
static final List<String> DOCUMENT_PROPERTY_ORDER = Arrays.asList(SpdxConstantsCompatV2.PROP_DOCUMENT_NAMESPACE.getName(),
SpdxConstantsCompatV2.PROP_SPDX_SPEC_VERSION.getName(),
SpdxConstantsCompatV2.PROP_SPDX_CREATION_INFO.getName(),
SpdxConstantsCompatV2.PROP_NAME.getName(),
SpdxConstantsCompatV2.PROP_SPDX_DATA_LICENSE.getName(),
SpdxConstantsCompatV2.RDFS_PROP_COMMENT.getName(),
SpdxConstantsCompatV2.PROP_SPDX_EXTERNAL_DOC_REF.getName(),
SpdxConstantsCompatV2.PROP_DOCUMENT_DESCRIBES.getName(),
SpdxConstantsCompatV2.PROP_DOCUMENT_PACKAGES.getName(),
SpdxConstantsCompatV2.PROP_DOCUMENT_FILES.getName(),
SpdxConstantsCompatV2.PROP_DOCUMENT_SNIPPETS.getName(),
SpdxConstantsCompatV2.PROP_SPDX_EXTRACTED_LICENSES.getName(),
SpdxConstantsCompatV2.PROP_ANNOTATION.getName(),
SpdxConstantsCompatV2.PROP_DOCUMENT_RELATIONSHIPS.getName());
static final Map<String, List<String>> propertyOrderMap;
static {
HashMap<String, List<String>> hm = new HashMap<>();
Expand Down
37 changes: 18 additions & 19 deletions src/main/java/org/spdx/jacksonstore/SpdxJsonLDContext.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* Copyright (c) 2020 Source Auditor Inc.
*
* <p>
* SPDX-License-Identifier: Apache-2.0
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -52,12 +52,12 @@ public class SpdxJsonLDContext {
}

/**
* Maps XML Schema primitive types to Java classes supported by the SPDX stores.
* See https://www.w3.org/TR/xmlschema-2 for a description of XML schema types.
*/
public static Map<String, Class<? extends Object>> XMLSCHEMA_TYPE_TO_JAVA_CLASS;
* Maps XML Schema primitive types to Java classes supported by the SPDX stores.
* See <a href="https://www.w3.org/TR/xmlschema-2">xml-schema</a> for a description of XML schema types.
*/
public static Map<String, Class<?>> XMLSCHEMA_TYPE_TO_JAVA_CLASS;
static {
Map<String, Class<? extends Object>> schemaToClass = new HashMap<>();
Map<String, Class<?>> schemaToClass = new HashMap<>();
schemaToClass.put("string", String.class);
schemaToClass.put("boolean", Boolean.class);
schemaToClass.put("decimal", String.class);
Expand Down Expand Up @@ -110,7 +110,7 @@ public class SpdxJsonLDContext {
static private SpdxJsonLDContext instance;
static final String JSON_LD_PATH = "/resources/spdx-2-3-revision-2-onotology.context.json";
static final ObjectMapper JSON_MAPPER = new ObjectMapper();
private JsonNode contexts;
private final JsonNode contexts;

private SpdxJsonLDContext() throws InvalidSPDXAnalysisException {
try (InputStream is = SpdxJsonLDContext.class.getResourceAsStream(JSON_LD_PATH)) {
Expand Down Expand Up @@ -144,8 +144,8 @@ public static synchronized SpdxJsonLDContext getInstance() throws InvalidSPDXAna
}

/**
* @param propertyName
* @return type type for the property name if the type is specified in the JSON-LD context
* @param propertyName Name of the property
* @return type for the property name if the type is specified in the JSON-LD context
*/
public Optional<String> getType(String propertyName) {
JsonNode propContext = this.contexts.get(propertyName);
Expand Down Expand Up @@ -182,7 +182,7 @@ public Optional<String> getType(String propertyName) {
}

/**
* @param property
* @param property JSON name of the property
* @return true if the property is a list
*/
public boolean isList(String property) {
Expand All @@ -195,12 +195,11 @@ public boolean isList(String property) {
return false;
}
if (jnContainer.isArray()) {
Iterator<JsonNode> iter = jnContainer.iterator();
while (iter.hasNext()) {
if (LIST_CONTAINER_TYPES.contains(iter.next().asText())) {
return true;
}
}
for (JsonNode jsonNode : jnContainer) {
if (LIST_CONTAINER_TYPES.contains(jsonNode.asText())) {
return true;
}
}
return false;
} else {
return LIST_CONTAINER_TYPES.contains(jnContainer.asText());
Expand Down

0 comments on commit 135822c

Please sign in to comment.