Skip to content
Merged
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
213 changes: 213 additions & 0 deletions TestFiles/spdx-parser-source/org/spdx/rdfparser/DOAPProject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/**
* Copyright (c) 2011 Source Auditor Inc.
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.spdx.rdfparser;

import java.util.ArrayList;

import org.apache.jena.graph.Node;
import org.apache.jena.graph.Triple;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.util.iterator.ExtendedIterator;

/**
* Contains a DOAP project
* Currently, only the home page and name properties are supported
* @author Gary O'Neall
*
*/
public class DOAPProject {

static final String UNKNOWN_URI = "UNKNOWN";
private String name = null;
private String homePage = null;
private Node projectNode = null;
private Resource projectResource = null;
private Model model = null;
private String uri = null;

/**
* This method will create a DOAP Project object from a DOAP document
* which already exists. The DOAP project is read from the uri and
* the model is created from the existing data.
* @param model Jena model to populate
* @param projectUrl The URL of the DOAP project
* @return
* @throws InvalidSPDXAnalysisException
*/
static DOAPProject getExistingProject(Model model, String projectUrl) throws InvalidSPDXAnalysisException {
Resource projectResource = model.createResource(projectUrl);
model.read(projectUrl);
return new DOAPProject(model, projectResource.asNode());
}

public DOAPProject(Model model, Node node) throws InvalidSPDXAnalysisException {
this.model = model;
this.projectNode = node;
if (projectNode.isBlank()) {
this.projectResource = model.createResource(node.getBlankNodeId());
} else if (projectNode.isURI()) {
this.projectResource = model.createResource(node.getURI());
this.uri = node.getURI();
} else {
throw(new InvalidSPDXAnalysisException("Can not create a DOAP project from a literal node"));
}

// name
Node p = model.getProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_NAME).asNode();
Triple m = Triple.createMatch(projectNode, p, null);
ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
while (tripleIter.hasNext()) {
Triple t = tripleIter.next();
this.name = t.getObject().toString(false);
}
// home page
p = model.getProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_HOMEPAGE).asNode();
m = Triple.createMatch(projectNode, p, null);
tripleIter = model.getGraph().find(m);
while (tripleIter.hasNext()) {
Triple t = tripleIter.next();
this.homePage = t.getObject().toString(false);
}
}

/**
* @param projectName
* @param homePage
*/
public DOAPProject(String projectName, String homePage) {
this.name = projectName;
this.homePage = homePage;
}

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
if (this.projectNode != null && this.model != null) {
Property p = model.createProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_NAME);
model.removeAll(projectResource, p, null);
p = model.createProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_NAME);
projectResource.addProperty(p, name);
}
}

/**
* @return the homePage
*/
public String getHomePage() {
return homePage;
}

/**
* @param homePage the homePage to set
*/
public void setHomePage(String homePage) {
this.homePage = homePage;
if (this.projectNode != null && this.model != null) {
Property p = model.createProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_HOMEPAGE);
model.removeAll(projectResource, p, null);
if (homePage != null) {
p = model.createProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_HOMEPAGE);
Resource homePageResource = model.createResource(homePage);
projectResource.addProperty(p, homePageResource);
}
}
}

public String getProjectUri() {
if (projectNode == null) {
if (uri == null || uri.isEmpty()) {
return UNKNOWN_URI;
} else {
return uri;
}
} else {
if (projectNode.isURI()) {
return projectNode.getURI();
} else {
return UNKNOWN_URI;
}
}
}

public Resource createResource(Model model) {
Resource type = model.createResource(SpdxRdfConstants.DOAP_NAMESPACE + SpdxRdfConstants.CLASS_DOAP_PROJECT);
Resource retval;
if (uri != null && !uri.isEmpty() && !uri.equals(UNKNOWN_URI)) {
retval = model.createResource(uri, type);
} else {
retval = model.createResource(type);
}
populateModel(model, retval);
return retval;
}

/**
* @param model Jena model to populate
* @param projectResource Project resource to populate
*/
private void populateModel(Model model, Resource projectResource) {
this.model = model;
this.projectNode = projectResource.asNode();
this.projectResource = projectResource;

// Name
if (name != null) {
Property p = model.createProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_NAME);
projectResource.addProperty(p, name);
}

// HomePage
if (homePage != null) {
Property p = model.createProperty(SpdxRdfConstants.DOAP_NAMESPACE, SpdxRdfConstants.PROP_PROJECT_HOMEPAGE);
projectResource.addProperty(p, homePage);
}
}

/**
* @return
*/
public ArrayList<String> verify() {
return new ArrayList<String>(); // anything to verify?
}

