From df10b4a96bce6280bbb34c3f0cfdbaf12e369853 Mon Sep 17 00:00:00 2001
From: BenceJanosSzabo <bence.janos.szabo@ericsson.com>
Date: Mon, 11 Sep 2017 13:43:54 +0200
Subject: [PATCH] xsd2ttcn: special characters are escaped in text variant (Bug
 522050)

Change-Id: Ie643ec32bd9f83f9fe944ce51e6fcfe452b661f7
Signed-off-by: BenceJanosSzabo <bence.janos.szabo@ericsson.com>
---
 ...xample_org_enumeration_restriction3_e.ttcn |  6 +++
 .../xsd/enumeration_restriction3.xsd          |  3 ++
 xsdconvert/GeneralFunctions.cc                | 48 +++++++++++++++++--
 3 files changed, 52 insertions(+), 5 deletions(-)

diff --git a/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_enumeration_restriction3_e.ttcn b/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_enumeration_restriction3_e.ttcn
index 0272295a0..734ae7166 100644
--- a/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_enumeration_restriction3_e.ttcn
+++ b/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_enumeration_restriction3_e.ttcn
@@ -53,10 +53,16 @@ with {
 type enumerated CType
 {
 	b2,
+	numbers,
+	something,
+	something_1,
 	uNKNOWN
 }
 with {
   variant "text 'b2' as capitalized";
+  variant "text 'numbers' as '&lt;&gt;&quot;&amp;&apos;numbers&lt;&gt;&quot;&amp;&apos;'";
+  variant "text 'something' as '&apos;&gt;something&apos;&gt;'";
+  variant "text 'something_1' as '&quot;&gt;something&quot;&gt;&amp;&amp;'";
   variant "text 'uNKNOWN' as capitalized";
 };
 
diff --git a/regression_test/XML/XmlWorkflow/xsd/enumeration_restriction3.xsd b/regression_test/XML/XmlWorkflow/xsd/enumeration_restriction3.xsd
index 8b2e64981..b2f1d2150 100644
--- a/regression_test/XML/XmlWorkflow/xsd/enumeration_restriction3.xsd
+++ b/regression_test/XML/XmlWorkflow/xsd/enumeration_restriction3.xsd
@@ -14,6 +14,9 @@
 		<maxLength value="32" />
 		<enumeration value="B2" />
 		<enumeration value="UNKNOWN" />
+		<enumeration value="'>something'>" />
+		<enumeration value='">something">&amp;&#38;' />
+		<enumeration value="&lt;&gt;&quot;&amp;&apos;numbers&#60;&#62;&#34;&#38;&#39;" />
 	</restriction>
 </simpleType>
 
diff --git a/xsdconvert/GeneralFunctions.cc b/xsdconvert/GeneralFunctions.cc
index a4682f0c9..087557f85 100644
--- a/xsdconvert/GeneralFunctions.cc
+++ b/xsdconvert/GeneralFunctions.cc
@@ -49,7 +49,7 @@ extern bool t_flag_used;
 // 				variant - generated variant string for TTCN-3
 //
 
-void XSDName2TTCN3Name(const Mstring& in, QualifiedNames & used_names, modeType type_of_the_name,
+void XSDName2TTCN3Name(const Mstring& in_str, QualifiedNames & used_names, modeType type_of_the_name,
   Mstring & res, Mstring & variant, bool no_replace) {
   static const char* TTCN3_reserved_words[] = {
     "action", "activate", "address", "alive", "all", "alt", "altstep", "and", "and4b", "any", "any2unistr", "anytype", "apply",
@@ -121,6 +121,21 @@ void XSDName2TTCN3Name(const Mstring& in, QualifiedNames & used_names, modeType
   };
 
   Mstring ns_uri(variant);
+  
+  Mstring in = in_str;
+  
+  // First of all if the enum value contains &#38; , change it to &
+  if (type_of_the_name == enum_id_name) {
+    for (size_t i = 0; i < in.size(); i++) {
+      if (in[i] == '&' && i+4 < in.size() && in[i+1] == '#' && in[i+2] == '3' && in[i+3] == '8' && in[i+4] == ';') {
+        // Here convert the &#38; to &
+        in.eraseChar(i+4);
+        in.eraseChar(i+3);
+        in.eraseChar(i+2);
+        in.eraseChar(i+1);
+      }
+    }
+  }
 
   res.clear();
   variant.clear();
@@ -387,17 +402,40 @@ void XSDName2TTCN3Name(const Mstring& in, QualifiedNames & used_names, modeType
           variant += "\"name as '" + in + "'\"";
         }
         break;
-      case enum_id_name:
-        if (tmp1 == tmp2) { // If the only difference is the case of the first letter
+      case enum_id_name: {
+        // Escape some special characters.
+        Mstring escaped_in;
+        bool found_spec = false;
+        for (size_t i = 0; i < in.size(); i++) {
+          if (in[i] == '\'') {
+            escaped_in = escaped_in + "&apos;";
+            found_spec = true;
+          } else if (in[i] == '\"') {
+            escaped_in = escaped_in + "&quot;";
+            found_spec = true;
+          } else if (in[i] == '>') {
+            escaped_in = escaped_in + "&gt;";
+            found_spec = true;
+          } else if (in[i] == '<') {
+            escaped_in = escaped_in + "&lt;";
+            found_spec = true;
+          } else if (in[i] == '&') {
+            escaped_in = escaped_in + "&amp;";
+            found_spec = true;
+          } else {
+            escaped_in = escaped_in + in[i];
+          }
+        }
+        if (tmp1 == tmp2 && found_spec == false) { // If the only difference is the case of the first letter
           if (isupper(in[0])) {
             variant += "\"text \'" + res + "\' as capitalized\"";
           } else {
             variant += "\"text \'" + res + "\' as uncapitalized\"";
           }
         } else { // Otherwise if other letters have changed too
-          variant += "\"text \'" + res + "\' as '" + in + "'\"";
+          variant += "\"text \'" + res + "\' as '" + escaped_in + "'\"";
         }
-        break;
+        break; }
       default:
         break;
     }
-- 
GitLab