From dbd1375af8c532fffdc889b02a73c9c4a54c7f64 Mon Sep 17 00:00:00 2001
From: Arnaud Cuccuru
Date: Thu, 24 Oct 2013 12:38:08 +0200
Subject: Adding a NamingUtils class to the sandbox version of the alf editor.
---
.../.launch/Launch Runtime Eclipse.launch | 2 +-
.../org.eclipse.papyrus.alf/META-INF/MANIFEST.MF | 1 +
.../org/eclipse/papyrus/alf/utils/NamingUtils.java | 66 ++++++++++++++++++++++
3 files changed, 68 insertions(+), 1 deletion(-)
create mode 100644 sandbox/Alf/org.eclipse.papyrus.alf/src/org/eclipse/papyrus/alf/utils/NamingUtils.java
diff --git a/sandbox/Alf/org.eclipse.papyrus.alf/.launch/Launch Runtime Eclipse.launch b/sandbox/Alf/org.eclipse.papyrus.alf/.launch/Launch Runtime Eclipse.launch
index 5f7b41343aa..e7148f9d28d 100644
--- a/sandbox/Alf/org.eclipse.papyrus.alf/.launch/Launch Runtime Eclipse.launch
+++ b/sandbox/Alf/org.eclipse.papyrus.alf/.launch/Launch Runtime Eclipse.launch
@@ -22,7 +22,7 @@
-
+
diff --git a/sandbox/Alf/org.eclipse.papyrus.alf/META-INF/MANIFEST.MF b/sandbox/Alf/org.eclipse.papyrus.alf/META-INF/MANIFEST.MF
index 0b681d73191..26aa98402fb 100644
--- a/sandbox/Alf/org.eclipse.papyrus.alf/META-INF/MANIFEST.MF
+++ b/sandbox/Alf/org.eclipse.papyrus.alf/META-INF/MANIFEST.MF
@@ -31,4 +31,5 @@ Export-Package: org.eclipse.papyrus.alf,
org.eclipse.papyrus.alf.parser.antlr.internal,
org.eclipse.papyrus.alf.scoping,
org.eclipse.papyrus.alf.services,
+ org.eclipse.papyrus.alf.utils,
org.eclipse.papyrus.alf.validation
diff --git a/sandbox/Alf/org.eclipse.papyrus.alf/src/org/eclipse/papyrus/alf/utils/NamingUtils.java b/sandbox/Alf/org.eclipse.papyrus.alf/src/org/eclipse/papyrus/alf/utils/NamingUtils.java
new file mode 100644
index 00000000000..3c397eba236
--- /dev/null
+++ b/sandbox/Alf/org.eclipse.papyrus.alf/src/org/eclipse/papyrus/alf/utils/NamingUtils.java
@@ -0,0 +1,66 @@
+package org.eclipse.papyrus.alf.utils;
+
+import org.eclipse.uml2.uml.Namespace;
+
+
+public class NamingUtils {
+
+
+ /**
+ * Checks if a string represents a valid Java-name:
+ * - it starts with a letter or "_"
+ * - other characters are either letters, figures or "_"
+ *
+ * @param name
+ * @return true if the name is Java-compliant, false otherwise
+ */
+ public static boolean isJavaCompliant(String name) {
+ if(name.length() == 0)
+ return false;
+ int firstChar = 0;
+ char[] dst = new char[name.length()];
+ name.getChars(0, name.length(), dst, firstChar);
+ if(dst[0] >= 'a' && dst[0] <= 'z')
+ ;
+ else if(dst[0] >= 'A' && dst[0] <= 'Z')
+ ;
+ else if(dst[0] == '_')
+ ;
+ else
+ return false;
+ for(int i = 1; i < dst.length; i++) {
+ if(dst[i] >= 'a' && dst[i] <= 'z')
+ ;
+ else if(dst[i] >= 'A' && dst[i] <= 'Z')
+ ;
+ else if(dst[i] >= '0' && dst[i] <= '9')
+ ;
+ else if(dst[i] == '_')
+ ;
+ else
+ return false;
+ }
+ return true;
+ }
+
+ public static String getNormalizedName(String name) {
+ return "'" + name + "'";
+ }
+
+ public static String getNormalizedQualifiedName(Namespace namespace, String context) {
+
+ String qualifiedName = context;
+
+ while(namespace != null) {
+ if(NamingUtils.isJavaCompliant(namespace.getName())) {
+ qualifiedName = namespace.getName() + "::" + qualifiedName;
+ } else {
+ qualifiedName = "'" + namespace.getName() + "'" + "::" + qualifiedName;
+ }
+ namespace = namespace.getNamespace();
+ }
+
+ return qualifiedName;
+ }
+
+}
--
cgit v1.2.3