/**
 * 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.List;
import java.util.Map;

import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spdx.rdfparser.license.AnyLicenseInfo;
import org.spdx.rdfparser.license.DuplicateExtractedLicenseIdException;
import org.spdx.rdfparser.license.LicenseInfoFactory;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * Contains and SPDXFile object and updates the RDF model.
 *
 * This class is for compatibility with the 1.2 version of the SPDX library
 * and is no longer used for version 2.0 and above.
 * @author Gary O'Neall
 *
 */
@Deprecated
public class SPDXFile implements Comparable<SPDXFile>, Cloneable {

	static final Logger logger = LoggerFactory.getLogger(SPDXFile.class.getName());
	IModelContainer modelContainer;
	private Model model = null;
	private Resource resource = null;
	private String name;
	private AnyLicenseInfo concludedLicenses;
	private SPDXChecksum sha1;
	private String type;
	private AnyLicenseInfo[] seenLicenses;
	private String licenseComments;
	private String copyright;
	private DOAPProject[] artifactOf;
	private String comment = null;
	private SPDXFile[] fileDependencies;
	private String[] contributors;
	private String noticeText;

	public static final Map<String, String> FILE_TYPE_TO_RESOURCE = Maps.newHashMap();
	public static final Map<String, String> RESOURCE_TO_FILE_TYPE = Maps.newHashMap();

	static {
		FILE_TYPE_TO_RESOURCE.put(SpdxRdfConstants.FILE_TYPE_SOURCE,
				SpdxRdfConstants.SPDX_NAMESPACE + SpdxRdfConstants.PROP_FILE_TYPE_SOURCE);
		RESOURCE_TO_FILE_TYPE.put(SpdxRdfConstants.SPDX_NAMESPACE + SpdxRdfConstants.PROP_FILE_TYPE_SOURCE,
				SpdxRdfConstants.FILE_TYPE_SOURCE);
		FILE_TYPE_TO_RESOURCE.put(SpdxRdfConstants.FILE_TYPE_BINARY,
				SpdxRdfConstants.SPDX_NAMESPACE + SpdxRdfConstants.PROP_FILE_TYPE_BINARY);
		RESOURCE_TO_FILE_TYPE.put(SpdxRdfConstants.SPDX_NAMESPACE + SpdxRdfConstants.PROP_FILE_TYPE_BINARY,
				SpdxRdfConstants.FILE_TYPE_BINARY);
		FILE_TYPE_TO_RESOURCE.put(SpdxRdfConstants.FILE_TYPE_ARCHIVE,
				SpdxRdfConstants.SPDX_NAMESPACE + SpdxRdfConstants.PROP_FILE_TYPE_ARCHIVE);
		RESOURCE_TO_FILE_TYPE.put(SpdxRdfConstants.SPDX_NAMESPACE + SpdxRdfConstants.PROP_FILE_TYPE_ARCHIVE,
				SpdxRdfConstants.FILE_TYPE_ARCHIVE);
		FILE_TYPE_TO_RESOURCE.put(SpdxRdfConstants.FILE_TYPE_OTHER,
				SpdxRdfConstants.SPDX_NAMESPACE + SpdxRdfConstants.PROP_FILE_TYPE_OTHER);
		RESOURCE_TO_FILE_TYPE.put(SpdxRdfConstants.SPDX_NAMESPACE + SpdxRdfConstants.PROP_FILE_TYPE_OTHER,
				SpdxRdfConstants.FILE_TYPE_OTHER);
	};

