use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText in project sw360portal by sw360.
the class CLIParser method getLicenseInfos.
@Override
public <T> List<LicenseInfoParsingResult> getLicenseInfos(Attachment attachment, User user, T context) throws TException {
AttachmentContent attachmentContent = attachmentContentProvider.getAttachmentContent(attachment);
LicenseInfo licenseInfo = new LicenseInfo().setFilenames(Arrays.asList(attachmentContent.getFilename()));
LicenseInfoParsingResult result = new LicenseInfoParsingResult().setLicenseInfo(licenseInfo);
InputStream attachmentStream = null;
try {
attachmentStream = attachmentConnector.getAttachmentStream(attachmentContent, user, context);
Document doc = getDocument(attachmentStream);
Set<String> copyrights = getCopyrights(doc);
licenseInfo.setCopyrights(copyrights);
Set<LicenseNameWithText> licenseNamesWithTexts = getLicenseNameWithTexts(doc);
licenseInfo.setLicenseNamesWithTexts(licenseNamesWithTexts);
result.setStatus(LicenseInfoRequestStatus.SUCCESS);
} catch (ParserConfigurationException | IOException | XPathExpressionException | SAXException | SW360Exception e) {
log.error(e);
result.setStatus(LicenseInfoRequestStatus.FAILURE).setMessage("Error while parsing CLI file: " + e.toString());
} finally {
closeQuietly(attachmentStream, log);
}
return Collections.singletonList(result);
}
use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText in project sw360portal by sw360.
the class CombinedCLIParserTest method testGetCLI.
@Test
public void testGetCLI() throws Exception {
Attachment cliAttachment = new Attachment("A1", "a.xml");
when(connector.getAttachmentStream(anyObject(), anyObject(), anyObject())).thenReturn(new ReaderInputStream(new StringReader(cliTestfile)));
List<LicenseInfoParsingResult> results = parser.getLicenseInfos(cliAttachment, new User(), new Project());
assertThat(results.size(), is(1));
LicenseInfoParsingResult res = results.get(0);
assertLicenseInfoParsingResult(res);
assertThat(res.getLicenseInfo().getFilenames(), containsInAnyOrder("a.xml"));
assertThat(res.getLicenseInfo().getLicenseNamesWithTexts().size(), is(3));
assertThat(res.getLicenseInfo().getLicenseNamesWithTexts().stream().map(LicenseNameWithText::getLicenseText).collect(Collectors.toSet()), containsInAnyOrder("License1Text", "License2Text", "License3&'Text"));
LicenseNameWithText l2 = res.getLicenseInfo().getLicenseNamesWithTexts().stream().filter(l -> l.getLicenseName().equals("License2")).findFirst().orElseThrow(AssertionError::new);
assertThat(l2.getAcknowledgements(), is("License2Acknowledgements"));
assertThat(res.getLicenseInfo().getCopyrights().size(), is(5));
assertThat(res.getLicenseInfo().getCopyrights(), containsInAnyOrder("Copyright1", "Copyright2", "Copyright3", "Copyright4", "Copyright5"));
assertThat(res.getVendor(), is("VendorA"));
assertThat(res.getName(), is("r1"));
assertThat(res.getVersion(), is("1.0"));
}
use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText in project sw360portal by sw360.
the class OutputGenerator method renderTemplateWithDefaultValues.
/**
* Creates a velocity context and fills it with the default, commonly used
* values:
* <ul>
* <li>allLicenseNamesWithTexts: list of {@link LicenseNameWithText} objects,
* sorted by license name</li>
* <li>licenseNameWithTextToReferenceId: map from the license name to a unique
* id inside the file. May be used to reference a license.</li>
* <li>licenseInfoResults: map of {@link LicenseInfoParsingResult} objects,
* where the key is the name of the release. The licenses within the objects are
* sorted by name. Contains only the results with status {@link LicenseInfoRequestStatus#SUCCESS}</li>
* <li>licenseInfoErrorResults: map {@link List}of {@link LicenseInfoParsingResult} objects,
* where the key is the name of the release. Contains only the results with status other than
* {@link LicenseInfoRequestStatus#SUCCESS}. These results are not merged, that's why the map values are lists.</li>
* <li>acknowledgments: map of acknowledgments for a release where the key is
* the release and the value is a set of strings (acknowledgments)</li>
* </ul>
* The given file will be used as velocity template and will be rendered with
* the described data.
*
* @param projectLicenseInfoResults
* parsing results to be rendered
* @param file
* name of template file
* @return rendered template
*/
protected String renderTemplateWithDefaultValues(Collection<LicenseInfoParsingResult> projectLicenseInfoResults, String file, String projectTitle, String licenseInfoHeaderText) {
VelocityContext vc = getConfiguredVelocityContext();
// set header
vc.put(LICENSE_INFO_PROJECT_TITLE, projectTitle);
vc.put(LICENSE_INFO_HEADER_TEXT, licenseInfoHeaderText);
// sorted lists of all license to be displayed at the end of the file at once
List<LicenseNameWithText> licenseNamesWithTexts = getSortedLicenseNameWithTexts(projectLicenseInfoResults);
vc.put(ALL_LICENSE_NAMES_WITH_TEXTS, licenseNamesWithTexts);
// assign a reference id to each license in order to only display references for
// each release. The references will point to
// the list with all details at the and of the file (see above)
int referenceId = 1;
Map<LicenseNameWithText, Integer> licenseToReferenceId = Maps.newHashMap();
for (LicenseNameWithText licenseNamesWithText : licenseNamesWithTexts) {
licenseToReferenceId.put(licenseNamesWithText, referenceId++);
}
vc.put(LICENSE_REFERENCE_ID_MAP_CONTEXT_PROPERTY, licenseToReferenceId);
Map<Boolean, List<LicenseInfoParsingResult>> partitionedResults = projectLicenseInfoResults.stream().collect(Collectors.partitioningBy(r -> r.getStatus() == LicenseInfoRequestStatus.SUCCESS));
List<LicenseInfoParsingResult> goodResults = partitionedResults.get(true);
Map<String, List<LicenseInfoParsingResult>> badResultsPerRelease = partitionedResults.get(false).stream().collect(Collectors.groupingBy(this::getComponentLongName));
vc.put(LICENSE_INFO_ERROR_RESULTS_CONTEXT_PROPERTY, badResultsPerRelease);
// be sure that the licenses inside a release are sorted by id. This looks nicer
SortedMap<String, LicenseInfoParsingResult> sortedLicenseInfos = getSortedLicenseInfos(goodResults);
// this will effectively change the objects in the collection and therefore the
// objects in the sorted map above
sortLicenseNamesWithinEachLicenseInfoById(sortedLicenseInfos.values(), licenseToReferenceId);
vc.put(LICENSE_INFO_RESULTS_CONTEXT_PROPERTY, sortedLicenseInfos);
// also display acknowledgments
SortedMap<String, Set<String>> acknowledgements = getSortedAcknowledgements(sortedLicenseInfos);
vc.put(ACKNOWLEDGEMENTS_CONTEXT_PROPERTY, acknowledgements);
StringWriter sw = new StringWriter();
Velocity.mergeTemplate(file, "utf-8", vc, sw);
IOUtils.closeQuietly(sw);
return sw.toString();
}
use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText in project sw360portal by sw360.
the class CombinedCLIParser method getLicenseInfos.
@Override
public <T> List<LicenseInfoParsingResult> getLicenseInfos(Attachment attachment, User user, T context) throws TException {
AttachmentContent attachmentContent = attachmentContentProvider.getAttachmentContent(attachment);
InputStream attachmentStream = null;
List<LicenseInfoParsingResult> parsingResults = new ArrayList<>();
Map<String, Release> releasesByExternalId = prepareReleasesByExternalId(getCorrelationKey());
try {
attachmentStream = attachmentConnector.getAttachmentStream(attachmentContent, user, context);
Document doc = getDocument(attachmentStream);
Map<String, Set<String>> copyrightSetsByExternalId = getCopyrightSetsByExternalIdsMap(doc);
Map<String, Set<LicenseNameWithText>> licenseNamesWithTextsByExternalId = getLicenseNamesWithTextsByExternalIdsMap(doc);
Set<String> allExternalIds = Sets.union(copyrightSetsByExternalId.keySet(), licenseNamesWithTextsByExternalId.keySet());
allExternalIds.forEach(extId -> {
LicenseInfoParsingResult parsingResult = getLicenseInfoParsingResultForExternalId(attachmentContent, releasesByExternalId, copyrightSetsByExternalId, licenseNamesWithTextsByExternalId, extId);
parsingResults.add(parsingResult);
});
} catch (ParserConfigurationException | IOException | XPathExpressionException | SAXException | SW360Exception e) {
log.error(e);
parsingResults.add(new LicenseInfoParsingResult().setStatus(LicenseInfoRequestStatus.FAILURE).setMessage("Error while parsing combined CLI file: " + e.toString()));
} finally {
closeQuietly(attachmentStream, log);
}
return parsingResults;
}
use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText in project sw360portal by sw360.
the class CombinedCLIParser method getLicenseInfoParsingResultForExternalId.
@NotNull
private LicenseInfoParsingResult getLicenseInfoParsingResultForExternalId(AttachmentContent attachmentContent, Map<String, Release> releasesByExternalId, Map<String, Set<String>> copyrightSetsByExternalId, Map<String, Set<LicenseNameWithText>> licenseNamesWithTextsByExternalId, String extId) {
LicenseInfo licenseInfo = new LicenseInfo().setFilenames(Arrays.asList(attachmentContent.getFilename()));
licenseInfo.setCopyrights(copyrightSetsByExternalId.get(extId));
licenseInfo.setLicenseNamesWithTexts(licenseNamesWithTextsByExternalId.get(extId));
LicenseInfoParsingResult parsingResult = new LicenseInfoParsingResult().setLicenseInfo(licenseInfo);
Release release = releasesByExternalId.get(extId);
if (release != null) {
parsingResult.setVendor(release.isSetVendor() ? release.getVendor().getShortname() : "");
parsingResult.setName(release.getName());
parsingResult.setVersion(release.getVersion());
} else {
parsingResult.setName("No info found for external component ID " + extId);
}
parsingResult.setStatus(LicenseInfoRequestStatus.SUCCESS);
return parsingResult;
}
Aggregations