Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » JMerge question
JMerge question [message #426894] Fri, 23 January 2009 19:01 Go to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
I have a merge.xml file;

<?xml version="1.0" encoding="UTF-8" ?>
<merge:options
xmlns:merge=" http://www.eclipse.org/org/eclipse/emf/codegen/jmerge/Option s">


<merge:dictionaryPatten
name="getCommon"
select="Method/getName"
match="(get)Common"/>

<merge:pull
targetMarkup="^get$"
sourceGet="Method/getBody"
targetPut="Method/setBody" />


</merge:options>

The source ( i.e. the generated file ) is as follows;

package test;

public class TheClass
{
public void getCommon()
{
System.out.println("Generated");
}
}


The target as follows;

package test;

public class TheClass
{
public void getCommon()
{
System.out.println("Edited");
}
}

The output is;

package test;

public class TheClass
{
public void getCommon()
{
System.out.println("Edited");
}
}


Surely it should use the source or generated method body?

I have tried other regex like expressions for the dictionaryPattern match
attribute value;

match=".*(get)Common.*"

match=".*(get)\w+"

match="^(get)\w+$"

to no avail...

What am I doing wrong?


I use the following to run the merge under an OSGi (Declarative) Service;

public class Test
{

/**
* @param args
*/
public static void main(String[] args)
{
try
{
System.out.println("Run Merge");
String dir = "C:/path/to/workspace/JMergeTest/sources/";
String generatedFilePath = dir + "generated/TheClass.java";
String alteredFilePath = dir + "edited/TheClass.java";
String mergedFilePath = dir + "mergeoutput/TheClass.java";

// ...
JMerger merger = getJMerger();

String generated = getSource(generatedFilePath);
String altered = getSource(alteredFilePath);

// set source
merger.setSourceCompilationUnit(
merger.createCompilationUnitForContents(generated));

// set target
merger.setTargetCompilationUnit(
merger.createCompilationUnitForContents(altered));

// merge source and target
merger.merge();

// extract merged contents and write them out
writeSource(mergedFilePath, merger.getTargetCompilationUnitContents());
}
catch(Throwable t)
{
System.out.println("Unexpected error "+ t);
t.printStackTrace();
}
finally
{
System.out.println("Merge run");
}
}


private static void writeSource(String mergedFilePath,
String targetCompilationUnitContents) throws IOException
{
PrintWriter out = new PrintWriter(new FileWriter(mergedFilePath));
out.print(targetCompilationUnitContents);
out.close();
}


private static String getSource(String filePath) throws IOException
{
BufferedReader reader = new BufferedReader( new FileReader(filePath));
String line;
String contents = "";
while((line = reader.readLine()) != null )
{
contents += (line + "\n");
}
return contents;
}


private static JMerger getJMerger() {
// build URI for merge document
String uri = "file:///path/to/workspace/JMergeTest/";
uri += "templates/merge.xml";

JControlModel controlModel = new JControlModel();
controlModel.initialize(new JDOMFacadeHelper(), uri);
JMerger jmerger = new JMerger(controlModel);
return jmerger;
}

}
Re: JMerge question [message #426898 is a reply to message #426894] Fri, 23 January 2009 22:25 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
JF,

Comments below.

JF wrote:
> I have a merge.xml file;
>
> <?xml version="1.0" encoding="UTF-8" ?> <merge:options
> xmlns:merge=" http://www.eclipse.org/org/eclipse/emf/codegen/jmerge/Option s">
>
>
>
> <merge:dictionaryPatten
> name="getCommon"
> select="Method/getName"
> match="(get)Common"/>
>
> <merge:pull
> targetMarkup="^get$"
> sourceGet="Method/getBody"
> targetPut="Method/setBody" />
>
>
> </merge:options>
>
> The source ( i.e. the generated file ) is as follows;
>
> package test;
>
> public class TheClass
> {
> public void getCommon() {
> System.out.println("Generated");
> }
> }
>
>
> The target as follows;
>
> package test;
>
> public class TheClass
> {
> public void getCommon() {
> System.out.println("Edited");
> }
> }
>
> The output is;
>
> package test;
>
> public class TheClass
> {
> public void getCommon() {
> System.out.println("Edited");
> }
> }
>
>
> Surely it should use the source or generated method body?
Nothing jumps out as obviously wrong.
>
> I have tried other regex like expressions for the dictionaryPattern
> match attribute value;
>
> match=".*(get)Common.*"
>
> match=".*(get)\w+"
>
> match="^(get)\w+$"
>
> to no avail...
>
> What am I doing wrong?
>
>
> I use the following to run the merge under an OSGi (Declarative) Service;
>
> public class Test
> {
>
> /**
> * @param args
> */
> public static void main(String[] args)
> {
> try
> {
> System.out.println("Run Merge");
> String dir = "C:/path/to/workspace/JMergeTest/sources/";
> String generatedFilePath = dir + "generated/TheClass.java";
> String alteredFilePath = dir + "edited/TheClass.java";
> String mergedFilePath = dir + "mergeoutput/TheClass.java";
>
> // ...
> JMerger merger = getJMerger();
>
> String generated = getSource(generatedFilePath);
> String altered = getSource(alteredFilePath);
>
> // set source
> merger.setSourceCompilationUnit(
> merger.createCompilationUnitForContents(generated));
>
> // set target
> merger.setTargetCompilationUnit(
> merger.createCompilationUnitForContents(altered));
>
> // merge source and target
> merger.merge();
>
> // extract merged contents and write them out
> writeSource(mergedFilePath,
> merger.getTargetCompilationUnitContents());
> }
> catch(Throwable t)
> {
> System.out.println("Unexpected error "+ t);
> t.printStackTrace();
> }
> finally
> {
> System.out.println("Merge run");
> }
> }
>
>
> private static void writeSource(String mergedFilePath,
> String
> targetCompilationUnitContents) throws IOException
> {
> PrintWriter out = new PrintWriter(new
> FileWriter(mergedFilePath));
> out.print(targetCompilationUnitContents);
> out.close();
> }
>
>
> private static String getSource(String filePath) throws IOException
> {
> BufferedReader reader = new BufferedReader( new
> FileReader(filePath));
> String line;
> String contents = "";
> while((line = reader.readLine()) != null )
> {
> contents += (line + "\n");
> }
> return contents;
> }
>
>
> private static JMerger getJMerger() {
> // build URI for merge document
> String uri = "file:///path/to/workspace/JMergeTest/";
> uri += "templates/merge.xml";
>
> JControlModel controlModel = new JControlModel();
> controlModel.initialize(new JDOMFacadeHelper(), uri);
> JMerger jmerger = new JMerger(controlModel);
> return jmerger;
> }
>
> }
If you package up something I can run (exported zipped projects), I'll
have a look to figure out what's going wrong.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: JMerge question [message #426916 is a reply to message #426898] Sun, 25 January 2009 14:30 Go to previous messageGo to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
Thanks, Attempting to upload zip
Re: JMerge question [message #426921 is a reply to message #426898] Mon, 26 January 2009 09:51 Go to previous messageGo to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
Here is the zip.
Thanks


Re: JMerge question [message #426923 is a reply to message #426921] Mon, 26 January 2009 11:45 Go to previous messageGo to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
Sorry ... typo in the merge.xml file was at fault
Re: JMerge question [message #426924 is a reply to message #426923] Mon, 26 January 2009 11:54 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
JF,

Excellent. Just as I was about to install it. :-)


JF wrote:
> Sorry ... typo in the merge.xml file was at fault
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Handling cross references
Next Topic:Load Ecore model as resource
Goto Forum:
  


Current Time: Wed Apr 24 13:45:10 GMT 2024

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

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

Back to the top