Skip to content

Commit

Permalink
Issue #477: (v3) Narrow the set of named elements to check for valid IDL
Browse files Browse the repository at this point in the history
Signed-off-by: Ernesto Posse <eposse.gmail.com>
  • Loading branch information
Ernesto Posse committed Jul 12, 2024
1 parent 76a3201 commit 10d1b81
Showing 1 changed file with 75 additions and 2 deletions.
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,26 +30,80 @@
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.
*
* @author Ernesto Posse
*/
public class ValidNameConstraint extends AbstractModelConstraint {

private static final String VALID_NAME_REGEX = "[a-zA-Z_](\\w|\\.)*";
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;
Expand All @@ -59,4 +117,19 @@ public IStatus validate(IValidationContext ctx) {

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;
}
}

0 comments on commit 10d1b81

Please sign in to comment.