Sparx Systems Forum

Enterprise Architect => General Board => Topic started by: Spaider on September 01, 2003, 08:02:52 am

Title: Quotation mark in template output (EA 3.6)
Post by: Spaider on September 01, 2003, 08:02:52 am
Now I'm studying code templates and faced with unexpected trouble:

How can I generate quotation marks in resulting code?

(e.g. for generating string parameters).
Most common escape sequences doesn't work. I've tried \", \034, \0x22, I've tried doubling quotation marks. No positive results :(

Thanks in advance!
Title: Re: Quotation mark in template output (EA 3.6)
Post by: Sam_Mancarella on September 01, 2003, 04:51:20 pm
EA3.60 (635) currently does not allow escaping of the percent symbol (%), dollars symbol ($) or double-quote symbols ("). Presently, to escape a % or a $, it would have to be assigned to a variable as a string literal, viz:

$dollar_sign = "$"
$percent_sign = "%"

$something = $dollar_sign + "foobar" + $dollar_sign

This behaviour is described under "Planned Extensions" in the "Code Template Framework" section of EA's help.

To escape a double-quote in templates is somewhat different. You would have to output the double-quote as literal text.

For example - the Operation Body template in VBNet and Visual Basic has the following statement:
 %opName% = ""

Which assigns a return value of "" if the operation is a function and it is of String type.

To achieve a similar result for setting default parameter values (for example), you could use the following "Parameter" template in VB  and VBNet:

%PI=" "%
%paramDefault ? "Optional"%
%paramKind == "in" ? "ByVal" : ""%
%paramKind == "inout" ? "ByRef" : ""%
%paramKind == "out" ? "ByRef" : ""%
%paramName%
As
%PI=""%
%paramType%
%if paramType == "string" and paramDefault != ""%
="%paramDefault%"
%else%
%paramDefault ? "=" value%
%endIf%

NB: To use this template, you would have to ensure that your default string parameter value is saved without quotes.

Hope this helps.

Sam Mancarella
Sparx Systems

Title: Re: Quotation mark in template output (EA 3.6)
Post by: Spaider on September 02, 2003, 02:03:22 am
Quote
Hope this helps.


Unfortunately, it doesn't :(

I have slightly different situation. Here's an example: suppose, I want to notify that some property has changed. I had method NotifyPropertyChanged () for this. It takes one argument of type System.String (I'm writing in C#).  I.e. the code snippet for property setter will be something like:


public double Amount
{
  set
  {
      m_Amount=value;
      NotifyPropertyChanged ("Abount");
  }
}


How should I modify default template


$att=%opTag:"attribute_name"=="" ? "<unknown>" : value%
%if opTag:"writeonly" == ""%
$read="\tget{\n\t\treturn " + $att + ";\n\t}"
%endIf%
%if opTag:"readonly" == ""%
$write="\tset{\n\t\t" + $att + " = value;\n\t}"
%endIf%


to achive this?

PS Don't treat me lazy :) I've tried all variants I can imagine (not forgetting RTFM)
Title: Re: Quotation mark in template output (EA 3.6)
Post by: benc on September 02, 2003, 05:21:16 pm
Hi Spaider,

I think the problem stems from not  currently being able to use the quotes in variable definitions.

I may be misreading your intention here, but I think the following modifications to the default OperationBody template (with property stereotype) achieves what you are after.

1. Change the line:
$write="\tset{\n\t\t" + $att + " = value;\n\t}"

to

$write="\tset{\n\t\t" + $att + " = value;"

2. Change the line that finally outputs $write. So:

$read
$write

becomes

$read
%if $write != ""%
$write
\t\tNotifyPropertyChanged("%opName%");\n\t}
%endIf%

3. Now when I generate the property I get:

public String Amount{
 get{
   return att;
 }
 set{
   att = value;
   NotifyPropertyChanged("Amount");
 }
}


Admittedly, this solution is far from elegant, because the quotation marks could not used as part of the first variable definition for $write.  We will be updating the template syntax ASAP to support use of the ", %, $, characters.  For the short term at least, this will probably take the form of special macros- something %qt%, %pc%, %dl%.  Then they can be mixed with variable references/definitions and literal text.

I hope this helps.  Let me know if I've misread the problem.

Regards
Ben
Title: Re: Quotation mark in template output (EA 3.6)
Post by: Spaider on September 02, 2003, 10:12:11 pm
Many thanks!

This time everything is OK.
Title: Re: Quotation mark in template output (EA 3.6)
Post by: benc on September 04, 2003, 10:24:52 pm
Hi Spaider,

As of build 638, you can use the %pc%, %dl% and %qt% macros to get %, $, " characters respectively.  You can use these with variable definitions or as a direct substitutions.  Should provide a neater solution for getting the quotation marks into your property bodies.

Regards,
Ben