diff --git a/pom.xml b/pom.xml index ce6a06a..c827e00 100644 --- a/pom.xml +++ b/pom.xml @@ -141,7 +141,7 @@ This store supports serializing and deserializing files in JSON, YAML and XML fo org.spdx java-spdx-library - 2.0.0-Alpha + 2.0.0-RC1 com.google.code.gson diff --git a/src/main/java/org/spdx/jacksonstore/JacksonDeSerializer.java b/src/main/java/org/spdx/jacksonstore/JacksonDeSerializer.java index b407c77..95adf8b 100644 --- a/src/main/java/org/spdx/jacksonstore/JacksonDeSerializer.java +++ b/src/main/java/org/spdx/jacksonstore/JacksonDeSerializer.java @@ -1,14 +1,15 @@ /** * Copyright (c) 2020 Source Auditor Inc. * + *

* SPDX-License-Identifier: Apache-2.0 - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. @@ -68,21 +69,19 @@ public class JacksonDeSerializer { /** * Property names that should not be restored as part of the deserialization */ - static final Set SKIPPED_PROPERTIES = Collections.unmodifiableSet(new HashSet(Arrays.asList(new String[] { - SpdxConstantsCompatV2.PROP_DOCUMENT_PACKAGES.getName(), - SpdxConstantsCompatV2.PROP_DOCUMENT_FILES.getName(), - SpdxConstantsCompatV2.PROP_DOCUMENT_SNIPPETS.getName(), - SpdxConstantsCompatV2.SPDX_IDENTIFIER, - SpdxConstantsCompatV2.PROP_DOCUMENT_RELATIONSHIPS.getName(), - SpdxConstantsCompatV2.PROP_DOCUMENT_NAMESPACE.getName() - }))); + static final Set SKIPPED_PROPERTIES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(SpdxConstantsCompatV2.PROP_DOCUMENT_PACKAGES.getName(), + SpdxConstantsCompatV2.PROP_DOCUMENT_FILES.getName(), + SpdxConstantsCompatV2.PROP_DOCUMENT_SNIPPETS.getName(), + SpdxConstantsCompatV2.SPDX_IDENTIFIER, + SpdxConstantsCompatV2.PROP_DOCUMENT_RELATIONSHIPS.getName(), + SpdxConstantsCompatV2.PROP_DOCUMENT_NAMESPACE.getName()))); - private CompatibleModelStoreWrapper store; + private final CompatibleModelStoreWrapper store; @SuppressWarnings("unused") - private Format format; + private final Format format; private IModelCopyManager modelCopyManager; - private Map>> addedRelationships = new HashMap<>(); + private final Map>> addedRelationships = new HashMap<>(); /** * @param store store to store any documents in @@ -97,7 +96,7 @@ public JacksonDeSerializer(IModelStore store, Format format) { /** * @param store store to store any documents in * @param modelCopyManager copy manager to use when copying from the SPDX listed license model store - * @parm format Format expected for the serialized data + * @param format Format expected for the serialized data */ public JacksonDeSerializer(IModelStore store, IModelCopyManager modelCopyManager, Format format) { Objects.requireNonNull(store, "Model store can not be null"); @@ -112,7 +111,7 @@ public JacksonDeSerializer(IModelStore store, IModelCopyManager modelCopyManager * Stores an SPDX document converted from the JsonNode doc * @param documentNamespace namespace for the document * @param doc JsonNode containing the SPDX document - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ @SuppressWarnings("unchecked") public void storeDocument(String documentNamespace, JsonNode doc) throws InvalidSPDXAnalysisException { @@ -170,12 +169,12 @@ public void storeDocument(String documentNamespace, JsonNode doc) throws Invalid /** * Restores a single SPDX element of a specific type - * @param documentUri - * @param type - * @param jsonNode - * @param addedElements + * @param documentUri namespace for the document + * @param type SPDX Model object type + * @param jsonNode node containing an element + * @param addedElements map of any elements added to the mode * @param spdxIdProperties Properties which contain an SPDX ID which needs to be replaced - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private void restoreElement(String documentUri, String type, @Nullable JsonNode jsonNode, Map addedElements, Map spdxIdProperties) throws InvalidSPDXAnalysisException { @@ -208,12 +207,12 @@ private void restoreElement(String documentUri, String type, @Nullable JsonNode /** * Restores SPDX elements of a specific type - * @param documentUri - * @param type - * @param jsonNode - * @param addedElements + * @param documentUri namespace for the document + * @param type SPDX Model object type + * @param jsonNode node containing an element + * @param addedElements map of any elements added to the mode * @param spdxIdProperties Properties which contain an SPDX ID which needs to be replaced - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private void restoreElements(String documentUri, String type, @Nullable JsonNode jsonNode, Map addedElements, Map spdxIdProperties) throws InvalidSPDXAnalysisException { @@ -234,10 +233,10 @@ private void restoreElements(String documentUri, String type, @Nullable JsonNode /** * Restore the relationships adding them as properties to the correct elements - * @param documentNamespace - * @param jsonNode - * @param addedElements - * @throws InvalidSPDXAnalysisException + * @param documentNamespace namespace for the document + * @param jsonNode node containing an element + * @param addedElements map of any elements added to the mode + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private void restoreRelationships(String documentNamespace, JsonNode jsonNode, Map addedElements) throws InvalidSPDXAnalysisException { @@ -257,10 +256,10 @@ private void restoreRelationships(String documentNamespace, JsonNode jsonNode, /** * Restore a relationship adding it as properties to the correct elements - * @param documentNamespace - * @param relationship - * @param addedElements - * @throws InvalidSPDXAnalysisException + * @param documentNamespace namespace for the document + * @param relationship JSON Node containing the relationship + * @param addedElements map of any elements added to the mode + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private void restoreRelationship(String documentNamespace, JsonNode relationship, Map addedElements) throws InvalidSPDXAnalysisException { @@ -276,7 +275,7 @@ private void restoreRelationship(String documentNamespace, JsonNode relationship if (Objects.isNull(relationshipTypeNode) || !relationshipTypeNode.isTextual()) { throw new InvalidSPDXAnalysisException("Missing required relationship type"); } - String relationshipTypeUri = null; + String relationshipTypeUri; try { relationshipTypeUri = RelationshipType.valueOf(relationshipTypeNode.asText()).getIndividualURI(); } catch(Exception ex) { @@ -307,9 +306,9 @@ private void restoreRelationship(String documentNamespace, JsonNode relationship * @param relatedElement related element * @param relationshipComment optional comment for the relationship * @return the ID of the relationship - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ - private String addRelationship(String documentNamespace, String elementId, SimpleUriValue relationshipType, Object relatedElement, Optional relationshipComment) throws InvalidSPDXAnalysisException { + private String addRelationship(String documentNamespace, String elementId, SimpleUriValue relationshipType, Object relatedElement, @SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional relationshipComment) throws InvalidSPDXAnalysisException { String relatedElementId; if (relatedElement instanceof TypedValue) { relatedElementId = CompatibleModelStoreWrapper.objectUriToId(store, ((TypedValue)relatedElement).getObjectUri(), documentNamespace); @@ -321,7 +320,7 @@ private String addRelationship(String documentNamespace, String elementId, Simpl throw new InvalidSPDXAnalysisException("Related element is not of an Element type for relationship to element "+elementId); } // check for duplicates - Map relatedElementRelationships = null; + Map relatedElementRelationships; Map> elementRelationships = addedRelationships.get(elementId); if (Objects.nonNull(elementRelationships)) { relatedElementRelationships = elementRelationships.get(relatedElementId); @@ -359,7 +358,7 @@ private String addRelationship(String documentNamespace, String elementId, Simpl * @param id ID for the element containing the properties * @param node Node containing the property values * @param spdxIdProperties Properties which contain an SPDX ID which needs to be replaced - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private void restoreObjectPropertyValues(String documentUri, String id, JsonNode node, Map spdxIdProperties) throws InvalidSPDXAnalysisException { @@ -367,14 +366,15 @@ private void restoreObjectPropertyValues(String documentUri, String id, JsonNode while (fieldIterator.hasNext()) { Entry field = fieldIterator.next(); if (SKIPPED_PROPERTIES.contains(field.getKey())) { - continue; + //noinspection UnnecessaryContinue + continue; } else if (SpdxConstantsCompatV2.PROP_DOCUMENT_DESCRIBES.getName().equals(field.getKey())) { // These needs to be converted to a DocumentDescribes relationship convertFieldToRelationship(documentUri, id, field.getValue(), RelationshipType.DESCRIBES, spdxIdProperties); } else if (SpdxConstantsCompatV2.PROP_PACKAGE_FILE.getName().equals( MultiFormatStore.collectionPropertyNameToPropertyName(field.getKey()))) { - // These needs to be converted to a CONTAINSS relationship + // These needs to be converted to a CONTAINS relationship convertFieldToRelationship(documentUri, id, field.getValue(), RelationshipType.CONTAINS, spdxIdProperties); } else { @@ -392,15 +392,15 @@ private void restoreObjectPropertyValues(String documentUri, String id, JsonNode * @param spdxIdField Either a single SPDX ID or an array of SPDX IDs * @param relationshipType Relationship type to use * @param spdxIdProperties properties to add the relationship to - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private void convertFieldToRelationship(String documentUri, String id, JsonNode spdxIdField, RelationshipType relationshipType, Map spdxIdProperties) throws InvalidSPDXAnalysisException { if ((spdxIdField instanceof ArrayNode)) { - for (JsonNode spdxidNode:((ArrayNode)spdxIdField)) { + for (JsonNode spdxIdNode: spdxIdField) { String relationshipId = addRelationship(documentUri, id, new SimpleUriValue(relationshipType.getIndividualURI()), - spdxidNode.asText(), Optional.empty()); + spdxIdNode.asText(), Optional.empty()); spdxIdProperties.put(relationshipId, SpdxConstantsCompatV2.PROP_RELATED_SPDX_ELEMENT.getName()); // Add the SPDX ID to the list to be translated back to elements later } } else { @@ -419,7 +419,7 @@ private void convertFieldToRelationship(String documentUri, String id, JsonNode * @param value JSON node containing the value * @param spdxIdProperties Properties which contain an SPDX ID which needs to be replaced * @param list true if this property is a list type - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private void setPropertyValueForJsonNode(String documentUri, String id, String property, JsonNode value, Map spdxIdProperties, boolean list) throws InvalidSPDXAnalysisException { @@ -435,13 +435,13 @@ private void setPropertyValueForJsonNode(String documentUri, String id, String p // ignore te null; Optional propertyType = SpdxJsonLDContext.getInstance().getType(property); if (list) { - store.addValueToCollection(documentUri, id, + store.addValueToCollection(documentUri, id, MultiFormatStore.collectionPropertyNameToPropertyName(property), toStoredObject(documentUri, id, - property, value, propertyType, spdxIdProperties, list)); + property, value, propertyType, spdxIdProperties, true)); } else { store.setValue(documentUri, id, property, toStoredObject(documentUri, id, - property, value, propertyType, spdxIdProperties, list)); + property, value, propertyType, spdxIdProperties, false)); } } } @@ -455,16 +455,17 @@ private void setPropertyValueForJsonNode(String documentUri, String id, String p * @param spdxIdProperties Properties which contain an SPDX ID which needs to be replaced * @param list true if this property is a list type * @return the object to be stored - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ - private Object toStoredObject(String documentUri, String id, String property, JsonNode value, - Optional propertyType, Map spdxIdProperties, boolean list) throws InvalidSPDXAnalysisException { + @SuppressWarnings("OptionalUsedAsFieldOrParameterType") + private Object toStoredObject(String documentUri, String id, String property, JsonNode value, + Optional propertyType, Map spdxIdProperties, boolean list) throws InvalidSPDXAnalysisException { switch (value.getNodeType()) { case ARRAY: throw new InvalidSPDXAnalysisException("Can not convert a JSON array to a stored object"); case BOOLEAN: if (propertyType.isPresent()) { - Class toStoreClass = SpdxJsonLDContext.XMLSCHEMA_TYPE_TO_JAVA_CLASS.get(propertyType.get()); + Class toStoreClass = SpdxJsonLDContext.XMLSCHEMA_TYPE_TO_JAVA_CLASS.get(propertyType.get()); if (Objects.isNull(toStoreClass)) { // assume it is a boolean type return value.asBoolean(); @@ -473,7 +474,7 @@ private Object toStoredObject(String documentUri, String id, String property, Js } else if (Boolean.class.equals(toStoreClass)) { return value.asBoolean(); } else { - throw new InvalidSPDXAnalysisException("Can not convert a JSON BOOLEAN to a "+toStoreClass.toString()); + throw new InvalidSPDXAnalysisException("Can not convert a JSON BOOLEAN to a "+ toStoreClass); } } else { return value.asBoolean(); @@ -481,16 +482,16 @@ private Object toStoredObject(String documentUri, String id, String property, Js case NULL: throw new InvalidSPDXAnalysisException("Can not convert a JSON NULL to a stored object"); case NUMBER: { if (propertyType.isPresent()) { - Class toStoreClass = SpdxJsonLDContext.XMLSCHEMA_TYPE_TO_JAVA_CLASS.get(propertyType.get()); + Class toStoreClass = SpdxJsonLDContext.XMLSCHEMA_TYPE_TO_JAVA_CLASS.get(propertyType.get()); if (Objects.isNull(toStoreClass)) { - // assume it is a integer type + // assume it is an integer type return value.asInt(); } else if (String.class.equals(toStoreClass)) { return Double.toString(value.asDouble()); } else if (Integer.class.equals(toStoreClass)) { return value.asInt(); } else { - throw new InvalidSPDXAnalysisException("Can not convert a JSON NUMBER to a "+toStoreClass.toString()); + throw new InvalidSPDXAnalysisException("Can not convert a JSON NUMBER to a "+ toStoreClass); } } else { return value.asInt(); @@ -522,28 +523,24 @@ private Object toStoredObject(String documentUri, String id, String property, Js case BINARY: case MISSING: case POJO: - default: throw new InvalidSPDXAnalysisException("Unsupported JSON node type: "+value.toString()); + default: throw new InvalidSPDXAnalysisException("Unsupported JSON node type: "+ value); } } - - /** - * @param spdxIdProperties Properties which contain an SPDX ID which needs to be replaced - * - * @throws InvalidSPDXAnalysisException - */ + /** * Gets the property value for a string JsonNode * @param documentUri document URI * @param id ID of the object to store the value * @param property property name * @param value JSON node containing the value - * @param propertyType + * @param propertyType SPDX model type for the property * @param list true if this property is a list type * @return the appropriate object to store - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ - private Object getStringPropertyValueForJsonNode(String documentUri, String id, String property, JsonNode value, - Optional propertyType, Map spdxIdProperties, boolean list) throws InvalidSPDXAnalysisException { + @SuppressWarnings("OptionalUsedAsFieldOrParameterType") + private Object getStringPropertyValueForJsonNode(String documentUri, String id, String property, JsonNode value, + Optional propertyType, Map spdxIdProperties, boolean list) throws InvalidSPDXAnalysisException { Class clazz = null; if (propertyType.isPresent()) { // check for SPDX model types @@ -565,14 +562,7 @@ private Object getStringPropertyValueForJsonNode(String documentUri, String id, } else if (SpdxDocument.class.isAssignableFrom(clazz)) { // Convert any IndividualUriValue values final String uriValue = value.asText(); - return new IndividualUriValue() { - - @Override - public String getIndividualURI() { - return uriValue; - } - - }; + return (IndividualUriValue) () -> uriValue; } else if (ReferenceType.class.isAssignableFrom(clazz)) { String referenceTypeValue = value.asText(); try { @@ -587,14 +577,7 @@ public String getIndividualURI() { } final String uriValue = referenceTypeValue; // Check for listed reference types - return new IndividualUriValue() { - - @Override - public String getIndividualURI() { - return uriValue; - } - - }; + return (IndividualUriValue) () -> uriValue; } else if (SpdxElement.class.isAssignableFrom(clazz)) { // store the ID and save it in the spdxIdProperties to replace with the actual class later // once everything is restored @@ -633,10 +616,10 @@ public String getIndividualURI() { /** - * @param documentUri - * @param jsonObject + * @param documentUri namespace for the document + * @param jsonObject JSON Object to search for an ID * @return the ID for the JSON object based on what property values are available - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private String findObjectIdInJsonObject(String documentUri, JsonNode jsonObject) throws InvalidSPDXAnalysisException { JsonNode retval = jsonObject.get(SpdxConstantsCompatV2.SPDX_IDENTIFIER); @@ -659,48 +642,28 @@ private String findObjectIdInJsonObject(String documentUri, JsonNode jsonObject) /** * Convert an ID into the value object to be stored - * @param documentNamespace + * @param documentNamespace namespace for the document * @param spdxId ID to be replaced by the actual object * @param addedElements SPDX elements added - * @return - * @throws InvalidSPDXAnalysisException + * @return value object to be stored + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private Object idToObjectValue(String documentNamespace, String spdxId, Map addedElements) throws InvalidSPDXAnalysisException { TypedValue fixedValue = addedElements.get(spdxId); if (Objects.isNull(fixedValue)) { if (spdxId.equals(SpdxConstantsCompatV2.NONE_VALUE)) { - return new IndividualUriValue() { - - @Override - public String getIndividualURI() { - return SpdxConstantsCompatV2.URI_VALUE_NONE; - } - - }; + return (IndividualUriValue) () -> SpdxConstantsCompatV2.URI_VALUE_NONE; } else if (spdxId.equals(SpdxConstantsCompatV2.NOASSERTION_VALUE)) { - return new IndividualUriValue() { - - @Override - public String getIndividualURI() { - return SpdxConstantsCompatV2.URI_VALUE_NOASSERTION; - } - - }; + return (IndividualUriValue) () -> SpdxConstantsCompatV2.URI_VALUE_NOASSERTION; } else if (spdxId.startsWith("DocumentRef-")) { final IModelStore modelStore = store; - IndividualUriValue spdxExternalElementRef = new IndividualUriValue() { - - @Override - public String getIndividualURI() { - try { - return ExternalSpdxElement.externalSpdxElementIdToURI(spdxId, modelStore, documentNamespace, modelCopyManager); - } catch (InvalidSPDXAnalysisException e) { - throw new RuntimeException(e); - } - } - - }; - return spdxExternalElementRef; + return (IndividualUriValue) () -> { + try { + return ExternalSpdxElement.externalSpdxElementIdToURI(spdxId, modelStore, documentNamespace, modelCopyManager); + } catch (InvalidSPDXAnalysisException e) { + throw new RuntimeException(e); + } + }; } else { throw new InvalidSPDXAnalysisException("No SPDX element found for SPDX ID "+spdxId); } diff --git a/src/main/java/org/spdx/jacksonstore/JacksonSerializer.java b/src/main/java/org/spdx/jacksonstore/JacksonSerializer.java index 832dadf..6938e37 100644 --- a/src/main/java/org/spdx/jacksonstore/JacksonSerializer.java +++ b/src/main/java/org/spdx/jacksonstore/JacksonSerializer.java @@ -1,14 +1,14 @@ /** * Copyright (c) 2020 Source Auditor Inc. - * + *

* SPDX-License-Identifier: Apache-2.0 - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. @@ -62,7 +62,7 @@ /** * Serializer for a model store to convert the document model object into a JsonNode - * + *

* the method docToJsonNode(String documentUri) will generate the JSON node for a document * @author Gary O'Neall * @@ -75,7 +75,7 @@ public class JacksonSerializer { * Class to for the name of the XML element to Document * */ - class Document extends ObjectNode { + static class Document extends ObjectNode { /** * @@ -112,10 +112,10 @@ public int compare(JsonNode arg0, JsonNode arg1) { return -1; } else { List list0 = new ArrayList<>(); - arg0.spliterator().forEachRemaining((node) -> list0.add(node)); + arg0.spliterator().forEachRemaining(list0::add); list0.sort(NODE_COMPARATOR); List list1 = new ArrayList<>(); - arg1.spliterator().forEachRemaining((node) -> list1.add(node)); + arg1.spliterator().forEachRemaining(list1::add); list1.sort(NODE_COMPARATOR); for (int i = 0; i < list0.size(); i++) { int retval = compare(list0.get(i), list1.get(i)); @@ -158,7 +158,7 @@ private int compareObject(JsonNode arg0, JsonNode arg1) { return compareExternalRef(refCategory0.asText(), refType0.asText(), refLocator0.asText(), arg1); } List fieldNames = new ArrayList<>(); - arg0.fieldNames().forEachRemaining((String field) -> fieldNames.add(field)); + arg0.fieldNames().forEachRemaining(fieldNames::add); Collections.sort(fieldNames); int retval = 0; for (String fieldName:fieldNames) { @@ -218,10 +218,10 @@ private int compareExtractedLicense(String licenseId, JsonNode compare) { }; - private ObjectMapper mapper; - private CompatibleModelStoreWrapper store; - private Format format; - private Verbose verbose; + private final ObjectMapper mapper; + private final CompatibleModelStoreWrapper store; + private final Format format; + private final Verbose verbose; /** * @param mapper Jackson Object Mapper to use for creating JSON objects @@ -242,7 +242,7 @@ public JacksonSerializer(ObjectMapper mapper, Format format, Verbose verbose, IM /** * @param documentUris list of document uris * @return JSON array of all documents which have the document Uris - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ public ArrayNode docsToJsonNode(List documentUris) throws InvalidSPDXAnalysisException { ArrayNode retval = mapper.createArrayNode(); @@ -255,7 +255,7 @@ public ArrayNode docsToJsonNode(List documentUris) throws InvalidSPDXAna /** * @param documentUri URI for the document to be converted * @return ObjectNode for an SPDX document in Jackson JSON tree format - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ public ObjectNode docToJsonNode(String documentUri) throws InvalidSPDXAnalysisException { Objects.requireNonNull(documentUri,"Null Document URI"); @@ -267,15 +267,15 @@ public ObjectNode docToJsonNode(String documentUri) throws InvalidSPDXAnalysisEx ObjectNode doc = typedValueToObjectNode(documentUri, document, relationships); doc.put(SpdxConstantsCompatV2.PROP_DOCUMENT_NAMESPACE.getName(), documentUri); ArrayNode packages = getDocElements(documentUri, SpdxConstantsCompatV2.CLASS_SPDX_PACKAGE, relationships); - if (packages.size() > 0) { + if (!packages.isEmpty()) { doc.set(SpdxConstantsCompatV2.PROP_DOCUMENT_PACKAGES.getName(), packages); } ArrayNode files = getDocElements(documentUri, SpdxConstantsCompatV2.CLASS_SPDX_FILE, relationships); - if (files.size() > 0) { + if (!files.isEmpty()) { doc.set(SpdxConstantsCompatV2.PROP_DOCUMENT_FILES.getName(), files); } ArrayNode snippets = getDocElements(documentUri, SpdxConstantsCompatV2.CLASS_SPDX_SNIPPET, relationships); - if (snippets.size() > 0) { + if (!snippets.isEmpty()) { doc.set(SpdxConstantsCompatV2.PROP_DOCUMENT_SNIPPETS.getName(), snippets); } //Remove duplicate relationships @@ -307,19 +307,13 @@ public ObjectNode docToJsonNode(String documentUri) throws InvalidSPDXAnalysisEx doc.set(SpdxConstantsCompatV2.PROP_DOCUMENT_RELATIONSHIPS.getName(), deDupedRelationships); ObjectNode output; switch (format) { - case YAML: { - output = doc; - break; - } case XML: { output = new Document(JsonNodeFactory.instance); output.setAll(doc); break; } - case JSON: { - output = doc; - break; - } + case JSON: + case YAML: case JSON_PRETTY: default: { output = doc; @@ -338,14 +332,14 @@ public ObjectNode docToJsonNode(String documentUri) throws InvalidSPDXAnalysisEx * @param storedItem stored value to convert to a JSON serializable form * @param relationships ArrayNode of relationships to add any found relationships * @return ObjectNode with all fields added from the stored typedValue - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private ObjectNode typedValueToObjectNode(String documentUri, TypedValue storedItem, ArrayNode relationships) throws InvalidSPDXAnalysisException { ObjectNode retval = mapper.createObjectNode(); Set hasFileIds = new HashSet<>(); // keep track of any hasFile properties added to avoid duplicates Set documentDescribesIds = new HashSet<>(); // keep track of any documentDescribes properties added to avoid duplicates - List docPropNames = new ArrayList(store.getPropertyValueNames(storedItem.getObjectUri())); + List docPropNames = new ArrayList<>(store.getPropertyValueNames(storedItem.getObjectUri())); docPropNames.sort(new PropertyComparator(storedItem.getType())); Class clazz = SpdxModelFactoryCompatV2.SPDX_TYPE_TO_CLASS_V2.get(storedItem.getType()); IdType idType = store.getIdType(storedItem.getObjectUri()); @@ -354,7 +348,7 @@ private ObjectNode typedValueToObjectNode(String documentUri, TypedValue storedI if (IdType.SpdxId.equals(idType)) { retval.put(SpdxConstantsCompatV2.SPDX_IDENTIFIER, CompatibleModelStoreWrapper.objectUriToId(store, storedItem.getObjectUri(), documentUri)); } else if (!IdType.Anonymous.equals(idType)) { - logger.error("Invalid ID "+storedItem.getObjectUri()+". Must be an SPDX Identifier or Anonymous"); + logger.error("Invalid ID {}. Must be an SPDX Identifier or Anonymous", storedItem.getObjectUri()); throw new InvalidSPDXAnalysisException("Invalid ID "+storedItem.getObjectUri()+". Must be an SPDX Identifier or Anonymous"); } } else if (ExternalDocumentRef.class.isAssignableFrom(clazz)) { @@ -371,7 +365,7 @@ private ObjectNode typedValueToObjectNode(String documentUri, TypedValue storedI } else if (store.isCollectionProperty(documentUri, id, propertyName)) { Iterator propertyValues = store.listValues(documentUri, id, propertyName); ArrayNode valuesArray; - if (SpdxConstantsCompatV2.PROP_PACKAGE_FILE.getName().equals(propertyName) && hasFileIds.size() > 0) { + if (SpdxConstantsCompatV2.PROP_PACKAGE_FILE.getName().equals(propertyName) && !hasFileIds.isEmpty()) { // Need to filter out existing hasFileIds List hasFilesToAdd = new ArrayList<>(); while (propertyValues.hasNext()) { @@ -390,7 +384,7 @@ private ObjectNode typedValueToObjectNode(String documentUri, TypedValue storedI valuesArray = toArrayNode(documentUri, hasFilesToAdd.iterator(), relationships); - } else if (SpdxConstantsCompatV2.PROP_DOCUMENT_DESCRIBES.getName().equals(propertyName) && documentDescribesIds.size() > 0) { + } else if (SpdxConstantsCompatV2.PROP_DOCUMENT_DESCRIBES.getName().equals(propertyName) && !documentDescribesIds.isEmpty()) { // Need to filter out existing documentDescribes List describesToAdd = new ArrayList<>(); while (propertyValues.hasNext()) { @@ -428,19 +422,17 @@ private ObjectNode typedValueToObjectNode(String documentUri, TypedValue storedI * @param documentUri Document namespace or Uri * @param type type of document element to get (Package, File, or Snippet) * @return JsonArray of document elements matching the type - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private ArrayNode getDocElements(String documentUri, String type, ArrayNode relationships) throws InvalidSPDXAnalysisException { - return store.getAllItems(documentUri, type).collect(() -> mapper.createArrayNode(), (an, item) -> + return store.getAllItems(documentUri, type).collect(mapper::createArrayNode, (an, item) -> { try { an.add(typedValueToObjectNode(documentUri, item, relationships)); } catch (InvalidSPDXAnalysisException e) { throw new RuntimeException(e); } - }, (an1, an2) -> { - an1.addAll(an2); - }); + }, ArrayNode::addAll); } /** @@ -450,7 +442,7 @@ private ArrayNode getDocElements(String documentUri, String type, ArrayNode rela * @param propertyName property name for the extracted licenses * @param relationships list of relationships - just so we can pass it to toJsonObject * @return an ArrayNode of extracted license elements - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private ArrayNode toExtractedLicensesArrayNode(String documentUri, String id, String propertyName, ArrayNode relationships) throws InvalidSPDXAnalysisException { @@ -473,21 +465,21 @@ private ArrayNode toExtractedLicensesArrayNode(String documentUri, String id, St * @param documentUri Document namespace or Uri * @param element element containing the relationship * @param elementNode the JSON node for the serialized element - * @param valueList List of all relationships values * @param relationships ArrayNode of relationships to add any found relationships * @param hasFileIds set of hasFile property file Ids added to the element - used to avoid duplicates - * @param documentDescribesids set of documentDescribes element IDs added to the element - used to avoid duplicates - * @throws InvalidSPDXAnalysisException + * @param documentDescribesIds set of documentDescribes element IDs added to the element - used to avoid duplicates + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ - private void addJsonRelationships(String documentUri, TypedValue element, ObjectNode elementNode, - ArrayNode relationships, Set hasFileIds, Set documentDescribesIds) throws InvalidSPDXAnalysisException { + @SuppressWarnings("unused") + private void addJsonRelationships(String documentUri, TypedValue element, ObjectNode elementNode, + ArrayNode relationships, Set hasFileIds, Set documentDescribesIds) throws InvalidSPDXAnalysisException { Iterator valueList = store.listValues(documentUri, CompatibleModelStoreWrapper.objectUriToId(store, element.getObjectUri(), documentUri), SpdxConstantsCompatV2.PROP_RELATIONSHIP); while (valueList.hasNext()) { Object value = valueList.next(); if (!(value instanceof TypedValue)) { - throw new SpdxInvalidTypeException("Expected relationship type, value list element was of type "+value.getClass().toString()); + throw new SpdxInvalidTypeException("Expected relationship type, value list element was of type "+ value.getClass()); } TypedValue tvValue = (TypedValue)value; String tvId = CompatibleModelStoreWrapper.objectUriToId(store, tvValue.getObjectUri(), documentUri); @@ -496,7 +488,7 @@ private void addJsonRelationships(String documentUri, TypedValue element, Object } Optional relatedSpdxElement = store.getValue(documentUri, tvId, SpdxConstantsCompatV2.PROP_RELATED_SPDX_ELEMENT); if (!relatedSpdxElement.isPresent()) { - logger.warn("Missing related SPDX element for a relationship for "+element.getObjectUri()+". Skipping the serialization of this relationship."); + logger.warn("Missing related SPDX element for a relationship for {}. Skipping the serialization of this relationship.", element.getObjectUri()); continue; } Optional relationshipComment = store.getValue(documentUri, tvId, SpdxConstantsCompatV2.RDFS_PROP_COMMENT); @@ -516,16 +508,16 @@ private void addJsonRelationships(String documentUri, TypedValue element, Object throw new SpdxInvalidTypeException("SPDX element must be of SpdxElement, SpdxNoneElement, SpdxNoAssertionElement or external SPDX element type. URI does not match pattern for external element: "+externalUri); } } else { - throw new SpdxInvalidTypeException("SPDX element must be of SpdxElement or external SPDX element type. Found type "+relatedSpdxElement.get().getClass().toString()); + throw new SpdxInvalidTypeException("SPDX element must be of SpdxElement or external SPDX element type. Found type "+ relatedSpdxElement.get().getClass()); } Optional relationshipType = store.getValue(documentUri, tvId, SpdxConstantsCompatV2.PROP_RELATIONSHIP_TYPE); if (!relationshipType.isPresent()) { - logger.warn("Missing type for a relationship for "+element.getObjectUri()+". Skipping the serialization of this relationship."); + logger.warn("Missing type for a relationship for {}. Skipping the serialization of this relationship.", element.getObjectUri()); continue; } if (!(relationshipType.get() instanceof IndividualUriValue)) { - throw new SpdxInvalidTypeException("Expected RelationshipType type for relationshipType property. Unexpected type "+relatedSpdxElement.get().getClass().toString()); + throw new SpdxInvalidTypeException("Expected RelationshipType type for relationshipType property. Unexpected type "+ relatedSpdxElement.get().getClass()); } String relationshipTypeStr = individualUriToString(documentUri, ((IndividualUriValue)relationshipType.get()).getIndividualURI()); ObjectNode relationship = mapper.createObjectNode(); @@ -533,9 +525,7 @@ private void addJsonRelationships(String documentUri, TypedValue element, Object element.getObjectUri(), documentUri)); relationship.put(SpdxConstantsCompatV2.PROP_RELATIONSHIP_TYPE.getName(), relationshipTypeStr); relationship.put(SpdxConstantsCompatV2.PROP_RELATED_SPDX_ELEMENT.getName(), relatedElementId); - if (relationshipComment.isPresent()) { - relationship.put(SpdxConstantsCompatV2.RDFS_PROP_COMMENT.getName(), (String)relationshipComment.get()); - } + relationshipComment.ifPresent(o -> relationship.put(SpdxConstantsCompatV2.RDFS_PROP_COMMENT.getName(), (String) o)); relationships.add(relationship); } } @@ -545,8 +535,8 @@ private void addJsonRelationships(String documentUri, TypedValue element, Object * @param documentUri Document namespace or Uri * @param valueList list of values to convert * @param relationships running total of any relationships found - any relationships are added - * @return - * @throws InvalidSPDXAnalysisException + * @return JSON ArrayNode representing the list of values + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private ArrayNode toArrayNode(String documentUri, Iterator valueList, ArrayNode relationships) throws InvalidSPDXAnalysisException { ArrayNode unsorted = mapper.createArrayNode(); @@ -554,9 +544,9 @@ private ArrayNode toArrayNode(String documentUri, Iterator valueList, Ar Object value = valueList.next(); addValueToArrayNode(unsorted, documentUri, value, relationships); } - List nodeList = new ArrayList<>(); - unsorted.spliterator().forEachRemaining((node) -> nodeList.add(node)); - Collections.sort(nodeList, NODE_COMPARATOR); + @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") List nodeList = new ArrayList<>(); + unsorted.spliterator().forEachRemaining(nodeList::add); + nodeList.sort(NODE_COMPARATOR); return mapper.createArrayNode().addAll(unsorted); } @@ -567,7 +557,7 @@ private ArrayNode toArrayNode(String documentUri, Iterator valueList, Ar * @param documentUri Document namespace or Uri * @param value Value to convert to a JSON serializable form * @param relationships ArrayNode of relationships to add any found relationships - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private void setValueField(ObjectNode node, String field, String documentUri, Object value, ArrayNode relationships) throws InvalidSPDXAnalysisException { Object nodeValue = toSerializable(documentUri, value, relationships); @@ -580,7 +570,7 @@ private void setValueField(ObjectNode node, String field, String documentUri, Ob } else if (nodeValue instanceof Boolean) { node.put(field, (Boolean)nodeValue); }else { - throw new SpdxInvalidTypeException("Can not serialize the JSON type for "+nodeValue.getClass().toString()); + throw new SpdxInvalidTypeException("Can not serialize the JSON type for "+ nodeValue.getClass()); } } @@ -590,7 +580,7 @@ private void setValueField(ObjectNode node, String field, String documentUri, Ob * @param documentUri Document namespace or Uri * @param value Value to convert to a JSON serializable form * @param relationships ArrayNode of relationships to add any found relationships - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private Object toSerializable(String documentUri, Object value, ArrayNode relationships) throws InvalidSPDXAnalysisException { if (value instanceof IndividualUriValue) { @@ -621,7 +611,7 @@ private Object toSerializable(String documentUri, Object value, ArrayNode relati * @param documentUri Document namespace or Uri * @param uri URI value * @return JSON form of the Enum or literal value represented by the URI - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private String individualUriToString(String documentUri, String uri) throws InvalidSPDXAnalysisException { Object enumval = SpdxEnumFactoryCompatV2.uriToEnum.get(uri); @@ -651,7 +641,7 @@ private String individualUriToString(String documentUri, String uri) throws Inva * @param documentUri Document namespace or Uri * @param value Value to convert to a JSON serializable form * @param relationships ArrayNode of relationships to add any found relationships - * @throws InvalidSPDXAnalysisException + * @throws InvalidSPDXAnalysisException on SPDX parsing errors */ private void addValueToArrayNode(ArrayNode node, String documentUri, Object value, ArrayNode relationships) throws InvalidSPDXAnalysisException { Object nodeValue = toSerializable(documentUri, value, relationships); @@ -664,7 +654,7 @@ private void addValueToArrayNode(ArrayNode node, String documentUri, Object valu } else if (nodeValue instanceof Boolean) { node.add((Boolean)nodeValue); }else { - throw new SpdxInvalidTypeException("Can not serialize the JSON type for "+nodeValue.getClass().toString()); + throw new SpdxInvalidTypeException("Can not serialize the JSON type for "+ nodeValue.getClass()); } } } diff --git a/src/main/java/org/spdx/jacksonstore/MultiFormatStore.java b/src/main/java/org/spdx/jacksonstore/MultiFormatStore.java index 8db73bb..0d8bce6 100644 --- a/src/main/java/org/spdx/jacksonstore/MultiFormatStore.java +++ b/src/main/java/org/spdx/jacksonstore/MultiFormatStore.java @@ -1,14 +1,14 @@ /** * Copyright (c) 2020 Source Auditor Inc. - * + *

* SPDX-License-Identifier: Apache-2.0 - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. @@ -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; @@ -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 + *

+ * Note that the serialization/deserialization methods are synchronized to prevent the format or verbose changing while serializing * * @author Gary O'Neall * @@ -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, @@ -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 */ @@ -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) { @@ -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; @@ -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) { @@ -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); @@ -266,7 +264,7 @@ public synchronized SpdxDocument deSerialize(InputStream stream, boolean overwri } List documentNamespaces = new ArrayList<>(); if (root instanceof ArrayNode) { - for (JsonNode docNode:(ArrayNode)root) { + for (JsonNode docNode: root) { documentNamespaces.add(getNamespaceFromDoc(docNode)); } } else { @@ -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); @@ -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 valuesToDelete = this.getAllItems(documentNamespace, null).collect(Collectors.toList()); @@ -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 getDocumentUris() throws InvalidSPDXAnalysisException { Set retval = new HashSet<>(); diff --git a/src/main/java/org/spdx/jacksonstore/PropertyComparator.java b/src/main/java/org/spdx/jacksonstore/PropertyComparator.java index 6e3ab1c..fdd2465 100644 --- a/src/main/java/org/spdx/jacksonstore/PropertyComparator.java +++ b/src/main/java/org/spdx/jacksonstore/PropertyComparator.java @@ -1,14 +1,14 @@ /** * Copyright (c) 2020 Source Auditor Inc. - * + *

* SPDX-License-Identifier: Apache-2.0 - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. @@ -30,7 +30,7 @@ /** * Comparator for property names for different SPDX class types - * + *

* 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 * @@ -39,22 +39,20 @@ */ public class PropertyComparator implements Comparator { - static final List 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 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> propertyOrderMap; static { HashMap> hm = new HashMap<>(); diff --git a/src/main/java/org/spdx/jacksonstore/SpdxJsonLDContext.java b/src/main/java/org/spdx/jacksonstore/SpdxJsonLDContext.java index 8f6096d..4cd1fd2 100644 --- a/src/main/java/org/spdx/jacksonstore/SpdxJsonLDContext.java +++ b/src/main/java/org/spdx/jacksonstore/SpdxJsonLDContext.java @@ -1,14 +1,14 @@ /** * Copyright (c) 2020 Source Auditor Inc. - * + *

* SPDX-License-Identifier: Apache-2.0 - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. @@ -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> XMLSCHEMA_TYPE_TO_JAVA_CLASS; + * Maps XML Schema primitive types to Java classes supported by the SPDX stores. + * See xml-schema for a description of XML schema types. + */ + public static Map> XMLSCHEMA_TYPE_TO_JAVA_CLASS; static { - Map> schemaToClass = new HashMap<>(); + Map> schemaToClass = new HashMap<>(); schemaToClass.put("string", String.class); schemaToClass.put("boolean", Boolean.class); schemaToClass.put("decimal", String.class); @@ -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)) { @@ -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 getType(String propertyName) { JsonNode propContext = this.contexts.get(propertyName); @@ -182,7 +182,7 @@ public Optional 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) { @@ -195,12 +195,11 @@ public boolean isList(String property) { return false; } if (jnContainer.isArray()) { - Iterator 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());