Skip to content

Commit debc0ee

Browse files
authored
Merge pull request #24 from spdx/fixcompareerrors
Resolve various issues identified when comparing SPDX document
2 parents 0362b8a + 8fd921f commit debc0ee

File tree

61 files changed

+10717
-33
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+10717
-33
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/**
2+
* Copyright (c) 2011 Source Auditor Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package org.spdx.rdfparser;
18+
19+
import java.util.ArrayList;
20+
21+
import org.apache.jena.graph.Node;
22+
import org.apache.jena.graph.Triple;
23+
import org.apache.jena.rdf.model.Model;
24+
import org.apache.jena.rdf.model.Property;
25+
import org.apache.jena.rdf.model.Resource;
26+
import org.apache.jena.util.iterator.ExtendedIterator;
27+
28+
/**
29+
* Contains a DOAP project
30+
* Currently, only the home page and name properties are supported
31+
* @author Gary O'Neall
32+
*
33+
*/
34+
public class DOAPProject {
35+
36+
static final String UNKNOWN_URI = "UNKNOWN";
37+
private String name = null;
38+
private String homePage = null;
39+
private Node projectNode = null;
40+
private Resource projectResource = null;
41+
private Model model = null;
42+
private String uri = null;
43+
44+
/**
45+
* This method will create a DOAP Project object from a DOAP document
46+
* which already exists. The DOAP project is read from the uri and
47+
* the model is created from the existing data.
48+
* @param model Jena model to populate
49+
* @param projectUrl The URL of the DOAP project
50+
* @return
51+
* @throws InvalidSPDXAnalysisException
52+
*/
53+
static DOAPProject getExistingProject(Model model, String projectUrl) throws InvalidSPDXAnalysisException {
54+
Resource projectResource = model.createResource(projectUrl);
55+
model.read(projectUrl);
56+
return new DOAPProject(model, projectResource.asNode());
57+
}
58+
59+
public DOAPProject(Model model, Node node) throws InvalidSPDXAnalysisException {
60+
this.model = model;
61+
this.projectNode = node;
62+
if (projectNode.isBlank()) {
63+
this.projectResource = model.createResource(node.getBlankNodeId());
64+
} else if (projectNode.isURI()) {
65+
this.projectResource = model.createResource(node.getURI());
66+
this.uri = node.getURI();
67+
} else {
68+
throw(new InvalidSPDXAnalysisException("Can not create a DOAP project from a literal node"));
69+
}
70+
71+
// name
72+
Node p = model.getProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_NAME).asNode();
73+
Triple m = Triple.createMatch(projectNode, p, null);
74+
ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
75+
while (tripleIter.hasNext()) {
76+
Triple t = tripleIter.next();
77+
this.name = t.getObject().toString(false);
78+
}
79+
// home page
80+
p = model.getProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_HOMEPAGE).asNode();
81+
m = Triple.createMatch(projectNode, p, null);
82+
tripleIter = model.getGraph().find(m);
83+
while (tripleIter.hasNext()) {
84+
Triple t = tripleIter.next();
85+
this.homePage = t.getObject().toString(false);
86+
}
87+
}
88+
89+
/**
90+
* @param projectName
91+
* @param homePage
92+
*/
93+
public DOAPProject(String projectName, String homePage) {
94+
this.name = projectName;
95+
this.homePage = homePage;
96+
}
97+
98+
/**
99+
* @return the name
100+
*/
101+
public String getName() {
102+
return name;
103+
}
104+
105+
/**
106+
* @param name the name to set
107+
*/
108+
public void setName(String name) {
109+
this.name = name;
110+
if (this.projectNode != null && this.model != null) {
111+
Property p = model.createProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_NAME);
112+
model.removeAll(projectResource, p, null);
113+
p = model.createProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_NAME);
114+
projectResource.addProperty(p, name);
115+
}
116+
}
117+
118+
/**
119+
* @return the homePage
120+
*/
121+
public String getHomePage() {
122+
return homePage;
123+
}
124+
125+
/**
126+
* @param homePage the homePage to set
127+
*/
128+
public void setHomePage(String homePage) {
129+
this.homePage = homePage;
130+
if (this.projectNode != null && this.model != null) {
131+
Property p = model.createProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_HOMEPAGE);
132+
model.removeAll(projectResource, p, null);
133+
if (homePage != null) {
134+
p = model.createProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_HOMEPAGE);
135+
Resource homePageResource = model.createResource(homePage);
136+
projectResource.addProperty(p, homePageResource);
137+
}
138+
}
139+
}
140+
141+
public String getProjectUri() {
142+
if (projectNode == null) {
143+
if (uri == null || uri.isEmpty()) {
144+
return UNKNOWN_URI;
145+
} else {
146+
return uri;
147+
}
148+
} else {
149+
if (projectNode.isURI()) {
150+
return projectNode.getURI();
151+
} else {
152+
return UNKNOWN_URI;
153+
}
154+
}
155+
}
156+
157+
public Resource createResource(Model model) {
158+
Resource type = model.createResource(SpdxRdfConstants.DOAP_NAMESPACE + SpdxRdfConstants.CLASS_DOAP_PROJECT);
159+
Resource retval;
160+
if (uri != null && !uri.isEmpty() && !uri.equals(UNKNOWN_URI)) {
161+
retval = model.createResource(uri, type);
162+
} else {
163+
retval = model.createResource(type);
164+
}
165+
populateModel(model, retval);
166+
return retval;
167+
}
168+
169+
/**
170+
* @param model Jena model to populate
171+
* @param projectResource Project resource to populate
172+
*/
173+
private void populateModel(Model model, Resource projectResource) {
174+
this.model = model;
175+
this.projectNode = projectResource.asNode();
176+
this.projectResource = projectResource;
177+
178+
// Name
179+
if (name != null) {
180+
Property p = model.createProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_NAME);
181+
projectResource.addProperty(p, name);
182+
}
183+
184+
// HomePage
185+
if (homePage != null) {
186+
Property p = model.createProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_HOMEPAGE);
187+
projectResource.addProperty(p, homePage);
188+
}
189+
}
190+
191+
/**
192+
* @return
193+
*/
194+
public ArrayList<String> verify() {
195+
return new ArrayList<String>(); // anything to verify?
196+
}
197+
198+
/**
199+
* @param uri
200+
* @throws InvalidSPDXAnalysisException
201+
*/
202+
public void setUri(String uri) throws InvalidSPDXAnalysisException {
203+
if (this.projectResource != null) {
204+
if (!this.projectResource.hasURI(uri)) {
205+
throw(new InvalidSPDXAnalysisException("Can not set a URI value for a resource which has already been created."));
206+
}
207+
}
208+
if (!uri.equals(UNKNOWN_URI) &&!SpdxVerificationHelper.isValidUri(uri)) {
209+
throw(new InvalidSPDXAnalysisException("Invalid URI for DOAP Project "+this.name+": "+uri));
210+
}
211+
this.uri = uri;
212+
}
213+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/**
2+
* Copyright (c) 2011 Source Auditor Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package org.spdx.rdfparser;
18+
19+
import java.io.File;
20+
import java.util.ArrayList;
21+
import java.util.regex.Pattern;
22+
23+
/**
24+
* Generates a verification code for a specific directory
25+
* @author Gary O'Neall
26+
*
27+
*/
28+
public class GenerateVerificationCode {
29+
30+
/**
31+
* Print an SPDX Verification code for a directory of files
32+
* args[0] is the source directory containing the files
33+
* args[1] is an optional regular expression of skipped files. The expression is applied against a file path relative the the source directory supplied
34+
* @param args
35+
*/
36+
public static void main(String[] args) {
37+
if (args.length < 1 || args.length > 2) {
38+
error("Incorrect number of arguments.");
39+
System.exit(1);
40+
}
41+
File sourceDirectory = new File(args[0]);
42+
if (!sourceDirectory.exists()) {
43+
error("Source directory "+args[0]+" does not exist.");
44+
System.exit(1);
45+
}
46+
if (!sourceDirectory.isDirectory()) {
47+
error("File "+args[0]+" is not a directory.");
48+
System.exit(1);
49+
}
50+
String skippedRegex = null;
51+
File[] skippedFiles = new File[0];
52+
if (args.length > 1) {
53+
skippedRegex = args[1];
54+
skippedFiles = collectSkippedFiles(skippedRegex, sourceDirectory);
55+
}
56+
try {
57+
VerificationCodeGenerator vcg = new VerificationCodeGenerator(new JavaSha1ChecksumGenerator());
58+
SpdxPackageVerificationCode verificationCode = vcg.generatePackageVerificationCode(sourceDirectory, skippedFiles);
59+
printVerificationCode(verificationCode);
60+
System.exit(0);
61+
} catch (Exception ex) {
62+
error("Error creating verification code: "+ex.getMessage());
63+
}
64+
}
65+
66+
/**
67+
* Collect files to be skipped
68+
* @param skippedRegex Regular Expression for file paths to be skipped
69+
* @param dir Directory to scan for collecting skipped files
70+
* @return
71+
*/
72+
private static File[] collectSkippedFiles(String skippedRegex, File dir) {
73+
Pattern skippedPattern = Pattern.compile(skippedRegex);
74+
ArrayList<File> skippedFiles = new ArrayList<File>();
75+
collectSkippedFiles(skippedPattern, skippedFiles, dir.getPath(), dir);
76+
File[] retval = new File[skippedFiles.size()];
77+
retval = skippedFiles.toArray(retval);
78+
return retval;
79+
}
80+
81+
/**
82+
* Internal method to recurse through the source directory collecting files to skip
83+
* @param skippedPattern
84+
* @param skippedFiles
85+
* @param rootPath
86+
* @param dir
87+
* @return
88+
*/
89+
private static void collectSkippedFiles(Pattern skippedPattern,
90+
ArrayList<File> skippedFiles, String rootPath, File dir) {
91+
if (dir.isFile()) {
92+
String relativePath = dir.getPath().substring(rootPath.length()+1);
93+
if (skippedPattern.matcher(relativePath).matches()) {
94+
skippedFiles.add(dir);
95+
}
96+
} else if (dir.isDirectory()) {
97+
File[] children = dir.listFiles();
98+
for (int i = 0; i < children.length; i++) {
99+
if (children[i].isFile()) {
100+
String relativePath = children[i].getPath().substring(rootPath.length()+1);
101+
if (skippedPattern.matcher(relativePath).matches()) {
102+
skippedFiles.add(children[i]);
103+
}
104+
} else if (children[i].isDirectory()) {
105+
collectSkippedFiles(skippedPattern, skippedFiles, rootPath, children[i]);
106+
}
107+
}
108+
}
109+
}
110+
111+
/**
112+
* @param verificationCode
113+
*/
114+
private static void printVerificationCode(
115+
SpdxPackageVerificationCode verificationCode) {
116+
System.out.println("Verification code value: "+verificationCode.getValue());
117+
String[] excludedFiles = verificationCode.getExcludedFileNames();
118+
if (excludedFiles != null && excludedFiles.length > 0) {
119+
System.out.println("Excluded files:");
120+
for (int i = 0; i < excludedFiles.length; i++) {
121+
System.out.println("\t"+excludedFiles[i]);
122+
}
123+
} else {
124+
System.out.println("No excluded files");
125+
}
126+
}
127+
128+
/**
129+
* @param string
130+
*/
131+
private static void error(String string) {
132+
System.out.println(string);
133+
usage();
134+
}
135+
136+
/**
137+
*
138+
*/
139+
private static void usage() {
140+
System.out.println("Usage: GenerateVerificationCode sourceDirectory");
141+
System.out.println("where sourceDirectory is the root of the archive file for which the verification code is generated");
142+
}
143+
144+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2011 Source Auditor Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package org.spdx.rdfparser;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
/**
23+
* Interface for implementations of generators of file checksums
24+
* @author Gary O'Neall
25+
*
26+
*/
27+
public interface IFileChecksumGenerator {
28+
public String getFileChecksum(File file) throws IOException;
29+
}

0 commit comments

Comments
 (0)