Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [JET2] Using abbreviate XPATH form in non java files
[JET2] Using abbreviate XPATH form in non java files [message #644347] Fri, 10 December 2010 16:20 Go to next message
Johan Schulz is currently offline Johan SchulzFriend
Messages: 20
Registered: November 2010
Junior Member
Hi,

im trying to use the short XPATH query type {$myVar} in

  • .classpath
  • .project
  • MANIFEST.MF
  • .properties

to build my project skeleton. These are missing if i use the <ws:project tag> (there must be any enhancement request for next summer, i think <ws:manifest> was the proposal).

this works quite well but i cant use any short XPATH expressions inside non java files, they are treated as strings. do i rewrite them to use the <c:get select="$myVar" /> syntax everything works fine.
Do i have to escape these short expressions?


Another problem is that i cannot pass any variables to a calling template, is there any workaround or do i have to query all neede information again in the subtemplates?


Regards and have a nice weekend Smile

Johan



Re: [JET2] Using abbreviate XPATH form in non java files [message #644369 is a reply to message #644347] Fri, 10 December 2010 17:28 Go to previous messageGo to next message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
Johan:

The correct form is ${$myVar} - it looks ugly, but that's the way it is.

As for passing variables, JET2 has a very simple variable model - all variables are global. There are a couple of approaches to make this more rational:

1) the c:includes tag has a passVariables attribute. If you pass a comma separated list of variable names (without the $), the called template will see only those. At the end of the template, all variables are restored to their pre-invocation state.

2) markup your model with <c:set>. In general, I find that doing any calculation inside a template that is generating content just obscures things. Instead, I advocate doing calculations prior to the template invocation, and storing those values as 'temporary' attributes on your model elements. Here's an example:

Imagine you have a model something like this:
<root>
   <class name="foo">
       <property name="firstProp" type="int"/>
       <property name="secondProp" type="boolean"/>
  </class>
</root>


Now suppose that you want to generate getter and setter methods from each <property> element. You need to calculate a bunch of different values:
* the name of the getter (for secondProp, you'd probably want isSecondProp)
* the name of the setter.
* the name of the field represented the backing property
* the name of the parameter in the setter

You could do all of this in the template that generates the getter/setter code, but, to me, that just obscures the template. Instead, I typically traverse the model like this:

<c:iterate select="/root/class" var="class">
    <c:iterate select="$class/property" var="property">
         <%-- calculate values needed for getter/setter generation --%>
         <c:set name="setter" select="$property" >set${uppercaseFirst(@name)}</c:set>
         <c:set name="getter" select="$property" ><c:choose select="@type"><c:when test=" 'boolean' ">is</c:when><c:otherwise>get</c:otherwise></c:choose>${uppercaseFirst(@name)}</c:set>
          ... and so on ...
    </c:iterate>
</c:iterate>


Then, in my template, I write nice simple code like this:

<%-- relying on current object being a class,
 the loop with change the current object to a property
 --%>
<c:iterate select="property">
      /**
       * Return the value of property ${@name}
       */
      public ${@type} ${@getter}() {
          return ${@fieldName};
      }

      /**
       * Set the value of property ${@name}
       * @param ${@argName}
       */
     public void ${@setter}(${@type} ${@argName}) {
        this.${@fieldName} = ${@argName};
    }

</c:iterate>


Let me know if this works for you.

Paul
Re: [JET2] Using abbreviate XPATH form in non java files [message #644553 is a reply to message #644369] Mon, 13 December 2010 09:28 Go to previous message
Johan Schulz is currently offline Johan SchulzFriend
Messages: 20
Registered: November 2010
Junior Member
Hi,

sorry for my late reply, i needed a computer break at the weekend Smile.

Paul Elder wrote on Fri, 10 December 2010 12:28

The correct form is ${$myVar} - it looks ugly, but that's the way it is.



that dollar sign was the missing piece, thanks.
Now it works like a charm.

Thanks for the template tips too, ill rewrite them to match the much shorter version.


Regards

Johan




Previous Topic:[Acceleo] Intermittent Bug: Nonsense compiler errors that disappear after file is touched
Next Topic:[JET2] JET2 Editor?
Goto Forum:
  


Current Time: Fri Nov 28 17:33:21 GMT 2014

Powered by FUDForum. Page generated in 0.02287 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software