	/**
	 * Construct an SPDX File form the fileNode
	 * @param fileNode RDF Graph node representing the SPDX File
	 * @throws InvalidSPDXAnalysisException
	 */
	public SPDXFile(IModelContainer modelContainer, Node fileNode) throws InvalidSPDXAnalysisException {
		this.modelContainer = modelContainer;
		this.model = modelContainer.getModel();
		this.resource = RdfParserHelper.convertToResource(model, fileNode);
		// name
		Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_NAME).asNode();
		Triple m = Triple.createMatch(fileNode, p, null);
		ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
		while (tripleIter.hasNext()) {
			Triple t = tripleIter.next();
			this.name = t.getObject().toString(false);
		}
		// checksum - sha1
		p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_CHECKSUM).asNode();
		m = Triple.createMatch(fileNode, p, null);
		tripleIter = model.getGraph().find(m);
		while (tripleIter.hasNext()) {
			Triple t = tripleIter.next();
			this.sha1  = new SPDXChecksum(model, t.getObject());
		}
		// type
		p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_TYPE).asNode();
		m = Triple.createMatch(fileNode, p, null);
		tripleIter = model.getGraph().find(m);
		while (tripleIter.hasNext()) {
			Triple t = tripleIter.next();
			if (t.getObject().isLiteral()) {
				// the following is for compatibility with previous versions of the tool which used literals for the file type
				this.type = t.getObject().toString(false);
			} else if (t.getObject().isURI()) {
				this.type = RESOURCE_TO_FILE_TYPE.get(t.getObject().getURI());
				if (this.type == null) {
					throw(new InvalidSPDXAnalysisException("Invalid URI for file type resource - must be one of the individual file types in http://spdx.org/rdf/terms"));
				}
			} else {
				throw(new InvalidSPDXAnalysisException("Invalid file type property - must be a URI type specified in http://spdx.org/rdf/terms"));
			}
		}
		// concluded License
		List<AnyLicenseInfo> alLic = Lists.newArrayList();
		p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LICENSE_CONCLUDED).asNode();
		m = Triple.createMatch(fileNode, p, null);
		tripleIter = model.getGraph().find(m);
		while (tripleIter.hasNext()) {
			Triple t = tripleIter.next();
			alLic.add(LicenseInfoFactory.getLicenseInfoFromModel(modelContainer, t.getObject()));
		}
		if (alLic.size() > 1) {
			throw(new InvalidSPDXAnalysisException("Too many concluded licenses for file"));
		}
		if (alLic.size() == 0) {
			throw(new InvalidSPDXAnalysisException("Missing required concluded license"));
		}
		this.concludedLicenses = alLic.get(0);
		// seenLicenses
		alLic.clear();
		p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_SEEN_LICENSE).asNode();
		m = Triple.createMatch(fileNode, p, null);
		tripleIter = model.getGraph().find(m);
		while (tripleIter.hasNext()) {
			Triple t = tripleIter.next();
			alLic.add(LicenseInfoFactory.getLicenseInfoFromModel(modelContainer, t.getObject()));
		}
		this.seenLicenses = alLic.toArray(new AnyLicenseInfo[alLic.size()]);

		// fileDependencies
		p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_FILE_DEPENDENCY).asNode();
		m = Triple.createMatch(fileNode, p, null);
		List<SPDXFile> alDependencies = Lists.newArrayList();
		tripleIter = model.getGraph().find(m);
		while (tripleIter.hasNext()) {
			Triple t = tripleIter.next();
			alDependencies.add(new SPDXFile(modelContainer, t.getObject()));
		}
		this.fileDependencies = alDependencies.toArray(new SPDXFile[alDependencies.size()]);

		//licenseComments
		p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LIC_COMMENTS).asNode();
		m = Triple.createMatch(fileNode, p, null);
		tripleIter = model.getGraph().find(m);
		while (tripleIter.hasNext()) {
			Triple t = tripleIter.next();
			this.licenseComments = t.getObject().toString(false);
		}
		//copyright
		p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_COPYRIGHT).asNode();
		m = Triple.createMatch(fileNode, p, null);
		tripleIter = model.getGraph().find(m);
		while (tripleIter.hasNext()) {
			Triple t = tripleIter.next();
			if (t.getObject().isURI()) {
				// check for standard value types
				if (t.getObject().getURI().equals(SpdxRdfConstants.URI_VALUE_NOASSERTION)) {
					this.copyright = SpdxRdfConstants.NOASSERTION_VALUE;
				} else if (t.getObject().getURI().equals(SpdxRdfConstants.URI_VALUE_NONE)) {
					this.copyright = SpdxRdfConstants.NONE_VALUE;
				} else {
					this.copyright = t.getObject().toString(false);
				}
			} else {
				this.copyright = t.getObject().toString(false);
			}
		}
		//comment
		p = model.getProperty(SpdxRdfConstants.RDFS_NAMESPACE, SpdxRdfConstants.RDFS_PROP_COMMENT).asNode();
		m = Triple.createMatch(fileNode, p, null);
		tripleIter = model.getGraph().find(m);
		while (tripleIter.hasNext()) {
			this.comment = tripleIter.next().getObject().toString(false);
		}
		// contributors
		List<String> alContributors = Lists.newArrayList();
		p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_CONTRIBUTOR).asNode();
		m = Triple.createMatch(fileNode, p, null);
		tripleIter = model.getGraph().find(m);
		while (tripleIter.hasNext()) {
			Triple t = tripleIter.next();
			alContributors.add(t.getObject().toString(false));
		}
		this.contributors = alContributors.toArray(new String[alContributors.size()]);
		//artifactOf
		List<DOAPProject> alProjects = Lists.newArrayList();
		p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_ARTIFACTOF).asNode();
		m = Triple.createMatch(fileNode, p, null);
		tripleIter = model.getGraph().find(m);
		while (tripleIter.hasNext()) {
			Triple t = tripleIter.next();
			alProjects.add(new DOAPProject(model, t.getObject()));
		}
		this.artifactOf = alProjects.toArray(new DOAPProject[alProjects.size()]);
		// noticeText
		p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_NOTICE).asNode();
		m = Triple.createMatch(fileNode, p, null);
		tripleIter = model.getGraph().find(m);
		while (tripleIter.hasNext()) {
			this.noticeText = tripleIter.next().getObject().toString(false);
		}
	}

	/**
	 * Create a resource for this SPDX file
	 * @param uri URI for the file resource
	 * @param doc SPDX document containing the SPDX file
	 * @return
	 * @throws InvalidSPDXAnalysisException
	 */
	public Resource createResource(SPDXDocument doc, String uri) throws InvalidSPDXAnalysisException {
		this.modelContainer = doc;
		Model model = doc.getModel();
		Resource type = model.createResource(SpdxRdfConstants.SPDX_NAMESPACE + SpdxRdfConstants.CLASS_SPDX_FILE);
		Resource retval = findFileResource(model, this);	// prevent duplicate files in the model
		if (retval == null) {
			retval = model.createResource(uri, type);
		}
		populateModel(doc, retval);
		this.resource = retval;
		return retval;
	}

	/**
	 * Populates a Jena RDF model with the information from this file declaration
	 * @param licenseResource
	 * @param model
	 * @throws InvalidSPDXAnalysisException
	 */
	private void populateModel(SPDXDocument doc, Resource fileResource) throws InvalidSPDXAnalysisException {
		Model model = doc.getModel();
		// name
		Property p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_NAME);
		fileResource.addProperty(p, this.getName());

		// sha1
		p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_CHECKSUM);
		model.removeAll(fileResource, p, null);
		if (this.sha1 != null) {
			Resource cksumResource = this.sha1.getResource();
			if (cksumResource == null) {
				cksumResource = this.sha1.createResource(model);
			}
			fileResource.addProperty(p, cksumResource);
		}
		// type
		p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_TYPE);
		model.removeAll(fileResource, p, null);
		Resource fileTypeResource = fileTypeStringToTypeResource(this.getType(), model);
		fileResource.addProperty(p, fileTypeResource);

		// concludedLicenses
		p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LICENSE_CONCLUDED);
		model.removeAll(fileResource, p, null);
		if (this.concludedLicenses != null) {
			Resource lic = this.concludedLicenses.createResource(doc);
			fileResource.addProperty(p, lic);
		}

		// seenLicenses
		p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_SEEN_LICENSE);
		model.removeAll(fileResource, p, null);
		if (this.seenLicenses != null && this.seenLicenses.length > 0) {
			for (int i = 0; i < this.seenLicenses.length; i++) {
				Resource lic = this.seenLicenses[i].createResource(doc);
				fileResource.addProperty(p, lic);
			}
		}

		// fileDependencies
		p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_FILE_DEPENDENCY);
		model.removeAll(fileResource, p, null);
		if (this.fileDependencies != null && this.fileDependencies.length > 0) {
			for (int i = 0; i < this.fileDependencies.length; i++) {
				Resource dep = this.fileDependencies[i].getResource();
				if (dep == null) {
					// see if the file already exists in the model - prevents creating a duplicate file
					dep = findFileResource(model, this.fileDependencies[i]);
					if (dep == null) {	// need to add this file dependency to a resource
						dep = this.fileDependencies[i].createResource(doc, doc.getDocumentNamespace() + doc.getNextSpdxElementRef());
					}
				}
				fileResource.addProperty(p, dep);
			}
		}
		//licenseComments
		p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LIC_COMMENTS);
		model.removeAll(fileResource, p, null);
		if (this.licenseComments != null) {
			fileResource.addProperty(p, this.getLicenseComments());
		}
		//copyright
		p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_COPYRIGHT);
		model.removeAll(fileResource, p, null);
		if (this.copyright != null) {
			if (copyright.equals(SpdxRdfConstants.NONE_VALUE)) {
				Resource r = model.createResource(SpdxRdfConstants.URI_VALUE_NONE);
				fileResource.addProperty(p, r);
			} else if (copyright.equals(SpdxRdfConstants.NOASSERTION_VALUE)) {
				Resource r = model.createResource(SpdxRdfConstants.URI_VALUE_NOASSERTION);
				fileResource.addProperty(p, r);
			} else {
				fileResource.addProperty(p, this.getCopyright());
			}
		}
		//comment
		p = model.createProperty(SpdxRdfConstants.RDFS_NAMESPACE, SpdxRdfConstants.RDFS_PROP_COMMENT);
		model.removeAll(fileResource, p, null);
		if (this.comment != null) {
			fileResource.addProperty(p, this.comment);
		}

		//artifactof
		p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_ARTIFACTOF);
		model.removeAll(fileResource, p, null);
		if (this.artifactOf != null) {
			p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_ARTIFACTOF);
			for (int i = 0; i < artifactOf.length; i++) {
				Resource projectResource = artifactOf[i].createResource(model);
				fileResource.addProperty(p, projectResource);
			}
		}

		//contributors
		p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_CONTRIBUTOR);
		model.removeAll(fileResource, p, null);
		if (this.contributors != null) {
			for (int i = 0; i < this.contributors.length; i++) {
				fileResource.addProperty(p, this.contributors[i]);
			}
		}

		//noticeText
		p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_NOTICE);
		model.removeAll(fileResource, p, null);
		if (this.noticeText != null) {
			fileResource.addProperty(p, this.getNoticeText());
		}
		this.model = model;
		this.resource = fileResource;
	}

	/**
	 * Finds the resource for an existing file in the model
	 * @param spdxFile
	 * @return resource of an SPDX file with the same name and checksum.  Null if none found
	 * @throws InvalidSPDXAnalysisException
	 */
	static protected Resource findFileResource(Model model, SPDXFile spdxFile) throws InvalidSPDXAnalysisException {
		// find any matching file names
		Node fileNameProperty = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_NAME).asNode();
		Triple fileNameMatch = Triple.createMatch(null, fileNameProperty, NodeFactory.createLiteral(spdxFile.getName()));

		ExtendedIterator<Triple> filenameMatchIter = model.getGraph().find(fileNameMatch);
		if (filenameMatchIter.hasNext()) {
			Triple fileMatchTriple = filenameMatchIter.next();
			Node fileNode = fileMatchTriple.getSubject();
			// check the checksum
			Node checksumProperty = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_CHECKSUM).asNode();
			Triple checksumMatch = Triple.createMatch(fileNode, checksumProperty, null);
			ExtendedIterator<Triple> checksumMatchIterator = model.getGraph().find(checksumMatch);
			if (checksumMatchIterator.hasNext()) {
				Triple checksumMatchTriple = checksumMatchIterator.next();
				SPDXChecksum cksum = new SPDXChecksum(model, checksumMatchTriple.getObject());
				if (cksum.getValue().compareToIgnoreCase(spdxFile.sha1.getValue()) == 0) {
					return RdfParserHelper.convertToResource(model, fileNode);
				}
			}
		}
		// if we get to here, we did not find a match
		return null;
	}
	public SPDXFile(String name, String type, String sha1,
			AnyLicenseInfo concludedLicenses,
			AnyLicenseInfo[] seenLicenses, String licenseComments,
			String copyright, DOAPProject[] artifactOf, String comment,
			SPDXFile[] fileDependencies, String[] contributors,
			String noticeText) {
		this.name = name;
		this.type = type;
		this.sha1 = new SPDXChecksum(SpdxRdfConstants.ALGORITHM_SHA1, sha1);
		this.concludedLicenses = concludedLicenses;
		this.seenLicenses = seenLicenses;
		this.licenseComments = licenseComments;
		this.copyright = copyright;
		this.artifactOf = artifactOf;
		this.comment = comment;
		if (fileDependencies == null) {
			this.fileDependencies = new SPDXFile[0];
		} else {
			this.fileDependencies = fileDependencies;
		}
		if (contributors == null) {
			this.contributors = new String[0];
		} else {
			this.contributors = contributors;
		}
		this.noticeText = noticeText;
	}

	public SPDXFile(String name, String type, String sha1,
			AnyLicenseInfo concludedLicenses,
			AnyLicenseInfo[] seenLicenses, String licenseComments,
			String copyright, DOAPProject[] artifactOf) {
		this(name, type, sha1, concludedLicenses, seenLicenses,
				licenseComments, copyright, artifactOf, null, null, null, null);
	}

	public SPDXFile(String name, String type, String sha1,
			AnyLicenseInfo concludedLicenses,
			AnyLicenseInfo[] seenLicenses, String licenseComments,
			String copyright, DOAPProject[] artifactOf, String comment) {
		this(name, type, sha1, concludedLicenses, seenLicenses,
				licenseComments, copyright, artifactOf, comment, null, null, null);
	}


	/**
	 * @return the seenLicenses
	 * @throws InvalidSPDXAnalysisException
	 */
	public AnyLicenseInfo[] getSeenLicenses() {
		if (this.model != null && this.resource != null) {
			try {
				List<Node> alLicNode = Lists.newArrayList();
				Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_SEEN_LICENSE).asNode();
				Triple m = Triple.createMatch(this.resource.asNode(), p, null);
				ExtendedIterator <Triple> tripleIter = model.getGraph().find(m);
				while (tripleIter.hasNext()) {
					Triple t = tripleIter.next();
					alLicNode.add(t.getObject());
				}
				boolean seenLicensesMatch = (this.seenLicenses.length == alLicNode.size());
				int i = 0;
				while (seenLicensesMatch && i < this.seenLicenses.length) {
					Resource licResource = this.seenLicenses[i++].getResource();
					if (licResource == null) {
						seenLicensesMatch = false;
					} else {
						boolean found = false;
						for (int j = 0; j < alLicNode.size(); j++) {
							if (alLicNode.get(j).equals(licResource.asNode())) {
								found = true;
								break;
							}
						}
						if (!found) {
							seenLicensesMatch = false;
						}
					}
				}
				if (!seenLicensesMatch) {
					this.seenLicenses = new AnyLicenseInfo[alLicNode.size()];
					for (int k = 0; i < this.seenLicenses.length; k++) {
						this.seenLicenses[k] = LicenseInfoFactory.getLicenseInfoFromModel(
								modelContainer, alLicNode.get(k));
					}
				}
			} catch(InvalidSPDXAnalysisException e) {
				// just use the original exception
			}
		}
		return seenLicenses;
	}
	/**
	 * @param seenLicenses the seenLicenses to set
	 * @throws InvalidSPDXAnalysisException
	 */
	public void setSeenLicenses(AnyLicenseInfo[] seenLicenses) throws InvalidSPDXAnalysisException {
		this.seenLicenses = seenLicenses;
		if (this.model != null && this.resource != null) {
			Property p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_SEEN_LICENSE);
			model.removeAll(this.resource, p, null);
			p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_SEEN_LICENSE);

			for (int i = 0; i < seenLicenses.length; i++) {
				Resource lic;
				try {
					lic = seenLicenses[i].createResource(this.modelContainer);
				} catch (DuplicateExtractedLicenseIdException e) {
					throw(new InvalidSPDXAnalysisException("Seen licenses contains a non-standard license ID "+
							" which already exists in the model with different license text:" + seenLicenses[i].toString()));
				}
				this.resource.addProperty(p, lic);
			}
		}
	}
	/**
	 * @return the licenseComments
	 */
	public String getLicenseComments() {
		if (this.model != null && this.resource != null) {
			Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LIC_COMMENTS).asNode();
			Triple m = Triple.createMatch(this.resource.asNode(), p, null);
			ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
			while (tripleIter.hasNext()) {
				Triple t = tripleIter.next();
				this.licenseComments = t.getObject().toString(false);
			}
		}
		return licenseComments;
	}
	/**
	 * @param licenseComments the licenseComments to set
	 */
	public void setLicenseComments(String licenseComments) {
		this.licenseComments = licenseComments;
		if (this.model != null && this.resource != null) {
			Property p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LIC_COMMENTS);
			model.removeAll(this.resource, p, null);
			p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LIC_COMMENTS);
			this.resource.addProperty(p, this.getLicenseComments());
		}
	}

	/**
	 * @return the noticeText
	 */
	public String getNoticeText() {
		if (this.model != null && this.resource != null) {
			Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_NOTICE).asNode();
			Triple m = Triple.createMatch(this.resource.asNode(), p, null);
			ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
			while (tripleIter.hasNext()) {
				this.noticeText = tripleIter.next().getObject().toString(false);
			}
		}
		return this.noticeText;
	}
	/**
	 * @param noticeTexgt the noticeText to set
	 */
	public void setNoticeText(String noticeText) {
		this.noticeText = noticeText;
		if (this.model != null && this.resource != null) {
			Property p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_NOTICE);
			model.removeAll(this.resource, p, null);
			if (this.noticeText != null) {
				p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_NOTICE);
				this.resource.addProperty(p, this.getNoticeText());
			}
		}
	}

	public String getComment() {
		if (this.model != null && this.resource != null) {
			Node p = model.getProperty(SpdxRdfConstants.RDFS_NAMESPACE, SpdxRdfConstants.RDFS_PROP_COMMENT).asNode();
			Triple m = Triple.createMatch(this.resource.asNode(), p, null);
			ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
			while (tripleIter.hasNext()) {
				this.comment = tripleIter.next().getObject().toString(false);
			}
		}
		return this.comment;
	}

	public void setComment(String comment) {
	this.comment = comment;
		if (this.model != null && this.resource != null) {
			Property p = model.createProperty(SpdxRdfConstants.RDFS_NAMESPACE, SpdxRdfConstants.RDFS_PROP_COMMENT);
			model.removeAll(this.resource, p, null);
			p = model.createProperty(SpdxRdfConstants.RDFS_NAMESPACE, SpdxRdfConstants.RDFS_PROP_COMMENT);
			this.resource.addProperty(p, this.comment);
		}
	}
	/**
	 * @return the copyright
	 */
	public String getCopyright() {
		if (this.model != null && this.resource != null) {
			Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_COPYRIGHT).asNode();
			Triple m = Triple.createMatch(this.resource.asNode(), p, null);
			ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
			while (tripleIter.hasNext()) {
				Triple t = tripleIter.next();
				if (t.getObject().isURI()) {
					// check for standard value types
					if (t.getObject().getURI().equals(SpdxRdfConstants.URI_VALUE_NOASSERTION)) {
						this.copyright = SpdxRdfConstants.NOASSERTION_VALUE;
					} else if (t.getObject().getURI().equals(SpdxRdfConstants.URI_VALUE_NONE)) {
						this.copyright = SpdxRdfConstants.NONE_VALUE;
					} else {
						this.copyright = t.getObject().toString(false);
					}
				} else {
					this.copyright = t.getObject().toString(false);
				}
			}
		}
		return copyright;
	}
	/**
	 * @param copyright the copyright to set
	 */
	public void setCopyright(String copyright) {
		this.copyright = copyright;
		if (this.model != null && this.resource != null) {
			Property p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_COPYRIGHT);
			model.removeAll(this.resource, p, null);
			p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_COPYRIGHT);
			if (copyright.equals(SpdxRdfConstants.NONE_VALUE)) {
				Resource r = model.createResource(SpdxRdfConstants.URI_VALUE_NONE);
				this.resource.addProperty(p, r);
			} else if (copyright.equals(SpdxRdfConstants.NOASSERTION_VALUE)) {
				Resource r = model.createResource(SpdxRdfConstants.URI_VALUE_NOASSERTION);
				this.resource.addProperty(p, r);
			} else {
				this.resource.addProperty(p, this.getCopyright());
			}
		}
	}


	/**
	 * @return the name
	 */
	public String getName() {
		if (this.model != null && this.resource != null) {
			Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_NAME).asNode();
			Triple m = Triple.createMatch(this.resource.asNode(), p, null);
			ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
			while (tripleIter.hasNext()) {
				Triple t = tripleIter.next();
				this.name = t.getObject().toString(false);
			}
		}
		return this.name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
		if (this.model != null && this.resource != null) {
			Property p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_NAME);
			model.removeAll(this.resource, p, null);
			p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_NAME);
			this.resource.addProperty(p, this.getName());
		}
	}
	/**
	 * @return the fileLicenses
	 * @throws InvalidSPDXAnalysisException
	 */
	public AnyLicenseInfo getConcludedLicenses() {
		if (this.model != null && this.resource != null) {
			try {
				Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LICENSE_CONCLUDED).asNode();
				Triple m = Triple.createMatch(this.resource.asNode(), p, null);
				ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
				Triple t = null;
				while (tripleIter.hasNext()) {
					t = tripleIter.next();
					if (this.concludedLicenses != null &&
							t.getObject().equals(this.concludedLicenses.getResource().asNode())) {
						break;
					}
				}
				if (this.concludedLicenses == null ||
						!t.getObject().equals(this.concludedLicenses.getResource().asNode())) {
					this.concludedLicenses = LicenseInfoFactory.getLicenseInfoFromModel(modelContainer, t.getObject());
				}
			}	catch(InvalidSPDXAnalysisException e) {
				// just use the original property
			}
		}
		return this.concludedLicenses;
	}
	/**
	 * @param fileLicenses the fileLicenses to set
	 * @throws InvalidSPDXAnalysisException
	 */
	public void setConcludedLicenses(AnyLicenseInfo fileLicenses) throws InvalidSPDXAnalysisException {
		this.concludedLicenses = fileLicenses;
		if (this.model != null && this.resource != null) {
			Property p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LICENSE_CONCLUDED);
			model.removeAll(this.resource, p, null);
			p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_LICENSE_CONCLUDED);
			Resource lic = null;
			try {
				lic = fileLicenses.createResource(this.modelContainer);
			} catch (DuplicateExtractedLicenseIdException e) {
				throw(new InvalidSPDXAnalysisException("Concluded licenses contains a non-standard license with inconsistent text with " +
						"                                   an existing license: "+fileLicenses.toString()));
			}
			this.resource.addProperty(p, lic);
		}
	}
	/**
	 * @return the sha1
	 * @throws InvalidSPDXAnalysisException
	 */
	public String getSha1()  {
		if (this.model != null && this.resource != null) {
			try {
				Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_CHECKSUM).asNode();
				Triple m = Triple.createMatch(this.resource.asNode(), p, null);
				ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
				Triple t = null;
				while (tripleIter.hasNext()) {
					t = tripleIter.next();
					if (this.sha1 != null && nodesEquals(this.sha1.getResource().asNode(), t.getObject())) {
						break;
					}
				}
				if (this.sha1 == null || !nodesEquals(this.sha1.getResource().asNode(), t.getObject())) {
					this.sha1 = new SPDXChecksum(model, t.getObject());
				}
			} catch (InvalidSPDXAnalysisException e) {
				// just use the original sha1
			}
		}
		return this.sha1.getValue();
	}

	private boolean nodesEquals(Node n1, Node n2) {
		return n1.equals(n2);
	}

	/**
	 * @param sha1 the sha1 to set
	 */
	public void setSha1(String sha1) {
		this.sha1 = new SPDXChecksum(SpdxRdfConstants.ALGORITHM_SHA1, sha1);
		if (this.model != null && this.resource != null) {
			Property p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_CHECKSUM);
			model.removeAll(this.resource, p, null);
			Resource cksumResource = this.sha1.createResource(model);
			p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_CHECKSUM);
			this.resource.addProperty(p, cksumResource);
		}
	}
	/**
	 * @return the type
	 * @throws InvalidSPDXAnalysisException
	 */
	public String getType() {
		if (this.model != null && this.resource != null) {
			try {
				Node p = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_TYPE).asNode();
				Triple m = Triple.createMatch(this.resource.asNode(), p, null);
				ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);
				while (tripleIter.hasNext()) {
					Triple t = tripleIter.next();
					if (t.getObject().isLiteral()) {
						// the following is for compatibility with previous versions of the tool which used literals for the file type
						this.type = t.getObject().toString(false);
					} else if (t.getObject().isURI()) {
						this.type = RESOURCE_TO_FILE_TYPE.get(t.getObject().getURI());
						if (this.type == null) {
							throw(new InvalidSPDXAnalysisException("Invalid URI for file type resource - must be one of the individual file types in http://spdx.org/rdf/terms"));
						}
					} else {
						throw(new InvalidSPDXAnalysisException("Invalid file type property - must be a URI type specified in http://spdx.org/rdf/terms"));
					}
				}
			}catch(InvalidSPDXAnalysisException e) {
				// just use the original
			}
		}
		return this.type;
	}
	/**
	 * @param type the type to set
	 * @throws InvalidSPDXAnalysisException
	 */
	public void setType(String type) throws InvalidSPDXAnalysisException {
		this.type = type;
		if (this.model != null && this.resource != null) {
			Resource typeResource = fileTypeStringToTypeResource(type, this.model);
			Property p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_TYPE);
			model.removeAll(this.resource, p, null);
			p = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, SpdxRdfConstants.PROP_FILE_TYPE);
			this.resource.addProperty(p, typeResource);
		}
	}

	/**
	 * Converts a string file type to an RDF resource
	 * @param fileType
	 * @return
	 * @throws InvalidSPDXAnalysisException
	 */
	public static Resource  fileTypeStringToTypeResource(String fileType, Model model) throws InvalidSPDXAnalysisException {
		String resourceUri = FILE_TYPE_TO_RESOURCE.get(fileType);
		if (resourceUri == null) {
			// not sure if we want to throw an exception here or just set to "Other"
			throw(new InvalidSPDXAnalysisException("Invalid file type: "+fileType));
