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

Replace conditional with polymorphism in LicenseInfoFactory #301

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions src/org/spdx/rdfparser/license/ConjunctiveLicenseSetStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.spdx.rdfparser.license;

import org.apache.jena.graph.Node;
import org.spdx.rdfparser.IModelContainer;
import org.spdx.rdfparser.InvalidSPDXAnalysisException;

public class ConjunctiveLicenseSetStrategy implements ILicenseInfoSetStrategy {

@Override
public AnyLicenseInfo getLicenseInfoSet(IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException {
return new ConjunctiveLicenseSet(modelContainer, node);
}
}
12 changes: 12 additions & 0 deletions src/org/spdx/rdfparser/license/DisjunctiveLicenseSetStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.spdx.rdfparser.license;

import org.apache.jena.graph.Node;
import org.spdx.rdfparser.IModelContainer;
import org.spdx.rdfparser.InvalidSPDXAnalysisException;

public class DisjunctiveLicenseSetStrategy implements ILicenseInfoSetStrategy {
@Override
public AnyLicenseInfo getLicenseInfoSet(IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException {
return new DisjunctiveLicenseSet(modelContainer, node);
}
}
12 changes: 12 additions & 0 deletions src/org/spdx/rdfparser/license/ExtractedLicenseInfoStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.spdx.rdfparser.license;

import org.apache.jena.graph.Node;
import org.spdx.rdfparser.IModelContainer;
import org.spdx.rdfparser.InvalidSPDXAnalysisException;

public class ExtractedLicenseInfoStrategy implements ILicenseInfoSetStrategy {
@Override
public AnyLicenseInfo getLicenseInfoSet(IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException {
return new ExtractedLicenseInfo(modelContainer, node);
}
}
10 changes: 10 additions & 0 deletions src/org/spdx/rdfparser/license/ILicenseInfoSetStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.spdx.rdfparser.license;

import org.apache.jena.graph.Node;
import org.spdx.rdfparser.IModelContainer;
import org.spdx.rdfparser.InvalidSPDXAnalysisException;

public interface ILicenseInfoSetStrategy {
public AnyLicenseInfo getLicenseInfoSet(IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException;

}
33 changes: 18 additions & 15 deletions src/org/spdx/rdfparser/license/LicenseInfoFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.spdx.rdfparser.SpdxRdfConstants;
import org.spdx.spdxspreadsheet.InvalidLicenseStringException;

import java.util.HashMap;

/**
* Factory for creating SPDXLicenseInfo objects from a Jena model
* @author Gary O'Neall
Expand Down Expand Up @@ -165,26 +167,27 @@ private static AnyLicenseInfo getLicenseInfoByType(IModelContainer modelContaine
throw(new InvalidSPDXAnalysisException("Invalid type for licenseInfo - not an SPDX type"));
}
String type = typeUri.substring(SpdxRdfConstants.SPDX_NAMESPACE.length());
if (type.equals(SpdxRdfConstants.CLASS_SPDX_CONJUNCTIVE_LICENSE_SET)) {
return new ConjunctiveLicenseSet(modelContainer, node);
} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_DISJUNCTIVE_LICENSE_SET)) {
return new DisjunctiveLicenseSet(modelContainer, node);
} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_EXTRACTED_LICENSING_INFO)) {
return new ExtractedLicenseInfo(modelContainer, node);
} else if (type.equals(SpdxRdfConstants.CLASS_SPDX_LICENSE)) {
return new SpdxListedLicense(modelContainer, node);
} else if (type.equals(SpdxRdfConstants.CLASS_OR_LATER_OPERATOR)) {
return new OrLaterOperator(modelContainer, node);
} else if (type.equals(SpdxRdfConstants.CLASS_WITH_EXCEPTION_OPERATOR)) {
return new WithExceptionOperator(modelContainer, node);
} else {
throw(new InvalidSPDXAnalysisException("Invalid type for licenseInfo '"+type+"'"));
}
return getAnyLicenseInfoSetByType(modelContainer, node, type);
} else {
return null;
}
}

private static AnyLicenseInfo getAnyLicenseInfoSetByType(IModelContainer modelContainer, Node node, String type) throws InvalidSPDXAnalysisException {
HashMap<String, ILicenseInfoSetStrategy> map = new HashMap();
map.put(SpdxRdfConstants.CLASS_SPDX_CONJUNCTIVE_LICENSE_SET, new ConjunctiveLicenseSetStrategy());
map.put(SpdxRdfConstants.CLASS_SPDX_DISJUNCTIVE_LICENSE_SET, new DisjunctiveLicenseSetStrategy());
map.put(SpdxRdfConstants.CLASS_SPDX_EXTRACTED_LICENSING_INFO, new ExtractedLicenseInfoStrategy());
map.put(SpdxRdfConstants.CLASS_SPDX_LICENSE, new SpdxListedLicenseStrategy());
map.put(SpdxRdfConstants.CLASS_OR_LATER_OPERATOR, new OrLaterOperatorStrategy());
map.put(SpdxRdfConstants.CLASS_WITH_EXCEPTION_OPERATOR, new WithExceptionOperatorStrategy());
if (!map.containsKey(type)) {
throw(new InvalidSPDXAnalysisException("Invalid type for licenseInfo '"+ type +"'"));
}
return map.get(type).getLicenseInfoSet(modelContainer, node);
}


/**
* Parses a license string and converts it into a SPDXLicenseInfo object
* Syntax - A license set must start and end with a parenthesis "("
Expand Down
12 changes: 12 additions & 0 deletions src/org/spdx/rdfparser/license/OrLaterOperatorStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.spdx.rdfparser.license;

import org.apache.jena.graph.Node;
import org.spdx.rdfparser.IModelContainer;
import org.spdx.rdfparser.InvalidSPDXAnalysisException;

public class OrLaterOperatorStrategy implements ILicenseInfoSetStrategy {
@Override
public AnyLicenseInfo getLicenseInfoSet(IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException {
return new OrLaterOperator(modelContainer, node);
}
}
12 changes: 12 additions & 0 deletions src/org/spdx/rdfparser/license/SpdxListedLicenseStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.spdx.rdfparser.license;

import org.apache.jena.graph.Node;
import org.spdx.rdfparser.IModelContainer;
import org.spdx.rdfparser.InvalidSPDXAnalysisException;

public class SpdxListedLicenseStrategy implements ILicenseInfoSetStrategy {
@Override
public AnyLicenseInfo getLicenseInfoSet(IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException {
return new SpdxListedLicense(modelContainer, node);
}
}
12 changes: 12 additions & 0 deletions src/org/spdx/rdfparser/license/WithExceptionOperatorStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.spdx.rdfparser.license;

import org.apache.jena.graph.Node;
import org.spdx.rdfparser.IModelContainer;
import org.spdx.rdfparser.InvalidSPDXAnalysisException;

public class WithExceptionOperatorStrategy implements ILicenseInfoSetStrategy {
@Override
public AnyLicenseInfo getLicenseInfoSet(IModelContainer modelContainer, Node node) throws InvalidSPDXAnalysisException {
return new WithExceptionOperator(modelContainer, node);
}
}