Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » Custom tag, SortImportStatments
Custom tag, SortImportStatments [message #43980] Fri, 04 April 2008 09:04 Go to next message
Markus Backman is currently offline Markus BackmanFriend
Messages: 3
Registered: July 2009
Junior Member
Hi

I have implemented a JET2 plugin that from a xml model generates an EJB
component that follows my companies architeture and guidelines. This works
great and will certainly reduce the development time of new EJB services.
But we also uses Checkstyle to control the sourcecode syntax and this
creates a problem as one of the checkstyle rules that is in use controls
the import order. That is that import java.* comes first then import
packages in alpabetic order. As the model input defines what package the
generated classes will belong to it is impossible to create a right import
order in the template for all model input.

For example

import se.swedbank.infrastructure.....NotGeneratedClass;
import se.swedbank.<c:get select="$pkg" />.....AGenerateClass;

If the model for example sets a $pkg with the name "agreement" checkstyle
will complain at the newly generated code as se.swedbank.i is alpabeticly
after se.swedbank.a.

So I am thinking of creating a custom taglib that looks something like
this.

<cust:sortImport>
import se.swedbank.infrastructure.....NotGeneratedClass;
import se.swedbank.<c:get select="$pkg" />.....AGenerateClass;
</cust:sortImport>

And after evaluation it will print the following in the generated class

import se.swedbank.agreement.....AGenerateClass;
import se.swedbank.infrastructure.....NotGeneratedClass;

And so to my questions. Is this possible? Is AbstractContainerTag the
baseclass to use? Can a ContainerTag handle a body that contains other
tags? Will the tags within the body be evaluated first? Is there any code
example that could point me in the right direction?

Best regards,
Markus
Re: Custom tag, SortImportStatments [message #44016 is a reply to message #43980] Mon, 07 April 2008 12:40 Go to previous messageGo to next message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
Markus:

A couple of ideas:

1) Have you played at all with the java:importsLocation and java:imports
Tags? They can do much of the imports management for you. Here's how you use
it...

package ...;
import ...; // static imports...
<java:importsLocation package="..."/>

.... then in your code...
<java:import>someFQClassName</java:import>

This would do everything, but sort your imports in the right order. These
tags use EMF's codegen's ImportManager, which sorts imports purely
alphabetically and does not rewrite statically imported classes. IF your
java.* imports are all static (i.e. known when you write the template, take
the following approach:

package ...;

import java....; // all your Java imports...

<java:importsLocation package="..."/>

.... use <java:import>...</java:import> around all your other types.

Getting more involved would be figuring out how to subclass ImportManager
and get the imports collection sorted the way you want. This would probably
involve cloning the current java:importsLocation tag.

2) If you are not using the ImportManager tags, then writing a JET custom
tag to sort its contents is not difficult. Your sortImport tag would be
declared as a 'functionTag'. This is a simple for of a container tag that
takes is content as an input, and then writes a compute result. You end up
having to implement a single function:

public class SortImportsTag extends AbstractFunctionTag {


public String doFunction(TagInfo td, JET2Context context, String
bodyContent)

throws JET2TagException {

// TODO parse through bodyContent, sort the imports as you like, and return
the sorted result.

return null;

}


}


"Markus Backman" <markus.backman@swedbank.se> wrote in message
news:4cfc478221d79cae753094d4ea306956$1@www.eclipse.org...
> Hi
>
> I have implemented a JET2 plugin that from a xml model generates an EJB
> component that follows my companies architeture and guidelines. This works
> great and will certainly reduce the development time of new EJB services.
> But we also uses Checkstyle to control the sourcecode syntax and this
> creates a problem as one of the checkstyle rules that is in use controls
> the import order. That is that import java.* comes first then import
> packages in alpabetic order. As the model input defines what package the
> generated classes will belong to it is impossible to create a right import
> order in the template for all model input.
>
> For example
>
> import se.swedbank.infrastructure.....NotGeneratedClass;
> import se.swedbank.<c:get select="$pkg" />.....AGenerateClass;
>
> If the model for example sets a $pkg with the name "agreement" checkstyle
> will complain at the newly generated code as se.swedbank.i is alpabeticly
> after se.swedbank.a.
> So I am thinking of creating a custom taglib that looks something like
> this.
>
> <cust:sortImport>
> import se.swedbank.infrastructure.....NotGeneratedClass;
> import se.swedbank.<c:get select="$pkg" />.....AGenerateClass;
> </cust:sortImport>
>
> And after evaluation it will print the following in the generated class
>
> import se.swedbank.agreement.....AGenerateClass;
> import se.swedbank.infrastructure.....NotGeneratedClass;
>
> And so to my questions. Is this possible? Is AbstractContainerTag the
> baseclass to use? Can a ContainerTag handle a body that contains other
> tags? Will the tags within the body be evaluated first? Is there any code
> example that could point me in the right direction?
>
> Best regards,
> Markus
>
>
Re: Custom tag, SortImportStatments [message #661545 is a reply to message #44016] Fri, 25 March 2011 09:01 Go to previous message
pallavi is currently offline pallaviFriend
Messages: 1
Registered: March 2011
Junior Member
Hi there,,,

Can any one plz provide exact Code for Above requirement.I am not getting how to use Body content.this is urgent..
Previous Topic:[xpand/mwe] worflow configuration example
Next Topic:[MWE2] Referring to paths/files inside standalone JAR
Goto Forum:
  


Current Time: Fri Apr 26 15:06:52 GMT 2024

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

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

Back to the top