Skip to content

Commit

Permalink
Fix issues identified by SonarCloud
Browse files Browse the repository at this point in the history
Signed-off-by: Gary O'Neall <[email protected]>
  • Loading branch information
goneall committed Sep 2, 2024
1 parent 775eb3f commit f158cdf
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 105 deletions.
12 changes: 7 additions & 5 deletions src/main/java/org/spdx/core/CoreModelObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public abstract class CoreModelObject {
static final Logger logger = LoggerFactory.getLogger(CoreModelObject.class);

static final String PROPERTY_MSG = "Property ";

private static final String ATTEMPTING_EXTERNAL_MSG = "Attempting to set {0} for an external model object";
protected IModelStore modelStore;
protected String objectUri;
protected String specVersion;
Expand Down Expand Up @@ -341,7 +343,7 @@ public void setPropertyValue(PropertyDescriptor propertyDescriptor, @Nullable Ob
throw new InvalidSPDXAnalysisException("Can not set a property for the literal value "+((IndividualUriValue)this).getIndividualURI());
}
if (isExternal()) {
logger.warn("Attempting to set "+propertyDescriptor+" for an external model object");
logger.warn(ATTEMPTING_EXTERNAL_MSG, propertyDescriptor);
return;
}
ModelObjectHelper.setPropertyValue(this.modelStore, objectUri, propertyDescriptor, value,
Expand Down Expand Up @@ -462,7 +464,7 @@ public Optional<Boolean> getBooleanPropertyValue(PropertyDescriptor propertyDesc
*/
public void removeProperty(PropertyDescriptor propertyDescriptor) throws InvalidSPDXAnalysisException {
if (isExternal()) {
logger.warn("Attempting to set "+propertyDescriptor+" for an external model object");
logger.warn(ATTEMPTING_EXTERNAL_MSG, propertyDescriptor);
return;
}
ModelObjectHelper.removeProperty(modelStore, objectUri, propertyDescriptor);
Expand All @@ -485,7 +487,7 @@ public ModelUpdate updateRemoveProperty(PropertyDescriptor propertyDescriptor) {
*/
public void clearValueCollection(PropertyDescriptor propertyDescriptor) throws InvalidSPDXAnalysisException {
if (isExternal()) {
logger.warn("Attempting to set "+propertyDescriptor+" for an external model object");
logger.warn(ATTEMPTING_EXTERNAL_MSG, propertyDescriptor);
return;
}
ModelObjectHelper.clearValueCollection(modelStore, objectUri, propertyDescriptor);
Expand All @@ -511,7 +513,7 @@ public ModelUpdate updateClearValueCollection(PropertyDescriptor propertyDescrip
*/
public void addPropertyValueToCollection(PropertyDescriptor propertyDescriptor, Object value) throws InvalidSPDXAnalysisException {
if (isExternal()) {
logger.warn("Attempting to set "+propertyDescriptor+" for an external model object");
logger.warn(ATTEMPTING_EXTERNAL_MSG, propertyDescriptor);
return;
}
ModelObjectHelper.addValueToCollection(modelStore, objectUri, propertyDescriptor, value,
Expand Down Expand Up @@ -540,7 +542,7 @@ public ModelUpdate updateAddPropertyValueToCollection(PropertyDescriptor propert
*/
public void removePropertyValueFromCollection(PropertyDescriptor propertyDescriptor, Object value) throws InvalidSPDXAnalysisException {
if (isExternal()) {
logger.warn("Attempting to set "+propertyDescriptor+" for an external model object");
logger.warn(ATTEMPTING_EXTERNAL_MSG, propertyDescriptor);
return;
}
ModelObjectHelper.removePropertyValueFromCollection(modelStore, objectUri, propertyDescriptor, value);
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/org/spdx/core/ModelCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public class ModelCollection<T extends Object> implements Collection<Object> {
*/
protected Map<String, IExternalElementInfo> externalMap;
private Class<?> type;
//TODO: See if this boolean is needed before deleting the comments
// private boolean licensePrimitiveAssignable; // If true, NONE and NOASSERTION should be converted to NoneLicense and NoAssertionLicense


/**
* @author Gary O'Neall
*
Expand Down Expand Up @@ -111,13 +109,9 @@ public ModelCollection(IModelStore modelStore, String objectUri, PropertyDescrip
}
if (Objects.nonNull(type)) {
this.type = type;
// TODO: make sure the following commented logic is not needed
// licensePrimitiveAssignable = type.isAssignableFrom(SpdxNoneLicense.class) || type.isAssignableFrom(SpdxNoAssertionLicense.class);
if (!modelStore.isCollectionMembersAssignableTo(objectUri, propertyDescriptor, type)) {
throw new SpdxInvalidTypeException("Incompatible type for property "+propertyDescriptor+": "+type.toString());
}
// } else {
// licensePrimitiveAssignable = false;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/spdx/core/SimpleUriValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public Object toModelObject(IModelStore store, IModelCopyManager copyManager,
} else {
retval = ModelRegistry.getModelRegistry().getExternalElement(store, uri, copyManager, type, specVersion);
if (Objects.isNull(retval)) {
logger.warn(this.getIndividualURI() + " does not match an enum, individual, or external pattern");
logger.warn("{0} does not match an enum, individual, or external pattern", this.getIndividualURI());
retval = this;
}
}
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/org/spdx/licenseTemplate/LicenseTextHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public class LicenseTextHelper {

private LicenseTextHelper() {
// static class
};
}

/**
* Returns true if two sets of license text is considered a match per
Expand Down Expand Up @@ -271,11 +271,7 @@ public static boolean canSkip(String token) {
*/
public static boolean tokensEquivalent(String tokenA, String tokenB) {
if (tokenA == null) {
if (tokenB == null) {
return true;
} else {
return false;
}
return tokenB == null;
} else if (tokenB == null) {
return false;
} else {
Expand Down Expand Up @@ -349,12 +345,12 @@ public static String replaceMultWord(String s) {
public static String normalizeText(String s) {
// First normalize single quotes, then normalize two single quotes to a double quote, normalize double quotes
// then normalize non-breaking spaces to spaces
return s.replaceAll("‘|’|‛|‚|`", "'") // Take care of single quotes first
.replaceAll("http://", "https://") // Normalize the http protocol scheme
.replaceAll("''","\"") // This way, we can change doulbe single quotes to a single double cquote
.replaceAll("“|”|‟|„", "\"") // Now we can normalize the double quotes
return s.replaceAll("[‘’‛‚`]", "'") // Take care of single quotes first
.replace("http://", "https://") // Normalize the http protocol scheme
.replace("''","\"") // This way, we can change doulbe single quotes to a single double cquote
.replaceAll("[“”‟„]", "\"") // Now we can normalize the double quotes
.replaceAll("\\u00A0", " ") // replace non-breaking spaces with spaces since Java does not handle the former well
.replaceAll("—|–","-") // replace em dash, en dash with simple dash
.replaceAll("[—–]","-") // replace em dash, en dash with simple dash
.replaceAll("\\u2028", "\n"); // replace line separator with newline since Java does not handle the former well
}

Expand All @@ -363,6 +359,6 @@ public static String normalizeText(String s) {
* @return s without any line separators (---, ***, ===)
*/
public static String removeLineSeparators(String s) {
return s.replaceAll("(-|=|\\*){3,}\\s*$", ""); // Remove ----, ***, and ====
return s.replaceAll("[-=*]{3,}\\s*$", ""); // Remove ----, ***, and ====
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public List<PropertyDescriptor> getPropertyValueDescriptors(
* @return all property names stored for the Object URI
* @throws InvalidSPDXAnalysisException on any SPDX exception
*/
public Collection<? extends String> getPropertyValueNames(
public Collection<String> getPropertyValueNames(
String objectUri) throws InvalidSPDXAnalysisException {
return StreamSupport.stream(getPropertyValueDescriptors(objectUri).spliterator(), false)
.map(descriptor -> descriptor.getName())
Expand All @@ -266,7 +266,7 @@ public Collection<? extends String> getPropertyValueNames(
* @return all property names stored for the documentUri#id
* @throws InvalidSPDXAnalysisException on any SPDX exception
*/
public Collection<? extends String> getPropertyValueNames(
public Collection<String> getPropertyValueNames(
String documentUri, String id) throws InvalidSPDXAnalysisException {
return getPropertyValueNames(documentUriIdToUri(documentUri, id, baseStore));
}
Expand Down Expand Up @@ -575,7 +575,7 @@ public IModelStore getBaseModelStore() {
@Override
public boolean equals(Object comp) {
return comp instanceof CompatibleModelStoreWrapper && getBaseModelStore().equals(((CompatibleModelStoreWrapper)comp).getBaseModelStore());
// TODO: Return true if the base is equal since this contains no properties
// Return true if the base is equal since this contains no properties
}

@Override
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/org/spdx/storage/NullModelStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
public class NullModelStore implements IModelStore {

private static final String NULL_MODEL_MSG = "Null model store - can only be used with constants and individuals";

@Override
public void close() throws Exception {
// Nothing to close
Expand All @@ -34,7 +36,7 @@ public boolean exists(String objectUri) {
@Override
public void create(TypedValue typedValue)
throws InvalidSPDXAnalysisException {
throw new InvalidSPDXAnalysisException("Null model store - can only be used with constants and individuals");
throw new InvalidSPDXAnalysisException(NULL_MODEL_MSG);
}

@Override
Expand All @@ -47,7 +49,7 @@ public List<PropertyDescriptor> getPropertyValueDescriptors(
public void setValue(String objectUri,
PropertyDescriptor propertyDescriptor, Object value)
throws InvalidSPDXAnalysisException {
throw new InvalidSPDXAnalysisException("Null model store - can only be used with constants and individuals");
throw new InvalidSPDXAnalysisException(NULL_MODEL_MSG);
}

@Override
Expand All @@ -59,14 +61,14 @@ public Optional<Object> getValue(String objectUri,

@Override
public String getNextId(IdType idType) throws InvalidSPDXAnalysisException {
throw new InvalidSPDXAnalysisException("Null model store - can only be used with constants and individuals");
throw new InvalidSPDXAnalysisException(NULL_MODEL_MSG);
}

@Override
public void removeProperty(String objectUri,
PropertyDescriptor propertyDescriptor)
throws InvalidSPDXAnalysisException {
throw new InvalidSPDXAnalysisException("Null model store - can only be used with constants and individuals");
throw new InvalidSPDXAnalysisException(NULL_MODEL_MSG);
}

@Override
Expand Down Expand Up @@ -97,35 +99,35 @@ public void leaveCriticalSection(IModelStoreLock lock) {
public boolean removeValueFromCollection(String objectUri,
PropertyDescriptor propertyDescriptor, Object value)
throws InvalidSPDXAnalysisException {
throw new InvalidSPDXAnalysisException("Null model store - can only be used with constants and individuals");
throw new InvalidSPDXAnalysisException(NULL_MODEL_MSG);
}

@Override
public int collectionSize(String objectUri,
PropertyDescriptor propertyDescriptor)
throws InvalidSPDXAnalysisException {
throw new InvalidSPDXAnalysisException("Null model store - can only be used with constants and individuals");
throw new InvalidSPDXAnalysisException(NULL_MODEL_MSG);
}

@Override
public boolean collectionContains(String objectUri,
PropertyDescriptor propertyDescriptor, Object value)
throws InvalidSPDXAnalysisException {
throw new InvalidSPDXAnalysisException("Null model store - can only be used with constants and individuals");
throw new InvalidSPDXAnalysisException(NULL_MODEL_MSG);
}

@Override
public void clearValueCollection(String objectUri,
PropertyDescriptor propertyDescriptor)
throws InvalidSPDXAnalysisException {
throw new InvalidSPDXAnalysisException("Null model store - can only be used with constants and individuals");
throw new InvalidSPDXAnalysisException(NULL_MODEL_MSG);
}

@Override
public boolean addValueToCollection(String objectUri,
PropertyDescriptor propertyDescriptor, Object value)
throws InvalidSPDXAnalysisException {
throw new InvalidSPDXAnalysisException("Null model store - can only be used with constants and individuals");
throw new InvalidSPDXAnalysisException(NULL_MODEL_MSG);
}

@Override
Expand All @@ -139,21 +141,21 @@ public Iterator<Object> listValues(String objectUri,
public boolean isCollectionMembersAssignableTo(String objectUri,
PropertyDescriptor propertyDescriptor, Class<?> clazz)
throws InvalidSPDXAnalysisException {
throw new InvalidSPDXAnalysisException("Null model store - can only be used with constants and individuals");
throw new InvalidSPDXAnalysisException(NULL_MODEL_MSG);
}

@Override
public boolean isPropertyValueAssignableTo(String objectUri,
PropertyDescriptor propertyDescriptor, Class<?> clazz,
String specVersion) throws InvalidSPDXAnalysisException {
throw new InvalidSPDXAnalysisException("Null model store - can only be used with constants and individuals");
throw new InvalidSPDXAnalysisException(NULL_MODEL_MSG);
}

@Override
public boolean isCollectionProperty(String objectUri,
PropertyDescriptor propertyDescriptor)
throws InvalidSPDXAnalysisException {
throw new InvalidSPDXAnalysisException("Null model store - can only be used with constants and individuals");
throw new InvalidSPDXAnalysisException(NULL_MODEL_MSG);
}

@Override
Expand All @@ -175,7 +177,7 @@ public Optional<TypedValue> getTypedValue(String objectUri)

@Override
public void delete(String objectUri) throws InvalidSPDXAnalysisException {
throw new InvalidSPDXAnalysisException("Null model store - can only be used with constants and individuals");
throw new InvalidSPDXAnalysisException(NULL_MODEL_MSG);
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/spdx/core/TestCoreModelObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void setUp() throws Exception {
*/
@After
public void tearDown() throws Exception {
// Nothing to tear down (yet)
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/spdx/core/TestModelObjectHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void setUp() throws Exception {
*/
@After
public void tearDown() throws Exception {
// Nothing to tear down (yet)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ public class LicenseTextHelperTest extends TestCase {
/**
* @throws java.lang.Exception
*/
@Override
public void setUp() throws Exception {
super.setUp();
}

/**
* @throws java.lang.Exception
*/
@Override
public void tearDown() throws Exception {
super.tearDown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.spdx.licenseTemplate.LicenseTemplateRule.RuleType;

Expand All @@ -31,34 +27,6 @@
*/
public class TestHtmlTemplateOutputHandler {

/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

/**
* @throws java.lang.Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
}

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
}

/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}

/**
* Test method for {@link org.spdx.licenseTemplate.HtmlTemplateOutputHandler#normalText(java.lang.String)}.
*/
Expand Down
Loading

0 comments on commit f158cdf

Please sign in to comment.