xref: /aosp_15_r20/external/spdx-tools/tvsaver/saver2v2/save_file.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3package saver2v2
4
5import (
6	"fmt"
7	"io"
8	"sort"
9
10	"github.com/spdx/tools-golang/spdx/common"
11	"github.com/spdx/tools-golang/spdx/v2_2"
12)
13
14func renderFile2_2(f *v2_2.File, w io.Writer) error {
15	if f.FileName != "" {
16		fmt.Fprintf(w, "FileName: %s\n", f.FileName)
17	}
18	if f.FileSPDXIdentifier != "" {
19		fmt.Fprintf(w, "SPDXID: %s\n", common.RenderElementID(f.FileSPDXIdentifier))
20	}
21	for _, s := range f.FileTypes {
22		fmt.Fprintf(w, "FileType: %s\n", s)
23	}
24
25	for _, checksum := range f.Checksums {
26		fmt.Fprintf(w, "FileChecksum: %s: %s\n", checksum.Algorithm, checksum.Value)
27	}
28
29	if f.LicenseConcluded != "" {
30		fmt.Fprintf(w, "LicenseConcluded: %s\n", f.LicenseConcluded)
31	}
32	for _, s := range f.LicenseInfoInFiles {
33		fmt.Fprintf(w, "LicenseInfoInFile: %s\n", s)
34	}
35	if f.LicenseComments != "" {
36		fmt.Fprintf(w, "LicenseComments: %s\n", textify(f.LicenseComments))
37	}
38	if f.FileCopyrightText != "" {
39		fmt.Fprintf(w, "FileCopyrightText: %s\n", textify(f.FileCopyrightText))
40	}
41	for _, aop := range f.ArtifactOfProjects {
42		fmt.Fprintf(w, "ArtifactOfProjectName: %s\n", aop.Name)
43		if aop.HomePage != "" {
44			fmt.Fprintf(w, "ArtifactOfProjectHomePage: %s\n", aop.HomePage)
45		}
46		if aop.URI != "" {
47			fmt.Fprintf(w, "ArtifactOfProjectURI: %s\n", aop.URI)
48		}
49	}
50	if f.FileComment != "" {
51		fmt.Fprintf(w, "FileComment: %s\n", textify(f.FileComment))
52	}
53	if f.FileNotice != "" {
54		fmt.Fprintf(w, "FileNotice: %s\n", textify(f.FileNotice))
55	}
56	for _, s := range f.FileContributors {
57		fmt.Fprintf(w, "FileContributor: %s\n", s)
58	}
59	for _, s := range f.FileAttributionTexts {
60		fmt.Fprintf(w, "FileAttributionText: %s\n", textify(s))
61	}
62	for _, s := range f.FileDependencies {
63		fmt.Fprintf(w, "FileDependency: %s\n", s)
64	}
65
66	fmt.Fprintf(w, "\n")
67
68	// also render any snippets for this file
69	// get slice of Snippet identifiers so we can sort them
70	snippetKeys := []string{}
71	for k := range f.Snippets {
72		snippetKeys = append(snippetKeys, string(k))
73	}
74	sort.Strings(snippetKeys)
75	for _, sID := range snippetKeys {
76		s := f.Snippets[common.ElementID(sID)]
77		renderSnippet2_2(s, w)
78	}
79
80	return nil
81}
82