com.metaeffekt.artifact.enrichment.validation.reason.InventoryValidationReason Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2021-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.metaeffekt.artifact.enrichment.validation.reason;
import org.metaeffekt.core.inventory.processor.model.AbstractModelBase;
import org.metaeffekt.core.inventory.processor.model.Artifact;
import org.metaeffekt.core.inventory.processor.model.VulnerabilityMetaData;
public class InventoryValidationReason {
private final AbstractModelBase source;
private final String text;
private final ReasonIdentifier reason;
public InventoryValidationReason(AbstractModelBase source, ReasonIdentifier reason, String text) {
this.source = source;
this.text = text;
this.reason = reason;
}
public InventoryValidationReason(ReasonIdentifier reason, String text) {
this(null, reason, text);
}
public String getText() {
return text;
}
public ReasonIdentifier getReason() {
return reason;
}
public AbstractModelBase getSource() {
return source;
}
private String getSourceName() {
if (source == null) {
return null;
} else if (source instanceof Artifact) {
return "Id=" + ((Artifact) source).get(Artifact.Attribute.ID);
} else if (source instanceof VulnerabilityMetaData) {
return "Name=" + ((VulnerabilityMetaData) source).get(VulnerabilityMetaData.Attribute.NAME);
} else {
return "class=" + source.getClass().getSimpleName();
}
}
/**
* Generates a message like this:
* [0] Test reason [Id=some-id-3.4]:
* Custom text
* With the fields:
* [0] - reason ordinal
* Test reason - reason short description
* Id=some-id-3.4 - source name
* Custom text - custom text
*
* @return the error/warning message
*/
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
if (reason.getLongDescription() != null) {
sb.append("[").append(reason.ordinal()).append("] ");
}
if (reason.getShortDescription() != null) {
sb.append(reason.getShortDescription());
}
final String sourceName = getSourceName();
if (sourceName != null) {
sb.append(" [").append(sourceName).append("]:\n");
} else {
sb.append(":\n");
}
sb.append(text);
return sb.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy