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

Fix to reopened Issue #477 for streams/v3 #515

Merged
merged 2 commits into from
Jul 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
package com.zeligsoft.domain.dds4ccm.constraints.java;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern;

Expand All @@ -26,9 +30,18 @@
import org.eclipse.emf.validation.IValidationContext;

import com.zeligsoft.base.zdl.Activator;
import com.zeligsoft.base.zdl.staticapi.util.AbstractBaseZDLFactory;
import com.zeligsoft.base.zdl.staticapi.util.ZDLFactoryRegistry;
import com.zeligsoft.base.zdl.util.ZDLUtil;
import com.zeligsoft.domain.dds4ccm.DDS4CCMNames;
import com.zeligsoft.domain.idl3plus.IDL3PlusNames;
import com.zeligsoft.domain.omg.ccm.CCMNames;
import com.zeligsoft.domain.omg.corba.CXDomainNames;
import com.zeligsoft.domain.zml.util.ZMLMMNames;

import org.eclipse.uml2.common.util.CacheAdapter;
import org.eclipse.uml2.uml.Class;

/**
* Validates whether an entity name is valid, i.e. that it doesn't contain illegal characters.
*
Expand All @@ -39,23 +52,84 @@ public class ValidNameConstraint extends AbstractModelConstraint {
private static final String VALID_NAME_REGEX = "[a-zA-Z_]\\w*";
private static final Pattern VALID_NAME_PATTERN = Pattern.compile(VALID_NAME_REGEX);
private static final Predicate<String> VALID_NAME_PREDICATE = VALID_NAME_PATTERN.asMatchPredicate();

/* CHECKABLE_NAMED_ELEMENTS is a set containing the names of all concepts and only
* the concepts of elements that are to be checked by validation. This list should
* match the names of all elements that result in IDL identifiers.
* See https://github.com/ZeligsoftDev/CX4CBDDS/issues/477 */
private static final Set<String> CHECKABLE_NAMED_ELEMENT_CONCEPTS = new HashSet<String>(Arrays.asList(
CCMNames.CCMCOMPONENT,
CCMNames.CCMMODEL,
CCMNames.EVENT,
CCMNames.EVENT_PORT,
CCMNames.INTERFACE_PORT,
DDS4CCMNames.DDS4_CCMMODEL,
DDS4CCMNames.DDSMESSAGE,
DDS4CCMNames.MESSAGE_FIELD,
IDL3PlusNames.CONNECTOR_DEF,
IDL3PlusNames.EXTENDED_PORT_TYPE,
// IDL3PlusNames.MODULE_BINDING,
IDL3PlusNames.MODULE_INSTANTIATION,
// IDL3PlusNames.PARAMETER_BINDING,
IDL3PlusNames.TEMPLATE_MODULE,
IDL3PlusNames.TYPED_CONNECTOR,
CXDomainNames.CXATTRIBUTE,
CXDomainNames.CXARRAY,
CXDomainNames.CXBOUND,
CXDomainNames.CXCASE,
CXDomainNames.CXCASE__LABEL,
CXDomainNames.CXCLASSIFIER,
CXDomainNames.CXCONSTANT,
CXDomainNames.CXENUM,
CXDomainNames.CXEXCEPTION,
CXDomainNames.CXFIELD,
CXDomainNames.CXINTERFACE,
CXDomainNames.CXMODULE,
CXDomainNames.CXOPERATION,
CXDomainNames.CXPARAMETER,
CXDomainNames.CXPRIMITIVE,
CXDomainNames.CXSEQUENCE,
CXDomainNames.CXSTRING,
CXDomainNames.CXSTRUCT,
CXDomainNames.CXTYPE_DEF,
CXDomainNames.CXUNION,
CXDomainNames.CXUNION_FIELD,
CXDomainNames.CXWSTRING,
CXDomainNames.NATIVE
));

@Override
public IStatus validate(IValidationContext ctx) {

EObject objToVerify = ctx.getTarget();

if (ZDLUtil.isZDLConcept(objToVerify, ZMLMMNames.NAMED_ELEMENT)) {
if (isCheckableElement(objToVerify)) {
Object nameObj = ZDLUtil.getValue(objToVerify, ZMLMMNames.NAMED_ELEMENT, ZMLMMNames.NAMED_ELEMENT__NAME);
if (nameObj instanceof String) {
String name = (String)nameObj;
if (VALID_NAME_PREDICATE.test(name)) {
return ctx.createSuccessStatus();
}
}
return ctx.createFailureStatus(nameObj.toString());
String invalidName = nameObj != null ? nameObj.toString() : "<null>";
return ctx.createFailureStatus(invalidName);
}

return null;
}

private boolean isCheckableElement(EObject obj) {
if (!ZDLUtil.isZDLConcept(obj, ZMLMMNames.NAMED_ELEMENT))
return false;
List<Class> concepts = ZDLUtil.getZDLConcepts(obj);
// Set<Class> allConcepts = ZDLUtil.getAllZDLConcepts(objToVerify);
if (concepts == null)
return false;
for (Class umlClass: concepts) {
String qualifiedName = umlClass.getQualifiedName();
if (CHECKABLE_NAMED_ELEMENT_CONCEPTS.contains(qualifiedName))
return true;
}
return false;
}
}