private ActionResult DisplayCompositeLicenseExpression(LicenseOperator licenseExpressionRoot, string licenseExpression) { var meaningfulSegments = _licenseExpressionSegmentator.GetLicenseExpressionSegments(licenseExpressionRoot); var allSegments = _licenseExpressionSegmentator.SplitFullExpression(licenseExpression, meaningfulSegments); return(View("CompositeLicenseExpression", new CompositeLicenseExpressionViewModel(allSegments))); }
/// <summary> /// Does an in-order traversal of a license expression tree restoring the sequence of tokens /// used in the expression (omitting all parentheses and whitespace) /// </summary> /// <param name="licenseExpressionRoot">Root of the license expression tree</param> /// <returns>The list of license expression token in the order they appeared in the original expression.</returns> public List <CompositeLicenseExpressionSegment> GetLicenseExpressionSegments(LicenseOperator licenseExpressionRoot) { if (licenseExpressionRoot == null) { throw new ArgumentNullException(nameof(licenseExpressionRoot)); } var segmentList = new List <CompositeLicenseExpressionSegment>(); InOrderTraversal(licenseExpressionRoot, segmentList); return(segmentList); }
private ActionResult DisplayCompositeLicenseExpression(LicenseOperator licenseExpressionRoot, string licenseExpression) { var meaningfulSegments = _licenseExpressionSegmentator.GetLicenseExpressionSegments(licenseExpressionRoot); var allSegments = _licenseExpressionSegmentator.SplitFullExpression(licenseExpression, meaningfulSegments); var stopwatch = Stopwatch.StartNew(); foreach (var segment in allSegments.Where(IsLicenseOrException)) { var isValidSegment = false; if (NuGetLicenseData.ExceptionList.TryGetValue(segment.Value, out var exceptionData)) { isValidSegment = true; } else { try { var license = NuGetLicenseExpression.Parse(segment.Value) as NuGetLicense; if (license != null && license.IsStandardLicense) { isValidSegment = true; } } catch (NuGetLicenseExpressionParsingException e) { stopwatch.Stop(); _logger.LogError(0, e, "Got exception while attempting to parse license expression {LicenseExpression}", segment.Value); return(InvalidRequest($"Unable to parse the license expression: {segment.Value}")); } } if (!isValidSegment) { stopwatch.Stop(); _logger.LogInformation("Validating composite licenseExpression uses: {ElapsedTime} s", stopwatch.Elapsed.TotalSeconds); return(UnknownLicense(segment.Value)); } try { // The Licenses folder in App_Data does not contain all the SPDX licenses' content. // If the customer tries to fetch the content of a SPDX expression which we don't support, it will return UnknownLicense here. if (!_licenseFileService.DoesLicenseFileExist(segment.Value)) { stopwatch.Stop(); _logger.LogInformation("Validating composite licenseExpression uses: {ElapsedTime} s", stopwatch.Elapsed.TotalSeconds); return(UnknownLicense(segment.Value)); } } catch (ArgumentException e) { stopwatch.Stop(); _logger.LogError(0, e, "Got exception while attempting to get license contents due to the invalid license: {licenseIdentifier}", segment.Value); return(InvalidRequest()); } } stopwatch.Stop(); _logger.LogInformation("Validating composite licenseExpression uses: {ElapsedTime} s", stopwatch.Elapsed.TotalSeconds); return(View("CompositeLicenseExpression", new CompositeLicenseExpressionViewModel(allSegments))); }