├── src ├── main │ ├── resources │ │ └── project.properties │ └── java │ │ └── org │ │ └── spdx │ │ └── tools │ │ ├── compare │ │ ├── package-info.java │ │ ├── FileSpdxIdSheet.java │ │ ├── FileAnnotationSheet.java │ │ ├── FileRelationshipSheet.java │ │ ├── FileCopyrightSheet.java │ │ ├── FileChecksumSheet.java │ │ ├── FileConcludedSheet.java │ │ ├── FileNoticeSheet.java │ │ ├── FileCommentSheet.java │ │ ├── FileContributorsSheet.java │ │ ├── FileLicenseCommentsSheet.java │ │ ├── FileAttributionSheet.java │ │ ├── FileTypeSheet.java │ │ ├── NormalizedFileNameComparator.java │ │ ├── FileLicenseInfoSheet.java │ │ ├── CreatorSheet.java │ │ └── VerificationSheet.java │ │ ├── schema │ │ ├── package-info.java │ │ ├── SchemaException.java │ │ └── OwlToJsonContext.java │ │ ├── SpdxConverterException.java │ │ ├── SpdxVerificationException.java │ │ ├── InvalidFileNameException.java │ │ ├── OnlineToolException.java │ │ ├── SpdxVersion.java │ │ ├── Main.java │ │ ├── MatchingStandardLicenses.java │ │ ├── RdfSchemaToXsd.java │ │ ├── RdfSchemaToJsonContext.java │ │ ├── RdfSchemaToJsonSchema.java │ │ ├── SpdxViewer.java │ │ └── GenerateVerificationCode.java └── test │ └── java │ └── org │ └── spdx │ └── tools │ ├── schema │ └── OwlToXSDTest.java │ ├── GenerateVerificationCodeTest.java │ ├── LatestSchemaVersionTest.java │ ├── SpdxConverterTestV3.java │ ├── VerifyTest.java │ └── CompareSpdxDocsTest.java ├── scripts └── tools-java-wrapper.sh ├── testResources ├── SPDXSpreadsheetExample-v2.2.xls ├── SPDXSpreadsheetExample-v2.2.xlsx ├── SPDXSpreadsheetExample-v2.3.xls ├── SPDXSpreadsheetExample-v2.3.xlsx ├── sourcefiles │ ├── package-info.java │ ├── FileSpdxIdSheet.java │ ├── FileAnnotationSheet.java │ ├── FileRelationshipSheet.java │ ├── FileCopyrightSheet.java │ ├── FileConcludedSheet.java │ ├── FileChecksumSheet.java │ ├── FileCommentSheet.java │ ├── FileNoticeSheet.java │ ├── FileLicenseCommentsSheet.java │ ├── FileContributorsSheet.java │ ├── FileAttributionSheet.java │ ├── FileTypeSheet.java │ ├── NormalizedFileNameComparator.java │ ├── FileLicenseInfoSheet.java │ ├── CreatorSheet.java │ ├── VerificationSheet.java │ └── DocumentRelationshipSheet.java ├── SPDXJsonLDExample-v3.0.1.json └── double.jsonld ├── .github ├── dependabot.yml └── workflows │ ├── docs.yml │ ├── build.yml │ └── docker_deploy.yml ├── tools-java.iml ├── resources └── log4j2.xml ├── .gitignore ├── RELEASE-CHECKLIST.md ├── dependency-check-supress.xml ├── Dockerfile ├── examples └── org │ └── spdx │ └── examples │ ├── SpdxExtensionExample.java │ ├── ExistingSpdxDocumentV2Compat.java │ └── SimpleSpdxDocumentV2Compat.java ├── CONTRIBUTING.md └── README.md /src/main/resources/project.properties: -------------------------------------------------------------------------------- 1 | version=${project.version} 2 | -------------------------------------------------------------------------------- /scripts/tools-java-wrapper.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | java -jar /usr/lib/java/spdx/tools-java-@@VERSION@@-jar-with-dependencies.jar "$@" -------------------------------------------------------------------------------- /testResources/SPDXSpreadsheetExample-v2.2.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spdx/tools-java/HEAD/testResources/SPDXSpreadsheetExample-v2.2.xls -------------------------------------------------------------------------------- /testResources/SPDXSpreadsheetExample-v2.2.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spdx/tools-java/HEAD/testResources/SPDXSpreadsheetExample-v2.2.xlsx -------------------------------------------------------------------------------- /testResources/SPDXSpreadsheetExample-v2.3.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spdx/tools-java/HEAD/testResources/SPDXSpreadsheetExample-v2.3.xls -------------------------------------------------------------------------------- /testResources/SPDXSpreadsheetExample-v2.3.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spdx/tools-java/HEAD/testResources/SPDXSpreadsheetExample-v2.3.xlsx -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "maven" 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" 7 | labels: 8 | - "dependencies" 9 | -------------------------------------------------------------------------------- /tools-java.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | pom.xml.tag 3 | pom.xml.releaseBackup 4 | pom.xml.versionsBackup 5 | pom.xml.next 6 | release.properties 7 | dependency-reduced-pom.xml 8 | buildNumber.properties 9 | .mvn/timing.properties 10 | 11 | # Avoid ignoring Maven wrapper jar file (.jar files are usually ignored) 12 | !/.mvn/wrapper/maven-wrapper.jar 13 | 14 | # Eclipse IDE files 15 | .classpath 16 | .project 17 | .settings/ 18 | 19 | # IntelliJ IDE 20 | .idea 21 | -------------------------------------------------------------------------------- /RELEASE-CHECKLIST.md: -------------------------------------------------------------------------------- 1 | # Release Checklist for the SPDX Java Tools 2 | 3 | - [ ] Check for any warnings from the compiler and findbugs 4 | - [ ] Run unit tests for all packages that depend on the application 5 | - [ ] Run dependency check to find any potential vulnerabilities `mvn dependency-check:check` 6 | - [ ] Run `mvn release:prepare` - you will be prompted for the release - typically take the defaults 7 | - [ ] Run `mvn release:perform` 8 | - [ ] Release artifacts to Maven Central 9 | - [ ] Create a Git release including release notes 10 | - [ ] Zip up the files from the Maven archive and add them to the release 11 | - [ ] Update README to refer to the new release in the Syntax section 12 | -------------------------------------------------------------------------------- /dependency-check-supress.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | ^pkg:maven/com\.ibm\.icu/icu4j@.*$ 9 | CVE-2025-5222 10 | 11 | 12 | 16 | ^pkg:maven/org\.spdx/spdx-java-model-2_X@.*$ 17 | cpe:/a:x.org:x.org 18 | 19 | -------------------------------------------------------------------------------- /testResources/sourcefiles/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | 20 | /** 21 | * Classes related to the Compare SPDX Docs utilities 22 | * 23 | * @author Gary O'Neall 24 | */ 25 | package org.spdx.tools.compare; 26 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | 20 | /** 21 | * Classes related to the Compare SPDX Docs utilities 22 | * 23 | * @author Gary O'Neall 24 | */ 25 | package org.spdx.tools.compare; 26 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # syntax=docker/dockerfile:1.4 2 | 3 | # Set Java versions 4 | ARG JAVA_VERSION=17 5 | 6 | # Use Maven eclipse Temurin based 7 | FROM maven:3.8-eclipse-temurin-$JAVA_VERSION as build 8 | 9 | ARG TOOLS_JAVA_VERSION=1.1.5-SNAPSHOT 10 | 11 | WORKDIR /build 12 | 13 | # BUILD 14 | RUN --mount=type=cache,target=/root/.m2 \ 15 | --mount=type=bind,source=$PWD,target=/build,rw \ 16 | mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install \ 17 | && mkdir -p /usr/lib/java/spdx \ 18 | && cp target/tools-java-$TOOLS_JAVA_VERSION-jar-with-dependencies.jar /usr/lib/java/spdx/ 19 | 20 | 21 | # Configure the wrapper script 22 | COPY scripts/tools-java-wrapper.sh /usr/bin/tools-java 23 | 24 | # Make the bwrapper match tools version 25 | RUN sed -i "s/@@VERSION@@/$TOOLS_JAVA_VERSION/g" /usr/bin/tools-java \ 26 | && chmod +x /usr/bin/tools-java 27 | 28 | 29 | # Deploy image 30 | FROM eclipse-temurin:$JAVA_VERSION as run 31 | 32 | COPY --from=build /usr/lib/java/spdx /usr/lib/java/spdx 33 | COPY --from=build /usr/bin/tools-java /usr/bin/tools-java 34 | 35 | ENTRYPOINT [ "/usr/bin/tools-java" ] -------------------------------------------------------------------------------- /.github/workflows/docs.yml: -------------------------------------------------------------------------------- 1 | name: Generate and publish API JavaDocs 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | 7 | jobs: 8 | docs: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Checkout repository 13 | uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 14 | with: 15 | fetch-depth: 0 16 | 17 | - name: Set up JDK 18 | uses: actions/setup-java@dded0888837ed1f317902acf8a20df0ad188d165 # v5.0.0 19 | with: 20 | distribution: 'temurin' 21 | java-version: 17 22 | 23 | - name: Cache Maven packages 24 | uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 25 | with: 26 | path: ~/.m2 27 | key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} 28 | 29 | - name: Generate docs 30 | run: mvn javadoc:javadoc 31 | 32 | - name: Deploy docs 33 | uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4.0.0 34 | with: 35 | github_token: ${{ secrets.GITHUB_TOKEN }} 36 | publish_dir: ./target/reports/apidocs 37 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/schema/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | 20 | /** 21 | * This package contains classes implementing tools for managing various schemas for the SPDX documents. 22 | * 23 | * Current schemas supported include: 24 | * 25 | * - RDF/OWL in RDF/XML format 26 | * - JSON Schema Draft 7 27 | * - JSON LD (Linked Data) 28 | */ 29 | package org.spdx.tools.schema; -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: Java CI with Maven 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v4 19 | with: 20 | fetch-depth: 0 21 | - name: Set up JDK 17 22 | uses: actions/setup-java@v4 23 | with: 24 | distribution: 'temurin' 25 | java-version: 17 26 | - name: Cache SonarCloud packages 27 | uses: actions/cache@v4 28 | with: 29 | path: ~/.sonar/cache 30 | key: ${{ runner.os }}-sonar 31 | restore-keys: ${{ runner.os }}-sonar 32 | - name: Cache Maven packages 33 | uses: actions/cache@v4 34 | with: 35 | path: ~/.m2 36 | key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} 37 | restore-keys: ${{ runner.os }}-m2 38 | - name: Build and analyze 39 | env: 40 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 41 | SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} 42 | run: | 43 | if [[ $SONAR_TOKEN != "" ]]; then 44 | mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install sonar:sonar 45 | else 46 | mvn --batch-mode --update-snapshots verify 47 | fi 48 | -------------------------------------------------------------------------------- /src/test/java/org/spdx/tools/schema/OwlToXSDTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | package org.spdx.tools.schema; 7 | 8 | import java.io.File; 9 | import java.io.FileInputStream; 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | 13 | import org.apache.jena.ontology.OntModel; 14 | import org.apache.jena.ontology.OntModelSpec; 15 | import org.apache.jena.rdf.model.ModelFactory; 16 | import org.apache.ws.commons.schema.XmlSchema; 17 | import org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException; 18 | 19 | import junit.framework.TestCase; 20 | 21 | public class OwlToXSDTest extends TestCase { 22 | 23 | static final String OWL_FILE_PATH = "testResources" + File.separator + "spdx-2-2-revision-8-ontology.owl.xml"; 24 | 25 | protected void setUp() throws Exception { 26 | super.setUp(); 27 | } 28 | 29 | protected void tearDown() throws Exception { 30 | super.tearDown(); 31 | } 32 | 33 | public void testConvertToXsd() throws IOException, XmlSchemaSerializerException, SchemaException { 34 | OwlToXsd otx = null; 35 | try (InputStream is = new FileInputStream(new File(OWL_FILE_PATH))) { 36 | OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); 37 | model.read(is, "RDF/XML"); 38 | otx = new OwlToXsd(model); 39 | } 40 | XmlSchema result = otx.convertToXsd(); 41 | 42 | assertNotNull(result); 43 | assertNotNull(result.getElementByName("Document")); 44 | String expectedIRI = "http://spdx.org/rdf/terms"; 45 | assertEquals(expectedIRI, result.getTargetNamespace()); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /examples/org/spdx/examples/SpdxExtensionExample.java: -------------------------------------------------------------------------------- 1 | package org.spdx.examples; 2 | 3 | import org.spdx.core.IModelCopyManager; 4 | import org.spdx.core.InvalidSPDXAnalysisException; 5 | import org.spdx.library.model.v3_0_1.extension.Extension; 6 | import org.spdx.storage.IModelStore; 7 | import org.spdx.storage.PropertyDescriptor; 8 | 9 | import javax.annotation.Nullable; 10 | import java.util.Optional; 11 | 12 | public class SpdxExtensionExample extends Extension { 13 | 14 | static final PropertyDescriptor EXTENSION_PROPERTY_DESCRIPTOR = new PropertyDescriptor("extensionProp", "https://my/extension/namespace/"); 15 | 16 | public SpdxExtensionExample(IModelStore modelStore, String objectUri, @Nullable IModelCopyManager copyManager, boolean create, String idPrefix) throws InvalidSPDXAnalysisException { 17 | super(modelStore, objectUri, copyManager, create, idPrefix); 18 | } 19 | 20 | public SpdxExtensionExample(IModelStore modelStore, String objectUri, @Nullable IModelCopyManager copyManager, boolean create, String specVersion, String idPrefix) throws InvalidSPDXAnalysisException { 21 | super(modelStore, objectUri, copyManager, create, idPrefix); 22 | } 23 | 24 | public SpdxExtensionExample setExtensionProperty(String value) throws InvalidSPDXAnalysisException { 25 | setPropertyValue(EXTENSION_PROPERTY_DESCRIPTOR, value); 26 | return this; 27 | } 28 | 29 | public Optional getExtensionProperty() throws InvalidSPDXAnalysisException { 30 | return getStringPropertyValue(EXTENSION_PROPERTY_DESCRIPTOR); 31 | } 32 | 33 | @Override 34 | public String getType() { 35 | return "Extension.example"; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/SpdxConverterException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools; 20 | 21 | /** 22 | * Exceptions related to the SPDX converter utility 23 | * 24 | * @author Gary O'Neall 25 | */ 26 | public class SpdxConverterException extends Exception { 27 | 28 | /** 29 | * 30 | */ 31 | private static final long serialVersionUID = 1L; 32 | 33 | /** 34 | * @param arg0 35 | */ 36 | public SpdxConverterException(String arg0) { 37 | super(arg0); 38 | } 39 | 40 | /** 41 | * @param arg0 42 | */ 43 | public SpdxConverterException(Throwable arg0) { 44 | super(arg0); 45 | } 46 | 47 | /** 48 | * @param arg0 49 | * @param arg1 50 | */ 51 | public SpdxConverterException(String arg0, Throwable arg1) { 52 | super(arg0, arg1); 53 | } 54 | 55 | /** 56 | * @param arg0 57 | * @param arg1 58 | * @param arg2 59 | * @param arg3 60 | */ 61 | public SpdxConverterException(String arg0, Throwable arg1, boolean arg2, boolean arg3) { 62 | super(arg0, arg1, arg2, arg3); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/SpdxVerificationException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools; 20 | 21 | /** 22 | * Exceptions for the SPDX Verify tools 23 | * 24 | * @author Gary O'Neall 25 | */ 26 | public class SpdxVerificationException extends Exception { 27 | 28 | /** 29 | * 30 | */ 31 | private static final long serialVersionUID = 1L; 32 | 33 | /** 34 | * 35 | */ 36 | public SpdxVerificationException() { 37 | } 38 | 39 | /** 40 | * @param arg0 41 | */ 42 | public SpdxVerificationException(String arg0) { 43 | super(arg0); 44 | } 45 | 46 | /** 47 | * @param arg0 48 | */ 49 | public SpdxVerificationException(Throwable arg0) { 50 | super(arg0); 51 | } 52 | 53 | /** 54 | * @param arg0 55 | * @param arg1 56 | */ 57 | public SpdxVerificationException(String arg0, Throwable arg1) { 58 | super(arg0, arg1); 59 | } 60 | 61 | /** 62 | * @param arg0 63 | * @param arg1 64 | * @param arg2 65 | * @param arg3 66 | */ 67 | public SpdxVerificationException(String arg0, Throwable arg1, boolean arg2, boolean arg3) { 68 | super(arg0, arg1, arg2, arg3); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/InvalidFileNameException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools; 20 | 21 | /** 22 | * Illegal parameter fro a file name 23 | * @author Gary O'Neall 24 | */ 25 | public class InvalidFileNameException extends Exception { 26 | 27 | 28 | /** 29 | * 30 | */ 31 | private static final long serialVersionUID = 1L; 32 | 33 | /** 34 | * @param message 35 | */ 36 | public InvalidFileNameException(String message) { 37 | super(message); 38 | } 39 | 40 | /** 41 | * @param cause 42 | */ 43 | public InvalidFileNameException(Throwable cause) { 44 | super(cause); 45 | } 46 | 47 | /** 48 | * @param message 49 | * @param cause 50 | */ 51 | public InvalidFileNameException(String message, Throwable cause) { 52 | super(message, cause); 53 | } 54 | 55 | /** 56 | * @param message 57 | * @param cause 58 | * @param enableSuppression 59 | * @param writableStackTrace 60 | */ 61 | public InvalidFileNameException(String message, Throwable cause, boolean enableSuppression, 62 | boolean writableStackTrace) { 63 | super(message, cause, enableSuppression, writableStackTrace); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/test/java/org/spdx/tools/GenerateVerificationCodeTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | package org.spdx.tools; 7 | 8 | import java.io.File; 9 | 10 | import org.spdx.core.DefaultModelStore; 11 | import org.spdx.core.InvalidSPDXAnalysisException; 12 | import org.spdx.core.ModelRegistry; 13 | import org.spdx.library.ModelCopyManager; 14 | import org.spdx.library.model.v2.SpdxModelInfoV2_X; 15 | import org.spdx.library.model.v2.SpdxPackageVerificationCode; 16 | import org.spdx.library.model.v3_0_1.SpdxModelInfoV3_0; 17 | import org.spdx.storage.simple.InMemSpdxStore; 18 | 19 | import junit.framework.TestCase; 20 | 21 | public class GenerateVerificationCodeTest extends TestCase { 22 | 23 | static final String TEST_DIR = "testResources"; 24 | static final String FILES = TEST_DIR + File.separator + "sourcefiles"; 25 | 26 | protected void setUp() throws Exception { 27 | super.setUp(); 28 | ModelRegistry.getModelRegistry().registerModel(new SpdxModelInfoV3_0()); 29 | ModelRegistry.getModelRegistry().registerModel(new SpdxModelInfoV2_X()); 30 | DefaultModelStore.initialize(new InMemSpdxStore(), "http://default/namespace", new ModelCopyManager()); 31 | } 32 | 33 | protected void tearDown() throws Exception { 34 | super.tearDown(); 35 | } 36 | 37 | public void testGenerateVerificationCode() throws OnlineToolException, InvalidSPDXAnalysisException { 38 | String skippedRegex = "Ver.*"; 39 | SpdxPackageVerificationCode result = GenerateVerificationCode.generateVerificationCode(FILES, skippedRegex); 40 | assertFalse(result.getValue().isEmpty()); 41 | assertEquals(1, result.getExcludedFileNames().size()); 42 | assertTrue(result.getExcludedFileNames().contains("./VerificationSheet.java")); 43 | SpdxPackageVerificationCode result2 = GenerateVerificationCode.generateVerificationCode(FILES, null); 44 | assertFalse(result.equivalent(result2)); 45 | assertEquals(0, result2.getExcludedFileNames().size()); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /.github/workflows/docker_deploy.yml: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------------------------------------- 2 | # Copyright Helio Chissini de Castro 2023. 3 | # SPDX-License-Identifier: Apache-2.0 4 | # ----------------------------------------------------------------------------- 5 | 6 | name: Docker Build 7 | 8 | on: 9 | push: 10 | paths-ignore: 11 | - "**.md" 12 | tags: 13 | - 'v*' 14 | env: 15 | REGISTRY: ghcr.io 16 | 17 | jobs: 18 | docker_push: 19 | name: Build Docker Image 20 | runs-on: ubuntu-22.04 21 | permissions: 22 | contents: read 23 | packages: write 24 | 25 | steps: 26 | - name: Checkout main repository 27 | uses: actions/checkout@v3 28 | 29 | - name: Set environment variables 30 | run: | 31 | echo "ORG_BASE_NAME=${GITHUB_REPOSITORY}" >> $GITHUB_ENV 32 | echo "TOOLS_JAVA_VERSION=${GITHUB_REF_NAME/v/}" >> $GITHUB_ENV 33 | - name: Echoing current version 34 | run: | 35 | echo "$TOOLS_JAVA_VERSION" 36 | echo $GITHUB_REF_NAME 37 | 38 | - name: Set up Docker Buildx 39 | uses: docker/setup-buildx-action@v2 40 | 41 | - name: Login to GitHub Container Registry 42 | uses: docker/login-action@v2 43 | with: 44 | registry: ${{ env.REGISTRY }} 45 | username: ${{ github.actor }} 46 | password: ${{ secrets.GITHUB_TOKEN }} 47 | 48 | - name: Extract components metadata 49 | id: meta_base 50 | uses: docker/metadata-action@v4 51 | with: 52 | images: | 53 | ${{ env.REGISTRY }}/${{ env.ORG_BASE_NAME }}/tools-java 54 | 55 | - name: Build Container 56 | uses: docker/build-push-action@v3 57 | with: 58 | context: . 59 | push: true 60 | load: false 61 | build-args: | 62 | TOOLS_JAVA_VERSION=${{ env.TOOLS_JAVA_VERSION }} 63 | tags: | 64 | ${{ steps.meta_base.outputs.tags }} 65 | labels: ${{ steps.meta_base.outputs.labels }} 66 | cache-from: type=gha,scope=tools_java 67 | cache-to: type=gha,scope=tools_java,mode=max 68 | -------------------------------------------------------------------------------- /testResources/sourcefiles/FileSpdxIdSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2015 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import org.apache.poi.ss.usermodel.Workbook; 22 | import org.spdx.library.model.SpdxFile; 23 | import org.spdx.utility.compare.SpdxCompareException; 24 | import org.spdx.utility.compare.SpdxComparer; 25 | 26 | /** 27 | * Sheet containing file SPDX ID 28 | * @author Gary O'Neall 29 | */ 30 | public class FileSpdxIdSheet extends AbstractFileCompareSheet { 31 | 32 | private static final int FILE_TYPE_COL_WIDTH = 20; 33 | 34 | public FileSpdxIdSheet(Workbook workbook, String sheetName) { 35 | super(workbook, sheetName); 36 | } 37 | static void create(Workbook wb, String sheetName) { 38 | AbstractFileCompareSheet.create(wb, sheetName, FILE_TYPE_COL_WIDTH); 39 | } 40 | 41 | /* (non-Javadoc) 42 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.model.SpdxFile, int, org.spdx.rdfparser.model.SpdxFile, int) 43 | */ 44 | @Override 45 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 46 | SpdxFile fileB, int docIndexB) throws SpdxCompareException { 47 | return fileA.getId().equals(fileB.getId()); 48 | } 49 | 50 | /* (non-Javadoc) 51 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.model.SpdxFile) 52 | */ 53 | @Override 54 | String getFileValue(SpdxFile spdxFile) { 55 | return spdxFile.getId(); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/OnlineToolException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2017 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools; 20 | 21 | /** 22 | * Default Exception thrown to the Online Tool 23 | * 24 | * @author Rohit Lodha 25 | */ 26 | public class OnlineToolException extends Exception { 27 | 28 | /** 29 | * Serial version UID 30 | */ 31 | private static final long serialVersionUID = 1L; 32 | 33 | /** 34 | * Default constructor for OnlineToolException. 35 | */ 36 | public OnlineToolException() { 37 | } 38 | 39 | /** 40 | * Constructs an OnlineToolException with the specified message. 41 | * @param arg0 42 | */ 43 | public OnlineToolException(String arg0) { 44 | super(arg0); 45 | } 46 | 47 | /** 48 | * Constructs an OnlineToolException with the specified throwable. 49 | * @param arg0 50 | */ 51 | public OnlineToolException(Throwable arg0) { 52 | super(arg0); 53 | } 54 | 55 | /** 56 | * Constructs an OnlineToolException with the specified message and throwable. 57 | * @param arg0 58 | * @param arg1 59 | */ 60 | public OnlineToolException(String arg0, Throwable arg1) { 61 | super(arg0, arg1); 62 | } 63 | 64 | /** 65 | * Constructs an OnlineToolException with the specified message, throwable, and booleans. 66 | * @param arg0 67 | * @param arg1 68 | * @param arg2 69 | * @param arg3 70 | */ 71 | public OnlineToolException(String arg0, Throwable arg1, boolean arg2, boolean arg3) { 72 | super(arg0, arg1, arg2, arg3); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/FileSpdxIdSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2015 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import org.apache.poi.ss.usermodel.Workbook; 22 | import org.spdx.library.model.v2.SpdxFile; 23 | import org.spdx.utility.compare.SpdxCompareException; 24 | import org.spdx.utility.compare.SpdxComparer; 25 | 26 | /** 27 | * Sheet comparing file SPDX IDs 28 | * @author Gary O'Neall 29 | */ 30 | public class FileSpdxIdSheet extends AbstractFileCompareSheet { 31 | 32 | private static final int FILE_TYPE_COL_WIDTH = 20; 33 | 34 | public FileSpdxIdSheet(Workbook workbook, String sheetName) { 35 | super(workbook, sheetName); 36 | } 37 | static void create(Workbook wb, String sheetName) { 38 | AbstractFileCompareSheet.create(wb, sheetName, FILE_TYPE_COL_WIDTH); 39 | } 40 | 41 | /* (non-Javadoc) 42 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.model.SpdxFile, int, org.spdx.rdfparser.model.SpdxFile, int) 43 | */ 44 | @Override 45 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 46 | SpdxFile fileB, int docIndexB) throws SpdxCompareException { 47 | return fileA.getId().equals(fileB.getId()); 48 | } 49 | 50 | /* (non-Javadoc) 51 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.model.SpdxFile) 52 | */ 53 | @Override 54 | String getFileValue(SpdxFile spdxFile) { 55 | return spdxFile.getId(); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/schema/SchemaException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.schema; 20 | 21 | /** 22 | * Exception thrown for schema-related errors 23 | * 24 | * @author Gary O'Neall 25 | */ 26 | public class SchemaException extends Exception { 27 | 28 | /** 29 | * Serial version UID for serialization 30 | */ 31 | private static final long serialVersionUID = 1L; 32 | 33 | /** 34 | * Constructs a new SchemaException with the specified detail message 35 | * 36 | * @param message the detail message 37 | */ 38 | public SchemaException(String message) { 39 | super(message); 40 | } 41 | 42 | /** 43 | * Constructs a new SchemaException with the specified detail message and 44 | * cause 45 | * 46 | * @param message the detail message 47 | * @param cause the cause 48 | */ 49 | public SchemaException(String message, Throwable cause) { 50 | super(message, cause); 51 | } 52 | 53 | /** 54 | * Constructs a new SchemaException with the specified detail message, 55 | * cause, and configurations 56 | * 57 | * @param message the detail message 58 | * @param cause the cause 59 | * @param enableSuppression whether or not suppression is enabled 60 | * @param writableStackTrace whether or not the stack trace is writable 61 | */ 62 | public SchemaException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 63 | super(message, cause, enableSuppression, writableStackTrace); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /testResources/sourcefiles/FileAnnotationSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2015 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import org.apache.poi.ss.usermodel.Workbook; 22 | import org.spdx.library.InvalidSPDXAnalysisException; 23 | import org.spdx.library.model.SpdxFile; 24 | import org.spdx.utility.compare.SpdxCompareException; 25 | import org.spdx.utility.compare.SpdxComparer; 26 | 27 | /** 28 | * Sheet comparing file annotations 29 | * @author Gary O'Neall 30 | */ 31 | public class FileAnnotationSheet extends AbstractFileCompareSheet { 32 | 33 | static final int ANNOTATION_COL_WIDTH = 80; 34 | 35 | public FileAnnotationSheet(Workbook workbook, String sheetName) { 36 | super(workbook, sheetName); 37 | } 38 | 39 | static void create(Workbook wb, String sheetName) { 40 | AbstractFileCompareSheet.create(wb, sheetName, ANNOTATION_COL_WIDTH); 41 | } 42 | 43 | /* (non-Javadoc) 44 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.model.SpdxFile, int, org.spdx.rdfparser.model.SpdxFile, int) 45 | */ 46 | @Override 47 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 48 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 49 | return CompareHelper.equivalent(fileA.getAnnotations(), fileB.getAnnotations()); 50 | } 51 | 52 | /* (non-Javadoc) 53 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.model.SpdxFile) 54 | */ 55 | @Override 56 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 57 | return CompareHelper.annotationsToString(spdxFile.getAnnotations()); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/FileAnnotationSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2015 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import org.apache.poi.ss.usermodel.Workbook; 22 | import org.spdx.core.InvalidSPDXAnalysisException; 23 | import org.spdx.library.model.v2.SpdxFile; 24 | import org.spdx.utility.compare.SpdxCompareException; 25 | import org.spdx.utility.compare.SpdxComparer; 26 | 27 | /** 28 | * Sheet comparing file annotations 29 | * @author Gary O'Neall 30 | */ 31 | public class FileAnnotationSheet extends AbstractFileCompareSheet { 32 | 33 | static final int ANNOTATION_COL_WIDTH = 80; 34 | 35 | public FileAnnotationSheet(Workbook workbook, String sheetName) { 36 | super(workbook, sheetName); 37 | } 38 | 39 | static void create(Workbook wb, String sheetName) { 40 | AbstractFileCompareSheet.create(wb, sheetName, ANNOTATION_COL_WIDTH); 41 | } 42 | 43 | /* (non-Javadoc) 44 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.model.SpdxFile, int, org.spdx.rdfparser.model.SpdxFile, int) 45 | */ 46 | @Override 47 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 48 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 49 | return CompareHelper.equivalent(fileA.getAnnotations(), fileB.getAnnotations()); 50 | } 51 | 52 | /* (non-Javadoc) 53 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.model.SpdxFile) 54 | */ 55 | @Override 56 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 57 | return CompareHelper.annotationsToString(spdxFile.getAnnotations()); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /testResources/sourcefiles/FileRelationshipSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2015 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import org.apache.poi.ss.usermodel.Workbook; 22 | import org.spdx.library.InvalidSPDXAnalysisException; 23 | import org.spdx.library.model.SpdxFile; 24 | import org.spdx.utility.compare.SpdxCompareException; 25 | import org.spdx.utility.compare.SpdxComparer; 26 | 27 | /** 28 | * Sheet comparing file relationships 29 | * @author Gary O'Neall 30 | */ 31 | public class FileRelationshipSheet extends AbstractFileCompareSheet { 32 | 33 | static final int RELATIONSHIP_COL_WIDTH = 60; 34 | 35 | public FileRelationshipSheet(Workbook workbook, String sheetName) { 36 | super(workbook, sheetName); 37 | } 38 | 39 | static void create(Workbook wb, String sheetName) { 40 | AbstractFileCompareSheet.create(wb, sheetName, RELATIONSHIP_COL_WIDTH); 41 | } 42 | 43 | /* (non-Javadoc) 44 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.model.SpdxFile, int, org.spdx.rdfparser.model.SpdxFile, int) 45 | */ 46 | @Override 47 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 48 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 49 | return SpdxComparer.collectionsEquivalent(fileA.getRelationships(), fileB.getRelationships()); 50 | } 51 | 52 | /* (non-Javadoc) 53 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.model.SpdxFile) 54 | */ 55 | @Override 56 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 57 | return CompareHelper.relationshipsToString(spdxFile.getRelationships()); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/FileRelationshipSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2015 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import org.apache.poi.ss.usermodel.Workbook; 22 | import org.spdx.core.InvalidSPDXAnalysisException; 23 | import org.spdx.library.model.v2.SpdxFile; 24 | import org.spdx.utility.compare.SpdxCompareException; 25 | import org.spdx.utility.compare.SpdxComparer; 26 | 27 | /** 28 | * Sheet comparing file relationships 29 | * @author Gary O'Neall 30 | */ 31 | public class FileRelationshipSheet extends AbstractFileCompareSheet { 32 | 33 | static final int RELATIONSHIP_COL_WIDTH = 60; 34 | 35 | public FileRelationshipSheet(Workbook workbook, String sheetName) { 36 | super(workbook, sheetName); 37 | } 38 | 39 | static void create(Workbook wb, String sheetName) { 40 | AbstractFileCompareSheet.create(wb, sheetName, RELATIONSHIP_COL_WIDTH); 41 | } 42 | 43 | /* (non-Javadoc) 44 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.model.SpdxFile, int, org.spdx.rdfparser.model.SpdxFile, int) 45 | */ 46 | @Override 47 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 48 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 49 | return SpdxComparer.collectionsEquivalent(fileA.getRelationships(), fileB.getRelationships()); 50 | } 51 | 52 | /* (non-Javadoc) 53 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.model.SpdxFile) 54 | */ 55 | @Override 56 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 57 | return CompareHelper.relationshipsToString(spdxFile.getRelationships()); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /testResources/sourcefiles/FileCopyrightSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2015 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import org.apache.poi.ss.usermodel.Workbook; 22 | import org.spdx.library.InvalidSPDXAnalysisException; 23 | import org.spdx.library.model.SpdxFile; 24 | import org.spdx.utility.compare.SpdxCompareException; 25 | import org.spdx.utility.compare.SpdxComparer; 26 | 27 | /** 28 | * Sheet comparing file copyrights 29 | * @author Gary O'Neall 30 | */ 31 | public class FileCopyrightSheet extends AbstractFileCompareSheet { 32 | 33 | static final int COPYRIGHT_COL_WIDTH = 60; 34 | 35 | public FileCopyrightSheet(Workbook workbook, String sheetName) { 36 | super(workbook, sheetName); 37 | } 38 | 39 | static void create(Workbook wb, String sheetName) { 40 | AbstractFileCompareSheet.create(wb, sheetName, COPYRIGHT_COL_WIDTH); 41 | } 42 | 43 | /* (non-Javadoc) 44 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.model.SpdxFile, int, org.spdx.rdfparser.model.SpdxFile, int) 45 | */ 46 | @Override 47 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 48 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 49 | return SpdxComparer.stringsEqual(fileA.getCopyrightText(), 50 | fileB.getCopyrightText()); 51 | } 52 | 53 | /* (non-Javadoc) 54 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.model.SpdxFile) 55 | */ 56 | @Override 57 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 58 | String retval = spdxFile.getCopyrightText(); 59 | if (retval == null) { 60 | retval = "NONE"; 61 | } 62 | return retval; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/FileCopyrightSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2015 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import org.apache.poi.ss.usermodel.Workbook; 22 | import org.spdx.core.InvalidSPDXAnalysisException; 23 | import org.spdx.library.model.v2.SpdxFile; 24 | import org.spdx.utility.compare.SpdxCompareException; 25 | import org.spdx.utility.compare.SpdxComparer; 26 | 27 | /** 28 | * Sheet comparing file copyrights 29 | * @author Gary O'Neall 30 | */ 31 | public class FileCopyrightSheet extends AbstractFileCompareSheet { 32 | 33 | static final int COPYRIGHT_COL_WIDTH = 60; 34 | 35 | public FileCopyrightSheet(Workbook workbook, String sheetName) { 36 | super(workbook, sheetName); 37 | } 38 | 39 | static void create(Workbook wb, String sheetName) { 40 | AbstractFileCompareSheet.create(wb, sheetName, COPYRIGHT_COL_WIDTH); 41 | } 42 | 43 | /* (non-Javadoc) 44 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.model.SpdxFile, int, org.spdx.rdfparser.model.SpdxFile, int) 45 | */ 46 | @Override 47 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 48 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 49 | return SpdxComparer.stringsEqual(fileA.getCopyrightText(), 50 | fileB.getCopyrightText()); 51 | } 52 | 53 | /* (non-Javadoc) 54 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.model.SpdxFile) 55 | */ 56 | @Override 57 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 58 | String retval = spdxFile.getCopyrightText(); 59 | if (retval == null) { 60 | retval = "NONE"; 61 | } 62 | return retval; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /testResources/sourcefiles/FileConcludedSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileCopyrightText: Copyright (c) 2013 Black Duck Software Inc. 4 | * SPDX-FileType: SOURCE 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * https://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | package org.spdx.tools.compare; 21 | 22 | import org.apache.poi.ss.usermodel.Workbook; 23 | import org.spdx.library.InvalidSPDXAnalysisException; 24 | import org.spdx.library.model.SpdxFile; 25 | import org.spdx.utility.compare.SpdxCompareException; 26 | import org.spdx.utility.compare.SpdxComparer; 27 | 28 | /** 29 | * @author Source Auditor 30 | */ 31 | public class FileConcludedSheet extends AbstractFileCompareSheet { 32 | 33 | private static final int LICENSE_COL_WIDTH = 40; 34 | 35 | /** 36 | * @param workbook 37 | * @param sheetName 38 | */ 39 | public FileConcludedSheet(Workbook workbook, String sheetName) { 40 | super(workbook, sheetName); 41 | } 42 | 43 | static void create(Workbook wb, String sheetName) { 44 | AbstractFileCompareSheet.create(wb, sheetName, LICENSE_COL_WIDTH); 45 | } 46 | 47 | /* (non-Javadoc) 48 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 49 | */ 50 | @Override 51 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 52 | return spdxFile.getLicenseConcluded().toString(); 53 | } 54 | 55 | /* (non-Javadoc) 56 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 57 | */ 58 | @Override 59 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 60 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 61 | return comparer.compareLicense(docIndexA, fileA.getLicenseConcluded(), docIndexB, fileB.getLicenseConcluded()); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /testResources/sourcefiles/FileChecksumSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileCopyrightText: Copyright (c) 2013 Black Duck Software Inc. 4 | * SPDX-FileType: SOURCE 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * https://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | package org.spdx.tools.compare; 21 | 22 | import java.util.Objects; 23 | 24 | import org.apache.poi.ss.usermodel.Workbook; 25 | import org.spdx.library.InvalidSPDXAnalysisException; 26 | import org.spdx.library.model.SpdxFile; 27 | import org.spdx.utility.compare.SpdxCompareException; 28 | import org.spdx.utility.compare.SpdxComparer; 29 | 30 | /** 31 | * Sheet of comparison results for file checksums 32 | * @author Gary O'Neall 33 | */ 34 | public class FileChecksumSheet extends AbstractFileCompareSheet { 35 | 36 | private static final int CHECKSUM_COL_WIDTH = 41; 37 | 38 | /** 39 | * @param workbook 40 | * @param sheetName 41 | */ 42 | public FileChecksumSheet(Workbook workbook, String sheetName) { 43 | super(workbook, sheetName); 44 | } 45 | 46 | static void create(Workbook wb, String sheetName) { 47 | AbstractFileCompareSheet.create(wb, sheetName, CHECKSUM_COL_WIDTH); 48 | } 49 | 50 | /* (non-Javadoc) 51 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 52 | */ 53 | @Override 54 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 55 | return spdxFile.getSha1(); 56 | } 57 | 58 | /* (non-Javadoc) 59 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 60 | */ 61 | @Override 62 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 63 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 64 | return Objects.equals(fileA.getSha1(), fileB.getSha1()); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/FileChecksumSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileCopyrightText: Copyright (c) 2013 Black Duck Software Inc. 4 | * SPDX-FileType: SOURCE 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * https://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | package org.spdx.tools.compare; 21 | 22 | import java.util.Objects; 23 | 24 | import org.apache.poi.ss.usermodel.Workbook; 25 | import org.spdx.core.InvalidSPDXAnalysisException; 26 | import org.spdx.library.model.v2.SpdxFile; 27 | import org.spdx.utility.compare.SpdxCompareException; 28 | import org.spdx.utility.compare.SpdxComparer; 29 | 30 | /** 31 | * Sheet of comparison results for file checksums 32 | * @author Gary O'Neall 33 | */ 34 | public class FileChecksumSheet extends AbstractFileCompareSheet { 35 | 36 | private static final int CHECKSUM_COL_WIDTH = 41; 37 | 38 | /** 39 | * @param workbook 40 | * @param sheetName 41 | */ 42 | public FileChecksumSheet(Workbook workbook, String sheetName) { 43 | super(workbook, sheetName); 44 | } 45 | 46 | static void create(Workbook wb, String sheetName) { 47 | AbstractFileCompareSheet.create(wb, sheetName, CHECKSUM_COL_WIDTH); 48 | } 49 | 50 | /* (non-Javadoc) 51 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 52 | */ 53 | @Override 54 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 55 | return spdxFile.getSha1(); 56 | } 57 | 58 | /* (non-Javadoc) 59 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 60 | */ 61 | @Override 62 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 63 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 64 | return Objects.equals(fileA.getSha1(), fileB.getSha1()); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /testResources/sourcefiles/FileCommentSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import org.apache.poi.ss.usermodel.Workbook; 22 | import org.spdx.library.InvalidSPDXAnalysisException; 23 | import org.spdx.library.model.SpdxFile; 24 | import org.spdx.utility.compare.SpdxCompareException; 25 | import org.spdx.utility.compare.SpdxComparer; 26 | 27 | /** 28 | * Sheet containing results for file comment comparison 29 | * @author Gary O'Neall 30 | */ 31 | public class FileCommentSheet extends AbstractFileCompareSheet { 32 | 33 | private static final int FILE_COMMENT_COL_WIDTH = 60; 34 | 35 | /** 36 | * @param workbook 37 | * @param sheetName 38 | */ 39 | public FileCommentSheet(Workbook workbook, String sheetName) { 40 | super(workbook, sheetName); 41 | } 42 | 43 | static void create(Workbook wb, String sheetName) { 44 | AbstractFileCompareSheet.create(wb, sheetName, FILE_COMMENT_COL_WIDTH); 45 | } 46 | 47 | /* (non-Javadoc) 48 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 49 | */ 50 | @Override 51 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 52 | if (!spdxFile.getComment().isPresent()) { 53 | return ""; 54 | } else { 55 | return spdxFile.getComment().get(); 56 | } 57 | } 58 | 59 | /* (non-Javadoc) 60 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 61 | */ 62 | @Override 63 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 64 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 65 | return SpdxComparer.stringsEqual(fileA.getComment(), fileB.getComment()); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/FileConcludedSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileCopyrightText: Copyright (c) 2013 Black Duck Software Inc. 4 | * SPDX-FileType: SOURCE 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * https://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | package org.spdx.tools.compare; 21 | 22 | import org.apache.poi.ss.usermodel.Workbook; 23 | import org.spdx.core.InvalidSPDXAnalysisException; 24 | import org.spdx.library.model.v2.SpdxFile; 25 | import org.spdx.utility.compare.SpdxCompareException; 26 | import org.spdx.utility.compare.SpdxComparer; 27 | 28 | 29 | /** 30 | * Sheet with results for file concluded license comparison results 31 | * @author Source Auditor 32 | */ 33 | public class FileConcludedSheet extends AbstractFileCompareSheet { 34 | 35 | private static final int LICENSE_COL_WIDTH = 40; 36 | 37 | /** 38 | * @param workbook 39 | * @param sheetName 40 | */ 41 | public FileConcludedSheet(Workbook workbook, String sheetName) { 42 | super(workbook, sheetName); 43 | } 44 | 45 | static void create(Workbook wb, String sheetName) { 46 | AbstractFileCompareSheet.create(wb, sheetName, LICENSE_COL_WIDTH); 47 | } 48 | 49 | /* (non-Javadoc) 50 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 51 | */ 52 | @Override 53 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 54 | return spdxFile.getLicenseConcluded().toString(); 55 | } 56 | 57 | /* (non-Javadoc) 58 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 59 | */ 60 | @Override 61 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 62 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 63 | return comparer.compareLicense(docIndexA, fileA.getLicenseConcluded(), docIndexB, fileB.getLicenseConcluded()); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /testResources/sourcefiles/FileNoticeSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import java.util.Optional; 22 | 23 | import org.apache.poi.ss.usermodel.Workbook; 24 | import org.spdx.library.InvalidSPDXAnalysisException; 25 | import org.spdx.library.model.SpdxFile; 26 | import org.spdx.utility.compare.SpdxCompareException; 27 | import org.spdx.utility.compare.SpdxComparer; 28 | 29 | /** 30 | * Sheet for file notice comparison results 31 | * @author Gary O'Neall 32 | */ 33 | public class FileNoticeSheet extends AbstractFileCompareSheet { 34 | 35 | private static final int FILE_NOTICE_COL_WIDTH = 60; 36 | 37 | /** 38 | * @param workbook 39 | * @param sheetName 40 | */ 41 | public FileNoticeSheet(Workbook workbook, String sheetName) { 42 | super(workbook, sheetName); 43 | } 44 | 45 | static void create(Workbook wb, String sheetName) { 46 | AbstractFileCompareSheet.create(wb, sheetName, FILE_NOTICE_COL_WIDTH); 47 | } 48 | 49 | /* (non-Javadoc) 50 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 51 | */ 52 | @Override 53 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 54 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 55 | return SpdxComparer.stringsEqual(fileA.getNoticeText(), fileB.getNoticeText()); 56 | } 57 | 58 | /* (non-Javadoc) 59 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 60 | */ 61 | @Override 62 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 63 | Optional retval = spdxFile.getNoticeText(); 64 | if (!retval.isPresent()) { 65 | return ""; 66 | } else { 67 | return retval.get(); 68 | } 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/FileNoticeSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import java.util.Optional; 22 | 23 | import org.apache.poi.ss.usermodel.Workbook; 24 | import org.spdx.core.InvalidSPDXAnalysisException; 25 | import org.spdx.library.model.v2.SpdxFile; 26 | import org.spdx.utility.compare.SpdxCompareException; 27 | import org.spdx.utility.compare.SpdxComparer; 28 | 29 | /** 30 | * Sheet for file notice comparison results 31 | * @author Gary O'Neall 32 | */ 33 | public class FileNoticeSheet extends AbstractFileCompareSheet { 34 | 35 | private static final int FILE_NOTICE_COL_WIDTH = 60; 36 | 37 | /** 38 | * @param workbook 39 | * @param sheetName 40 | */ 41 | public FileNoticeSheet(Workbook workbook, String sheetName) { 42 | super(workbook, sheetName); 43 | } 44 | 45 | static void create(Workbook wb, String sheetName) { 46 | AbstractFileCompareSheet.create(wb, sheetName, FILE_NOTICE_COL_WIDTH); 47 | } 48 | 49 | /* (non-Javadoc) 50 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 51 | */ 52 | @Override 53 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 54 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 55 | return SpdxComparer.stringsEqual(fileA.getNoticeText(), fileB.getNoticeText()); 56 | } 57 | 58 | /* (non-Javadoc) 59 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 60 | */ 61 | @Override 62 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 63 | Optional retval = spdxFile.getNoticeText(); 64 | if (!retval.isPresent()) { 65 | return ""; 66 | } else { 67 | return retval.get(); 68 | } 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/FileCommentSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import java.util.Optional; 22 | 23 | import org.apache.poi.ss.usermodel.Workbook; 24 | import org.spdx.core.InvalidSPDXAnalysisException; 25 | import org.spdx.library.model.v2.SpdxFile; 26 | import org.spdx.utility.compare.SpdxCompareException; 27 | import org.spdx.utility.compare.SpdxComparer; 28 | 29 | /** 30 | * Sheet containing results for file comment comparison 31 | * @author Gary O'Neall 32 | */ 33 | public class FileCommentSheet extends AbstractFileCompareSheet { 34 | 35 | private static final int FILE_COMMENT_COL_WIDTH = 60; 36 | 37 | /** 38 | * @param workbook 39 | * @param sheetName 40 | */ 41 | public FileCommentSheet(Workbook workbook, String sheetName) { 42 | super(workbook, sheetName); 43 | } 44 | 45 | static void create(Workbook wb, String sheetName) { 46 | AbstractFileCompareSheet.create(wb, sheetName, FILE_COMMENT_COL_WIDTH); 47 | } 48 | 49 | /* (non-Javadoc) 50 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 51 | */ 52 | @Override 53 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 54 | Optional comment = spdxFile.getComment(); 55 | if (!comment.isPresent()) { 56 | return ""; 57 | } else { 58 | return comment.get(); 59 | } 60 | } 61 | 62 | /* (non-Javadoc) 63 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 64 | */ 65 | @Override 66 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 67 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 68 | return SpdxComparer.stringsEqual(fileA.getComment(), fileB.getComment()); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /testResources/sourcefiles/FileLicenseCommentsSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileCopyrightText: Copyright (c) 2013 Black Duck Software Inc. 4 | * SPDX-FileType: SOURCE 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * https://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | package org.spdx.tools.compare; 21 | 22 | import org.apache.poi.ss.usermodel.Workbook; 23 | import org.spdx.library.InvalidSPDXAnalysisException; 24 | import org.spdx.library.model.SpdxFile; 25 | import org.spdx.utility.compare.SpdxCompareException; 26 | import org.spdx.utility.compare.SpdxComparer; 27 | 28 | /** 29 | * Sheet of comparison results for file license comments 30 | * @author Gary O'Neall 31 | */ 32 | public class FileLicenseCommentsSheet extends AbstractFileCompareSheet { 33 | 34 | private static final int FILE_COMMENT_COL_WIDTH = 60; 35 | 36 | 37 | /** 38 | * @param workbook 39 | * @param sheetName 40 | */ 41 | public FileLicenseCommentsSheet(Workbook workbook, String sheetName) { 42 | super(workbook, sheetName); 43 | } 44 | 45 | static void create(Workbook wb, String sheetName) { 46 | AbstractFileCompareSheet.create(wb, sheetName, FILE_COMMENT_COL_WIDTH); 47 | } 48 | 49 | /* (non-Javadoc) 50 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 51 | */ 52 | @Override 53 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 54 | if (!spdxFile.getLicenseComments().isPresent()) { 55 | return ""; 56 | } else { 57 | return spdxFile.getLicenseComments().get(); 58 | } 59 | } 60 | 61 | /* (non-Javadoc) 62 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 63 | */ 64 | @Override 65 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 66 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 67 | return SpdxComparer.stringsEqual(fileA.getLicenseComments(), fileB.getLicenseComments()); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /testResources/sourcefiles/FileContributorsSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import java.util.Collection; 22 | import java.util.Iterator; 23 | 24 | import org.apache.poi.ss.usermodel.Workbook; 25 | import org.spdx.library.model.SpdxFile; 26 | import org.spdx.utility.compare.SpdxCompareException; 27 | import org.spdx.utility.compare.SpdxComparer; 28 | 29 | /** 30 | * Sheet with results for file AttributionText comparison results 31 | * @author Gary O'Neall 32 | */ 33 | public class FileContributorsSheet extends AbstractFileCompareSheet { 34 | 35 | private static final int FILE_CONTRIBUTOR_COL_WIDTH = 50; 36 | 37 | public FileContributorsSheet(Workbook workbook, String sheetName) { 38 | super(workbook, sheetName); 39 | } 40 | 41 | static void create(Workbook wb, String sheetName) { 42 | AbstractFileCompareSheet.create(wb, sheetName, FILE_CONTRIBUTOR_COL_WIDTH); 43 | } 44 | 45 | /* (non-Javadoc) 46 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 47 | */ 48 | @Override 49 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 50 | SpdxFile fileB, int docIndexB) throws SpdxCompareException { 51 | return SpdxComparer.stringCollectionsEqual(fileA.getFileContributors(), fileB.getFileContributors()); 52 | } 53 | 54 | /* (non-Javadoc) 55 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 56 | */ 57 | @Override 58 | String getFileValue(SpdxFile spdxFile) { 59 | StringBuilder sb = new StringBuilder(); 60 | Collection contributors = spdxFile.getFileContributors(); 61 | if (contributors != null && contributors.size() > 0) { 62 | Iterator iter = contributors.iterator(); 63 | sb.append(iter.next()); 64 | while (iter.hasNext()) { 65 | sb.append(", "); 66 | sb.append(iter.next()); 67 | } 68 | } 69 | return sb.toString(); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/FileContributorsSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import java.util.Collection; 22 | import java.util.Iterator; 23 | 24 | import org.apache.poi.ss.usermodel.Workbook; 25 | import org.spdx.library.model.v2.SpdxFile; 26 | import org.spdx.utility.compare.SpdxCompareException; 27 | import org.spdx.utility.compare.SpdxComparer; 28 | 29 | /** 30 | * Sheet with results for file AttributionText comparison results 31 | * @author Gary O'Neall 32 | */ 33 | public class FileContributorsSheet extends AbstractFileCompareSheet { 34 | 35 | private static final int FILE_CONTRIBUTOR_COL_WIDTH = 50; 36 | 37 | public FileContributorsSheet(Workbook workbook, String sheetName) { 38 | super(workbook, sheetName); 39 | } 40 | 41 | static void create(Workbook wb, String sheetName) { 42 | AbstractFileCompareSheet.create(wb, sheetName, FILE_CONTRIBUTOR_COL_WIDTH); 43 | } 44 | 45 | /* (non-Javadoc) 46 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 47 | */ 48 | @Override 49 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 50 | SpdxFile fileB, int docIndexB) throws SpdxCompareException { 51 | return SpdxComparer.stringCollectionsEqual(fileA.getFileContributors(), fileB.getFileContributors()); 52 | } 53 | 54 | /* (non-Javadoc) 55 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 56 | */ 57 | @Override 58 | String getFileValue(SpdxFile spdxFile) { 59 | StringBuilder sb = new StringBuilder(); 60 | Collection contributors = spdxFile.getFileContributors(); 61 | if (contributors != null && contributors.size() > 0) { 62 | Iterator iter = contributors.iterator(); 63 | sb.append(iter.next()); 64 | while (iter.hasNext()) { 65 | sb.append(", "); 66 | sb.append(iter.next()); 67 | } 68 | } 69 | return sb.toString(); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/FileLicenseCommentsSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileCopyrightText: Copyright (c) 2013 Black Duck Software Inc. 4 | * SPDX-FileType: SOURCE 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * https://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | package org.spdx.tools.compare; 21 | 22 | import java.util.Optional; 23 | 24 | import org.apache.poi.ss.usermodel.Workbook; 25 | import org.spdx.core.InvalidSPDXAnalysisException; 26 | import org.spdx.library.model.v2.SpdxFile; 27 | import org.spdx.utility.compare.SpdxCompareException; 28 | import org.spdx.utility.compare.SpdxComparer; 29 | 30 | /** 31 | * Sheet of comparison results for file license comments 32 | * @author Gary O'Neall 33 | */ 34 | public class FileLicenseCommentsSheet extends AbstractFileCompareSheet { 35 | 36 | private static final int FILE_COMMENT_COL_WIDTH = 60; 37 | 38 | 39 | /** 40 | * @param workbook 41 | * @param sheetName 42 | */ 43 | public FileLicenseCommentsSheet(Workbook workbook, String sheetName) { 44 | super(workbook, sheetName); 45 | } 46 | 47 | static void create(Workbook wb, String sheetName) { 48 | AbstractFileCompareSheet.create(wb, sheetName, FILE_COMMENT_COL_WIDTH); 49 | } 50 | 51 | /* (non-Javadoc) 52 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 53 | */ 54 | @Override 55 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 56 | Optional licenseComments = spdxFile.getLicenseComments(); 57 | if (!licenseComments.isPresent()) { 58 | return ""; 59 | } else { 60 | return licenseComments.get(); 61 | } 62 | } 63 | 64 | /* (non-Javadoc) 65 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 66 | */ 67 | @Override 68 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 69 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 70 | return SpdxComparer.stringsEqual(fileA.getLicenseComments(), fileB.getLicenseComments()); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /testResources/sourcefiles/FileAttributionSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import java.util.Collection; 22 | import java.util.Iterator; 23 | 24 | import org.apache.poi.ss.usermodel.Workbook; 25 | import org.spdx.library.InvalidSPDXAnalysisException; 26 | import org.spdx.library.model.SpdxFile; 27 | import org.spdx.utility.compare.SpdxCompareException; 28 | import org.spdx.utility.compare.SpdxComparer; 29 | 30 | /** 31 | * Sheet with results for file contributor comparison results 32 | * @author Gary O'Neall 33 | */ 34 | public class FileAttributionSheet extends AbstractFileCompareSheet { 35 | 36 | private static final int FILE_ATTRIBUTION_COL_WIDTH = 50; 37 | 38 | public FileAttributionSheet(Workbook workbook, String sheetName) { 39 | super(workbook, sheetName); 40 | } 41 | 42 | static void create(Workbook wb, String sheetName) { 43 | AbstractFileCompareSheet.create(wb, sheetName, FILE_ATTRIBUTION_COL_WIDTH); 44 | } 45 | 46 | /* (non-Javadoc) 47 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 48 | */ 49 | @Override 50 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 51 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 52 | return SpdxComparer.stringCollectionsEqual(fileA.getAttributionText(), fileB.getAttributionText()); 53 | } 54 | 55 | /* (non-Javadoc) 56 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 57 | */ 58 | @Override 59 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 60 | StringBuilder sb = new StringBuilder(); 61 | Collection attribution = spdxFile.getAttributionText(); 62 | if (attribution != null && attribution.size() > 0) { 63 | Iterator iter = attribution.iterator(); 64 | sb.append(iter.next()); 65 | while (iter.hasNext()) { 66 | sb.append(", "); 67 | sb.append(iter.next()); 68 | } 69 | } 70 | return sb.toString(); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/FileAttributionSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import java.util.Collection; 22 | import java.util.Iterator; 23 | 24 | import org.apache.poi.ss.usermodel.Workbook; 25 | import org.spdx.core.InvalidSPDXAnalysisException; 26 | import org.spdx.library.model.v2.SpdxFile; 27 | import org.spdx.utility.compare.SpdxCompareException; 28 | import org.spdx.utility.compare.SpdxComparer; 29 | 30 | /** 31 | * Sheet with results for file contributor comparison results 32 | * @author Gary O'Neall 33 | */ 34 | public class FileAttributionSheet extends AbstractFileCompareSheet { 35 | 36 | private static final int FILE_ATTRIBUTION_COL_WIDTH = 50; 37 | 38 | public FileAttributionSheet(Workbook workbook, String sheetName) { 39 | super(workbook, sheetName); 40 | } 41 | 42 | static void create(Workbook wb, String sheetName) { 43 | AbstractFileCompareSheet.create(wb, sheetName, FILE_ATTRIBUTION_COL_WIDTH); 44 | } 45 | 46 | /* (non-Javadoc) 47 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 48 | */ 49 | @Override 50 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 51 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 52 | return SpdxComparer.stringCollectionsEqual(fileA.getAttributionText(), fileB.getAttributionText()); 53 | } 54 | 55 | /* (non-Javadoc) 56 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 57 | */ 58 | @Override 59 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 60 | StringBuilder sb = new StringBuilder(); 61 | Collection attribution = spdxFile.getAttributionText(); 62 | if (attribution != null && attribution.size() > 0) { 63 | Iterator iter = attribution.iterator(); 64 | sb.append(iter.next()); 65 | while (iter.hasNext()) { 66 | sb.append(", "); 67 | sb.append(iter.next()); 68 | } 69 | } 70 | return sb.toString(); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/test/java/org/spdx/tools/LatestSchemaVersionTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileContributor: Arthit Suriyawongkul 3 | * SPDX-FileCopyrightText: 2025 SPDX contributors 4 | * SPDX-FileType: SOURCE 5 | * SPDX-License-Identifier: Apache-2.0 6 | */ 7 | package org.spdx.tools; 8 | 9 | import java.io.IOException; 10 | import java.net.URI; 11 | import java.nio.file.Files; 12 | import java.nio.file.Path; 13 | import java.nio.file.Paths; 14 | import java.util.Comparator; 15 | import java.util.Optional; 16 | 17 | import org.apache.commons.io.IOUtils; 18 | 19 | import junit.framework.TestCase; 20 | 21 | public class LatestSchemaVersionTest extends TestCase { 22 | 23 | private String VERSION_REGEX = "spdx-schema-v(\\d+\\.\\d+(\\.\\d+)?)\\.json"; 24 | 25 | public void testLatestSpdxSchemaVersionIsUpToDate() throws IOException { 26 | // Step 1: Find the latest JSON schema file in the resources directory 27 | Path resourcesDir = Paths.get("resources"); 28 | Optional latestSchemaFile = Files.list(resourcesDir) 29 | .filter(path -> path.getFileName().toString().matches(VERSION_REGEX)) 30 | .max(Comparator.comparing(path -> parseVersion(path.getFileName().toString()), 31 | versionComparator)); 32 | assertTrue("No SPDX schema file found in resources directory.", 33 | latestSchemaFile.isPresent()); 34 | 35 | Path schemaFilePath = latestSchemaFile.get(); 36 | String fileName = schemaFilePath.getFileName().toString(); 37 | String version = extractVersionNumber(fileName); 38 | 39 | // Step 2: Compare the content of the file with the content from the URL 40 | String localSchemaContent = Files.readString(schemaFilePath).replaceAll("\\s+", " "); 41 | String remoteSchemaUrl = "https://spdx.org/schema/" + version + "/spdx-json-schema.json"; 42 | String remoteSchemaContent = IOUtils.toString(URI.create(remoteSchemaUrl).toURL(), "UTF-8").replaceAll("\\s+", " "); 43 | assertEquals("The local SPDX schema file does not match the remote schema content.", 44 | localSchemaContent.trim(), remoteSchemaContent.trim()); 45 | } 46 | 47 | private String extractVersionNumber(String fileName) { 48 | return fileName.replaceAll(VERSION_REGEX, "$1"); 49 | } 50 | 51 | private int[] parseVersion(String fileName) { 52 | String version = extractVersionNumber(fileName); 53 | String[] parts = version.split("\\."); 54 | int[] versionNumbers = new int[3]; // [major, minor, patch] 55 | for (int i = 0; i < parts.length; i++) { 56 | versionNumbers[i] = Integer.parseInt(parts[i]); 57 | } 58 | return versionNumbers; 59 | } 60 | 61 | private Comparator versionComparator = (v1, v2) -> { 62 | for (int i = 0; i < 3; i++) { 63 | int comparison = Integer.compare(v1[i], v2[i]); 64 | if (comparison != 0) { 65 | return comparison; 66 | } 67 | } 68 | return 0; 69 | }; 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/SpdxVersion.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools; 20 | 21 | import java.io.IOException; 22 | import java.util.Comparator; 23 | import java.util.Properties; 24 | 25 | import org.spdx.library.ListedLicenses; 26 | import org.spdx.library.SpdxModelFactory; 27 | 28 | /** 29 | * Static helper methods for tools and library version information 30 | * 31 | * @author Hirumal Priyashan 32 | */ 33 | public class SpdxVersion { 34 | 35 | static class SpdxVersionComparer implements Comparator { 36 | 37 | /** 38 | * @param version version to normalize - may be an SPDX 2.X style or a 3.X SemVer style 39 | * @return version normalized to SemVer 40 | */ 41 | private String normalizeVersion(String version) { 42 | if (version.startsWith("SPDX-")) { 43 | return version.substring("SPDX-".length()); 44 | } else { 45 | return version; 46 | } 47 | } 48 | 49 | @Override 50 | public int compare(String versionA, String versionB) { 51 | return normalizeVersion(versionA).compareTo(normalizeVersion(versionB)); 52 | } 53 | 54 | 55 | } 56 | 57 | /** 58 | * Getter for the current tool Version 59 | * 60 | * @return Tool version 61 | */ 62 | public static String getCurrentToolVersion() { 63 | final Properties properties = new Properties(); 64 | try { 65 | properties.load(SpdxVersion.class.getClassLoader().getResourceAsStream("project.properties")); 66 | return properties.getProperty("version"); 67 | } catch (IOException e) { 68 | return "Unknown tool version"; 69 | } 70 | } 71 | 72 | /** 73 | * Getter for the library specification version 74 | * 75 | * @return The library specification version 76 | */ 77 | public static String getLibraryVersion() { 78 | return SpdxModelFactory.IMPLEMENTATION_VERSION; 79 | } 80 | 81 | /** 82 | * @return the latest spec version supported 83 | */ 84 | public static String getLatestSpecVersion() { 85 | return SpdxModelFactory.getLatestSpecVersion(); 86 | } 87 | 88 | /** 89 | * Getter for the license list version 90 | * 91 | * @return The license list version 92 | */ 93 | public static String getLicenseListVersion() { 94 | return ListedLicenses.getListedLicenses().getLicenseListVersion(); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /testResources/sourcefiles/FileTypeSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileCopyrightText: Copyright (c) 2013 Black Duck Software Inc. 4 | * SPDX-FileType: SOURCE 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * https://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | package org.spdx.tools.compare; 21 | 22 | import java.util.Arrays; 23 | 24 | import org.apache.poi.ss.usermodel.Workbook; 25 | import org.spdx.library.InvalidSPDXAnalysisException; 26 | import org.spdx.library.model.SpdxFile; 27 | import org.spdx.library.model.enumerations.FileType; 28 | import org.spdx.utility.compare.SpdxCompareException; 29 | import org.spdx.utility.compare.SpdxComparer; 30 | 31 | /** 32 | * Sheet containing File Type 33 | * @author Gary O'Neall 34 | */ 35 | public class FileTypeSheet extends AbstractFileCompareSheet { 36 | 37 | private static final int FILE_TYPE_COL_WIDTH = 20; 38 | 39 | /** 40 | * @param workbook 41 | * @param sheetName 42 | */ 43 | public FileTypeSheet(Workbook workbook, String sheetName) { 44 | super(workbook, sheetName); 45 | } 46 | 47 | static void create(Workbook wb, String sheetName) { 48 | AbstractFileCompareSheet.create(wb, sheetName, FILE_TYPE_COL_WIDTH); 49 | } 50 | 51 | /* (non-Javadoc) 52 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 53 | */ 54 | @Override 55 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 56 | if (spdxFile.getFileTypes() == null ||spdxFile.getFileTypes().size() == 0) { 57 | return ""; 58 | } else { 59 | FileType[] fileTypes = spdxFile.getFileTypes().toArray(new FileType[spdxFile.getFileTypes().size()]); 60 | String[] sFileTypes = new String[fileTypes.length]; 61 | for (int i = 0; i < fileTypes.length; i++) { 62 | sFileTypes[i] = fileTypes[i].toString(); 63 | } 64 | Arrays.sort(sFileTypes); 65 | StringBuilder sb = new StringBuilder(sFileTypes[0]); 66 | for (int i = 1; i < sFileTypes.length; i++) { 67 | sb.append(", "); 68 | sb.append(sFileTypes[i]); 69 | } 70 | return sb.toString(); 71 | } 72 | } 73 | 74 | /* (non-Javadoc) 75 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 76 | */ 77 | @Override 78 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 79 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 80 | return SpdxComparer.stringsEqual(getFileValue(fileA), getFileValue(fileB)); 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/Main.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools; 20 | 21 | import org.apache.commons.lang3.ArrayUtils; 22 | import org.spdx.library.SpdxModelFactory; 23 | 24 | /** 25 | * Dispatch individual tools 26 | * 27 | * @author Gary O'Neall 28 | */ 29 | public class Main { 30 | 31 | /** 32 | * @param args args[0] is the name of the tools with the remaining args being the tool parameters 33 | */ 34 | public static void main(String[] args) { 35 | if (args.length < 1) { 36 | usage(); 37 | return; 38 | } 39 | SpdxModelFactory.init(); 40 | String spdxTool = args[0]; 41 | args = ArrayUtils.removeElement(args, args[0]); 42 | if ("Convert".equals(spdxTool)) { 43 | SpdxConverter.main(args); 44 | } else if ("SPDXViewer".equals(spdxTool)) { 45 | SpdxViewer.main(args); 46 | } else if ("Verify".equals(spdxTool)) { 47 | Verify.main(args); 48 | } else if ("CompareDocs".equals(spdxTool)) { 49 | CompareSpdxDocs.main(args); 50 | } else if ("GenerateVerificationCode".equals(spdxTool)) { 51 | GenerateVerificationCode.main(args); 52 | } else if ("Version".equals(spdxTool)) { 53 | System.out.println("SPDX Tool Version: " + SpdxVersion.getCurrentToolVersion() + 54 | "; Specification Version: " + SpdxVersion.getLibraryVersion() + 55 | "; License List Version: " + SpdxVersion.getLicenseListVersion()); 56 | } else if ("MatchingStandardLicenses".equals(spdxTool)) { 57 | MatchingStandardLicenses.main(args); 58 | } else { 59 | usage(); 60 | } 61 | } 62 | 63 | private static void usage() { 64 | System.out.println("" 65 | + "Usage: java -jar spdx-tools-jar-with-dependencies.jar \n" 66 | + "function parameter example \n" 67 | + "------------------------------------------------------------------------------------------------------------------- \n" 68 | + "Convert inputFile outputFile [fromType] [toType] Examples/SPDXTagExample.tag TagToSpreadsheet.xls \n" 69 | + "SPDXViewer inputFile TestFiles/SPDXRdfExample.rdf \n" 70 | + "Verify inputFile [type] TestFiles/SPDXRdfExample.rdf \n" 71 | + "CompareDocs output.xlsx doc1 doc2 ... docN \n" 72 | + "GenerateVerificationCode sourceDirectory\n" 73 | + "Version\n" 74 | + "MatchingStandardLicenses licenseTextFile"); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/FileTypeSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileCopyrightText: Copyright (c) 2013 Black Duck Software Inc. 4 | * SPDX-FileType: SOURCE 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * https://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | package org.spdx.tools.compare; 21 | 22 | import java.util.Arrays; 23 | 24 | import org.apache.poi.ss.usermodel.Workbook; 25 | import org.spdx.core.InvalidSPDXAnalysisException; 26 | import org.spdx.library.model.v2.SpdxFile; 27 | import org.spdx.library.model.v2.enumerations.FileType; 28 | import org.spdx.utility.compare.SpdxCompareException; 29 | import org.spdx.utility.compare.SpdxComparer; 30 | 31 | /** 32 | * Sheet containing File Type 33 | * @author Gary O'Neall 34 | */ 35 | public class FileTypeSheet extends AbstractFileCompareSheet { 36 | 37 | private static final int FILE_TYPE_COL_WIDTH = 20; 38 | 39 | /** 40 | * @param workbook 41 | * @param sheetName 42 | */ 43 | public FileTypeSheet(Workbook workbook, String sheetName) { 44 | super(workbook, sheetName); 45 | } 46 | 47 | static void create(Workbook wb, String sheetName) { 48 | AbstractFileCompareSheet.create(wb, sheetName, FILE_TYPE_COL_WIDTH); 49 | } 50 | 51 | /* (non-Javadoc) 52 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 53 | */ 54 | @Override 55 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 56 | if (spdxFile.getFileTypes() == null ||spdxFile.getFileTypes().size() == 0) { 57 | return ""; 58 | } else { 59 | FileType[] fileTypes = spdxFile.getFileTypes().toArray(new FileType[spdxFile.getFileTypes().size()]); 60 | String[] sFileTypes = new String[fileTypes.length]; 61 | for (int i = 0; i < fileTypes.length; i++) { 62 | sFileTypes[i] = fileTypes[i].toString(); 63 | } 64 | Arrays.sort(sFileTypes); 65 | StringBuilder sb = new StringBuilder(sFileTypes[0]); 66 | for (int i = 1; i < sFileTypes.length; i++) { 67 | sb.append(", "); 68 | sb.append(sFileTypes[i]); 69 | } 70 | return sb.toString(); 71 | } 72 | } 73 | 74 | /* (non-Javadoc) 75 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.compare.SpdxComparer, org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 76 | */ 77 | @Override 78 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, 79 | SpdxFile fileB, int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 80 | return SpdxComparer.stringsEqual(getFileValue(fileA), getFileValue(fileB)); 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /testResources/sourcefiles/NormalizedFileNameComparator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import java.io.Serializable; 22 | import java.util.Comparator; 23 | import java.util.Optional; 24 | 25 | /** 26 | * Compares to file name strings normalizing them to a common format using the following rules: 27 | * - File separator character is "/" 28 | * - Must begin with "./" 29 | * @author Gary O'Neall 30 | */ 31 | public class NormalizedFileNameComparator implements Comparator>, Serializable { 32 | 33 | /** 34 | * 35 | */ 36 | private static final long serialVersionUID = 1L; 37 | static final char DOS_SEPARATOR = '\\'; 38 | static final char UNIX_SEPARATOR = '/'; 39 | static final String RELATIVE_DIR = "./"; 40 | /* (non-Javadoc) 41 | * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 42 | */ 43 | @Override 44 | public int compare(Optional fileName1, Optional fileName2) { 45 | String normalizedFileName1 = normalizeFileName(fileName1); 46 | String normalizedFileName2 = normalizeFileName(fileName2); 47 | return normalizedFileName1.compareTo(normalizedFileName2); 48 | } 49 | 50 | /** 51 | * Returns true if fileName2 matches fileName1 except for leading file name directories 52 | * @param fileName1 53 | * @param fileName2 54 | * @return true if fileName2 matches fileName1 except for leading file name directories 55 | */ 56 | public static boolean hasLeadingDir(String fileName1, String fileName2) { 57 | String compareName1 = fileName1; 58 | String compareName2 = fileName2; 59 | if (compareName1.startsWith(RELATIVE_DIR)) { 60 | compareName1 = compareName1.substring(RELATIVE_DIR.length()); 61 | } 62 | if (compareName2.startsWith(RELATIVE_DIR)) { 63 | compareName2 = compareName2.substring(RELATIVE_DIR.length()); 64 | } 65 | if (compareName1.length() <= compareName2.length()) { 66 | return false; 67 | } 68 | if (!compareName1.endsWith(compareName2)) { 69 | return false; 70 | } 71 | char schar = compareName1.charAt(compareName1.length()-compareName2.length()-1); 72 | return (schar == UNIX_SEPARATOR); 73 | } 74 | 75 | public static String normalizeFileName(Optional fileName) { 76 | if (!fileName.isPresent()) { 77 | return "[NO_NAME]"; 78 | } 79 | String retval = fileName.get().replace(DOS_SEPARATOR, UNIX_SEPARATOR); 80 | if (!retval.startsWith(RELATIVE_DIR)) { 81 | retval = RELATIVE_DIR + retval; 82 | } 83 | return retval; 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/NormalizedFileNameComparator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import java.io.Serializable; 22 | import java.util.Comparator; 23 | import java.util.Optional; 24 | 25 | /** 26 | * Compares to file name strings normalizing them to a common format using the following rules: 27 | * - File separator character is "/" 28 | * - Must begin with "./" 29 | * @author Gary O'Neall 30 | */ 31 | public class NormalizedFileNameComparator implements Comparator>, Serializable { 32 | 33 | /** 34 | * 35 | */ 36 | private static final long serialVersionUID = 1L; 37 | static final char DOS_SEPARATOR = '\\'; 38 | static final char UNIX_SEPARATOR = '/'; 39 | static final String RELATIVE_DIR = "./"; 40 | 41 | /* (non-Javadoc) 42 | * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 43 | */ 44 | @Override 45 | public int compare(Optional fileName1, Optional fileName2) { 46 | String normalizedFileName1 = normalizeFileName(fileName1); 47 | String normalizedFileName2 = normalizeFileName(fileName2); 48 | return normalizedFileName1.compareTo(normalizedFileName2); 49 | } 50 | 51 | /** 52 | * Returns true if fileName2 matches fileName1 except for leading file name directories 53 | * @param fileName1 54 | * @param fileName2 55 | * @return true if fileName2 matches fileName1 except for leading file name directories 56 | */ 57 | public static boolean hasLeadingDir(String fileName1, String fileName2) { 58 | String compareName1 = fileName1; 59 | String compareName2 = fileName2; 60 | if (compareName1.startsWith(RELATIVE_DIR)) { 61 | compareName1 = compareName1.substring(RELATIVE_DIR.length()); 62 | } 63 | if (compareName2.startsWith(RELATIVE_DIR)) { 64 | compareName2 = compareName2.substring(RELATIVE_DIR.length()); 65 | } 66 | if (compareName1.length() <= compareName2.length()) { 67 | return false; 68 | } 69 | if (!compareName1.endsWith(compareName2)) { 70 | return false; 71 | } 72 | char schar = compareName1.charAt(compareName1.length()-compareName2.length()-1); 73 | return (schar == UNIX_SEPARATOR); 74 | } 75 | 76 | public static String normalizeFileName(Optional fileName) { 77 | if (!fileName.isPresent()) { 78 | return "[NO_NAME]"; 79 | } 80 | String retval = fileName.get().replace(DOS_SEPARATOR, UNIX_SEPARATOR); 81 | if (!retval.startsWith(RELATIVE_DIR)) { 82 | retval = RELATIVE_DIR + retval; 83 | } 84 | return retval; 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /testResources/sourcefiles/FileLicenseInfoSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileCopyrightText: Copyright (c) 2013 Black Duck Software Inc. 4 | * SPDX-FileType: SOURCE 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * https://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | package org.spdx.tools.compare; 21 | 22 | import java.util.Collection; 23 | import java.util.Iterator; 24 | 25 | import org.apache.poi.ss.usermodel.Workbook; 26 | import org.spdx.library.InvalidSPDXAnalysisException; 27 | import org.spdx.library.model.SpdxFile; 28 | import org.spdx.library.model.license.AnyLicenseInfo; 29 | import org.spdx.utility.compare.SpdxCompareException; 30 | import org.spdx.utility.compare.SpdxComparer; 31 | 32 | /** 33 | * Sheet of the comparison results for the file seen licenses 34 | * @author Gary O'Neall 35 | */ 36 | public class FileLicenseInfoSheet extends AbstractFileCompareSheet { 37 | 38 | private static final int LICENSE_COL_WIDTH = 60; 39 | 40 | /** 41 | * @param workbook 42 | * @param sheetName 43 | */ 44 | public FileLicenseInfoSheet(Workbook workbook, String sheetName) { 45 | super(workbook, sheetName); 46 | } 47 | static void create(Workbook wb, String sheetName) { 48 | AbstractFileCompareSheet.create(wb, sheetName, LICENSE_COL_WIDTH); 49 | } 50 | 51 | /* (non-Javadoc) 52 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 53 | */ 54 | @Override 55 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 56 | if (spdxFile.getLicenseInfoFromFiles() == null || spdxFile.getLicenseInfoFromFiles().size() == 0) { 57 | return ""; 58 | } 59 | 60 | Iterator iter = spdxFile.getLicenseInfoFromFiles().iterator(); 61 | StringBuilder sb = new StringBuilder(iter.next().toString()); 62 | while (iter.hasNext()) { 63 | sb.append(", "); 64 | sb.append(iter.next().toString()); 65 | } 66 | return sb.toString(); 67 | } 68 | /* (non-Javadoc) 69 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 70 | */ 71 | @Override 72 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, SpdxFile fileB, 73 | int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 74 | Collection licenseInfosA = fileA.getLicenseInfoFromFiles(); 75 | Collection licenseInfosB = fileB.getLicenseInfoFromFiles(); 76 | if (licenseInfosA.size() != licenseInfosB.size()) { 77 | return false; 78 | } 79 | for (AnyLicenseInfo licA:licenseInfosA) { 80 | boolean found = false; 81 | for (AnyLicenseInfo licB:licenseInfosB) { 82 | if (comparer.compareLicense(docIndexA, licA, docIndexB, licB)) { 83 | found = true; 84 | break; 85 | } 86 | } 87 | if (!found) { 88 | return false; 89 | } 90 | } 91 | return true; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/FileLicenseInfoSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileCopyrightText: Copyright (c) 2013 Black Duck Software Inc. 4 | * SPDX-FileType: SOURCE 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * https://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | package org.spdx.tools.compare; 21 | 22 | import java.util.Collection; 23 | import java.util.Iterator; 24 | 25 | import org.apache.poi.ss.usermodel.Workbook; 26 | import org.spdx.core.InvalidSPDXAnalysisException; 27 | import org.spdx.library.model.v2.SpdxFile; 28 | import org.spdx.library.model.v2.license.AnyLicenseInfo; 29 | import org.spdx.utility.compare.SpdxCompareException; 30 | import org.spdx.utility.compare.SpdxComparer; 31 | 32 | /** 33 | * Sheet of the comparison results for the file seen licenses 34 | * @author Gary O'Neall 35 | */ 36 | public class FileLicenseInfoSheet extends AbstractFileCompareSheet { 37 | 38 | private static final int LICENSE_COL_WIDTH = 60; 39 | 40 | /** 41 | * @param workbook 42 | * @param sheetName 43 | */ 44 | public FileLicenseInfoSheet(Workbook workbook, String sheetName) { 45 | super(workbook, sheetName); 46 | } 47 | static void create(Workbook wb, String sheetName) { 48 | AbstractFileCompareSheet.create(wb, sheetName, LICENSE_COL_WIDTH); 49 | } 50 | 51 | /* (non-Javadoc) 52 | * @see org.spdx.compare.AbstractFileCompareSheet#getFileValue(org.spdx.rdfparser.SpdxFile) 53 | */ 54 | @Override 55 | String getFileValue(SpdxFile spdxFile) throws InvalidSPDXAnalysisException { 56 | if (spdxFile.getLicenseInfoFromFiles() == null || spdxFile.getLicenseInfoFromFiles().size() == 0) { 57 | return ""; 58 | } 59 | 60 | Iterator iter = spdxFile.getLicenseInfoFromFiles().iterator(); 61 | StringBuilder sb = new StringBuilder(iter.next().toString()); 62 | while (iter.hasNext()) { 63 | sb.append(", "); 64 | sb.append(iter.next().toString()); 65 | } 66 | return sb.toString(); 67 | } 68 | /* (non-Javadoc) 69 | * @see org.spdx.compare.AbstractFileCompareSheet#valuesMatch(org.spdx.rdfparser.SpdxFile, int, org.spdx.rdfparser.SpdxFile, int) 70 | */ 71 | @Override 72 | boolean valuesMatch(SpdxComparer comparer, SpdxFile fileA, int docIndexA, SpdxFile fileB, 73 | int docIndexB) throws SpdxCompareException, InvalidSPDXAnalysisException { 74 | Collection licenseInfosA = fileA.getLicenseInfoFromFiles(); 75 | Collection licenseInfosB = fileB.getLicenseInfoFromFiles(); 76 | if (licenseInfosA.size() != licenseInfosB.size()) { 77 | return false; 78 | } 79 | for (AnyLicenseInfo licA:licenseInfosA) { 80 | boolean found = false; 81 | for (AnyLicenseInfo licB:licenseInfosB) { 82 | if (comparer.compareLicense(docIndexA, licA, docIndexB, licB)) { 83 | found = true; 84 | break; 85 | } 86 | } 87 | if (!found) { 88 | return false; 89 | } 90 | } 91 | return true; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /testResources/SPDXJsonLDExample-v3.0.1.json: -------------------------------------------------------------------------------- 1 | { 2 | "@context": "https://spdx.org/rdf/3.0.1/spdx-context.jsonld", 3 | "@graph": [ 4 | { 5 | "type": "CreationInfo", 6 | "@id": "_:creationinfo", 7 | "createdBy": [ 8 | "http://spdx.example.com/Agent/JoshuaWatt" 9 | ], 10 | "specVersion": "3.0.1", 11 | "created": "2024-03-06T00:00:00Z" 12 | }, 13 | { 14 | "type": "Person", 15 | "spdxId": "http://spdx.example.com/Agent/JoshuaWatt", 16 | "name": "Joshua Watt", 17 | "creationInfo": "_:creationinfo", 18 | "externalIdentifier": [ 19 | { 20 | "type": "ExternalIdentifier", 21 | "externalIdentifierType": "email", 22 | "identifier": "JPEWhacker@gmail.com" 23 | } 24 | ] 25 | }, 26 | { 27 | "type": "SpdxDocument", 28 | "spdxId": "http://spdx.example.com/Document1", 29 | "creationInfo": "_:creationinfo", 30 | "rootElement": [ 31 | "http://spdx.example.com/BOM1" 32 | ], 33 | "element": [ 34 | "http://spdx.example.com/BOM1", 35 | "http://spdx.example.com/Agent/JoshuaWatt", 36 | "http://spdx.example.com/Package1/myprogram", 37 | "http://spdx.example.com/Relationship/1" 38 | ], 39 | "profileConformance": [ 40 | "core", 41 | "software" 42 | ] 43 | }, 44 | { 45 | "type": "software_Sbom", 46 | "spdxId": "http://spdx.example.com/BOM1", 47 | "creationInfo": "_:creationinfo", 48 | "rootElement": [ 49 | "http://spdx.example.com/Package1" 50 | ], 51 | "element": [ 52 | "http://spdx.example.com/Package1/myprogram", 53 | "http://spdx.example.com/Package1" 54 | ], 55 | "software_sbomType": [ 56 | "build" 57 | ] 58 | }, 59 | { 60 | "type": "software_Package", 61 | "spdxId": "http://spdx.example.com/Package1", 62 | "creationInfo": "_:creationinfo", 63 | "name": "my-package", 64 | "software_packageVersion": "1.0", 65 | "software_downloadLocation": "http://dl.example.com/my-package_1.0.0.tar", 66 | "builtTime": "2024-03-06T00:00:00Z", 67 | "originatedBy": [ 68 | "http://spdx.example.com/Agent/JoshuaWatt" 69 | ] 70 | }, 71 | { 72 | "type": "software_File", 73 | "spdxId": "http://spdx.example.com/Package1/myprogram", 74 | "creationInfo": "_:creationinfo", 75 | "name": "myprogram", 76 | "software_primaryPurpose": "executable", 77 | "software_additionalPurpose": [ 78 | "application" 79 | ], 80 | "software_copyrightText": "Copyright 2024, Joshua Watt", 81 | "builtTime": "2024-03-06T00:00:00Z", 82 | "originatedBy": [ 83 | "http://spdx.example.com/Agent/JoshuaWatt" 84 | ] 85 | }, 86 | { 87 | "type": "Relationship", 88 | "spdxId": "http://spdx.example.com/Relationship/1", 89 | "creationInfo": "_:creationinfo", 90 | "from": "http://spdx.example.com/Package1", 91 | "relationshipType": "contains", 92 | "to": [ 93 | "http://spdx.example.com/Package1/myprogram" 94 | ], 95 | "completeness": "complete" 96 | } 97 | ] 98 | } 99 | -------------------------------------------------------------------------------- /testResources/sourcefiles/CreatorSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileCopyrightText: Copyright (c) 2013 Black Duck Software Inc. 4 | * SPDX-FileType: SOURCE 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * https://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | package org.spdx.tools.compare; 21 | 22 | import java.util.Arrays; 23 | import java.util.List; 24 | 25 | import org.apache.poi.ss.usermodel.Cell; 26 | import org.apache.poi.ss.usermodel.CellStyle; 27 | import org.apache.poi.ss.usermodel.Row; 28 | import org.apache.poi.ss.usermodel.Sheet; 29 | import org.apache.poi.ss.usermodel.Workbook; 30 | import org.spdx.library.InvalidSPDXAnalysisException; 31 | import org.spdx.utility.compare.SpdxCompareException; 32 | import org.spdx.utility.compare.SpdxComparer; 33 | 34 | /** 35 | * Worksheet containing creator level information 36 | * Column 1 describes if the creator is the same or different 37 | * Columns 2 through N are for creators in each of the documents 38 | * @author Gary O'Neall 39 | */ 40 | public class CreatorSheet extends AbstractSheet { 41 | private static final int COL_WIDTH = 50; 42 | /** 43 | * @param workbook 44 | * @param sheetName 45 | */ 46 | public CreatorSheet(Workbook workbook, String sheetName) { 47 | super(workbook, sheetName); 48 | } 49 | 50 | /* (non-Javadoc) 51 | * @see org.spdx.spdxspreadsheet.AbstractSheet#verify() 52 | */ 53 | @Override 54 | public String verify() { 55 | // Nothing to verify 56 | return null; 57 | } 58 | 59 | /** 60 | * @param wb 61 | * @param sheetName 62 | */ 63 | public static void create(Workbook wb, String sheetName) { 64 | int sheetNum = wb.getSheetIndex(sheetName); 65 | if (sheetNum >= 0) { 66 | wb.removeSheetAt(sheetNum); 67 | } 68 | Sheet sheet = wb.createSheet(sheetName); 69 | CellStyle headerStyle = AbstractSheet.createHeaderStyle(wb); 70 | CellStyle defaultStyle = AbstractSheet.createLeftWrapStyle(wb); 71 | Row row = sheet.createRow(0); 72 | for (int i = 0; i < MultiDocumentSpreadsheet.MAX_DOCUMENTS; i++) { 73 | sheet.setColumnWidth(i, COL_WIDTH*256); 74 | sheet.setDefaultColumnStyle(i, defaultStyle); 75 | Cell cell = row.createCell(i); 76 | cell.setCellStyle(headerStyle); 77 | } 78 | } 79 | 80 | /** 81 | * @param comparer 82 | * @param docNames 83 | * @throws InvalidSPDXAnalysisException 84 | */ 85 | public void importCompareResults(SpdxComparer comparer, List docNames) throws SpdxCompareException, InvalidSPDXAnalysisException { 86 | if (comparer.getNumSpdxDocs() != docNames.size()) { 87 | throw(new SpdxCompareException("Number of document names does not match the number of SPDX documents")); 88 | } 89 | this.clear(); 90 | Row header = sheet.getRow(0); 91 | for (int i = 0; i < comparer.getNumSpdxDocs(); i++) { 92 | Cell headerCell = header.getCell(i); 93 | headerCell.setCellValue(docNames.get(i)); 94 | String[] creators = comparer.getSpdxDoc(i).getCreationInfo().getCreators().toArray(new String[comparer.getSpdxDoc(i).getCreationInfo().getCreators().size()]); 95 | Arrays.sort(creators); 96 | for (int j = 0; j < creators.length; j++) { 97 | Cell creatorCell = null; 98 | while (j+1 > this.getNumDataRows()) { 99 | this.addRow(); 100 | } 101 | creatorCell = sheet.getRow(j+1).createCell(i); 102 | creatorCell.setCellValue(creators[j]); 103 | } 104 | } 105 | } 106 | 107 | } -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/MatchingStandardLicenses.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2014 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools; 20 | 21 | import java.io.File; 22 | import java.io.IOException; 23 | import java.nio.charset.Charset; 24 | import java.nio.file.Files; 25 | 26 | import org.spdx.core.InvalidSPDXAnalysisException; 27 | import org.spdx.utility.compare.LicenseCompareHelper; 28 | import org.spdx.utility.compare.SpdxCompareException; 29 | 30 | /** 31 | * Tool to compare a license text to standard licenses. Lists all standard 32 | * license ID's that are equivalent using the SPDX Legal team's license matching 33 | * guidelines (http://spdx.org/spdx-license-list/matching-guidelines) 34 | * @author Gary O'Neall 35 | */ 36 | public class MatchingStandardLicenses { 37 | 38 | /** 39 | * This class should not be instantiated. Call the main method to invoke. 40 | */ 41 | private MatchingStandardLicenses() { 42 | 43 | } 44 | 45 | static int MIN_ARGS = 1; 46 | static int MAX_ARGS = 1; 47 | static final int ERROR_STATUS = 1; 48 | /** 49 | * @param args 50 | */ 51 | public static void main(String[] args) { 52 | if (args == null || args.length < MIN_ARGS || args.length > MAX_ARGS) { 53 | System.out.println("Invalid arguments"); 54 | usage(); 55 | System.exit(ERROR_STATUS); 56 | } 57 | @SuppressWarnings("null") 58 | File textFile = new File(args[0]); 59 | 60 | if (!textFile.exists()) { 61 | System.out.println("Text file "+textFile.getName()+" does not exist"); 62 | usage(); 63 | System.exit(ERROR_STATUS); 64 | } 65 | 66 | SpdxToolsHelper.initialize(); 67 | String licenseText = null; 68 | try { 69 | licenseText = readAll(textFile); 70 | } catch (IOException e) { 71 | System.out.println("Error reading file: "+e.getMessage()); 72 | System.exit(ERROR_STATUS); 73 | } 74 | 75 | String[] matchingLicenseIds = null; 76 | try { 77 | matchingLicenseIds = LicenseCompareHelper.matchingStandardLicenseIds(licenseText); 78 | } catch (InvalidSPDXAnalysisException e) { 79 | System.out.println("Error reading standard licenses: "+e.getMessage()); 80 | System.exit(ERROR_STATUS); 81 | } catch (SpdxCompareException e) { 82 | System.out.println("Error comparing licenses: "+e.getMessage()); 83 | System.exit(ERROR_STATUS); 84 | } 85 | 86 | if (matchingLicenseIds == null || matchingLicenseIds.length == 0) { 87 | System.out.println("No standard licenses matched."); 88 | } else { 89 | StringBuilder sb = new StringBuilder("The following license id(s) match: "); 90 | sb.append(matchingLicenseIds[0]); 91 | for (int i = 1; i < matchingLicenseIds.length; i++) { 92 | sb.append(", "); 93 | sb.append(matchingLicenseIds[i]); 94 | } 95 | System.out.println(sb.toString()); 96 | } 97 | System.exit(0); 98 | } 99 | 100 | /** 101 | * @param textFile 102 | * @return 103 | * @throws IOException 104 | */ 105 | private static String readAll(File textFile) throws IOException { 106 | return new String(Files.readAllBytes(textFile.toPath()), Charset.defaultCharset()); 107 | } 108 | 109 | private static void usage() { 110 | System.out.println("Usage:"); 111 | System.out.println("MatchingStandardLicenses textfile.txt"); 112 | System.out.println(" textfile.txt is a text file containing the license text to compare."); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | --- 2 | SPDX-FileType: DOCUMENTATION 3 | SPDX-License-Identifier: CC-BY-4.0 4 | --- 5 | 6 | Contributing 7 | ============ 8 | 9 | Thank you for your interest in `tools-java`. The project is open-source software, and bug reports, suggestions, and most especially patches are welcome. 10 | 11 | All contributions must include a "Signed-off-by" line in the commit message. 12 | 13 | This indicates that the contribution is made pursuant to the [Developer Certificate of Origin (DCO)](https://developercertificate.org/), a copy of which is included below. 14 | 15 | Issues 16 | ------ 17 | 18 | `tools-java` has a [project page on GitHub](https://github.com/spdx/tools-java/) where you can [create an issue](https://github.com/spdx/tools-java/issues/new) to report a bug, make a suggestion, or propose a substantial change or improvement that you might like to make. You may also wish to contact the SPDX working group technical team through its mailing list, [spdx-tech@lists.spdx.org](mailto:spdx-tech@lists.spdx.org). 19 | 20 | If you would like to work on a fix for any issue, please assign the issue to yourself prior to creating a patch. 21 | 22 | Patches 23 | ------- 24 | 25 | The source code for `tools-java` is hosted on [github.com/spdx/tools-java](https://github.com/spdx/tools-java). Please review [open pull requests](https://github.com/spdx/tools-java/pulls) and [active branches](https://github.com/spdx/tools-java/branches) before committing time to a substantial revision. Work along similar lines may already be in progress. 26 | 27 | To submit a patch via GitHub, fork the repository, create a topic branch from `master` for your work, and send a pull request when ready. If you would prefer to send a patch or grant access to pull from your own Git repository, please contact the project's contributors by e-mail. 28 | 29 | To contribute an implementation of a feature defined by a version of the SPDX specification later than the one supported by the current SPDX Tools release, clone the branch `spec/X.X`, where X.X is the major.minor version of the targeted specification (e.g. "3.1"). 30 | 31 | Once implemented, submit a pull request with `spec/X.X` branch as the parent branch. 32 | 33 | Licensing 34 | --------- 35 | 36 | New **code files** should include a [short-form SPDX ID](https://spdx.org/ids) at the top, indicating the project license for code, which is Apache-2.0. This should look like the following: 37 | 38 | ```java 39 | // SPDX-License-Identifier: Apache-2.0 40 | ``` 41 | 42 | Developer Certificate of Origin (DCO) 43 | ------------------------------------- 44 | 45 | ```text 46 | Developer Certificate of Origin 47 | Version 1.1 48 | 49 | Copyright (C) 2004, 2006 The Linux Foundation and its contributors. 50 | 1 Letterman Drive 51 | Suite D4700 52 | San Francisco, CA, 94129 53 | 54 | Everyone is permitted to copy and distribute verbatim copies of this 55 | license document, but changing it is not allowed. 56 | 57 | 58 | Developer's Certificate of Origin 1.1 59 | 60 | By making a contribution to this project, I certify that: 61 | 62 | (a) The contribution was created in whole or in part by me and I 63 | have the right to submit it under the open source license 64 | indicated in the file; or 65 | 66 | (b) The contribution is based upon previous work that, to the best 67 | of my knowledge, is covered under an appropriate open source 68 | license and I have the right under that license to submit that 69 | work with modifications, whether created in whole or in part 70 | by me, under the same open source license (unless I am 71 | permitted to submit under a different license), as indicated 72 | in the file; or 73 | 74 | (c) The contribution was provided directly to me by some other 75 | person who certified (a), (b) or (c) and I have not modified 76 | it. 77 | 78 | (d) I understand and agree that this project and the contribution 79 | are public and that a record of the contribution (including all 80 | personal information I submit with it, including my sign-off) is 81 | maintained indefinitely and may be redistributed consistent with 82 | this project or the open source license(s) involved. 83 | ``` 84 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/CreatorSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileCopyrightText: Copyright (c) 2013 Black Duck Software Inc. 4 | * SPDX-FileType: SOURCE 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * https://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | package org.spdx.tools.compare; 21 | 22 | import java.util.Arrays; 23 | import java.util.List; 24 | 25 | import org.apache.poi.ss.usermodel.Cell; 26 | import org.apache.poi.ss.usermodel.CellStyle; 27 | import org.apache.poi.ss.usermodel.Row; 28 | import org.apache.poi.ss.usermodel.Sheet; 29 | import org.apache.poi.ss.usermodel.Workbook; 30 | import org.spdx.core.InvalidSPDXAnalysisException; 31 | import org.spdx.library.model.v2.SpdxCreatorInformation; 32 | import org.spdx.utility.compare.SpdxCompareException; 33 | import org.spdx.utility.compare.SpdxComparer; 34 | 35 | /** 36 | * Worksheet containing creator level information 37 | * Column 1 describes if the creator is the same or different 38 | * Columns 2 through N are for creators in each of the documents 39 | * @author Gary O'Neall 40 | */ 41 | public class CreatorSheet extends AbstractSheet { 42 | private static final int COL_WIDTH = 50; 43 | /** 44 | * @param workbook 45 | * @param sheetName 46 | */ 47 | public CreatorSheet(Workbook workbook, String sheetName) { 48 | super(workbook, sheetName); 49 | } 50 | 51 | /* (non-Javadoc) 52 | * @see org.spdx.spdxspreadsheet.AbstractSheet#verify() 53 | */ 54 | @Override 55 | public String verify() { 56 | // Nothing to verify 57 | return null; 58 | } 59 | 60 | /** 61 | * @param wb 62 | * @param sheetName 63 | */ 64 | public static void create(Workbook wb, String sheetName) { 65 | int sheetNum = wb.getSheetIndex(sheetName); 66 | if (sheetNum >= 0) { 67 | wb.removeSheetAt(sheetNum); 68 | } 69 | Sheet sheet = wb.createSheet(sheetName); 70 | CellStyle headerStyle = AbstractSheet.createHeaderStyle(wb); 71 | CellStyle defaultStyle = AbstractSheet.createLeftWrapStyle(wb); 72 | Row row = sheet.createRow(0); 73 | for (int i = 0; i < MultiDocumentSpreadsheet.MAX_DOCUMENTS; i++) { 74 | sheet.setColumnWidth(i, COL_WIDTH*256); 75 | sheet.setDefaultColumnStyle(i, defaultStyle); 76 | Cell cell = row.createCell(i); 77 | cell.setCellStyle(headerStyle); 78 | } 79 | } 80 | 81 | /** 82 | * @param comparer 83 | * @param docNames 84 | * @throws InvalidSPDXAnalysisException 85 | */ 86 | public void importCompareResults(SpdxComparer comparer, List docNames) throws SpdxCompareException, InvalidSPDXAnalysisException { 87 | if (comparer.getNumSpdxDocs() != docNames.size()) { 88 | throw(new SpdxCompareException("Number of document names does not match the number of SPDX documents")); 89 | } 90 | this.clear(); 91 | Row header = sheet.getRow(0); 92 | for (int i = 0; i < comparer.getNumSpdxDocs(); i++) { 93 | Cell headerCell = header.getCell(i); 94 | headerCell.setCellValue(docNames.get(i)); 95 | SpdxCreatorInformation creationInfo = comparer.getSpdxDoc(i).getCreationInfo(); 96 | if (creationInfo != null) { 97 | String[] creators = creationInfo.getCreators().toArray(new String[creationInfo.getCreators().size()]); 98 | Arrays.sort(creators); 99 | for (int j = 0; j < creators.length; j++) { 100 | Cell creatorCell = null; 101 | while (j+1 > this.getNumDataRows()) { 102 | this.addRow(); 103 | } 104 | creatorCell = sheet.getRow(j+1).createCell(i); 105 | creatorCell.setCellValue(creators[j]); 106 | } 107 | } 108 | } 109 | } 110 | 111 | } 112 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/RdfSchemaToXsd.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools; 20 | 21 | import java.io.File; 22 | import java.io.FileInputStream; 23 | import java.io.FileNotFoundException; 24 | import java.io.FileOutputStream; 25 | import java.io.IOException; 26 | import java.io.InputStream; 27 | import java.io.OutputStream; 28 | import java.util.Objects; 29 | 30 | import org.apache.jena.ontology.OntModel; 31 | import org.apache.jena.ontology.OntModelSpec; 32 | import org.apache.jena.rdf.model.ModelFactory; 33 | import org.apache.ws.commons.schema.XmlSchema; 34 | import org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException; 35 | import org.spdx.tools.schema.OwlToXsd; 36 | import org.spdx.tools.schema.SchemaException; 37 | 38 | /** 39 | * Convert an RDF OWL document to an XML Schema 40 | * @author Gary O'Neall 41 | */ 42 | public class RdfSchemaToXsd { 43 | 44 | /** 45 | * @param args arg[0] RDF Schema file path; arg[1] output file path 46 | */ 47 | public static void main(String[] args) { 48 | if (args.length != 2) { 49 | System.err 50 | .println("Invalid number of arguments"); 51 | usage(); 52 | return; 53 | } 54 | File fromFile = new File(args[0]); 55 | if (!fromFile.exists()) { 56 | System.err 57 | .println("Input file "+args[0]+" does not exist."); 58 | usage(); 59 | return; 60 | } 61 | File toFile = new File(args[1]); 62 | if (toFile.exists()) { 63 | System.err.println("Output file "+args[1]+" already exists."); 64 | usage(); 65 | return; 66 | } 67 | InputStream is = null; 68 | OntModel model = null; 69 | try { 70 | is = new FileInputStream(fromFile); 71 | model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); 72 | model.read(is, "RDF/XML"); 73 | } catch (FileNotFoundException e) { 74 | System.err.println("File not found for "+fromFile.getName()); 75 | return; 76 | } finally { 77 | if (is != null) { 78 | try { 79 | is.close(); 80 | } catch (IOException e) { 81 | System.err.println("Error closing input file stream: "+e.getMessage()); 82 | } 83 | } 84 | } 85 | try { 86 | OwlToXsd owlToXsd = new OwlToXsd(model); 87 | XmlSchema xmlSchema = owlToXsd.convertToXsd(); 88 | OutputStream os = null; 89 | try { 90 | os = new FileOutputStream(toFile); 91 | xmlSchema.write(os); 92 | } catch (IOException e) { 93 | System.err.println("I/O error: "+e.getMessage()); 94 | return; 95 | } finally { 96 | if (Objects.nonNull(is)) { 97 | try { 98 | is.close(); 99 | } catch (IOException e) { 100 | System.err.println("Error closing input file stream: "+e.getMessage()); 101 | } 102 | } 103 | if (Objects.nonNull(os)) { 104 | try { 105 | os.close(); 106 | } catch (IOException e) { 107 | System.err.println("Error closing output file stream: "+e.getMessage()); 108 | } 109 | } 110 | } 111 | } catch (XmlSchemaSerializerException e1) { 112 | System.err.println("Error generating XSD schema: "+e1.getMessage()); 113 | } catch (SchemaException e1) { 114 | System.err.println("Error generating XSD schema: "+e1.getMessage()); 115 | } 116 | 117 | 118 | } 119 | 120 | public static void usage() { 121 | System.out.println("Usage:"); 122 | System.out.println("RdfSchemaToXsd rdfSchemaFile xsdFile"); 123 | System.out.println("\trdfSchemaFile RDF schema file in RDF/XML format"); 124 | System.out.println("\txsdFile output XML Schema"); 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /testResources/double.jsonld: -------------------------------------------------------------------------------- 1 | { 2 | "@context": "https://spdx.org/rdf/3.0.1/spdx-context.jsonld", 3 | "@graph": [ 4 | { 5 | "type": "Organization", 6 | "spdxId": "SPDXRef-MyOrganization:-73f9a129-5eea-4de8-b38b-96832cc72d57", 7 | "name": "MyOrganization", 8 | "creationInfo": "_:creationinfo" 9 | }, 10 | { 11 | "type": "CreationInfo", 12 | "@id": "_:creationinfo", 13 | "specVersion": "3.0.1", 14 | "createdBy": [ 15 | "SPDXRef-MyOrganization:-73f9a129-5eea-4de8-b38b-96832cc72d57" 16 | ], 17 | "created": "2025-01-07T07:01:21Z" 18 | }, 19 | { 20 | "type": "SpdxDocument", 21 | "spdxId": "SPDXRef-Document:-8b2134c3-1472-48c3-bbd9-53cdef129f09", 22 | "creationInfo": "_:creationinfo", 23 | "dataLicense": "SPDXRef-License:-DataLicenseCC1.0", 24 | "profileConformance": [ 25 | "core", 26 | "software", 27 | "security", 28 | "simpleLicensing" 29 | ], 30 | "rootElement": [ 31 | "BOM:ROOT" 32 | ] 33 | }, 34 | { 35 | "type": "simplelicensing_LicenseExpression", 36 | "spdxId": "SPDXRef-License:-DataLicenseCC1.0", 37 | "name": "Data License CC 1.0", 38 | "description": "Refer to this element if another element's data license is CC 1.0", 39 | "creationInfo": "_:creationinfo", 40 | "simplelicensing_licenseExpression": "CC-BY-1.0" 41 | }, 42 | { 43 | "type": "simplelicensing_LicenseExpression", 44 | "spdxId": "SPDXRef-License:-NoAssertion", 45 | "name": "NoAssertion", 46 | "description": "Refer to this element if another element's license can't be asserted.", 47 | "creationInfo": "_:creationinfo", 48 | "simplelicensing_licenseExpression": "NOASSERTION" 49 | }, 50 | { 51 | "type": "software_Package", 52 | "spdxId": "SPDX-ID:-73fde02b-0fda-50b2-ad2e-a219f85c7ce4", 53 | "creationInfo": "_:creationinfo", 54 | "name": "An example software", 55 | "originatedBy": [ 56 | "Organization: An example organization" 57 | ], 58 | "software_copyrightText": "NOASSERTION", 59 | "software_primaryPurpose": "application", 60 | "description": "This is an example software" 61 | }, 62 | { 63 | "type": "security_Vulnerability", 64 | "spdxId": "SPDXRef-Vulnerability:-CVE-2016-4285", 65 | "name": "CVE-2016-4285", 66 | "creationInfo": "_:creationinfo", 67 | "externalIdentifier": [ 68 | { 69 | "type": "ExternalIdentifier", 70 | "externalIdentifierType": "cve", 71 | "identifier": "CVE-2016-4285", 72 | "identifierLocator": [ 73 | "https://nvd.nist.gov/vuln/detail/CVE-2016-4285" 74 | ] 75 | } 76 | ] 77 | }, 78 | { 79 | "type": "security_CvssV3VulnAssessmentRelationship", 80 | "spdxId": "SPDXRef-CVSSAssessment:-CVE-2016-4285", 81 | "creationInfo": "_:creationinfo", 82 | "relationshipType": "hasAssessmentFor", 83 | "security_score": "8.8", 84 | "security_severity": "high", 85 | "security_vectorString": "CVSS:3.0/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H", 86 | "from": "SPDXRef-Vulnerability:-CVE-2016-4285", 87 | "to": [ 88 | "SPDX-ID:-73fde02b-0fda-50b2-ad2e-a219f85c7ce4" 89 | ] 90 | }, 91 | { 92 | "type": "security_VexAffectedVulnAssessmentRelationship", 93 | "spdxId": "SPDXRef-VexAffectedRelationship:-CVE-2016-4285", 94 | "creationInfo": "_:creationinfo", 95 | "relationshipType": "affects", 96 | "security_actionStatement": "no_assertion", 97 | "from": "SPDXRef-Vulnerability:-CVE-2016-4285", 98 | "to": [ 99 | "SPDX-ID:-73fde02b-0fda-50b2-ad2e-a219f85c7ce4" 100 | ] 101 | }, 102 | { 103 | "type": "software_Sbom", 104 | "spdxId": "BOM:ROOT", 105 | "creationInfo": "_:creationinfo", 106 | "software_sbomType": [ 107 | "analyzed" 108 | ], 109 | "rootElement": [ 110 | "SPDX-ID:-73fde02b-0fda-50b2-ad2e-a219f85c7ce4" 111 | ], 112 | "element": [ 113 | "SPDXRef-License:-DataLicenseCC1.0", 114 | "SPDXRef-License:-NoAssertion", 115 | "SPDX-ID:-73fde02b-0fda-50b2-ad2e-a219f85c7ce4", 116 | "SPDXRef-Vulnerability:-CVE-2016-4285", 117 | "SPDXRef-CVSSAssessment:-CVE-2016-4285", 118 | "SPDXRef-VexAffectedRelationship:-CVE-2016-4285" 119 | ] 120 | } 121 | ] 122 | } -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/RdfSchemaToJsonContext.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools; 20 | 21 | import java.io.File; 22 | import java.io.FileInputStream; 23 | import java.io.FileNotFoundException; 24 | import java.io.FileOutputStream; 25 | import java.io.IOException; 26 | import java.io.InputStream; 27 | import java.util.Objects; 28 | 29 | import org.apache.jena.ontology.OntModel; 30 | import org.apache.jena.ontology.OntModelSpec; 31 | import org.apache.jena.rdf.model.ModelFactory; 32 | import org.spdx.tools.schema.OwlToJsonContext; 33 | 34 | import com.fasterxml.jackson.core.JsonGenerator; 35 | import com.fasterxml.jackson.core.JsonProcessingException; 36 | import com.fasterxml.jackson.databind.node.ObjectNode; 37 | 38 | /** 39 | * Convert an RDF schema file containing SPDX property to a JSON context file for all properties in the SPDX namespace 40 | * @author Gary O'Neall 41 | */ 42 | public class RdfSchemaToJsonContext { 43 | 44 | /** 45 | * @param args arg[0] RDF Schema file path; arg[1] output file path 46 | */ 47 | public static void main(String[] args) { 48 | if (args.length != 2) { 49 | System.err 50 | .println("Invalid number of arguments"); 51 | usage(); 52 | return; 53 | } 54 | File fromFile = new File(args[0]); 55 | if (!fromFile.exists()) { 56 | System.err 57 | .println("Input file "+args[0]+" does not exist."); 58 | usage(); 59 | return; 60 | } 61 | File toFile = new File(args[1]); 62 | if (toFile.exists()) { 63 | System.err.println("Output file "+args[1]+" already exists."); 64 | usage(); 65 | return; 66 | } 67 | InputStream is = null; 68 | OwlToJsonContext owlToJsonContext = null; 69 | try { 70 | is = new FileInputStream(fromFile); 71 | OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); 72 | model.read(is, "RDF/XML"); 73 | owlToJsonContext = new OwlToJsonContext(model); 74 | } catch (FileNotFoundException e) { 75 | System.err.println("File not found for "+fromFile.getName()); 76 | return; 77 | } finally { 78 | if (is != null) { 79 | try { 80 | is.close(); 81 | } catch (IOException e) { 82 | System.err.println("Error closing input file stream: "+e.getMessage()); 83 | } 84 | } 85 | } 86 | if (Objects.isNull(owlToJsonContext)) { 87 | System.err.println("Unable to load ontology from file "+fromFile.getName()); 88 | return; 89 | } 90 | ObjectNode context = owlToJsonContext.convertToContext(); 91 | JsonGenerator jsonGenerator = null; 92 | try { 93 | jsonGenerator = OwlToJsonContext.JSON_MAPPER.getFactory().createGenerator(new FileOutputStream(toFile)); 94 | OwlToJsonContext.JSON_MAPPER.writeTree(jsonGenerator.useDefaultPrettyPrinter(), 95 | context); 96 | } catch (FileNotFoundException e) { 97 | System.err.println("File not found for "+fromFile.getName()); 98 | return; 99 | } catch (JsonProcessingException e) { 100 | System.err.println("JSON error "+e.getMessage()); 101 | return; 102 | } catch (IOException e) { 103 | System.err.println("I/O error: "+e.getMessage()); 104 | return; 105 | } finally { 106 | if (Objects.nonNull(jsonGenerator)) { 107 | try { 108 | jsonGenerator.close(); 109 | } catch (IOException e) { 110 | System.err.println("Error closing output file stream: "+e.getMessage()); 111 | } 112 | } 113 | } 114 | } 115 | 116 | public static void usage() { 117 | System.out.println("Usage:"); 118 | System.out.println("RdfSchemaToJsonContext rdfSchemaFile jsonContextFile"); 119 | System.out.println("\trdfSchemaFile RDF schema file in RDF/XML format"); 120 | System.out.println("\trdfSchemaFile output JSON context file"); 121 | } 122 | 123 | } 124 | -------------------------------------------------------------------------------- /testResources/sourcefiles/VerificationSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileCopyrightText: Copyright (c) 2013 Black Duck Software Inc. 4 | * SPDX-FileType: SOURCE 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * https://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | package org.spdx.tools.compare; 21 | 22 | import java.util.List; 23 | 24 | import org.apache.poi.ss.usermodel.Cell; 25 | import org.apache.poi.ss.usermodel.CellStyle; 26 | import org.apache.poi.ss.usermodel.Row; 27 | import org.apache.poi.ss.usermodel.Sheet; 28 | import org.apache.poi.ss.usermodel.Workbook; 29 | import org.spdx.spreadsheetstore.SpreadsheetException; 30 | 31 | /** 32 | * Worksheet containing verification errors 33 | * Columns are package names, rows are individual verification errors 34 | * @author Gary O'Neall 35 | */ 36 | public class VerificationSheet extends AbstractSheet { 37 | 38 | private static final int COL_WIDTH = 40; 39 | 40 | /** 41 | * @param workbook 42 | * @param sheetName 43 | */ 44 | public VerificationSheet(Workbook workbook, String sheetName) { 45 | super(workbook, sheetName); 46 | } 47 | 48 | /* (non-Javadoc) 49 | * @see org.spdx.spdxspreadsheet.AbstractSheet#verify() 50 | */ 51 | @Override 52 | public String verify() { 53 | // Nothing to verify 54 | return null; 55 | } 56 | 57 | /** 58 | * @param wb 59 | * @param sheetName 60 | */ 61 | public static void create(Workbook wb, String sheetName) { 62 | int sheetNum = wb.getSheetIndex(sheetName); 63 | if (sheetNum >= 0) { 64 | wb.removeSheetAt(sheetNum); 65 | } 66 | Sheet sheet = wb.createSheet(sheetName); 67 | CellStyle headerStyle = AbstractSheet.createHeaderStyle(wb); 68 | CellStyle defaultStyle = AbstractSheet.createLeftWrapStyle(wb); 69 | Row row = sheet.createRow(0); 70 | for (int i = 0; i < MultiDocumentSpreadsheet.MAX_DOCUMENTS; i++) { 71 | sheet.setColumnWidth(i, COL_WIDTH*256); 72 | sheet.setDefaultColumnStyle(i, defaultStyle); 73 | Cell cell = row.createCell(i); 74 | cell.setCellStyle(headerStyle); 75 | } 76 | } 77 | 78 | /** 79 | * Import verification errors 80 | * @param verificationErrors Array of verification error message arraylists ordered by docname 81 | * @param docNames Name of documents relating to the errors 82 | * @throws SpreadsheetException 83 | */ 84 | public void importVerificationErrors( 85 | List> verificationErrors, List docNames) throws SpreadsheetException { 86 | if (verificationErrors == null) { 87 | throw(new SpreadsheetException("Verification errors not specified on import (null value).")); 88 | } 89 | if (docNames == null) { 90 | throw(new SpreadsheetException("Document names errors not specified on import (null value).")); 91 | } 92 | if (verificationErrors.size() != docNames.size()) { 93 | throw(new SpreadsheetException("Number of verification errors does not equal the number of documents.")); 94 | } 95 | if (docNames.size() > MultiDocumentSpreadsheet.MAX_DOCUMENTS) { 96 | throw(new SpreadsheetException("Too many compare documents - must be less than "+String.valueOf(MultiDocumentSpreadsheet.MAX_DOCUMENTS+1))); 97 | } 98 | Row header = sheet.getRow(0); 99 | int lastRowCreated = 0; 100 | for (int i = 0; i < docNames.size(); i++) { 101 | Cell hCell = header.getCell(i); 102 | hCell.setCellValue(docNames.get(i)); 103 | List errors = verificationErrors.get(i); 104 | if (errors != null) { 105 | for (int j = 0; j < errors.size(); j++) { 106 | Row errorRow; 107 | if (j+1 > lastRowCreated) { 108 | errorRow = sheet.createRow(j+1); 109 | lastRowCreated = j+1; 110 | } else { 111 | errorRow = sheet.getRow(j+1); 112 | } 113 | Cell errorCell = errorRow.createCell(i); 114 | errorCell.setCellValue(errors.get(j)); 115 | } 116 | } 117 | } 118 | } 119 | 120 | } 121 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/compare/VerificationSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2013 Source Auditor Inc. 3 | * SPDX-FileCopyrightText: Copyright (c) 2013 Black Duck Software Inc. 4 | * SPDX-FileType: SOURCE 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * https://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | package org.spdx.tools.compare; 21 | 22 | import java.util.List; 23 | 24 | import org.apache.poi.ss.usermodel.Cell; 25 | import org.apache.poi.ss.usermodel.CellStyle; 26 | import org.apache.poi.ss.usermodel.Row; 27 | import org.apache.poi.ss.usermodel.Sheet; 28 | import org.apache.poi.ss.usermodel.Workbook; 29 | import org.spdx.spreadsheetstore.SpreadsheetException; 30 | 31 | /** 32 | * Worksheet containing verification errors 33 | * 34 | * Columns are package names, rows are individual verification errors 35 | * @author Gary O'Neall 36 | */ 37 | public class VerificationSheet extends AbstractSheet { 38 | 39 | private static final int COL_WIDTH = 40; 40 | 41 | /** 42 | * @param workbook 43 | * @param sheetName 44 | */ 45 | public VerificationSheet(Workbook workbook, String sheetName) { 46 | super(workbook, sheetName); 47 | } 48 | 49 | /* (non-Javadoc) 50 | * @see org.spdx.spdxspreadsheet.AbstractSheet#verify() 51 | */ 52 | @Override 53 | public String verify() { 54 | // Nothing to verify 55 | return null; 56 | } 57 | 58 | /** 59 | * @param wb 60 | * @param sheetName 61 | */ 62 | public static void create(Workbook wb, String sheetName) { 63 | int sheetNum = wb.getSheetIndex(sheetName); 64 | if (sheetNum >= 0) { 65 | wb.removeSheetAt(sheetNum); 66 | } 67 | Sheet sheet = wb.createSheet(sheetName); 68 | CellStyle headerStyle = AbstractSheet.createHeaderStyle(wb); 69 | CellStyle defaultStyle = AbstractSheet.createLeftWrapStyle(wb); 70 | Row row = sheet.createRow(0); 71 | for (int i = 0; i < MultiDocumentSpreadsheet.MAX_DOCUMENTS; i++) { 72 | sheet.setColumnWidth(i, COL_WIDTH*256); 73 | sheet.setDefaultColumnStyle(i, defaultStyle); 74 | Cell cell = row.createCell(i); 75 | cell.setCellStyle(headerStyle); 76 | } 77 | } 78 | 79 | /** 80 | * Import verification errors 81 | * @param verificationErrors Array of verification error message arraylists ordered by docname 82 | * @param docNames Name of documents relating to the errors 83 | * @throws SpreadsheetException 84 | */ 85 | public void importVerificationErrors( 86 | List> verificationErrors, List docNames) throws SpreadsheetException { 87 | if (verificationErrors == null) { 88 | throw(new SpreadsheetException("Verification errors not specified on import (null value).")); 89 | } 90 | if (docNames == null) { 91 | throw(new SpreadsheetException("Document names errors not specified on import (null value).")); 92 | } 93 | if (verificationErrors.size() != docNames.size()) { 94 | throw(new SpreadsheetException("Number of verification errors does not equal the number of documents.")); 95 | } 96 | if (docNames.size() > MultiDocumentSpreadsheet.MAX_DOCUMENTS) { 97 | throw(new SpreadsheetException("Too many compare documents - must be less than "+String.valueOf(MultiDocumentSpreadsheet.MAX_DOCUMENTS+1))); 98 | } 99 | Row header = sheet.getRow(0); 100 | int lastRowCreated = 0; 101 | for (int i = 0; i < docNames.size(); i++) { 102 | Cell hCell = header.getCell(i); 103 | hCell.setCellValue(docNames.get(i)); 104 | List errors = verificationErrors.get(i); 105 | if (errors != null) { 106 | for (int j = 0; j < errors.size(); j++) { 107 | Row errorRow; 108 | if (j+1 > lastRowCreated) { 109 | errorRow = sheet.createRow(j+1); 110 | lastRowCreated = j+1; 111 | } else { 112 | errorRow = sheet.getRow(j+1); 113 | } 114 | Cell errorCell = errorRow.createCell(i); 115 | errorCell.setCellValue(errors.get(j)); 116 | } 117 | } 118 | } 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/RdfSchemaToJsonSchema.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools; 20 | 21 | import java.io.File; 22 | import java.io.FileInputStream; 23 | import java.io.FileNotFoundException; 24 | import java.io.FileOutputStream; 25 | import java.io.IOException; 26 | import java.io.InputStream; 27 | import java.util.Objects; 28 | 29 | import org.apache.jena.ontology.OntModel; 30 | import org.apache.jena.ontology.OntModelSpec; 31 | import org.apache.jena.rdf.model.ModelFactory; 32 | import org.spdx.tools.schema.OwlToJsonSchema; 33 | 34 | import com.fasterxml.jackson.core.JsonGenerator; 35 | import com.fasterxml.jackson.core.JsonProcessingException; 36 | import com.fasterxml.jackson.databind.ObjectMapper; 37 | import com.fasterxml.jackson.databind.SerializationFeature; 38 | import com.fasterxml.jackson.databind.node.ObjectNode; 39 | 40 | /** 41 | * Convert an RDF schema file containing SPDX property to a JSON schema file for all properties in the SPDX namespace 42 | * @author Gary O'Neall 43 | */ 44 | public class RdfSchemaToJsonSchema { 45 | 46 | /** 47 | * @param args arg[0] RDF Schema file path; arg[1] output file path 48 | */ 49 | public static void main(String[] args) { 50 | if (args.length != 2) { 51 | System.err 52 | .println("Invalid number of arguments"); 53 | usage(); 54 | return; 55 | } 56 | File fromFile = new File(args[0]); 57 | if (!fromFile.exists()) { 58 | System.err 59 | .println("Input file "+args[0]+" does not exist."); 60 | usage(); 61 | return; 62 | } 63 | File toFile = new File(args[1]); 64 | if (toFile.exists()) { 65 | System.err.println("Output file "+args[1]+" already exists."); 66 | usage(); 67 | return; 68 | } 69 | InputStream is = null; 70 | OntModel model = null; 71 | try { 72 | is = new FileInputStream(fromFile); 73 | model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); 74 | model.read(is, "RDF/XML"); 75 | } catch (FileNotFoundException e) { 76 | System.err.println("File not found for "+fromFile.getName()); 77 | return; 78 | } finally { 79 | if (is != null) { 80 | try { 81 | is.close(); 82 | } catch (IOException e) { 83 | System.err.println("Error closing input file stream: "+e.getMessage()); 84 | } 85 | } 86 | } 87 | OwlToJsonSchema owlToJson = new OwlToJsonSchema(model); 88 | ObjectNode root = owlToJson.convertToJsonSchema(); 89 | ObjectMapper jsonMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); 90 | 91 | JsonGenerator jsonGenerator = null; 92 | try { 93 | jsonGenerator = jsonMapper.getFactory().createGenerator(new FileOutputStream(toFile)); 94 | jsonMapper.writeTree(jsonGenerator.useDefaultPrettyPrinter(), 95 | root); 96 | } catch (JsonProcessingException e) { 97 | System.err.println("JSON error "+e.getMessage()); 98 | return; 99 | } catch (IOException e) { 100 | System.err.println("I/O error: "+e.getMessage()); 101 | return; 102 | } finally { 103 | if (Objects.nonNull(is)) { 104 | try { 105 | is.close(); 106 | } catch (IOException e) { 107 | System.err.println("Error closing input file stream: "+e.getMessage()); 108 | } 109 | } 110 | if (Objects.nonNull(jsonGenerator)) { 111 | try { 112 | jsonGenerator.close(); 113 | } catch (IOException e) { 114 | System.err.println("Error closing output file stream: "+e.getMessage()); 115 | } 116 | } 117 | } 118 | 119 | } 120 | 121 | public static void usage() { 122 | System.out.println("Usage:"); 123 | System.out.println("RdfSchemaToJsonSchema rdfSchemaFile jsonSchemaFile"); 124 | System.out.println("\trdfSchemaFile RDF schema file in RDF/XML format"); 125 | System.out.println("\tjsonSchemaFile output JSON Schema file"); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/SpdxViewer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2010 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools; 20 | 21 | import java.io.File; 22 | import java.io.PrintWriter; 23 | import java.util.List; 24 | import java.util.Objects; 25 | import java.util.Properties; 26 | 27 | import org.spdx.core.InvalidSPDXAnalysisException; 28 | import org.spdx.library.model.v2.SpdxDocument; 29 | import org.spdx.storage.ISerializableModelStore; 30 | import org.spdx.tag.CommonCode; 31 | import org.spdx.tools.SpdxToolsHelper.SerFileType; 32 | 33 | /** 34 | * Simple pretty printer for SPDX RDF XML files. Writes output to System.out. 35 | * Usage: PrettyPrinter SPDXRdfXMLFile > textFile where SPDXRdfXMLFile is a 36 | * valid SPDX RDF XML file 37 | * 38 | * @author Gary O'Neall 39 | * @version 0.1 40 | */ 41 | public class SpdxViewer { 42 | 43 | static final int MIN_ARGS = 1; 44 | static final int MAX_ARGS = 2; 45 | static final int ERROR_STATUS = 1; 46 | 47 | /** 48 | * Pretty Printer for an SPDX Document 49 | * 50 | * @param args args[0] SPDX file path; args[1] [RDFXML|JSON|XLS|XLSX|YAML|TAG] an optional file type - if not present, file type of the to file will be used 51 | */ 52 | public static void main(String[] args) { 53 | if (args.length < MIN_ARGS) { 54 | System.err 55 | .println("Usage:\n SPDXViewer file [RDFXML|JSON|XLS|XLSX|YAML|TAG] \n" 56 | + "where file is the file path to a valid SPDX file\n" 57 | + "and [RDFXML|JSON|XLS|XLSX|YAML|TAG|JSONLD] is an optional file type\n" 58 | + "if not present, file type of the to file will be used"); 59 | return; 60 | } 61 | if (args.length > MAX_ARGS) { 62 | System.out.printf("Warning: Extra arguments will be ignored"); 63 | } 64 | SpdxToolsHelper.initialize(); 65 | SpdxDocument doc = null; 66 | ISerializableModelStore store = null; 67 | PrintWriter writer = null; 68 | try { 69 | File file = new File(args[0]); 70 | if (!file.exists()) { 71 | throw new SpdxVerificationException("File "+args[0]+" not found."); 72 | } 73 | if (!file.isFile()) { 74 | throw new SpdxVerificationException(args[0]+" is not a file."); 75 | } 76 | SerFileType fileType = null; 77 | if (args.length > 1) { 78 | try { 79 | fileType = SpdxToolsHelper.strToFileType(args[1]); 80 | } catch (Exception ex) { 81 | System.err.println("Invalid file type: "+args[1]); 82 | System.exit(ERROR_STATUS); 83 | } 84 | } else { 85 | fileType = SpdxToolsHelper.fileToFileType(file); 86 | } 87 | try { 88 | store = SpdxToolsHelper.fileTypeToStore(fileType); 89 | } catch (InvalidSPDXAnalysisException e) { 90 | throw new SpdxVerificationException("Error converting fileType to store",e); 91 | } 92 | 93 | try { 94 | doc = SpdxToolsHelper.readDocumentFromFileCompatV2(store, file); 95 | } catch (Exception ex) { 96 | System.out 97 | .print("Error creating SPDX Document: " + ex.getMessage()); 98 | return; 99 | } 100 | writer = new PrintWriter(System.out); 101 | List verify = doc.verify(); 102 | if (verify.size() > 0) { 103 | System.out.println("This SPDX Document is not valid due to:"); 104 | for (int i = 0; i < verify.size(); i++) { 105 | System.out.print("\t" + verify.get(i)+"\n"); 106 | } 107 | } 108 | // read the constants from a file 109 | Properties constants = CommonCode 110 | .getTextFromProperties("org/spdx/tag/SpdxViewerConstants.properties"); 111 | // print document to system output using human readable format 112 | CommonCode.printDoc(doc, writer, constants); 113 | } catch (InvalidSPDXAnalysisException e) { 114 | System.out.print("Error pretty printing SPDX Document: " 115 | + e.getMessage()); 116 | return; 117 | } catch (Exception e) { 118 | System.out.print("Unexpected error displaying SPDX Document: " 119 | + e.getMessage()); 120 | } finally { 121 | if (Objects.nonNull(writer)) { 122 | writer.close(); 123 | } 124 | if (Objects.nonNull(store)) { 125 | try { 126 | store.close(); 127 | } catch (Exception e) { 128 | System.out.println("Warning - unable to close SPDX file: "+e.getMessage()); 129 | } 130 | } 131 | } 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/test/java/org/spdx/tools/SpdxConverterTestV3.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2024 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | package org.spdx.tools; 7 | 8 | import java.io.File; 9 | import java.io.IOException; 10 | import java.nio.file.DirectoryStream; 11 | import java.nio.file.Files; 12 | import java.nio.file.LinkOption; 13 | import java.nio.file.Path; 14 | import java.util.List; 15 | import java.util.Objects; 16 | import java.util.Optional; 17 | 18 | import org.junit.After; 19 | import org.junit.Before; 20 | import org.spdx.core.DefaultModelStore; 21 | import org.spdx.core.InvalidSPDXAnalysisException; 22 | import org.spdx.library.ModelCopyManager; 23 | import org.spdx.library.SpdxModelFactory; 24 | import org.spdx.library.model.v3_0_1.core.Element; 25 | import org.spdx.library.model.v3_0_1.core.NamespaceMap; 26 | import org.spdx.library.model.v3_0_1.core.SpdxDocument; 27 | import org.spdx.library.model.v3_0_1.software.SpdxFile; 28 | import org.spdx.library.model.v3_0_1.software.SpdxPackage; 29 | import org.spdx.storage.simple.InMemSpdxStore; 30 | import org.spdx.tools.SpdxToolsHelper.SerFileType; 31 | import org.spdx.utility.compare.SpdxCompareException; 32 | 33 | import junit.framework.TestCase; 34 | 35 | /** 36 | * Test SPDX converter v3 37 | * 38 | * @author Gary O'Neall 39 | */ 40 | public class SpdxConverterTestV3 extends TestCase { 41 | 42 | static final String TEST_DIR = "testResources"; 43 | static final String TEST_JSON_FILE_PATH = TEST_DIR + File.separator + "SPDXJSONExample-v2.3.spdx.json"; 44 | 45 | Path tempDirPath; 46 | /** 47 | * @throws java.lang.Exception 48 | */ 49 | @Before 50 | public void setUp() throws Exception { 51 | SpdxModelFactory.init(); 52 | DefaultModelStore.initialize(new InMemSpdxStore(), "http://default/namespace", new ModelCopyManager()); 53 | tempDirPath = Files.createTempDirectory("spdx-tools-test-"); 54 | } 55 | 56 | /** 57 | * @throws java.lang.Exception 58 | */ 59 | @After 60 | public void tearDown() throws Exception { 61 | super.tearDown(); 62 | deleteDirAndFiles(tempDirPath); 63 | } 64 | 65 | public static void deleteDirAndFiles(Path dirOrFile) { 66 | if (Objects.isNull(dirOrFile)) { 67 | return; 68 | } 69 | if (!Files.exists(dirOrFile, LinkOption.NOFOLLOW_LINKS)) { 70 | return; 71 | } 72 | if (Files.isDirectory(dirOrFile, LinkOption.NOFOLLOW_LINKS)) { 73 | try (DirectoryStream files = Files.newDirectoryStream(dirOrFile)) { 74 | for (Path file : files) { 75 | deleteDirAndFiles(file); 76 | } 77 | } catch (IOException e) { 78 | System.err.println("IO error deleting directory or file "+e.getMessage()); 79 | } 80 | } 81 | try { 82 | Files.delete(dirOrFile); 83 | } catch (IOException e) { 84 | System.err.println("IO error deleting directory or file "+e.getMessage()); 85 | } 86 | } 87 | 88 | public void testV2JsonToV3JsonLD() throws SpdxConverterException, InvalidSPDXAnalysisException, IOException, SpdxCompareException { 89 | String jsonLdFileName = "result.jsonld"; 90 | Path outFilePath = tempDirPath.resolve(jsonLdFileName); 91 | SpdxConverter.convert(TEST_JSON_FILE_PATH, outFilePath.toString(), SerFileType.JSON, SerFileType.JSONLD); 92 | File result = new File(outFilePath.toString()); 93 | File source = new File(TEST_JSON_FILE_PATH); 94 | assertTrue(result.exists()); 95 | org.spdx.library.model.v2.SpdxDocument sourceDoc = SpdxToolsHelper.deserializeDocumentCompatV2(source, SerFileType.JSON); 96 | SpdxDocument resultDoc = SpdxToolsHelper.deserializeDocument(result, SerFileType.JSONLD); 97 | List verify = resultDoc.verify(); 98 | assertEquals(0, verify.size()); 99 | org.spdx.library.model.v2.SpdxElement[] sourceRoots = sourceDoc.getDocumentDescribes().toArray( 100 | new org.spdx.library.model.v2.SpdxElement[sourceDoc.getDocumentDescribes().size()]); 101 | assertEquals(2, sourceRoots.length); 102 | org.spdx.library.model.v2.SpdxPackage sourcePackage = (org.spdx.library.model.v2.SpdxPackage)( 103 | sourceRoots[0] instanceof org.spdx.library.model.v2.SpdxPackage ? sourceRoots[0] : sourceRoots[1]); 104 | org.spdx.library.model.v2.SpdxFile sourceFile = (org.spdx.library.model.v2.SpdxFile)( 105 | sourceRoots[0] instanceof org.spdx.library.model.v2.SpdxFile ? sourceRoots[0] : sourceRoots[1]); 106 | Element[] resultRoots = resultDoc.getRootElements().toArray(new Element[resultDoc.getRootElements().size()]); 107 | assertEquals(2, resultRoots.length); 108 | SpdxPackage resultPackage = (SpdxPackage)(resultRoots[0] instanceof SpdxPackage ? resultRoots[0] : resultRoots[1]); 109 | SpdxFile resultFile = (SpdxFile)(resultRoots[0] instanceof SpdxFile ? resultRoots[0] : resultRoots[1]); 110 | 111 | assertEquals(sourcePackage.getName().get(), resultPackage.getName().get()); 112 | assertEquals(sourceFile.getName().get(), resultFile.getName().get()); 113 | 114 | assertEquals(1, resultDoc.getNamespaceMaps().size()); 115 | Optional map = resultDoc.getNamespaceMaps().stream().findFirst(); 116 | assertTrue(map.isPresent()); 117 | assertEquals("http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301#", map.get().getNamespace()); 118 | assertEquals("DocumentRef-spdx-tool-1.2", map.get().getPrefix()); 119 | // TODO: create a more extensive set of checks 120 | } 121 | 122 | } 123 | -------------------------------------------------------------------------------- /src/test/java/org/spdx/tools/VerifyTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | package org.spdx.tools; 7 | 8 | import java.io.File; 9 | import java.util.List; 10 | 11 | import org.spdx.core.DefaultModelStore; 12 | import org.spdx.core.ModelRegistry; 13 | import org.spdx.library.ModelCopyManager; 14 | import org.spdx.library.model.v2.SpdxModelInfoV2_X; 15 | import org.spdx.library.model.v3_0_1.SpdxModelInfoV3_0; 16 | import org.spdx.storage.simple.InMemSpdxStore; 17 | import org.spdx.tools.SpdxToolsHelper.SerFileType; 18 | 19 | import junit.framework.TestCase; 20 | 21 | public class VerifyTest extends TestCase { 22 | 23 | static final String TEST_DIR = "testResources"; 24 | static final String TEST_JSONLD_FILE_PATH = TEST_DIR + File.separator + "SPDXJsonLDExample-v3.0.1.json"; 25 | static final String TEST_JSON_FILE_PATH = TEST_DIR + File.separator + "SPDXJSONExample-v2.3.spdx.json"; 26 | static final String JSON_V2_3_FILE_PATH = TEST_DIR + File.separator + "SPDXJSONExample-v2.3.spdx.json"; 27 | static final String JSON_V2_2_FILE_PATH = TEST_DIR + File.separator + "SPDXJSONExample-v2.2.spdx.json"; 28 | static final String JSON_BAD_VERSION_FILE_PATH = TEST_DIR + File.separator + "SPDXJSONExample-wrongversion.spdx.json"; 29 | static final String TEST_V23_FIELDS_IN_V22_FILE = TEST_DIR + File.separator + "SPDXWrongVersion.spdx.json"; 30 | static final String TEST_RDF_FILE_PATH = TEST_DIR + File.separator + "SPDXRdfExample-v2.3.spdx.rdf"; 31 | static final String TEST_SPREADSHEET_XLS_FILE_PATH = TEST_DIR + File.separator + "SPDXSpreadsheetExample-v2.3.xls"; 32 | static final String TEST_SPREADSHEET_XLSX_FILE_PATH = TEST_DIR + File.separator + "SPDXSpreadsheetExample-v2.3.xlsx"; 33 | static final String TEST_TAG_FILE_PATH = TEST_DIR + File.separator + "SPDXTagExample-v2.3.spdx"; 34 | static final String TEST_XML_FILE_PATH = TEST_DIR + File.separator + "SPDXXMLExample-v2.3.spdx.xml"; 35 | static final String TEST_YAML_FILE_PATH = TEST_DIR + File.separator + "SPDXYAMLExample-2.3.spdx.yaml"; 36 | static final String TEST_WARNING_FILE_PATH = TEST_DIR + File.separator + "SPDXTagExample-v2.2-warning.spdx"; 37 | static final String BAD_JSON_FILE_PATH = TEST_DIR + File.separator + "BadJSON.spdx.json"; 38 | static final String DOUBLE_JSON_LD_FILE_PATH = TEST_DIR + File.separator + "double.jsonld"; 39 | 40 | protected void setUp() throws Exception { 41 | super.setUp(); 42 | ModelRegistry.getModelRegistry().registerModel(new SpdxModelInfoV3_0()); 43 | ModelRegistry.getModelRegistry().registerModel(new SpdxModelInfoV2_X()); 44 | DefaultModelStore.initialize(new InMemSpdxStore(), "http://default/namespace", new ModelCopyManager()); 45 | } 46 | 47 | protected void tearDown() throws Exception { 48 | super.tearDown(); 49 | } 50 | 51 | public void testUpsupportedVersionFields() throws SpdxVerificationException { 52 | List result = Verify.verify(TEST_V23_FIELDS_IN_V22_FILE, SerFileType.JSON); 53 | assertFalse(result.isEmpty()); 54 | } 55 | 56 | public void testVerifyTagFile() throws SpdxVerificationException { 57 | List result = Verify.verifyTagFile(TEST_TAG_FILE_PATH); 58 | assertEquals(0, result.size()); 59 | } 60 | 61 | public void testVerifyRDFFile() throws SpdxVerificationException { 62 | List result = Verify.verifyRDFFile(TEST_RDF_FILE_PATH); 63 | assertEquals(0, result.size()); 64 | } 65 | 66 | public void testVerify() throws SpdxVerificationException { 67 | List result = Verify.verify(TEST_JSON_FILE_PATH, SerFileType.JSON); 68 | assertEquals(0, result.size()); 69 | result = Verify.verify(TEST_SPREADSHEET_XLS_FILE_PATH, SerFileType.XLS); 70 | assertEquals(0, result.size()); 71 | result = Verify.verify(TEST_SPREADSHEET_XLSX_FILE_PATH, SerFileType.XLSX); 72 | assertEquals(0, result.size()); 73 | result = Verify.verify(TEST_XML_FILE_PATH, SerFileType.XML); 74 | assertEquals(0, result.size()); 75 | result = Verify.verify(TEST_YAML_FILE_PATH, SerFileType.YAML); 76 | assertEquals(0, result.size()); 77 | } 78 | 79 | public void testVerifyWarning() throws SpdxVerificationException { 80 | List result = Verify.verify(TEST_WARNING_FILE_PATH, SerFileType.TAG); 81 | assertFalse(result.isEmpty()); 82 | assertTrue(result.get(0).contains("deprecated")); 83 | } 84 | 85 | public void testVerifyBadJSON() throws SpdxVerificationException { 86 | List result = Verify.verify(BAD_JSON_FILE_PATH, SerFileType.JSON); 87 | assertEquals(4, result.size()); 88 | } 89 | 90 | public void testVerifyJsonLD() throws SpdxVerificationException { 91 | List result = Verify.verify(TEST_JSONLD_FILE_PATH, SerFileType.JSONLD); 92 | assertTrue(result.isEmpty()); 93 | } 94 | 95 | // Test specific spec versions for the JSON format 96 | public void testVerifyJSONVersion() throws SpdxVerificationException { 97 | List result = Verify.verify(JSON_V2_2_FILE_PATH, SerFileType.JSON); 98 | assertEquals(0, result.size()); 99 | result = Verify.verify(JSON_V2_3_FILE_PATH, SerFileType.JSON); 100 | assertEquals(0, result.size()); 101 | result = Verify.verify(JSON_BAD_VERSION_FILE_PATH, SerFileType.JSON); // a 2.3 version syntax with a 2.2 specversion 102 | assertFalse(result.isEmpty()); 103 | } 104 | 105 | public void testVerifyDouble() throws SpdxVerificationException { 106 | List result = Verify.verify(DOUBLE_JSON_LD_FILE_PATH, SerFileType.JSONLD); 107 | assertEquals(0, result.size()); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/schema/OwlToJsonContext.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.schema; 20 | 21 | import java.util.Collections; 22 | import java.util.HashMap; 23 | import java.util.Map; 24 | import java.util.Map.Entry; 25 | import java.util.Objects; 26 | import java.util.TreeMap; 27 | 28 | import org.apache.jena.ontology.OntModel; 29 | import org.apache.jena.ontology.OntProperty; 30 | import org.apache.jena.util.iterator.ExtendedIterator; 31 | import org.spdx.jacksonstore.MultiFormatStore; 32 | import org.spdx.library.model.v2.SpdxConstantsCompatV2; 33 | 34 | import com.fasterxml.jackson.databind.ObjectMapper; 35 | import com.fasterxml.jackson.databind.SerializationFeature; 36 | import com.fasterxml.jackson.databind.node.ObjectNode; 37 | 38 | /** 39 | * Convert OWL RDF schema to a JSON Context file 40 | * 41 | * @author Gary O'Neall 42 | */ 43 | public class OwlToJsonContext extends AbstractOwlRdfConverter { 44 | 45 | static final Map NAMESPACES; 46 | 47 | static { 48 | Map namespaceMap = new HashMap<>(); 49 | namespaceMap.put(SpdxConstantsCompatV2.SPDX_NAMESPACE, "spdx"); 50 | namespaceMap.put(SpdxConstantsCompatV2.RDFS_NAMESPACE, "rdfs"); 51 | namespaceMap.put(SpdxConstantsCompatV2.RDF_NAMESPACE, "rdf"); 52 | namespaceMap.put(SpdxConstantsCompatV2.RDF_POINTER_NAMESPACE, "rdfpointer"); 53 | namespaceMap.put(SpdxConstantsCompatV2.OWL_NAMESPACE, "owl"); 54 | namespaceMap.put(SpdxConstantsCompatV2.DOAP_NAMESPACE, "doap"); 55 | namespaceMap.put(SpdxConstantsCompatV2.XML_SCHEMA_NAMESPACE, "xs"); 56 | NAMESPACES = Collections.unmodifiableMap(namespaceMap); 57 | } 58 | 59 | public static final ObjectMapper JSON_MAPPER = new ObjectMapper() 60 | .enable(SerializationFeature.INDENT_OUTPUT); 61 | 62 | public OwlToJsonContext(OntModel model) { 63 | super(model); 64 | } 65 | 66 | /** 67 | * @return Object node containing a JSON context for the model 68 | */ 69 | public ObjectNode convertToContext() { 70 | ObjectNode contexts = JSON_MAPPER.createObjectNode(); 71 | NAMESPACES.forEach((namespace, name) -> { 72 | contexts.put(name, namespace); 73 | }); 74 | // Manually added contexts - specific to the JSON format 75 | ObjectNode documentContext = JSON_MAPPER.createObjectNode(); 76 | documentContext.put("@type", "spdx:SpdxDocument"); 77 | documentContext.put("@id", "spdx:spdxDocument"); 78 | contexts.set("Document", documentContext); 79 | contexts.put(SpdxConstantsCompatV2.SPDX_IDENTIFIER, "@id"); 80 | contexts.put(SpdxConstantsCompatV2.EXTERNAL_DOCUMENT_REF_IDENTIFIER, "@id"); 81 | TreeMap sortedOntProperties = new TreeMap<>(); 82 | ExtendedIterator iter = model.listAllOntProperties(); 83 | while (iter.hasNext()) { 84 | OntProperty property = iter.next(); 85 | if (property.isURIResource()) { 86 | String propNamespace = uriToNamespace(property.getURI()); 87 | String propName = uriToPropName(property.getURI()); 88 | String id = propNamespace + propName; 89 | sortedOntProperties.put(id, property); 90 | } 91 | } 92 | for (Entry ontPropEntry:sortedOntProperties.entrySet()) { 93 | String propNamespace = uriToNamespace(ontPropEntry.getValue().getURI()); 94 | String propName = uriToPropName(ontPropEntry.getValue().getURI()); 95 | 96 | PropertyRestrictions propertyRestrictions = new PropertyRestrictions(ontPropEntry.getValue()); 97 | boolean hasListProperty = propertyRestrictions.isListProperty(); 98 | String typeUri = propertyRestrictions.getTypeUri(); 99 | if (hasListProperty) { 100 | String listPropName = MultiFormatStore.propertyNameToCollectionPropertyName(propName); 101 | ObjectNode listPropContext = JSON_MAPPER.createObjectNode(); 102 | listPropContext.put("@id", propNamespace + listPropName); 103 | if (Objects.nonNull(typeUri)) { 104 | listPropContext.put("@type", uriToNamespace(typeUri) + uriToPropName(typeUri)); 105 | } 106 | listPropContext.put("@container", "@set"); 107 | contexts.set(listPropName, listPropContext); 108 | } if (propertyRestrictions.isSingleProperty()) { 109 | ObjectNode propContext = JSON_MAPPER.createObjectNode(); 110 | propContext.put("@id", ontPropEntry.getKey()); 111 | if (Objects.nonNull(typeUri)) { 112 | propContext.put("@type", uriToNamespace(typeUri) + uriToPropName(typeUri)); 113 | } 114 | contexts.set(propName, propContext); 115 | } 116 | } 117 | ObjectNode context = JSON_MAPPER.createObjectNode(); 118 | context.set("@context", contexts); 119 | return context; 120 | } 121 | 122 | 123 | /** 124 | * @param uri 125 | * @return The namespace portion of the URI 126 | */ 127 | private String uriToNamespace(String uri) { 128 | int poundIndex = uri.lastIndexOf('#'); 129 | String propNamespace = uri.substring(0, poundIndex+1); 130 | if (NAMESPACES.containsKey(propNamespace)) { 131 | propNamespace = NAMESPACES.get(propNamespace) + ":"; 132 | } 133 | return propNamespace; 134 | } 135 | 136 | /** 137 | * @param uri 138 | * @return The name portion of the URI 139 | */ 140 | private String uriToPropName(String uri) { 141 | int poundIndex = uri.lastIndexOf('#'); 142 | return checkConvertRenamedPropertyName(uri.substring(poundIndex+1)); 143 | } 144 | 145 | } 146 | -------------------------------------------------------------------------------- /examples/org/spdx/examples/ExistingSpdxDocumentV2Compat.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2021 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | package org.spdx.examples; 7 | 8 | import java.io.File; 9 | import java.io.FileInputStream; 10 | import java.io.FileNotFoundException; 11 | import java.io.IOException; 12 | import java.io.InputStream; 13 | import java.util.Collection; 14 | import java.util.List; 15 | import java.util.stream.Collectors; 16 | 17 | import org.spdx.jacksonstore.MultiFormatStore; 18 | import org.spdx.jacksonstore.MultiFormatStore.Format; 19 | import org.spdx.core.InvalidSPDXAnalysisException; 20 | import org.spdx.library.ModelCopyManager; 21 | import org.spdx.library.SpdxModelFactory; 22 | import org.spdx.library.model.v2.SpdxConstantsCompatV2; 23 | import org.spdx.library.model.v2.SpdxDocument; 24 | import org.spdx.library.model.v2.SpdxElement; 25 | import org.spdx.storage.ISerializableModelStore; 26 | import org.spdx.storage.simple.InMemSpdxStore; 27 | 28 | /** 29 | * This example demonstrate opening an existing SPDX spec version 2.X document and accessing it. The format 30 | * for this example is assumed to be JSON (e.g. the output of the SimpleSpdxDocumentV2Compat example). 31 | * Different format can be used by using the associated store rather than the spdx-jackson store 32 | * (e.g. spdx-spreadsheet-store, spdx-tagvalue-store, or the spdx-rdf-store). 33 | *

