github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/language/java/archive/pomproperties.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package archive
    16  
    17  import (
    18  	"archive/zip"
    19  	"bufio"
    20  	"fmt"
    21  	"strings"
    22  
    23  	"github.com/google/osv-scalibr/log"
    24  )
    25  
    26  // PomProps for identifying Maven package.
    27  type PomProps struct {
    28  	GroupID    string
    29  	ArtifactID string
    30  	Version    string
    31  }
    32  
    33  // valid returns true if p is a valid pom property.
    34  func (p PomProps) valid() bool {
    35  	return p.GroupID != "" && !strings.Contains(p.GroupID, " ") && p.ArtifactID != "" && !strings.Contains(p.ArtifactID, " ") && p.Version != "" && !strings.Contains(p.Version, " ")
    36  }
    37  
    38  func parsePomProps(f *zip.File) (PomProps, error) {
    39  	p := PomProps{}
    40  	file, err := f.Open()
    41  	if err != nil {
    42  		return p, fmt.Errorf("failed to open file %q: %w", f.Name, err)
    43  	}
    44  	defer file.Close()
    45  
    46  	log.Debugf("Parsing pom.properties file %s\n", f.Name)
    47  
    48  	s := bufio.NewScanner(file)
    49  	for s.Scan() {
    50  		line := strings.TrimSpace(s.Text())
    51  		parts := strings.SplitN(line, "=", 2)
    52  		if len(parts) < 2 {
    53  			continue
    54  		}
    55  		attribute, value := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
    56  		switch attribute {
    57  		case "groupId":
    58  			p.GroupID = value
    59  		case "artifactId":
    60  			p.ArtifactID = value
    61  		case "version":
    62  			p.Version = value
    63  		}
    64  	}
    65  	if s.Err() != nil {
    66  		return p, fmt.Errorf("error while scanning zip file %q for pom properties: %w", f.Name, s.Err())
    67  	}
    68  	log.Debugf("Data from pom.properties: groupid: %s artifactid: %s version: %s", p.GroupID, p.ArtifactID, p.Version)
    69  	return p, nil
    70  }