|
| 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 | +} |
0 commit comments