From c8637c6b669292cf34fccad030acc2e0b45eca87 Mon Sep 17 00:00:00 2001
From: Minh Hiep Pham <minh.pham@telecom-paristech.fr>
Date: Fri, 8 Feb 2019 12:31:45 +0100
Subject: [PATCH] create isAValidPortName method

---
 .../java/tmltranslator/TMLSyntaxChecking.java |  2 +-
 src/main/java/ui/TAttribute.java              | 50 +++++++++++++++++++
 .../java/ui/tmlcompd/TMLCPrimitivePort.java   |  4 +-
 3 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/src/main/java/tmltranslator/TMLSyntaxChecking.java b/src/main/java/tmltranslator/TMLSyntaxChecking.java
index d1718c942c..cb925d53df 100755
--- a/src/main/java/tmltranslator/TMLSyntaxChecking.java
+++ b/src/main/java/tmltranslator/TMLSyntaxChecking.java
@@ -396,7 +396,7 @@ public class TMLSyntaxChecking {
                 TMLCPrimitiveComponent tmlcpc = (TMLCPrimitiveComponent) t.getReferenceObject();
                 for (TMLCPrimitivePort tmlcpp : tmlcpc.getAllInternalPrimitivePorts()) {
                     String s = tmlcpp.getPortName();
-                    if(!TAttribute.isAValidId(s, false, true, false,true)) {
+                    if(!TAttribute.isAValidPortName(s, false, true, false,true)) {
                         addError(t, null, WRONG_VARIABLE_IDENTIFIER + ": invalid port name (" + s + ")", TMLError.ERROR_STRUCTURE);
                     }
                 }
diff --git a/src/main/java/ui/TAttribute.java b/src/main/java/ui/TAttribute.java
index f5ff90c303..fee2fc79a8 100644
--- a/src/main/java/ui/TAttribute.java
+++ b/src/main/java/ui/TAttribute.java
@@ -226,6 +226,56 @@ public class TAttribute {
         return (b1 && b2 && b3 && b4 && b5 && b6 && b7);
     }
 
+    //checking port name
+    //author : minh hiep
+    public static boolean isAValidPortName(String id, boolean checkKeyword, boolean checkUPPAALKeyword, boolean checkJavaKeyword,
+                                      boolean checkTMLKeyword) {
+        // test whether _id is a word
+
+        if ((id == null) || (id.length() < 1)) {
+            return false;
+        }
+
+        String lowerid = id.toLowerCase();
+        boolean b1, b2, b3, b4, b5, b6, b7;
+        boolean b8 = true;
+
+        String [] splitName = id.split("\\s*,\\s*");
+        for (int i = 0; i < splitName.length; i ++) {
+            if (!splitName[i].substring(0, 1).matches("[a-zA-Z]")) {
+                b8 = false;
+            }
+        }
+
+        b1 = (id.substring(0, 1)).matches("[a-zA-Z]");
+        b2 = id.matches("\\w*|(\\w*\\s*,\\s*\\w+)*");
+        if (checkKeyword) {
+            b3 = !RTLOTOSKeyword.isAKeyword(lowerid);
+        } else {
+            b3 = true;
+        }
+        if (checkUPPAALKeyword) {
+            b7 = !UPPAALKeyword.isAKeyword(lowerid);
+        } else {
+            b7 = true;
+        }
+
+        if (checkJavaKeyword) {
+            b5 = !JKeyword.isAKeyword(lowerid);
+        } else {
+            b5 = true;
+        }
+
+        b4 = !((lowerid.equals(getStringType(0).toLowerCase())) || (lowerid.equals(getStringType(1).toLowerCase())) || (lowerid.equals(getStringType(2).toLowerCase())) || (lowerid.equals(getStringType(3).toLowerCase())) || (lowerid.equals(getStringType(4).toLowerCase())));
+
+        if (checkTMLKeyword) {
+            b6 = TMLTextSpecification.checkKeywords(lowerid);
+        } else {
+            b6 = true;
+        }
+
+        return (b1 && b2 && b3 && b4 && b5 && b6 && b7 && b8);
+    }
     public static boolean isAValidInitialValue(int type, String value) {
         //boolean b;
         int val;
diff --git a/src/main/java/ui/tmlcompd/TMLCPrimitivePort.java b/src/main/java/ui/tmlcompd/TMLCPrimitivePort.java
index b297ce627b..9aac195477 100755
--- a/src/main/java/ui/tmlcompd/TMLCPrimitivePort.java
+++ b/src/main/java/ui/tmlcompd/TMLCPrimitivePort.java
@@ -638,7 +638,7 @@ public abstract class TMLCPrimitivePort extends TGCScalableWithInternalComponent
                 if ((s != null) && (s.length() > 0)) {
                     // Check whether this name is already in use, or not
 
-                    if (!TAttribute.isAValidId(s, false, true, false)) {
+                    if (!TAttribute.isAValidPortName(s, false, true, false,false)) {
                         JOptionPane.showMessageDialog(frame,
                                 "Could not change the name of the port: the new name (" + s + ")  is not a valid name",
                                 "Error",
@@ -852,7 +852,7 @@ public abstract class TMLCPrimitivePort extends TGCScalableWithInternalComponent
                                 if ((s != null) && (s.length() > 0)) {
                                     // Check whether this name is already in use, or not
 
-                                    if (!TAttribute.isAValidId(s, false, false, false)) {
+                                    if (!TAttribute.isAValidPortName(s, false, true, false,false)) {
                                         JOptionPane.showMessageDialog(null,
                                                 "the port name (" + s + ") is not a valid name",
                                                 "Modeling could not be correctly loaded",
-- 
GitLab