/**
* @param uri
* @throws InvalidSPDXAnalysisException
*/
public void setUri(String uri) throws InvalidSPDXAnalysisException {
if (this.projectResource != null) {
if (!this.projectResource.hasURI(uri)) {
throw(new InvalidSPDXAnalysisException("Can not set a URI value for a resource which has already been created."));
}
}
if (!uri.equals(UNKNOWN_URI) &&!SpdxVerificationHelper.isValidUri(uri)) {
throw(new InvalidSPDXAnalysisException("Invalid URI for DOAP Project "+this.name+": "+uri));
}
this.uri = uri;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* Copyright (c) 2011 Source Auditor Inc.
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.spdx.rdfparser;

import java.io.File;
import java.util.ArrayList;
import java.util.regex.Pattern;

/**
* Generates a verification code for a specific directory
* @author Gary O'Neall
*
*/
public class GenerateVerificationCode {

/**
* Print an SPDX Verification code for a directory of files
* args[0] is the source directory containing the files
* args[1] is an optional regular expression of skipped files. The expression is applied against a file path relative the the source directory supplied
* @param args
*/
public static void main(String[] args) {
if (args.length < 1 || args.length > 2) {
error("Incorrect number of arguments.");
System.exit(1);
}
File sourceDirectory = new File(args[0]);
if (!sourceDirectory.exists()) {
error("Source directory "+args[0]+" does not exist.");
System.exit(1);
}
if (!sourceDirectory.isDirectory()) {
error("File "+args[0]+" is not a directory.");
System.exit(1);
}
String skippedRegex = null;
File[] skippedFiles = new File[0];
if (args.length > 1) {
skippedRegex = args[1];
skippedFiles = collectSkippedFiles(skippedRegex, sourceDirectory);
}
try {
VerificationCodeGenerator vcg = new VerificationCodeGenerator(new JavaSha1ChecksumGenerator());
SpdxPackageVerificationCode verificationCode = vcg.generatePackageVerificationCode(sourceDirectory, skippedFiles);
printVerificationCode(verificationCode);
System.exit(0);
} catch (Exception ex) {
error("Error creating verification code: "+ex.getMessage());
}
}

/**
* Collect files to be skipped
* @param skippedRegex Regular Expression for file paths to be skipped
* @param dir Directory to scan for collecting skipped files
* @return
*/
private static File[] collectSkippedFiles(String skippedRegex, File dir) {
Pattern skippedPattern = Pattern.compile(skippedRegex);
ArrayList<File> skippedFiles = new ArrayList<File>();
collectSkippedFiles(skippedPattern, skippedFiles, dir.getPath(), dir);
File[] retval = new File[skippedFiles.size()];
retval = skippedFiles.toArray(retval);
return retval;
}

/**
* Internal method to recurse through the source directory collecting files to skip
* @param skippedPattern
* @param skippedFiles
* @param rootPath
* @param dir
* @return
*/
private static void collectSkippedFiles(Pattern skippedPattern,
ArrayList<File> skippedFiles, String rootPath, File dir) {
if (dir.isFile()) {
String relativePath = dir.getPath().substring(rootPath.length()+1);
if (skippedPattern.matcher(relativePath).matches()) {
skippedFiles.add(dir);
}
} else if (dir.isDirectory()) {
File[] children = dir.listFiles();
for (int i = 0; i < children.length; i++) {
if (children[i].isFile()) {
String relativePath = children[i].getPath().substring(rootPath.length()+1);
if (skippedPattern.matcher(relativePath).matches()) {
skippedFiles.add(children[i]);
}
} else if (children[i].isDirectory()) {
collectSkippedFiles(skippedPattern, skippedFiles, rootPath, children[i]);
}
}
}
}

/**
* @param verificationCode
*/
private static void printVerificationCode(
SpdxPackageVerificationCode verificationCode) {
System.out.println("Verification code value: "+verificationCode.getValue());
String[] excludedFiles = verificationCode.getExcludedFileNames();
if (excludedFiles != null && excludedFiles.length > 0) {
System.out.println("Excluded files:");
for (int i = 0; i < excludedFiles.length; i++) {
System.out.println("\t"+excludedFiles[i]);
}
} else {
System.out.println("No excluded files");
}
}

/**
* @param string
*/
private static void error(String string) {
System.out.println(string);
usage();
}

/**
*
*/
private static void usage() {
System.out.println("Usage: GenerateVerificationCode sourceDirectory");
System.out.println("where sourceDirectory is the root of the archive file for which the verification code is generated");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2011 Source Auditor Inc.
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.spdx.rdfparser;

import java.io.File;
import java.io.IOException;

/**
* Interface for implementations of generators of file checksums
* @author Gary O'Neall
*
*/
public interface IFileChecksumGenerator {
public String getFileChecksum(File file) throws IOException;
}
Loading