code.gitea.io/gitea@v1.22.3/modules/packages/maven/metadata.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package maven
     5  
     6  import (
     7  	"encoding/xml"
     8  	"io"
     9  
    10  	"code.gitea.io/gitea/modules/validation"
    11  
    12  	"golang.org/x/net/html/charset"
    13  )
    14  
    15  // Metadata represents the metadata of a Maven package
    16  type Metadata struct {
    17  	GroupID      string        `json:"group_id,omitempty"`
    18  	ArtifactID   string        `json:"artifact_id,omitempty"`
    19  	Name         string        `json:"name,omitempty"`
    20  	Description  string        `json:"description,omitempty"`
    21  	ProjectURL   string        `json:"project_url,omitempty"`
    22  	Licenses     []string      `json:"licenses,omitempty"`
    23  	Dependencies []*Dependency `json:"dependencies,omitempty"`
    24  }
    25  
    26  // Dependency represents a dependency of a Maven package
    27  type Dependency struct {
    28  	GroupID    string `json:"group_id,omitempty"`
    29  	ArtifactID string `json:"artifact_id,omitempty"`
    30  	Version    string `json:"version,omitempty"`
    31  }
    32  
    33  type pomStruct struct {
    34  	XMLName     xml.Name `xml:"project"`
    35  	GroupID     string   `xml:"groupId"`
    36  	ArtifactID  string   `xml:"artifactId"`
    37  	Version     string   `xml:"version"`
    38  	Name        string   `xml:"name"`
    39  	Description string   `xml:"description"`
    40  	URL         string   `xml:"url"`
    41  	Licenses    []struct {
    42  		Name         string `xml:"name"`
    43  		URL          string `xml:"url"`
    44  		Distribution string `xml:"distribution"`
    45  	} `xml:"licenses>license"`
    46  	Dependencies []struct {
    47  		GroupID    string `xml:"groupId"`
    48  		ArtifactID string `xml:"artifactId"`
    49  		Version    string `xml:"version"`
    50  		Scope      string `xml:"scope"`
    51  	} `xml:"dependencies>dependency"`
    52  }
    53  
    54  // ParsePackageMetaData parses the metadata of a pom file
    55  func ParsePackageMetaData(r io.Reader) (*Metadata, error) {
    56  	var pom pomStruct
    57  
    58  	dec := xml.NewDecoder(r)
    59  	dec.CharsetReader = charset.NewReaderLabel
    60  	if err := dec.Decode(&pom); err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	if !validation.IsValidURL(pom.URL) {
    65  		pom.URL = ""
    66  	}
    67  
    68  	licenses := make([]string, 0, len(pom.Licenses))
    69  	for _, l := range pom.Licenses {
    70  		if l.Name != "" {
    71  			licenses = append(licenses, l.Name)
    72  		}
    73  	}
    74  
    75  	dependencies := make([]*Dependency, 0, len(pom.Dependencies))
    76  	for _, d := range pom.Dependencies {
    77  		dependencies = append(dependencies, &Dependency{
    78  			GroupID:    d.GroupID,
    79  			ArtifactID: d.ArtifactID,
    80  			Version:    d.Version,
    81  		})
    82  	}
    83  
    84  	return &Metadata{
    85  		GroupID:      pom.GroupID,
    86  		ArtifactID:   pom.ArtifactID,
    87  		Name:         pom.Name,
    88  		Description:  pom.Description,
    89  		ProjectURL:   pom.URL,
    90  		Licenses:     licenses,
    91  		Dependencies: dependencies,
    92  	}, nil
    93  }