blob: 80f830e69901c4885b7919283c09bc99c07b9d5e [file] [log] [blame]
/**
* Copyright (c) 2011, 2018 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
* generated by Xtext 2.11.0
*/
package org.eclipse.osbp.xtext.signal.validation;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.text.ParseException;
import java.util.HashSet;
import java.util.regex.PatternSyntaxException;
import javax.inject.Inject;
import org.eclipse.emf.common.util.EList;
import org.eclipse.osbp.xtext.datainterchange.DataInterchange;
import org.eclipse.osbp.xtext.datainterchange.validation.DataDSLValidator;
import org.eclipse.osbp.xtext.signal.CronScheduler;
import org.eclipse.osbp.xtext.signal.DailyScheduler;
import org.eclipse.osbp.xtext.signal.HourlyScheduler;
import org.eclipse.osbp.xtext.signal.MonthlyScheduler;
import org.eclipse.osbp.xtext.signal.SchedulerType;
import org.eclipse.osbp.xtext.signal.SignalDSLPackage;
import org.eclipse.osbp.xtext.signal.SignalDatainterchange;
import org.eclipse.osbp.xtext.signal.SignalDefinition;
import org.eclipse.osbp.xtext.signal.SignalFunction;
import org.eclipse.osbp.xtext.signal.SignalPackage;
import org.eclipse.osbp.xtext.signal.SignalScheduler;
import org.eclipse.osbp.xtext.signal.SignalWatcher;
import org.eclipse.osbp.xtext.signal.WeeklyScheduler;
import org.eclipse.osbp.xtext.signal.common.SignalConstants;
import org.eclipse.osbp.xtext.signal.jvmmodel.SignalDSLJvmModelInferrer;
import org.eclipse.osbp.xtext.signal.validation.AbstractSignalDSLValidator;
import org.eclipse.xtext.validation.Check;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Extension;
import org.quartz.CronExpression;
/**
* This class contains custom validation rules.
*
* See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#validation
*/
@SuppressWarnings("all")
public class SignalDSLValidator extends AbstractSignalDSLValidator {
@Inject
@Extension
private DataDSLValidator dv;
@Inject
@Extension
private SignalDSLJvmModelInferrer jvm;
@Check
public void checkDuplicateWatcherNames(final SignalPackage pkg) {
HashSet<String> signals = new HashSet<String>();
EList<SignalDefinition> _signals = pkg.getSignals();
for (final SignalDefinition signal : _signals) {
boolean _contains = signals.contains(signal.getName());
boolean _not = (!_contains);
if (_not) {
signals.add(signal.getName());
} else {
String _name = signal.getName();
String _plus = ("Watcher\'s and scheduler\'s ID have to be unique and [" + _name);
String _plus_1 = (_plus +
"] is already in use. Please change it.");
this.error(_plus_1, signal, SignalDSLPackage.Literals.SIGNAL_DEFINITION__NAME);
}
}
}
@Check
public void checkSignalSchedulerValidity(final SignalScheduler signal) {
if (((signal.getSchedulertype() != null) && (signal.getSchedulertype() instanceof CronScheduler))) {
SchedulerType _schedulertype = signal.getSchedulertype();
this.checkCronExpressionValidity(((CronScheduler) _schedulertype));
} else {
if (((signal.getSchedulertype() != null) && (signal.getSchedulertype() instanceof HourlyScheduler))) {
SchedulerType _schedulertype_1 = signal.getSchedulertype();
this.checkHourlySchedulerValidity(((HourlyScheduler) _schedulertype_1));
} else {
if (((signal.getSchedulertype() != null) && (signal.getSchedulertype() instanceof DailyScheduler))) {
SchedulerType _schedulertype_2 = signal.getSchedulertype();
this.checkDailySchedulerValidity(((DailyScheduler) _schedulertype_2));
} else {
if (((signal.getSchedulertype() != null) && (signal.getSchedulertype() instanceof WeeklyScheduler))) {
SchedulerType _schedulertype_3 = signal.getSchedulertype();
this.checkWeeklySchedulerValidity(((WeeklyScheduler) _schedulertype_3));
} else {
if (((signal.getSchedulertype() != null) && (signal.getSchedulertype() instanceof MonthlyScheduler))) {
SchedulerType _schedulertype_4 = signal.getSchedulertype();
this.checkMonthlySchedulerValidity(((MonthlyScheduler) _schedulertype_4));
}
}
}
}
}
}
public void checkCronExpressionValidity(final CronScheduler scheduler) {
String expression = scheduler.getExpression();
try {
boolean _isValidExpression = CronExpression.isValidExpression(expression);
boolean _not = (!_isValidExpression);
if (_not) {
CronExpression.validateExpression(expression);
}
} catch (final Throwable _t) {
if (_t instanceof ParseException) {
final ParseException exception = (ParseException)_t;
this.error((("The cron expression you have entered is invalid.\n\n" + exception) + SignalConstants.CRONEXP_NOTICE), scheduler, SignalDSLPackage.Literals.CRON_SCHEDULER__EXPRESSION);
} else {
throw Exceptions.sneakyThrow(_t);
}
}
}
public void checkHourlySchedulerValidity(final HourlyScheduler scheduler) {
boolean _not = (!((scheduler.getMinute() >= 0) && (scheduler.getMinute() <= 59)));
if (_not) {
this.error("The minute entry has to be between 0 and 59.", scheduler,
SignalDSLPackage.Literals.HOURLY_SCHEDULER__MINUTE);
}
}
public void checkDailySchedulerValidity(final DailyScheduler scheduler) {
boolean _not = (!((scheduler.getHour() >= 0) && (scheduler.getHour() <= 23)));
if (_not) {
this.error("The hour entry has to be between 0 and 23.", scheduler,
SignalDSLPackage.Literals.DAILY_SCHEDULER__HOUR);
}
boolean _not_1 = (!((scheduler.getMinute() >= 0) && (scheduler.getMinute() <= 59)));
if (_not_1) {
this.error("The minute entry has to be between 0 and 59.", scheduler,
SignalDSLPackage.Literals.DAILY_SCHEDULER__MINUTE);
}
}
public void checkWeeklySchedulerValidity(final WeeklyScheduler scheduler) {
boolean _not = (!((scheduler.getHour() >= 0) && (scheduler.getHour() <= 23)));
if (_not) {
this.error("The hour entry has to be between 0 and 23.", scheduler,
SignalDSLPackage.Literals.WEEKLY_SCHEDULER__HOUR);
}
boolean _not_1 = (!((scheduler.getMinute() >= 0) && (scheduler.getMinute() <= 59)));
if (_not_1) {
this.error("The minute entry has to be between 0 and 59.", scheduler,
SignalDSLPackage.Literals.WEEKLY_SCHEDULER__MINUTE);
}
}
public void checkMonthlySchedulerValidity(final MonthlyScheduler scheduler) {
boolean _not = (!((scheduler.getDayofmonth() >= 1) && (scheduler.getDayofmonth() <= 31)));
if (_not) {
this.error("The hour entry has to be between 1 and 31.", scheduler,
SignalDSLPackage.Literals.MONTHLY_SCHEDULER__DAYOFMONTH);
}
boolean _not_1 = (!((scheduler.getHour() >= 0) && (scheduler.getHour() <= 23)));
if (_not_1) {
this.error("The hour entry has to be between 0 and 23.", scheduler,
SignalDSLPackage.Literals.MONTHLY_SCHEDULER__HOUR);
}
boolean _not_2 = (!((scheduler.getMinute() >= 0) && (scheduler.getMinute() <= 59)));
if (_not_2) {
this.error("The minute entry has to be between 0 and 59.", scheduler,
SignalDSLPackage.Literals.MONTHLY_SCHEDULER__MINUTE);
}
}
@Check
public void checkWatcherValidity(final SignalWatcher watcher) {
if ((((watcher != null) && (watcher.getTasks() != null)) && (watcher.getInterchangegroup() != null))) {
int interchangeCount = this.jvm.taskCount(watcher, true);
int functionCount = this.jvm.taskCount(watcher, false);
int baseinterchangeCount = this.jvm.definedBaseInterchangeCount(watcher);
DataInterchange defaultInterchange = watcher.getDefaultInterchange();
SignalDatainterchange baseInterchange = this.jvm.definedBaseInterchange(watcher);
if ((watcher.isHasFileMask() && (interchangeCount > 1))) {
this.error("Only one interchange unit is allowed to be defined within a watcher with file mask.", watcher, SignalDSLPackage.Literals.SIGNAL_DEFINITION__TASKS);
}
if (((((defaultInterchange == null) && (baseInterchange == null)) && (interchangeCount >= 1)) || (((defaultInterchange == null) && (interchangeCount == 0)) && (functionCount > 0)))) {
this.error("You haven\'t specify the interchange unit, whose import path should be watched. You must define one with the keyword \'applyon\'.", watcher, SignalDSLPackage.Literals.SIGNAL_DEFINITION__TASKS);
}
if (((defaultInterchange != null) && (baseInterchange != null))) {
this.error("You can only mark one interchange unit with the keyword \'applyon\'.", watcher, SignalDSLPackage.Literals.SIGNAL_WATCHER__DEFAULT_INTERCHANGE);
}
if ((baseinterchangeCount > 1)) {
this.error("You can only mark one interchange unit with the keyword \'applyon\'.", watcher, SignalDSLPackage.Literals.SIGNAL_DEFINITION__NAME);
}
}
}
@Check
public void checkDuplicateFileMaskOrFileName(final SignalPackage pck) {
HashSet<String> filemasks = new HashSet<String>();
EList<SignalDefinition> _signals = pck.getSignals();
for (final SignalDefinition watcher : _signals) {
if ((watcher instanceof SignalWatcher)) {
boolean _isHasFileMask = ((SignalWatcher)watcher).isHasFileMask();
if (_isHasFileMask) {
boolean _contains = filemasks.contains(((SignalWatcher)watcher).getIdentifier());
boolean _not = (!_contains);
if (_not) {
filemasks.add(((SignalWatcher)watcher).getIdentifier());
} else {
this.error("This file mask is already in use in another watcher. Please change it.", watcher,
SignalDSLPackage.Literals.SIGNAL_WATCHER__IDENTIFIER);
}
} else {
boolean _contains_1 = filemasks.contains(((SignalWatcher)watcher).getIdentifier());
boolean _not_1 = (!_contains_1);
if (_not_1) {
filemasks.add(((SignalWatcher)watcher).getIdentifier());
} else {
this.error("This file name is already in use in another watcher. Please change it.", watcher,
SignalDSLPackage.Literals.SIGNAL_WATCHER__IDENTIFIER);
}
}
}
}
}
@Check
public void checkFileMaskAndFileNameValidity(final SignalWatcher watcher) {
this.checkFileNameExtension(watcher);
boolean _isHasFileMask = watcher.isHasFileMask();
if (_isHasFileMask) {
if ((((watcher != null) && (watcher.getIdentifier() != null)) && (!watcher.getIdentifier().isEmpty()))) {
try {
FileSystem _default = FileSystems.getDefault();
String _identifier = watcher.getIdentifier();
String _plus = ("glob:" + _identifier);
_default.getPathMatcher(_plus);
} catch (final Throwable _t) {
if (_t instanceof PatternSyntaxException) {
final PatternSyntaxException exception = (PatternSyntaxException)_t;
String _message = exception.getMessage();
String _plus_1 = ("The pattern of this file mask is invalid, please change it. Error: " + _message);
this.error(_plus_1, watcher, SignalDSLPackage.Literals.SIGNAL_WATCHER__IDENTIFIER);
} else if (_t instanceof UnsupportedOperationException) {
final UnsupportedOperationException exception_1 = (UnsupportedOperationException)_t;
String _message_1 = exception_1.getMessage();
String _plus_2 = ("The pattern of this file mask is not recognized, please change it. Error: " + _message_1);
this.error(_plus_2, watcher, SignalDSLPackage.Literals.SIGNAL_WATCHER__IDENTIFIER);
} else if (_t instanceof IllegalArgumentException) {
final IllegalArgumentException exception_2 = (IllegalArgumentException)_t;
String _message_2 = exception_2.getMessage();
String _plus_3 = ("The pattern of this file mask is invalid, please change it. Error: " + _message_2);
this.error(_plus_3, watcher, SignalDSLPackage.Literals.SIGNAL_WATCHER__IDENTIFIER);
} else {
throw Exceptions.sneakyThrow(_t);
}
}
}
}
}
public void checkFileNameExtension(final SignalWatcher watcher) {
if (((((watcher != null) && (watcher.getIdentifier() != null)) && (!watcher.getIdentifier().isEmpty())) && (watcher.getInterchangegroup() != null))) {
if (((!this.dv.isExtensionValid(watcher.getIdentifier())) && watcher.isHasFileMask())) {
this.error("The file mask extension you have entered is not currently supported. Only file masks ending with either one of the following extensions are allowed: \'.csv\' \'.edi\' \'.txt\' or \'.xml\' .", watcher, SignalDSLPackage.Literals.SIGNAL_WATCHER__IDENTIFIER);
} else {
if (((!this.dv.isExtensionValid(watcher.getIdentifier())) && (!watcher.isHasFileMask()))) {
this.error("The file name extension you have entered is not currently supported. Only file names ending with either one of the following extensions are allowed: \'.csv\' \'.edi\' \'.txt\' or \'.xml\' .", watcher, SignalDSLPackage.Literals.SIGNAL_WATCHER__IDENTIFIER);
}
}
}
}
public PathMatcher isFileMaskValid(final String filemask) {
if (((filemask != null) && (!filemask.isEmpty()))) {
try {
return FileSystems.getDefault().getPathMatcher(("glob:" + filemask));
} catch (final Throwable _t) {
if (_t instanceof Exception) {
final Exception exception = (Exception)_t;
return null;
} else {
throw Exceptions.sneakyThrow(_t);
}
}
}
return null;
}
public String escapeCharacters(final String pattern) {
if (((pattern != null) && (!pattern.isEmpty()))) {
String result = pattern;
int _indexOf = result.indexOf("*");
boolean _tripleNotEquals = (_indexOf != (-1));
if (_tripleNotEquals) {
result = result.replaceAll("\\*", "");
}
boolean _contains = result.contains("\\");
if (_contains) {
result = result.replaceAll("\\", "");
}
boolean _contains_1 = result.contains("?");
if (_contains_1) {
result = result.replaceAll("\\?", "");
}
return result;
}
return null;
}
@Check
public void checkFunctionTaskValidity(final SignalFunction function) {
if (((((function != null) && (function.getGroup() != null)) && (function.getDoExecuteFunction() != null)) && (function.getDoExecuteFunction().getParams() != null))) {
if ((function.isOnExportFile() || function.isOnImportFile())) {
int _size = function.getDoExecuteFunction().getParams().size();
boolean _tripleNotEquals = (_size != 1);
if (_tripleNotEquals) {
this.error("Only functions with one parameter from type java.nio.file.Path are allowed to be selected.", function, SignalDSLPackage.Literals.SIGNAL_FUNCTION__DO_EXECUTE_FUNCTION);
}
if (((function.getDoExecuteFunction().getParams().size() == 1) && (!function.getDoExecuteFunction().getParams().get(0).getParameterType().getType().getIdentifier().equals(Path.class.getName())))) {
this.error("Only functions with one parameter from type java.nio.file.Path are allowed to be selected.", function, SignalDSLPackage.Literals.SIGNAL_FUNCTION__DO_EXECUTE_FUNCTION);
}
} else {
if ((((!function.isOnExportFile()) && (!function.isOnImportFile())) && (!function.getDoExecuteFunction().getParams().isEmpty()))) {
this.error("Only functions without parameter are allowed to be selected.", function, SignalDSLPackage.Literals.SIGNAL_FUNCTION__DO_EXECUTE_FUNCTION);
}
}
}
}
}