34 | * This example depends on the Spdx-Java-Library and the spdx-java-jackson store libraries 35 | *

36 | * @author Gary O'Neall 37 | */ 38 | public class ExistingSpdxDocumentV2Compat { 39 | 40 | /** 41 | * @param args args[0] is the file path containing the SPDX document 42 | */ 43 | public static void main(String[] args) { 44 | if (args.length != 1) { 45 | usage(); 46 | System.exit(1); 47 | } 48 | File inputFile = new File(args[0]); 49 | if (!inputFile.exists()) { 50 | System.out.println("Input file does not exist: "+args[0]); 51 | System.exit(1); 52 | } 53 | 54 | /* 55 | * First thing we need is a store deserialize the SPDX document into. 56 | * We'll chose the MultiFormatStore since it supports serializing to JSON files 57 | * It takes an underlying model store as the first parameter - the inMemSpdxStore is a simple 58 | * built in store included in the Spdx-Java-Library. The second parameter is the format 59 | * to use when serializing or deserializing 60 | */ 61 | ISerializableModelStore modelStore = new MultiFormatStore(new InMemSpdxStore(), Format.JSON_PRETTY); 62 | /* 63 | * There are several other choices which can be made for a model store - most of which 64 | * are in separate libraries that need to be included as dependencies. Below are a few examples: 65 | * IModelStore modelStore = new InMemSpdxStore(); - the simplest store, but does not support serializing or deserializing 66 | * ISerializableModelStore modelStore = new TagValueStore(new InMemSpdxStore()); Supports serializing and deserializing SPDX tag/value files 67 | * ISerializableModelStore modelStore = new RdfStore(); Supports serializing and deserializing various RDF formats (e.g. RDF/XML, RDF/Turtle) 68 | * ISerializableModelStore modelStore = new SpreadsheetStore(new InMemSpdxStore()); Supports serializing and deserializing .XLS or .XLSX spreadsheets 69 | */ 70 | 71 | /* 72 | * The ModelCopyManager is used when using more than one Model Store. The Listed Licenses uses 73 | * it's own model store, so we will need a ModelCopyManager to manage the copying of the listed 74 | * license information over to the document model store 75 | */ 76 | ModelCopyManager copyManager = new ModelCopyManager(); 77 | // Let's deserialize the document 78 | try (InputStream stream = new FileInputStream(inputFile)) { 79 | modelStore.deSerialize(stream, false); 80 | 81 | } catch (FileNotFoundException e1) { 82 | System.out.println("Input file does not exist: "+args[0]); 83 | System.exit(1); 84 | } catch (IOException e1) { 85 | System.out.println("I/O error reading input file: "+args[0]); 86 | System.exit(1); 87 | } catch (InvalidSPDXAnalysisException e) { 88 | System.out.println("The SPDX document is not valid: "+e.getMessage()); 89 | System.exit(1); 90 | } 91 | // Now that the document is deserialized, we can access it using the SpdxModelFactory 92 | try { 93 | // To find all the SPDX documents in the model store, use the getObjects method from the 94 | // SpdxModelFactory passing in the SpdxDocument type 95 | // When using the factory method, we have to type cast the result 96 | @SuppressWarnings("unchecked") 97 | List allDocs = (List) SpdxModelFactory.getSpdxObjects(modelStore, copyManager, 98 | SpdxConstantsCompatV2.CLASS_SPDX_DOCUMENT, null, null) 99 | .collect(Collectors.toList()); 100 | SpdxDocument document = allDocs.get(0); 101 | String documentUri = document.getDocumentUri(); 102 | // If you know the document URI, you can simply create an SPDX document using the followint constructor 103 | SpdxDocument document2 = new SpdxDocument(modelStore, documentUri, copyManager, false); 104 | // Note that all class objects in the Spdx Java Library follow the same pattern - 105 | // to access any existing object in the store, simply create the object passing in 106 | // the document URI, model store and the ID for the object 107 | // Since the 2 documents are just references to the same object, they will always be equivalent 108 | if (!document.equivalent(document2)) { 109 | System.out.println("Oops - these 2 documents should be the same"); 110 | System.exit(1); 111 | } 112 | // Let's see find what the document is describing 113 | Collection described = document.getDocumentDescribes(); 114 | for (SpdxElement des:described) { 115 | System.out.println("The document described ID "+des.getId()); 116 | } 117 | System.exit(0); 118 | } catch (InvalidSPDXAnalysisException e) { 119 | System.out.println("Unexpected error reading SPDX document: "+e.getMessage()); 120 | System.exit(1); 121 | } 122 | } 123 | 124 | private static void usage() { 125 | System.out.println("Usage: ExistingSpxDocument inputFilePath"); 126 | } 127 | 128 | } 129 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SPDX Tools 2 | 3 | [![Maven Central Version](https://img.shields.io/maven-central/v/org.spdx/tools-java)](https://central.sonatype.com/artifact/org.spdx/tools-java) 4 | [![javadoc](https://javadoc.io/badge2/org.spdx/tools-java/javadoc.svg)](https://javadoc.io/doc/org.spdx/tools-java) 5 | 6 | A command-line utility for creating, converting, comparing, 7 | and validating SPDX documents across multiple formats. 8 | 9 | The Software Package Data Exchange (SPDX) specification is a standard format for communicating the components, licenses and copyrights associated with a software package. 10 | 11 | * [SPDX License List](https://spdx.org/licenses/) 12 | * [SPDX Vocabulary Specification](https://spdx.org/specifications) 13 | 14 | These tools are published by the SPDX Workgroup, 15 | see 16 | 17 | ## Versions Supported 18 | 19 | This utility supports versions 2.0, 2.1, 2.2, 2.3 and 3.0.1 of the SPDX specification. 20 | 21 | ## Code quality badges 22 | 23 | [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=tools-java&metric=bugs)](https://sonarcloud.io/dashboard?id=tools-java) 24 | [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=tools-java&metric=security_rating)](https://sonarcloud.io/dashboard?id=tools-java) 25 | [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=tools-java&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=tools-java) 26 | [![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=tools-java&metric=sqale_index)](https://sonarcloud.io/dashboard?id=tools-java) 27 | 28 | ## Getting Starting 29 | 30 | The SPDX Tools binaries can be downloaded from the [releases page](https://github.com/spdx/tools-java/releases) under the respective release. The package is also available in [Maven Central](https://central.sonatype.com/artifact/org.spdx/tools-java) (organization `org.spdx`, artifact `tools-java`). 31 | 32 | See the Syntax section below for the commands available. 33 | 34 | If you are a developer, there are examples in the [examples folder](examples/org/spdx/examples). 35 | 36 | ## Syntax 37 | 38 | The command line interface of the SPDX Tools can be used like this: 39 | 40 | java -jar tools-java-2.0.2-jar-with-dependencies.jar 41 | 42 | ## SPDX format converters 43 | 44 | The following converter tools support SPDX format: 45 | 46 | * Tag 47 | * RDF/XML 48 | * XLSX Spreadsheet 49 | * XLS Spreadsheet 50 | * JSON 51 | * XML 52 | * YAML 53 | * JSON-LD (SPDX spec version 3.0.1) 54 | 55 | Example to convert a SPDX file from Tag to RDF format: 56 | 57 | java -jar tools-java-2.0.2-jar-with-dependencies.jar Convert ../testResources/SPDXTagExample-v2.2.spdx TagToRDF.rdf 58 | 59 | The file formats can optionally be provided as the 3rd and 4th parameter for the input and output formats respectively. An optional 5th option `excludeLicenseDetails` will not copy the listed license properties to the output file. The following example will copy a JSON format to an RDF Turtle format without including the listed license properties: 60 | 61 | java -jar tools-java-2.0.2-jar-with-dependencies.jar Convert ../testResources/SPDXTagExample-v2.2.spdx TagToRDF.ttl TAG RDFTTL excludeLicenseDetails 62 | 63 | To convert from SPDX 2 to SPDX 3.0.1: 64 | 65 | * use the file extension `.jsonld.json` or `.jsonld`; 66 | * or add the options for the from and to file types: 67 | 68 | java -jar tools-java-2.0.2-jar-with-dependencies.jar Convert hello.spdx hello.spdx.json TAG JSONLD 69 | 70 | ## Compare utilities 71 | 72 | The following tools can be used to compare one or more SPDX documents: 73 | 74 | * CompareMultipleSpdxDocs with files 75 | 76 | Example to compare multiple SPDX files provided in RDF format and provide a spreadsheet with the results: 77 | 78 | java -jar tools-java-2.0.2-jar-with-dependencies.jar CompareDocs output.xlsx doc1 doc2 ... docN 79 | 80 | * CompareMultipleSpdxDocs with directory 81 | 82 | Example to compare all SPDX documents in a directory "/home/me/spdxdocs" and provide a spreadsheet with the results: 83 | 84 | java -jar tools-java-2.0.2-jar-with-dependencies.jar CompareDocs output.xlsx /home/me/spdxdocs 85 | 86 | ## SPDX Viewer 87 | 88 | The following tool can be used to "Pretty Print" an SPDX document. 89 | 90 | * SPDXViewer 91 | 92 | Sample usage: 93 | 94 | java -jar tools-java-2.0.2-jar-with-dependencies.jar SPDXViewer ../testResources/SPDXRdfExample-v2.2.spdx.rdf 95 | 96 | ## Verifier 97 | 98 | The following tool can be used to verify an SPDX document: 99 | 100 | * Verify 101 | 102 | Sample usage: 103 | 104 | java -jar tools-java-2.0.2-jar-with-dependencies.jar Verify ../testResources/SPDXRdfExample-v2.2.spdx.rdf 105 | 106 | ## Generators 107 | 108 | The following tool can be used to generate an SPDX verification code from a directory of source files: 109 | 110 | * GenerateVerificationCode sourceDirectory 111 | 112 | Sample usage: 113 | 114 | java -jar tools-java-2.0.2-jar-with-dependencies.jar GenerateVerificationCode sourceDirectory [ignoredFilesRegex] 115 | 116 | ## SPDX Validation Tool 117 | 118 | The SPDX Workgroup provides an online interface to validate, compare, and convert SPDX documents in addition to the command line options above. 119 | 120 | The [SPDX Online Tools](https://tools.spdx.org/) is an all-in-one portal to upload and parse SPDX documents for validation, comparison and conversion and search the SPDX license list. 121 | 122 | ## License 123 | 124 | A complete SPDX file is available including dependencies is available in the bintray and Maven repos. 125 | 126 | SPDX-License-Identifier: Apache-2.0 127 | PackageLicenseDeclared: Apache-2.0 128 | 129 | ## Development 130 | 131 | ### Build 132 | 133 | You need [Apache Maven](http://maven.apache.org/) to build the project: 134 | 135 | mvn clean install 136 | 137 | ## Contributing 138 | 139 | See the file [CONTRIBUTING.md](./CONTRIBUTING.md) for information on 140 | making contributions to the SPDX tools. 141 | 142 | ## Issues 143 | 144 | Report any security related issues by sending an email to [spdx-tools-security@lists.spdx.org](mailto:spdx-tools-security@lists.spdx.org) 145 | 146 | Non-security related issues should be added to the [SPDX Tools issues list](https://github.com/spdx/tools-java/issues) 147 | -------------------------------------------------------------------------------- /src/main/java/org/spdx/tools/GenerateVerificationCode.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2011 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools; 20 | 21 | import java.io.File; 22 | import java.io.IOException; 23 | import java.security.NoSuchAlgorithmException; 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | import java.util.Objects; 27 | import java.util.regex.Pattern; 28 | 29 | import javax.annotation.Nullable; 30 | 31 | import org.spdx.core.InvalidSPDXAnalysisException; 32 | import org.spdx.library.model.v2.SpdxPackageVerificationCode; 33 | import org.spdx.storage.IModelStore; 34 | import org.spdx.storage.simple.InMemSpdxStore; 35 | import org.spdx.utility.verificationcode.JavaSha1ChecksumGenerator; 36 | import org.spdx.utility.verificationcode.VerificationCodeGenerator; 37 | 38 | /** 39 | * Generates a verification code for a specific directory 40 | * @author Gary O'Neall 41 | */ 42 | public class GenerateVerificationCode { 43 | 44 | /** 45 | * Print an SPDX Verification code for a directory of files 46 | * args[0] is the source directory containing the files 47 | * args[1] is an optional regular expression of skipped files. The expression is applied against a file path relative the the source directory supplied 48 | * @param args 49 | */ 50 | public static void main(String[] args) { 51 | if (args.length < 1 || args.length > 2) { 52 | error("Incorrect number of arguments."); 53 | System.exit(1); 54 | } 55 | String directoryPath = args[0]; 56 | String skippedRegex = null; 57 | if (args.length > 1) { 58 | skippedRegex = args[1]; 59 | } 60 | 61 | SpdxToolsHelper.initialize(); 62 | try { 63 | SpdxPackageVerificationCode verificationCode = generateVerificationCode(directoryPath, skippedRegex); 64 | printVerificationCode(verificationCode); 65 | System.exit(0); 66 | } catch (Exception ex) { 67 | error("Error creating verification code: "+ex.getMessage()); 68 | System.exit(1); 69 | } 70 | } 71 | 72 | public static SpdxPackageVerificationCode generateVerificationCode(String directoryPath, @Nullable String skippedRegex) throws OnlineToolException { 73 | Objects.requireNonNull(directoryPath, "Directory path must not be null"); 74 | File sourceDirectory = new File(directoryPath); 75 | if (!sourceDirectory.exists()) { 76 | throw new OnlineToolException("Source directory "+directoryPath+" does not exist."); 77 | } 78 | if (!sourceDirectory.isDirectory()) { 79 | throw new OnlineToolException("File "+directoryPath+" is not a directory."); 80 | } 81 | File[] skippedFiles = new File[0]; 82 | if (Objects.nonNull(skippedRegex)) { 83 | skippedFiles = collectSkippedFiles(skippedRegex, sourceDirectory); 84 | } 85 | try { 86 | VerificationCodeGenerator vcg = new VerificationCodeGenerator(new JavaSha1ChecksumGenerator()); 87 | IModelStore ms = new InMemSpdxStore(); 88 | return vcg.generatePackageVerificationCode(sourceDirectory, skippedFiles, ms, "https://temp/URI"); 89 | } catch (NoSuchAlgorithmException e) { 90 | throw new OnlineToolException("Error creating checksum algorithm",e); 91 | } catch (IOException e) { 92 | throw new OnlineToolException("I/O Error generating verification code",e); 93 | } catch (InvalidSPDXAnalysisException e) { 94 | throw new OnlineToolException("SPDX Analysis Error generating verification code",e); 95 | } 96 | } 97 | 98 | /** 99 | * Collect files to be skipped 100 | * @param skippedRegex Regular Expression for file paths to be skipped 101 | * @param dir Directory to scan for collecting skipped files 102 | * @return 103 | */ 104 | private static File[] collectSkippedFiles(String skippedRegex, File dir) { 105 | Pattern skippedPattern = Pattern.compile(skippedRegex); 106 | List skippedFiles = new ArrayList<>(); 107 | collectSkippedFiles(skippedPattern, skippedFiles, dir.getPath(), dir); 108 | File[] retval = new File[skippedFiles.size()]; 109 | retval = skippedFiles.toArray(retval); 110 | return retval; 111 | } 112 | 113 | /** 114 | * Internal method to recurse through the source directory collecting files to skip 115 | * @param skippedPattern 116 | * @param skippedFiles 117 | * @param rootPath 118 | * @param dir 119 | * @return 120 | */ 121 | private static void collectSkippedFiles(Pattern skippedPattern, 122 | List skippedFiles, String rootPath, File dir) { 123 | if (dir.isFile()) { 124 | String relativePath = dir.getPath().substring(rootPath.length()+1); 125 | if (skippedPattern.matcher(relativePath).matches()) { 126 | skippedFiles.add(dir); 127 | } 128 | } else if (dir.isDirectory()) { 129 | File[] children = dir.listFiles(); 130 | if (children != null) { 131 | for (int i = 0; i < children.length; i++) { 132 | if (children[i].isFile()) { 133 | String relativePath = children[i].getPath().substring(rootPath.length()+1); 134 | if (skippedPattern.matcher(relativePath).matches()) { 135 | skippedFiles.add(children[i]); 136 | } 137 | } else if (children[i].isDirectory()) { 138 | collectSkippedFiles(skippedPattern, skippedFiles, rootPath, children[i]); 139 | } 140 | } 141 | } 142 | } 143 | } 144 | 145 | /** 146 | * @param verificationCode 147 | * @throws InvalidSPDXAnalysisException 148 | */ 149 | private static void printVerificationCode( 150 | SpdxPackageVerificationCode verificationCode) throws InvalidSPDXAnalysisException { 151 | System.out.println("Verification code value: "+verificationCode.getValue()); 152 | String[] excludedFiles = verificationCode.getExcludedFileNames().toArray(new String[verificationCode.getExcludedFileNames().size()]); 153 | if (excludedFiles != null && excludedFiles.length > 0) { 154 | System.out.println("Excluded files:"); 155 | for (int i = 0; i < excludedFiles.length; i++) { 156 | System.out.println("\t"+excludedFiles[i]); 157 | } 158 | } else { 159 | System.out.println("No excluded files"); 160 | } 161 | } 162 | 163 | /** 164 | * @param string 165 | */ 166 | private static void error(String string) { 167 | System.out.println(string); 168 | usage(); 169 | } 170 | 171 | /** 172 | * 173 | */ 174 | private static void usage() { 175 | System.out.println("Usage: GenerateVerificationCode sourceDirectory [skippedFiles]"); 176 | System.out.println("where sourceDirectory is the root of the archive file for which the verification code is generated and [skippedFiles] is an optional regular expression of skipped files. The expression is applied against a file path relative the the source directory supplied"); 177 | } 178 | 179 | } 180 | -------------------------------------------------------------------------------- /src/test/java/org/spdx/tools/CompareSpdxDocsTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2020 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools; 20 | 21 | import java.io.File; 22 | import java.io.IOException; 23 | import java.nio.file.Files; 24 | import java.nio.file.Path; 25 | import java.util.Objects; 26 | 27 | import org.apache.poi.ss.usermodel.Cell; 28 | import org.apache.poi.ss.usermodel.Row; 29 | import org.spdx.core.DefaultModelStore; 30 | import org.spdx.core.InvalidSPDXAnalysisException; 31 | import org.spdx.core.ModelRegistry; 32 | import org.spdx.library.ModelCopyManager; 33 | import org.spdx.library.model.v2.SpdxModelInfoV2_X; 34 | import org.spdx.library.model.v3_0_1.SpdxModelInfoV3_0; 35 | import org.spdx.spreadsheetstore.SpreadsheetException; 36 | import org.spdx.storage.simple.InMemSpdxStore; 37 | import org.spdx.tools.compare.DocumentSheet; 38 | import org.spdx.tools.compare.MultiDocumentSpreadsheet; 39 | 40 | import junit.framework.TestCase; 41 | 42 | /** 43 | * Test cases for CompareSpdxDocs 44 | * 45 | * @author Gary O'Neall 46 | */ 47 | public class CompareSpdxDocsTest extends TestCase { 48 | 49 | static final String TEST_DIR = "testResources"; 50 | static final String TEST_JSON_FILE_PATH_22 = TEST_DIR + File.separator + "SPDXJSONExample-v2.2.spdx.json"; 51 | static final String TEST_RDF_FILE_PATH_22 = TEST_DIR + File.separator + "SPDXRdfExample-v2.2.spdx.rdf"; 52 | static final String TEST_SPREADSHEET_XLS_FILE_PATH_22 = TEST_DIR + File.separator + "SPDXSpreadsheetExample-v2.2.xls"; 53 | static final String TEST_SPREADSHEET_XLSX_FILE_PATH_22 = TEST_DIR + File.separator + "SPDXSpreadsheetExample-v2.2.xlsx"; 54 | static final String TEST_TAG_FILE_PATH_22 = TEST_DIR + File.separator + "SPDXTagExample-v2.2.spdx"; 55 | static final String TEST_XML_FILE_PATH_22 = TEST_DIR + File.separator + "SPDXXMLExample-v2.2.spdx.xml"; 56 | static final String TEST_YAML_FILE_PATH_22 = TEST_DIR + File.separator + "SPDXYAMLExample-2.2.spdx.yaml"; 57 | 58 | static final String TEST_JSON_FILE_PATH_23 = TEST_DIR + File.separator + "SPDXJSONExample-v2.3.spdx.json"; 59 | static final String TEST_RDF_FILE_PATH_23 = TEST_DIR + File.separator + "SPDXRdfExample-v2.3.spdx.rdf"; 60 | static final String TEST_SPREADSHEET_XLS_FILE_PATH_23 = TEST_DIR + File.separator + "SPDXSpreadsheetExample-v2.3.xls"; 61 | static final String TEST_SPREADSHEET_XLSX_FILE_PATH_23 = TEST_DIR + File.separator + "SPDXSpreadsheetExample-v2.3.xlsx"; 62 | static final String TEST_TAG_FILE_PATH_23 = TEST_DIR + File.separator + "SPDXTagExample-v2.3.spdx"; 63 | static final String TEST_XML_FILE_PATH_23 = TEST_DIR + File.separator + "SPDXXMLExample-v2.3.spdx.xml"; 64 | static final String TEST_YAML_FILE_PATH_23 = TEST_DIR + File.separator + "SPDXYAMLExample-2.3.spdx.yaml"; 65 | 66 | static final String TEST_DIFF_FILE_COMMENT_FILE_PATH = TEST_DIR + File.separator + "DifferentFileComment.spdx.yaml"; 67 | 68 | 69 | 70 | Path tempDirPath; 71 | /* (non-Javadoc) 72 | * @see junit.framework.TestCase#setUp() 73 | */ 74 | protected void setUp() throws Exception { 75 | super.setUp(); 76 | ModelRegistry.getModelRegistry().registerModel(new SpdxModelInfoV3_0()); 77 | ModelRegistry.getModelRegistry().registerModel(new SpdxModelInfoV2_X()); 78 | DefaultModelStore.initialize(new InMemSpdxStore(), "http://default/namespace", new ModelCopyManager()); 79 | tempDirPath = Files.createTempDirectory("spdx-tools-test-"); 80 | } 81 | 82 | /* (non-Javadoc) 83 | * @see junit.framework.TestCase#tearDown() 84 | */ 85 | protected void tearDown() throws Exception { 86 | super.tearDown(); 87 | SpdxConverterTestV2.deleteDirAndFiles(tempDirPath); 88 | } 89 | 90 | public void testCompareDocumentsv23() throws OnlineToolException, InvalidSPDXAnalysisException, IOException, InvalidFileNameException { 91 | String outputFilePath = tempDirPath + File.separator + "comp.xlsx"; 92 | String[] params = new String[] {outputFilePath, 93 | TEST_JSON_FILE_PATH_23, 94 | TEST_RDF_FILE_PATH_23, 95 | TEST_SPREADSHEET_XLS_FILE_PATH_23, 96 | TEST_SPREADSHEET_XLSX_FILE_PATH_23, 97 | TEST_TAG_FILE_PATH_23, 98 | TEST_XML_FILE_PATH_23, 99 | TEST_YAML_FILE_PATH_23 100 | }; 101 | CompareSpdxDocs.onlineFunction(params); 102 | MultiDocumentSpreadsheet result = new MultiDocumentSpreadsheet(new File(outputFilePath), false, true); 103 | DocumentSheet docSheet = result.getDocumentSheet(); 104 | Row resultRow = docSheet.getSheet().getRow(docSheet.getFirstDataRow()); 105 | Cell cell = resultRow.getCell(1); 106 | int nextCol = 2; 107 | while (Objects.nonNull(cell) && !cell.getStringCellValue().isEmpty()) { 108 | assertTrue("Equals".equals(cell.getStringCellValue()) || "N/A".equals(cell.getStringCellValue())); 109 | cell = resultRow.getCell(nextCol++); 110 | } 111 | } 112 | 113 | public void testCompareDocumentsv22() throws OnlineToolException, SpreadsheetException { 114 | String outputFilePath = tempDirPath + File.separator + "comp.xlsx"; 115 | String[] params = new String[] {outputFilePath, TEST_JSON_FILE_PATH_22, 116 | TEST_RDF_FILE_PATH_22, TEST_SPREADSHEET_XLS_FILE_PATH_22, 117 | TEST_SPREADSHEET_XLSX_FILE_PATH_22, TEST_TAG_FILE_PATH_22, 118 | TEST_XML_FILE_PATH_22, 119 | TEST_YAML_FILE_PATH_22 120 | }; 121 | CompareSpdxDocs.onlineFunction(params); 122 | MultiDocumentSpreadsheet result = new MultiDocumentSpreadsheet(new File(outputFilePath), false, true); 123 | DocumentSheet docSheet = result.getDocumentSheet(); 124 | Row resultRow = docSheet.getSheet().getRow(docSheet.getFirstDataRow()); 125 | Cell cell = resultRow.getCell(1); 126 | int nextCol = 2; 127 | while (Objects.nonNull(cell) && !cell.getStringCellValue().isEmpty()) { 128 | String cellResult = cell.getStringCellValue(); 129 | assertTrue("Equals".equals(cell.getStringCellValue()) || "N/A".equals(cellResult)); 130 | cell = resultRow.getCell(nextCol++); 131 | } 132 | } 133 | 134 | 135 | public void testDifferentDocuments() throws OnlineToolException, SpreadsheetException { 136 | String outputFilePath = tempDirPath + File.separator + "comp.xlsx"; 137 | String[] params = new String[] {outputFilePath, 138 | TEST_YAML_FILE_PATH_22, TEST_DIFF_FILE_COMMENT_FILE_PATH 139 | }; 140 | CompareSpdxDocs.onlineFunction(params); 141 | MultiDocumentSpreadsheet result = new MultiDocumentSpreadsheet(new File(outputFilePath), false, true); 142 | DocumentSheet docSheet = result.getDocumentSheet(); 143 | Row resultRow = docSheet.getSheet().getRow(docSheet.getFirstDataRow()); 144 | Row titleRow = docSheet.getSheet().getRow(docSheet.getFirstDataRow()-1); 145 | Cell cell = resultRow.getCell(1); 146 | int nextCol = 2; 147 | while (Objects.nonNull(cell) && !cell.getStringCellValue().isEmpty()) { 148 | if ("Document Describes".equals(titleRow.getCell(nextCol-1).getStringCellValue()) || "Relationships".equals(titleRow.getCell(nextCol-1).getStringCellValue())) { 149 | assertTrue("Diff".equals(cell.getStringCellValue())); 150 | } else { 151 | assertTrue("Equals".equals(cell.getStringCellValue()) || "N/A".equals(cell.getStringCellValue())); 152 | } 153 | cell = resultRow.getCell(nextCol++); 154 | } 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /testResources/sourcefiles/DocumentRelationshipSheet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2015 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package org.spdx.tools.compare; 20 | 21 | import java.io.Serializable; 22 | import java.util.Arrays; 23 | import java.util.Comparator; 24 | import java.util.List; 25 | 26 | import org.apache.poi.ss.usermodel.Cell; 27 | import org.apache.poi.ss.usermodel.CellStyle; 28 | import org.apache.poi.ss.usermodel.Row; 29 | import org.apache.poi.ss.usermodel.Sheet; 30 | import org.apache.poi.ss.usermodel.Workbook; 31 | import org.spdx.library.InvalidSPDXAnalysisException; 32 | import org.spdx.library.model.Relationship; 33 | import org.spdx.utility.compare.SpdxCompareException; 34 | import org.spdx.utility.compare.SpdxComparer; 35 | 36 | /** 37 | * Sheet for document level relationships 38 | * @author Gary O'Neall 39 | */ 40 | public class DocumentRelationshipSheet extends AbstractSheet { 41 | 42 | private static class RelationshipComparator implements Comparator, Serializable { 43 | 44 | /** 45 | * Default 46 | */ 47 | private static final long serialVersionUID = 1L; 48 | 49 | /* (non-Javadoc) 50 | * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 51 | */ 52 | @Override 53 | public int compare(Relationship o1, Relationship o2) { 54 | try { 55 | if (o1 != null) { 56 | if (o2 != null) { 57 | Relationship r1 = o1; 58 | Relationship r2 = o2; 59 | int retval = r1.getRelationshipType().toString().compareTo(r2.getRelationshipType().toString()); 60 | if (retval != 0) { 61 | return retval; 62 | } else if (r1.getRelatedSpdxElement().isPresent() && !r2.getRelatedSpdxElement().isPresent()) { 63 | return 1; 64 | } else if (!r1.getRelatedSpdxElement().isPresent() && r2.getRelatedSpdxElement().isPresent()) { 65 | return -1; 66 | } else if (r1.getRelatedSpdxElement().get().equivalent(r2.getRelatedSpdxElement().get())) { 67 | return 0; 68 | } else if (r1.getRelatedSpdxElement().get().getName().isPresent() && 69 | r2.getRelatedSpdxElement().get().getName().isPresent()) { 70 | return r1.getRelatedSpdxElement().get().getName().get().compareTo( 71 | r2.getRelatedSpdxElement().get().getName().get()); 72 | } else { 73 | return r1.getRelatedSpdxElement().get().getId().compareTo(r2.getRelatedSpdxElement().get().getId()); 74 | } 75 | } else { 76 | return 1; 77 | } 78 | } else { 79 | return -1; 80 | } 81 | } catch(InvalidSPDXAnalysisException ex) { 82 | logger.error("Error comparing relationships", ex); 83 | throw new RuntimeException(ex); 84 | } 85 | 86 | } 87 | } 88 | 89 | RelationshipComparator relationshipComparator = new RelationshipComparator(); 90 | 91 | static final int TYPE_COL = 0; 92 | static final int TYPE_COL_WIDTH = 25; 93 | static final String TYPE_COL_TEXT_TITLE = "Type"; 94 | static final int FIRST_RELATIONSHIP_COL = 1; 95 | static final int FIRST_RELATIONSHIP_COL_WIDTH = 60; 96 | 97 | public DocumentRelationshipSheet(Workbook workbook, String sheetName) { 98 | super(workbook, sheetName); 99 | } 100 | 101 | /** 102 | * @param wb 103 | * @param sheetName 104 | */ 105 | public static void create(Workbook wb, String sheetName) { 106 | int sheetNum = wb.getSheetIndex(sheetName); 107 | if (sheetNum >= 0) { 108 | wb.removeSheetAt(sheetNum); 109 | } 110 | Sheet sheet = wb.createSheet(sheetName); 111 | CellStyle headerStyle = AbstractSheet.createHeaderStyle(wb); 112 | CellStyle defaultStyle = AbstractSheet.createLeftWrapStyle(wb); 113 | Row row = sheet.createRow(0); 114 | 115 | sheet.setColumnWidth(TYPE_COL, TYPE_COL_WIDTH*256); 116 | sheet.setDefaultColumnStyle(TYPE_COL, defaultStyle); 117 | Cell typeHeaderCell = row.createCell(TYPE_COL); 118 | typeHeaderCell.setCellStyle(headerStyle); 119 | typeHeaderCell.setCellValue(TYPE_COL_TEXT_TITLE); 120 | 121 | for (int i = FIRST_RELATIONSHIP_COL; i < MultiDocumentSpreadsheet.MAX_DOCUMENTS; i++) { 122 | sheet.setColumnWidth(i, FIRST_RELATIONSHIP_COL_WIDTH*256); 123 | sheet.setDefaultColumnStyle(i, defaultStyle); 124 | Cell cell = row.createCell(i); 125 | cell.setCellStyle(headerStyle); 126 | } 127 | } 128 | 129 | /** 130 | * @param comparer 131 | * @param docNames 132 | * @throws InvalidSPDXAnalysisException 133 | */ 134 | public void importCompareResults(SpdxComparer comparer, List docNames) throws SpdxCompareException, InvalidSPDXAnalysisException { 135 | if (comparer.getNumSpdxDocs() != docNames.size()) { 136 | throw(new SpdxCompareException("Number of document names does not match the number of SPDX documents")); 137 | } 138 | this.clear(); 139 | Row header = sheet.getRow(0); 140 | int[] relationshipsIndexes = new int[comparer.getNumSpdxDocs()]; 141 | Relationship[][] relationships = new Relationship[comparer.getNumSpdxDocs()][]; 142 | for (int i = 0; i < relationships.length; i++) { 143 | Cell headerCell = header.getCell(FIRST_RELATIONSHIP_COL+i); 144 | headerCell.setCellValue(docNames.get(i)); 145 | Relationship[] docRelationships = comparer.getSpdxDoc(i).getRelationships().toArray(new Relationship[comparer.getSpdxDoc(i).getRelationships().size()]); 146 | Arrays.sort(docRelationships, relationshipComparator); 147 | relationships[i] = docRelationships; 148 | relationshipsIndexes[i] = 0; 149 | } 150 | while (!allRelationshipsExhausted(relationships, relationshipsIndexes)) { 151 | Row currentRow = this.addRow(); 152 | Relationship nextRelationship = getNexRelationship(relationships, relationshipsIndexes); 153 | Cell typeCell = currentRow.createCell(TYPE_COL); 154 | typeCell.setCellValue(nextRelationship.getRelationshipType().toString()); 155 | for (int i = 0; i < relationships.length; i++) { 156 | if (relationships[i].length > relationshipsIndexes[i]) { 157 | Relationship compareRelationship = relationships[i][relationshipsIndexes[i]]; 158 | if (relationshipComparator.compare(nextRelationship, compareRelationship) == 0) { 159 | Cell relationshipCell = currentRow.createCell(FIRST_RELATIONSHIP_COL+i); 160 | relationshipCell.setCellValue(CompareHelper.relationshipToString(relationships[i][relationshipsIndexes[i]])); 161 | relationshipsIndexes[i]++; 162 | } 163 | } 164 | } 165 | } 166 | } 167 | 168 | 169 | /** 170 | * @param relationships 171 | * @param relationshipsIndexes 172 | * @return 173 | */ 174 | private Relationship getNexRelationship(Relationship[][] relationships, 175 | int[] relationshipsIndexes) { 176 | Relationship retval = null; 177 | for (int i = 0; i < relationships.length; i++) { 178 | if (relationships[i].length > relationshipsIndexes[i]) { 179 | Relationship candidate = relationships[i][relationshipsIndexes[i]]; 180 | if (retval == null || this.relationshipComparator.compare(retval, candidate) > 0) { 181 | retval = candidate; 182 | } 183 | } 184 | } 185 | return retval; 186 | } 187 | 188 | /** 189 | * @param relationships 190 | * @param relationshipsIndexes 191 | * @return 192 | */ 193 | private boolean allRelationshipsExhausted(Relationship[][] relationships, 194 | int[] relationshipsIndexes) { 195 | for (int i = 0; i < relationships.length; i++) { 196 | if (relationshipsIndexes[i] < relationships[i].length) { 197 | return false; 198 | } 199 | } 200 | return true; 201 | } 202 | 203 | /* (non-Javadoc) 204 | * @see org.spdx.spdxspreadsheet.AbstractSheet#verify() 205 | */ 206 | @Override 207 | public String verify() { 208 | return null; // Nothing to verify 209 | } 210 | 211 | } 212 | -------------------------------------------------------------------------------- /examples/org/spdx/examples/SimpleSpdxDocumentV2Compat.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SPDX-FileCopyrightText: Copyright (c) 2021 Source Auditor Inc. 3 | * SPDX-FileType: SOURCE 4 | * SPDX-License-Identifier: Apache-2.0 5 | */ 6 | package org.spdx.examples; 7 | 8 | import java.io.File; 9 | import java.io.FileOutputStream; 10 | import java.io.IOException; 11 | import java.io.OutputStream; 12 | import java.text.SimpleDateFormat; 13 | import java.util.Arrays; 14 | import java.util.Date; 15 | import java.util.List; 16 | 17 | import org.spdx.jacksonstore.MultiFormatStore; 18 | import org.spdx.jacksonstore.MultiFormatStore.Format; 19 | import org.spdx.core.InvalidSPDXAnalysisException; 20 | import org.spdx.library.LicenseInfoFactory; 21 | import org.spdx.library.ModelCopyManager; 22 | import org.spdx.library.model.v2.Relationship; 23 | import org.spdx.library.model.v2.SpdxConstantsCompatV2; 24 | import org.spdx.library.model.v2.SpdxDocument; 25 | import org.spdx.library.model.v2.SpdxPackage; 26 | import org.spdx.library.model.v2.enumerations.RelationshipType; 27 | import org.spdx.library.model.v2.license.AnyLicenseInfo; 28 | import org.spdx.storage.IModelStore.IdType; 29 | import org.spdx.storage.ISerializableModelStore; 30 | import org.spdx.storage.simple.InMemSpdxStore; 31 | 32 | /** 33 | * This example demonstrate programmatically creating an SPDX spec version 2.X document, adding document, files 34 | * and saving the document in a JSON file format 35 | * 36 | * This example depends on the Spdx-Java-Library and the spdx-java-jackson store libraries 37 | * 38 | * @author Gary O'Neall 39 | */ 40 | public class SimpleSpdxDocumentV2Compat { 41 | 42 | /** 43 | * @param args args[0] is the file path to store the resultant JSON file 44 | */ 45 | public static void main(String[] args) { 46 | if (args.length != 1) { 47 | usage(); 48 | System.exit(1); 49 | } 50 | File outFile = new File(args[0]); 51 | if (outFile.exists()) { 52 | System.out.println("Output file already exists: "+args[0]); 53 | System.exit(1); 54 | } 55 | 56 | /* 57 | * First thing we need is a store to store document as build the SPDX document 58 | * We'll chose the MultiFormatStore since it supports serializing to JSON files 59 | * It takes an underlying model store as the first parameter - the inMemSpdxStore is a simple 60 | * built in store included in the Spdx-Java-Library. The second parameter is the format 61 | * to use when serializing or deserializing 62 | */ 63 | ISerializableModelStore modelStore = new MultiFormatStore(new InMemSpdxStore(), Format.JSON_PRETTY); 64 | /* 65 | * There are several other choices which can be made for a model store - most of which 66 | * are in separate libraries that need to be included as dependencies. Below are a few examples: 67 | * IModelStore modelStore = new InMemSpdxStore(); - the simplest store, but does not support serializing or deserializing 68 | * ISerializableModelStore modelStore = new TagValueStore(new InMemSpdxStore()); Supports serializing and deserializing SPDX tag/value files 69 | * ISerializableModelStore modelStore = new RdfStore(); Supports serializing and deserializing various RDF formats (e.g. RDF/XML, RDF/Turtle) 70 | * ISerializableModelStore modelStore = new SpreadsheetStore(new InMemSpdxStore()); Supports serializing and deserializing .XLS or .XLSX spreadsheets 71 | */ 72 | 73 | /* 74 | * The documentUri is a unique identifier for the SPDX documents 75 | */ 76 | String documentUri = "https://org.spdx.examples/spdx/doc/b7490f5a-b6ac-45e7-9971-4c27f1db97f7"; 77 | /* 78 | * The ModelCopyManager is used when using more than one Model Store. The Listed Licenses uses 79 | * it's own model store, so we will need a ModelCopyManager to manage the copying of the listed 80 | * license information over to the document model store 81 | */ 82 | ModelCopyManager copyManager = new ModelCopyManager(); 83 | try { 84 | // Time to create the document 85 | SpdxDocument document = new SpdxDocument(modelStore, documentUri, copyManager, false); 86 | // Let's add a few required fields to the document 87 | SimpleDateFormat dateFormat = new SimpleDateFormat(SpdxConstantsCompatV2.SPDX_DATE_FORMAT); 88 | String creationDate = dateFormat.format(new Date()); 89 | document.setCreationInfo(document.createCreationInfo( 90 | Arrays.asList(new String[] {"Tool: Simple SPDX Document Example"}), 91 | creationDate)); 92 | /* 93 | * Now that we have the initial model object 'document' created, we can use the helper 94 | * methods from that cleass to create any other model elements such as the CreationInfo 95 | * above. These helper functions will use the same Document URI, Model Store and Model Copy Manager 96 | * as the document element. 97 | */ 98 | AnyLicenseInfo dataLicense = LicenseInfoFactory.parseSPDXLicenseStringCompatV2("CC0-1.0"); 99 | /* 100 | * Note that by passing in the modelStore and documentUri, the parsed license information is stored 101 | * in the same model store we are using for the document 102 | */ 103 | document.setDataLicense(dataLicense); 104 | document.setName("SPDX Example Document"); 105 | document.setSpecVersion("SPDX-2.2"); 106 | 107 | // Now that we have the basic document information filled in, let's create a package 108 | AnyLicenseInfo pkgConcludedLicense = LicenseInfoFactory.parseSPDXLicenseStringCompatV2("Apache-2.0 AND MIT"); 109 | AnyLicenseInfo pkgDeclaredLicense = LicenseInfoFactory.parseSPDXLicenseStringCompatV2("Apache-2.0"); 110 | String pkgId = modelStore.getNextId(IdType.SpdxId); 111 | // The ID's used for SPDX elements must be unique. Calling the model store getNextId function is a 112 | // convenient and safe method to make sure you have a correctly formatted and unique ID 113 | SpdxPackage pkg = document.createPackage(pkgId, "Example Package Name", pkgConcludedLicense, 114 | "Copyright example.org", pkgDeclaredLicense) 115 | .setFilesAnalyzed(false) // Default is true and we don't want to add all the required fields 116 | .setComment("This package is used as an example in creating an SPDX document from scratch") 117 | .setDownloadLocation("NOASSERTION") 118 | .build(); 119 | /* 120 | * Note that many of the more complex elements use a builder pattern as in the 121 | * example above 122 | */ 123 | 124 | /* Let's add the package relationships to the document 125 | * This step 126 | */ 127 | // This step will add a relationship between document and pkg as "DESCRIBES". 128 | document.getDocumentDescribes().add(pkg); 129 | // Let's create another package 130 | pkgId = modelStore.getNextId(IdType.SpdxId); 131 | SpdxPackage childPkg = document.createPackage(pkgId, "Child Example Package Name", pkgConcludedLicense, 132 | "Copyright example.org", pkgDeclaredLicense) 133 | .setFilesAnalyzed(false) // Default is true and we don't want to add all the required fields 134 | .setComment("This package is used as an example in creating an SPDX document from scratch") 135 | .setDownloadLocation("NOASSERTION") 136 | .build(); 137 | // Then create a DEPEND_ON relation by relationship factory 138 | Relationship relationship = pkg.createRelationship(childPkg, RelationshipType.DEPENDS_ON, ""); 139 | pkg.addRelationship(relationship); 140 | 141 | // That's for creating the simple document. Let's verify to make sure nothing is out of spec 142 | List warnings = document.verify(); 143 | if (warnings.size() > 0) { 144 | System.out.println("The document has the following warnings:"); 145 | for (String warning:warnings) { 146 | System.out.print("\t"); 147 | System.out.println(warning); 148 | } 149 | } 150 | // Last step is to serialize 151 | try (OutputStream outputStream = new FileOutputStream(outFile)) { 152 | modelStore.serialize(outputStream); 153 | } 154 | System.out.println("Example document written to "+args[0]); 155 | System.exit(0); 156 | } catch (InvalidSPDXAnalysisException e) { 157 | System.out.println("Unexpected error creating SPDX document: "+e.getMessage()); 158 | System.exit(1); 159 | } catch (IOException e) { 160 | System.out.println("I/O error writing output JSON file"); 161 | System.exit(1); 162 | } 163 | 164 | 165 | } 166 | 167 | private static void usage() { 168 | System.out.println("Usage: SimpleSpxDocument outputFilePath"); 169 | } 170 | 171 | } 172 | --------------------------------------------------------------------------------