Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed validation of icon paths #1572

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions org.lflang/src/org/lflang/validation/LFValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1227,31 +1227,26 @@ public void checkWidthSpec(WidthSpec widthSpec) {

@Check(CheckType.FAST)
public void checkReactorIconAttribute(Reactor reactor) {
var attrs = AttributeUtils.getAttributes(reactor);
var iconAttr = attrs.stream()
.filter(it -> it.getAttrName().equalsIgnoreCase("icon"))
.findFirst()
.orElse(null);
if (iconAttr != null) {
var path = iconAttr.getAttrParms().get(0).getValue();

var path = AttributeUtils.getIconPath(reactor);
if (path != null) {
var param = AttributeUtils.findAttributeByName(reactor, "icon").getAttrParms().get(0);
// Check file extension
var validExtensions = Set.of("bmp", "png", "gif", "ico", "jpeg");
var extensionStrart = path.lastIndexOf(".");
var extension = extensionStrart != -1 ? path.substring(extensionStrart + 1) : "";
if (!validExtensions.contains(extension.toLowerCase())) {
warning("File extension '" + extension + "' is not supported. Provide any of: " + String.join(", ", validExtensions),
iconAttr.getAttrParms().get(0), Literals.ATTR_PARM__VALUE);
param, Literals.ATTR_PARM__VALUE);
return;
}

// Check file location
var iconLocation = FileUtil.locateFile(path, reactor.eResource());
if (iconLocation == null) {
warning("Cannot locate icon file.", iconAttr.getAttrParms().get(0), Literals.ATTR_PARM__VALUE);
warning("Cannot locate icon file.", param, Literals.ATTR_PARM__VALUE);
}
if (("file".equals(iconLocation.getScheme()) || iconLocation.getScheme() == null) && !(new File(iconLocation.getPath()).exists())) {
warning("Icon does not exist.", iconAttr.getAttrParms().get(0), Literals.ATTR_PARM__VALUE);
warning("Icon does not exist.", param, Literals.ATTR_PARM__VALUE);
}
}
}
Expand Down