Aadl2NameValidators.java
/**
* Copyright (c) 2004-2025 Carnegie Mellon University and others. (see Contributors file).
* All Rights Reserved.
*
* NO WARRANTY. ALL MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE
* OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT
* MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.
*
* This program and the accompanying materials are made available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
* SPDX-License-Identifier: EPL-2.0
*
* Created, in part, with funding and support from the United States Government. (see Acknowledgments file).
*
* This program includes and/or can make use of certain third party source code, object code, documentation and other
* files ("Third Party Software"). The Third Party Software that is used by this program is dependent upon your system
* configuration. By using this program, You agree to comply with any and all relevant Third Party Software terms and
* conditions contained in any such Third Party Software or separate license file distributed with such Third Party
* Software. The parties who own the Third Party Software ("Third Party Licensors") are intended third party benefici-
* aries to this license with respect to the terms applicable to their Third Party Software. Third Party Software li-
* censes only apply to the Third Party Software and not any other portion of this program or this program as a whole.
*/
package org.osate.xtext.aadl2.ui.util;
import java.io.StringReader;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xtext.ParserRule;
import org.eclipse.xtext.naming.IQualifiedNameConverter;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.parser.IParser;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.scoping.IGlobalScopeProvider;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.util.OnChangeEvictingCache;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.osate.aadl2.Aadl2Package;
import org.osate.aadl2.modelsupport.resources.OsateResourceUtil;
import org.osate.xtext.aadl2.services.Aadl2GrammarAccess;
import org.osate.xtext.aadl2.ui.MyAadl2Activator;
@SuppressWarnings("all")
public final class Aadl2NameValidators {
private static final IParser parser = MyAadl2Activator.getInstance().getInjector(MyAadl2Activator.ORG_OSATE_XTEXT_AADL2_AADL2).<IParser>getInstance(IParser.class);
public static final Aadl2GrammarAccess grammarAccess = MyAadl2Activator.getInstance().getInjector(MyAadl2Activator.ORG_OSATE_XTEXT_AADL2_AADL2).<Aadl2GrammarAccess>getInstance(Aadl2GrammarAccess.class);
private static final IGlobalScopeProvider globalScopeProvider = MyAadl2Activator.getInstance().getInjector(MyAadl2Activator.ORG_OSATE_XTEXT_AADL2_AADL2).<IGlobalScopeProvider>getInstance(IGlobalScopeProvider.class);
private static final IQualifiedNameConverter qNameConverter = MyAadl2Activator.getInstance().getInjector(MyAadl2Activator.ORG_OSATE_XTEXT_AADL2_AADL2).<IQualifiedNameConverter>getInstance(IQualifiedNameConverter.class);
/**
* Validate ID terms (simple names)
*/
public static final Function1<? super String, ? extends Boolean> ID_VALIDATOR = Aadl2NameValidators.getFieldValidator(true, Aadl2NameValidators.grammarAccess.getINAMERule());
/**
* Validate package names (a::b::c)
*/
public static final Function1<? super String, ? extends Boolean> PACKAGE_NAME_VALIDATOR = Aadl2NameValidators.getFieldValidator(true, Aadl2NameValidators.grammarAccess.getPNAMERule());
private Aadl2NameValidators() {
super();
}
/**
* Get a predicate <code>(String) => boolean</code> for testing strings based on parse rules. Returns true if the
* string parses using the given rule. Intended for use in validating field values in UI dialogs.
* @param checkWhiteSpace <code>true</code> to require that the text not contain any whitespace
* @param rule The non-terminal parse rule to use for checking
*/
public static Function1<? super String, ? extends Boolean> getFieldValidator(final boolean checkWhiteSpace, final ParserRule rule) {
Function1<String, Boolean> _xifexpression = null;
if (checkWhiteSpace) {
final Function1<String, Boolean> _function = (String fieldValue) -> {
return Boolean.valueOf((fieldValue.matches("\\S+") &&
(!Aadl2NameValidators.parser.parse(rule, new StringReader(fieldValue)).hasSyntaxErrors())));
};
_xifexpression = _function;
} else {
final Function1<String, Boolean> _function_1 = (String fieldValue) -> {
StringReader _stringReader = new StringReader(fieldValue);
boolean _hasSyntaxErrors = Aadl2NameValidators.parser.parse(rule, _stringReader).hasSyntaxErrors();
return Boolean.valueOf((!_hasSyntaxErrors));
};
_xifexpression = _function_1;
}
return _xifexpression;
}
private static IEObjectDescription findModelUnitInScope(final IContainer parent, final String modelUnitName) {
IEObjectDescription _xblockexpression = null;
{
final IFolder fakeFolder = parent.getFolder(Path.forPosix(".fake"));
final Resource rsrc = new ResourceSetImpl().createResource(OsateResourceUtil.toResourceURI(fakeFolder));
rsrc.getContents().clear();
final IUnitOfWork<IScope, Resource> _function = (Resource resource) -> {
return Aadl2NameValidators.globalScopeProvider.getScope(resource, Aadl2Package.eINSTANCE.getPackageSection_ImportedUnit(), null);
};
final IScope scope = new OnChangeEvictingCache().<IScope, Resource>execWithTemporaryCaching(rsrc, _function);
final QualifiedName qualifiedName = Aadl2NameValidators.qNameConverter.toQualifiedName(modelUnitName);
_xblockexpression = scope.getSingleElement(qualifiedName);
}
return _xblockexpression;
}
/**
* Check if the given model unit exists in the scope of the given parent container. Returns
* <code>null</code> if it is does not, or an error message it it does.
*/
public static String validateFileNameInScope(final IContainer parent, final String modelUnitName) {
String _xblockexpression = null;
{
String errorMsg = null;
final IEObjectDescription foundInScope = Aadl2NameValidators.findModelUnitInScope(parent, modelUnitName);
if ((foundInScope != null)) {
final IFile foundFile = OsateResourceUtil.toIFile(foundInScope.getEObjectURI());
final IProject foundProject = foundFile.getProject();
String _xifexpression = null;
boolean _equals = foundInScope.getEClass().equals(Aadl2Package.eINSTANCE.getAadlPackage());
if (_equals) {
_xifexpression = "Package \'";
} else {
_xifexpression = "Property set \'";
}
final String label = _xifexpression;
IProject _project = parent.getProject();
boolean _tripleEquals = (foundProject == _project);
if (_tripleEquals) {
IPath _projectRelativePath = foundFile.getProjectRelativePath();
String _plus = (((label + modelUnitName) + "\' already exists in the selected project: \'") + _projectRelativePath);
String _plus_1 = (_plus + "\'");
errorMsg = _plus_1;
} else {
String _name = foundProject.getName();
String _plus_2 = (((label + modelUnitName) + "\' already exists in project \'") + _name);
String _plus_3 = (_plus_2 + "\' that the selected project depends on: \'");
IPath _projectRelativePath_1 = foundFile.getProjectRelativePath();
String _plus_4 = (_plus_3 + _projectRelativePath_1);
String _plus_5 = (_plus_4 + "\'");
errorMsg = _plus_5;
}
} else {
IProject[] _referencingProjects = parent.getProject().getReferencingProjects();
for (final IProject referencer : _referencingProjects) {
{
final IEObjectDescription foundInScopeOfReferencer = Aadl2NameValidators.findModelUnitInScope(referencer, modelUnitName);
if ((foundInScopeOfReferencer != null)) {
String _xifexpression_1 = null;
boolean _equals_1 = foundInScopeOfReferencer.getEClass().equals(Aadl2Package.eINSTANCE.getAadlPackage());
if (_equals_1) {
_xifexpression_1 = "Package \'";
} else {
_xifexpression_1 = "Property set \'";
}
final String label_1 = _xifexpression_1;
final IFile foundFile_1 = OsateResourceUtil.toIFile(foundInScopeOfReferencer.getEObjectURI());
final IProject foundProject_1 = foundFile_1.getProject();
if ((foundProject_1 == referencer)) {
String _name_1 = referencer.getName();
String _plus_6 = (((label_1 + modelUnitName) + "\' already exists in project \'") + _name_1);
String _plus_7 = (_plus_6 + "\' that depends on the selected project: \'");
IPath _projectRelativePath_2 = foundFile_1.getProjectRelativePath();
String _plus_8 = (_plus_7 + _projectRelativePath_2);
String _plus_9 = (_plus_8 + "\'");
errorMsg = _plus_9;
} else {
String _name_2 = foundProject_1.getName();
String _plus_10 = (((label_1 + modelUnitName) + "\' already exists in project \'") + _name_2);
String _plus_11 = (_plus_10 + "\' that is depended on by project \'");
String _name_3 = referencer.getName();
String _plus_12 = (_plus_11 + _name_3);
String _plus_13 = (_plus_12 + "\' that depends on the selected project: \'");
IPath _projectRelativePath_3 = foundFile_1.getProjectRelativePath();
String _plus_14 = (_plus_13 + _projectRelativePath_3);
String _plus_15 = (_plus_14 + "\'");
errorMsg = _plus_15;
}
}
}
}
}
_xblockexpression = errorMsg;
}
return _xblockexpression;
}
}