Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/org.eclipse.gmf.codegen/src/org/eclipse/gmf/codegen/util/CodegenEmitters.java21
-rw-r--r--plugins/org.eclipse.gmf.codegen/src/org/eclipse/gmf/codegen/util/Generator.java11
-rw-r--r--plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/DefaultTextMerger.java67
-rw-r--r--plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/FileServices.java72
-rw-r--r--plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/GeneratorBase.java132
-rw-r--r--plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/PropertiesTextMerger.java17
-rw-r--r--plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/TaggedTextMerger.java2
-rw-r--r--plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/TextMerger.java49
-rw-r--r--plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/graphdef/codegen/StandaloneGenerator.java11
-rw-r--r--tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/AllTests.java4
-rw-r--r--tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/PrpTextMergerTest.java51
-rw-r--r--tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/TaggedTextMergerTest.java (renamed from tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/TextMergerTest.java)15
-rw-r--r--tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/TestDefaultMergeService.java108
-rw-r--r--tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/XmlTextMergerTest.java18
14 files changed, 378 insertions, 200 deletions
diff --git a/plugins/org.eclipse.gmf.codegen/src/org/eclipse/gmf/codegen/util/CodegenEmitters.java b/plugins/org.eclipse.gmf.codegen/src/org/eclipse/gmf/codegen/util/CodegenEmitters.java
index 097ea366c..3ac29b6e9 100644
--- a/plugins/org.eclipse.gmf.codegen/src/org/eclipse/gmf/codegen/util/CodegenEmitters.java
+++ b/plugins/org.eclipse.gmf.codegen/src/org/eclipse/gmf/codegen/util/CodegenEmitters.java
@@ -15,6 +15,9 @@ import java.net.URL;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.codegen.jet.JETCompiler;
+import org.eclipse.emf.codegen.merge.java.JControlModel;
+import org.eclipse.emf.codegen.merge.java.JMerger;
+import org.eclipse.emf.codegen.util.CodeGenUtil;
import org.eclipse.emf.common.util.URI;
import org.eclipse.gmf.codegen.templates.commands.ReorientConnectionViewCommandGenerator;
import org.eclipse.gmf.codegen.templates.editor.ActionBarContributorGenerator;
@@ -96,10 +99,12 @@ import org.eclipse.gmf.internal.codegen.dispatch.NoSuchTemplateException;
import org.eclipse.gmf.internal.codegen.dispatch.StaticTemplateRegistry;
import org.eclipse.gmf.internal.codegen.dispatch.TemplateRegistry;
import org.eclipse.gmf.internal.common.codegen.BinaryEmitter;
+import org.eclipse.gmf.internal.common.codegen.DefaultTextMerger;
import org.eclipse.gmf.internal.common.codegen.GIFEmitter;
import org.eclipse.gmf.internal.common.codegen.JETEmitterAdapter;
import org.eclipse.gmf.internal.common.codegen.JETGIFEmitterAdapter;
import org.eclipse.gmf.internal.common.codegen.TextEmitter;
+import org.eclipse.gmf.internal.common.codegen.TextMerger;
import org.osgi.framework.Bundle;
/**
@@ -130,6 +135,22 @@ public class CodegenEmitters {
myFactory = new CachingEmitterFactory(new EmitterFactoryImpl(getTemplatePath(), registry, usePrecompiled, variables));
}
+ /**
+ * @return null if no merger is needed
+ */
+ public TextMerger createMergeService() {
+ URL controlFile = getJMergeControlFile();
+ if (controlFile != null){
+ JControlModel controlModel = new JControlModel();
+ controlModel.initialize(CodeGenUtil.instantiateFacadeHelper(JMerger.DEFAULT_FACADE_HELPER_CLASS), controlFile.toString());
+ if (!controlModel.canMerge()){
+ throw new IllegalStateException("Can not initialize JControlModel");
+ }
+ return new DefaultTextMerger(controlModel);
+ }
+ return null;
+ }
+
private static TemplateRegistry initRegistry() {
final StaticTemplateRegistry tr = new StaticTemplateRegistry(CodegenEmitters.class.getClassLoader());
put(tr, "/commands/ReorientConnectionViewCommand.javajet", ReorientConnectionViewCommandGenerator.class);
diff --git a/plugins/org.eclipse.gmf.codegen/src/org/eclipse/gmf/codegen/util/Generator.java b/plugins/org.eclipse.gmf.codegen/src/org/eclipse/gmf/codegen/util/Generator.java
index cdd556bb9..6c7c83f76 100644
--- a/plugins/org.eclipse.gmf.codegen/src/org/eclipse/gmf/codegen/util/Generator.java
+++ b/plugins/org.eclipse.gmf.codegen/src/org/eclipse/gmf/codegen/util/Generator.java
@@ -11,7 +11,6 @@
*/
package org.eclipse.gmf.codegen.util;
-import java.net.URL;
import java.util.Iterator;
import org.eclipse.core.runtime.IPath;
@@ -42,6 +41,7 @@ import org.eclipse.gmf.common.UnexpectedBehaviourException;
import org.eclipse.gmf.internal.common.codegen.GeneratorBase;
import org.eclipse.gmf.internal.common.codegen.ImportUtil;
import org.eclipse.gmf.internal.common.codegen.TextEmitter;
+import org.eclipse.gmf.internal.common.codegen.TextMerger;
/**
* Invokes templates to populate diagram editor project.
@@ -63,8 +63,13 @@ public class Generator extends GeneratorBase implements Runnable {
myEmitters = emitters;
}
- protected URL getJMergeControlFile() {
- return myEmitters.getJMergeControlFile();
+ @Override
+ protected TextMerger createMergeService() {
+ TextMerger service = myEmitters.createMergeService();
+ if (service != null) {
+ return service;
+ }
+ return super.createMergeService();
}
protected void customRun() throws InterruptedException, UnexpectedBehaviourException {
diff --git a/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/DefaultTextMerger.java b/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/DefaultTextMerger.java
new file mode 100644
index 000000000..688510407
--- /dev/null
+++ b/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/DefaultTextMerger.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Artem Tikhomirov (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.internal.common.codegen;
+
+import org.eclipse.emf.codegen.merge.java.JControlModel;
+import org.eclipse.emf.codegen.merge.java.JMerger;
+import org.eclipse.emf.codegen.merge.properties.PropertyMerger;
+
+/**
+ * @author artem
+ */
+public class DefaultTextMerger extends TextMerger {
+ protected static final String TAG = "gmf generator persistent region"; //$NON-NLS-1$
+
+ protected static final String BEGIN_TAG = TAG + " begin"; //$NON-NLS-1$
+
+ protected static final String END_TAG = TAG + " end"; //$NON-NLS-1$
+
+ private final JControlModel myControlModel;
+
+ private final TaggedTextMerger myXmlMerger;
+
+ public DefaultTextMerger(JControlModel jModel) {
+ assert jModel != null;
+ myControlModel = jModel;
+ myXmlMerger = new TaggedTextMerger("<!-- " + BEGIN_TAG + " -->", "<!-- " + END_TAG + " -->");
+ }
+
+ @Override
+ public String mergeJava(String oldText, String newText) {
+ if (getJControlModel() == null || !getJControlModel().canMerge()) {
+ return super.mergeJava(oldText, newText);
+ }
+ JMerger jMerge = new JMerger(getJControlModel());
+ jMerge.setSourceCompilationUnit(jMerge.createCompilationUnitForContents(oldText));
+ jMerge.setTargetCompilationUnit(jMerge.createCompilationUnitForContents(newText));
+ jMerge.merge();
+ return jMerge.getTargetCompilationUnitContents();
+ }
+
+ @Override
+ public String mergeProperties(String oldText, String newText) {
+ PropertyMerger propertyMerger = new PropertyMerger();
+ propertyMerger.setSourceProperties(newText);
+ propertyMerger.setTargetProperties(oldText);
+ propertyMerger.merge();
+ return propertyMerger.getTargetProperties();
+ }
+
+ @Override
+ public String mergeXML(String oldText, String newText) {
+ return myXmlMerger.process(oldText, newText);
+ }
+
+ private JControlModel getJControlModel() {
+ return myControlModel;
+ }
+}
diff --git a/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/FileServices.java b/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/FileServices.java
new file mode 100644
index 000000000..9c3c8861f
--- /dev/null
+++ b/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/FileServices.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Artem Tikhomirov (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.internal.common.codegen;
+
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+
+/**
+ * @author artem
+ */
+class FileServices {
+
+ public static String getFileContents(IFile file) {
+ StringBuffer contents = new StringBuffer();
+ char[] buffer = new char[1024];
+ int count;
+ try {
+ Reader in = new InputStreamReader(file.getContents(true), file.getCharset());
+ try {
+ while ((count = in.read(buffer)) > 0) {
+ contents.append(buffer, 0, count);
+ }
+ } finally {
+ in.close();
+ }
+ } catch (CoreException ce) {
+ ce.printStackTrace();
+ return null;
+ } catch (IOException ioe) {
+ ioe.printStackTrace();
+ return null;
+ }
+ return contents.toString();
+ }
+
+ /**
+ * @return <code>true</code> if the file contains the input stream contents
+ */
+// public static boolean contains(IFile f, InputStream is) {
+// int fc = 0;
+// int ic = 0;
+// InputStream fs = null;
+// try {
+// fs = f.getContents(true);
+// while ((fc = fs.read()) == (ic = is.read()) && fc >= 0);
+// } catch (CoreException ce) {
+// } catch (IOException ioe) {
+// } finally {
+// if (fs != null) {
+// try {
+// fs.close();
+// } catch (IOException ioe) {
+// }
+// }
+// }
+// return fc <0 && ic < 0;
+// }
+
+}
diff --git a/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/GeneratorBase.java b/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/GeneratorBase.java
index 30ae1d5d9..556302588 100644
--- a/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/GeneratorBase.java
+++ b/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/GeneratorBase.java
@@ -12,10 +12,6 @@
package org.eclipse.gmf.internal.common.codegen;
import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
@@ -38,8 +34,6 @@ import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
-import org.eclipse.emf.codegen.merge.java.JControlModel;
-import org.eclipse.emf.codegen.merge.java.JMerger;
import org.eclipse.emf.codegen.util.CodeGenUtil;
import org.eclipse.emf.codegen.util.CodeGenUtil.EclipseUtil;
import org.eclipse.emf.ecore.EClass;
@@ -62,7 +56,6 @@ import org.eclipse.text.edits.TextEdit;
*/
public abstract class GeneratorBase implements Runnable {
- private JControlModel myJControlModel;
private CodeFormatter myCodeFormatter;
private IProgressMonitor myProgress = new NullProgressMonitor();
@@ -71,8 +64,11 @@ public abstract class GeneratorBase implements Runnable {
private IProject myDestProject;
private final List<IStatus> myExceptions;
private IStatus myRunStatus = Status.CANCEL_STATUS;
+ private TextMerger myMerger;
- protected abstract URL getJMergeControlFile();
+ protected final URL getJMergeControlFile() {
+ return null;
+ }
protected abstract void customRun() throws InterruptedException, UnexpectedBehaviourException;
@@ -215,32 +211,29 @@ public abstract class GeneratorBase implements Runnable {
IProgressMonitor pm = getNextStepMonitor();
try {
setProgressTaskName(filePath.lastSegment());
- pm.beginTask(null, 4);
+ pm.beginTask(null, 5);
IPath containerPath = myDestProject.getFullPath().append(filePath.removeLastSegments(1));
EclipseUtil.findOrCreateContainer(containerPath, false, (IPath) null, new SubProgressMonitor(pm, 1));
String genText = emitter.generate(new SubProgressMonitor(pm, 1), param);
IFile f = myDestProject.getFile(filePath);
- boolean propertyFile = "properties".equals(filePath.getFileExtension());
+ final boolean propertyFile = "properties".equals(filePath.getFileExtension());
String charset = propertyFile ? "ISO-8859-1" : "UTF-8";
if (propertyFile) {
genText = Conversions.escapeUnicode(genText);
}
String oldText = null;
if (f.exists()) {
- oldText = getFileContents(f);
+ oldText = FileServices.getFileContents(f);
}
if (oldText != null) {
- TextMerger merger = TextMerger.getForFile(f.getName());
- if (merger != null) {
- genText = merger.process(oldText, genText);
- }
+ genText = mergePlainText(oldText, genText, f, new SubProgressMonitor(pm, 1));
if (!oldText.equals(genText)) {
f.setContents(new ByteArrayInputStream(genText.getBytes(charset)), true, true, new SubProgressMonitor(pm, 1));
} else {
pm.worked(1);
}
} else {
- f.create(new ByteArrayInputStream(genText.getBytes(charset)), true, new SubProgressMonitor(pm, 1));
+ f.create(new ByteArrayInputStream(genText.getBytes(charset)), true, new SubProgressMonitor(pm, 2));
}
f.getParent().refreshLocal(IResource.DEPTH_ONE, new SubProgressMonitor(pm, 1));
} catch (InvocationTargetException ex) {
@@ -256,29 +249,6 @@ public abstract class GeneratorBase implements Runnable {
}
}
- private static String getFileContents(IFile file) {
- StringBuffer contents = new StringBuffer();
- char[] buffer = new char[1024];
- int count;
- try {
- Reader in = new InputStreamReader(file.getContents(true), file.getCharset());
- try {
- while ((count = in.read(buffer)) > 0) {
- contents.append(buffer, 0, count);
- }
- } finally {
- in.close();
- }
- } catch (CoreException ce) {
- ce.printStackTrace();
- return null;
- } catch (IOException ioe) {
- ioe.printStackTrace();
- return null;
- }
- return contents.toString();
- }
-
/**
* Inspired by GenBaseImpl.EclipseUtil.findOrCreateContainer
* Although later (with EMF API adopting Platform changes) we might need to return URI here
@@ -315,29 +285,6 @@ public abstract class GeneratorBase implements Runnable {
}
}
- /**
- * @return <code>true</code> if the file contains the input stream contents
- */
- protected boolean contains(IFile f, InputStream is) {
- int fc = 0;
- int ic = 0;
- InputStream fs = null;
- try {
- fs = f.getContents(true);
- while ((fc = fs.read()) == (ic = is.read()) && fc >= 0);
- } catch (CoreException ce) {
- } catch (IOException ioe) {
- } finally {
- if (fs != null) {
- try {
- fs.close();
- } catch (IOException ioe) {
- }
- }
- }
- return fc <0 && ic < 0;
- }
-
protected final void doGenerateJavaClass(TextEmitter emitter, String qualifiedClassName, Object[] input) throws InterruptedException {
doGenerateJavaClass(emitter, CodeGenUtil.getPackageName(qualifiedClassName), CodeGenUtil.getSimpleClassName(qualifiedClassName), input);
}
@@ -355,9 +302,15 @@ public abstract class GeneratorBase implements Runnable {
String genText = emitter.generate(new SubProgressMonitor(pm, 1), input);
IPackageFragment pf = myDestRoot.createPackageFragment(packageName, true, new SubProgressMonitor(pm, 1));
ICompilationUnit cu = pf.getCompilationUnit(className + ".java"); //$NON-NLS-1$
- genText = mergeJavaCode(genText, cu, new SubProgressMonitor(pm, 1));
+ String oldContents = null;
+ if (cu.exists()) {
+ oldContents = cu.getSource();
+ genText = mergeJavaCode(oldContents, genText, new SubProgressMonitor(pm, 1));
+ } else {
+ pm.worked(1);
+ }
genText = formatCode(genText);
- if (!cu.exists() || !genText.equals(cu.getSource())) {
+ if (!genText.equals(oldContents)) {
pf.createCompilationUnit(cu.getElementName(), genText, true, new SubProgressMonitor(pm, 1));
} else {
pm.worked(1);
@@ -399,23 +352,40 @@ public abstract class GeneratorBase implements Runnable {
}
}
- protected final String mergeJavaCode(String generatedText, ICompilationUnit oldCU, IProgressMonitor pm) throws JavaModelException {
+ protected String mergeJavaCode(String oldContents, String generatedText, IProgressMonitor pm) throws JavaModelException {
pm.beginTask(GeneratorBaseMessages.merge, 1);
try {
- if (oldCU != null && oldCU.exists() && getJControlModel() != null) {
- JMerger jMerge = new JMerger(getJControlModel());
- jMerge.setSourceCompilationUnit(jMerge.createCompilationUnitForContents(generatedText));
- jMerge.setTargetCompilationUnit(jMerge.createCompilationUnitForContents(oldCU.getSource()));
- jMerge.merge();
- return jMerge.getTargetCompilationUnitContents();
- } else {
- return generatedText;
- }
+ return getMergeService().mergeJava(oldContents, generatedText);
} finally {
pm.done();
}
}
+ protected String mergePlainText(String oldText, String genText, IFile oldRes, IProgressMonitor pm) {
+ pm.beginTask(GeneratorBaseMessages.merge, 1);
+ try {
+ return getMergeService().process(oldRes.getFileExtension(), oldText, genText);
+ } finally {
+ pm.done();
+ }
+ }
+
+ private TextMerger getMergeService() {
+ if (myMerger == null) {
+ myMerger = createMergeService();
+ assert myMerger != null;
+ }
+ return myMerger;
+ }
+
+ /**
+ * By default, provides facility that doesn't perform any merge at all.
+ * @return facility to perform merges, should never return null.
+ */
+ protected TextMerger createMergeService() {
+ return new TextMerger();
+ }
+
protected void setProgressTaskName(String text) {
myProgress.subTask(text);
}
@@ -452,20 +422,6 @@ public abstract class GeneratorBase implements Runnable {
}
}
- private JControlModel getJControlModel() {
- if (myJControlModel == null) {
- URL controlFile = getJMergeControlFile();
- if (controlFile != null){
- myJControlModel = new JControlModel();
- myJControlModel.initialize(CodeGenUtil.instantiateFacadeHelper(JMerger.DEFAULT_FACADE_HELPER_CLASS), controlFile.toString());
- if (!myJControlModel.canMerge()){
- throw new IllegalStateException("Can not initialize JControlModel");
- }
- }
- }
- return myJControlModel;
- }
-
private CodeFormatter getCodeFormatter() {
if (myCodeFormatter == null) {
myCodeFormatter = ToolFactory.createCodeFormatter(null);
diff --git a/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/PropertiesTextMerger.java b/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/PropertiesTextMerger.java
deleted file mode 100644
index 3da391208..000000000
--- a/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/PropertiesTextMerger.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package org.eclipse.gmf.internal.common.codegen;
-
-import org.eclipse.emf.codegen.merge.properties.PropertyMerger;
-
-/**
- * @author dstadnik
- */
-public class PropertiesTextMerger extends TextMerger {
-
- public String process(String oldText, String newText) {
- PropertyMerger propertyMerger = new PropertyMerger();
- propertyMerger.setSourceProperties(newText);
- propertyMerger.setTargetProperties(oldText);
- propertyMerger.merge();
- return propertyMerger.getTargetProperties();
- }
-}
diff --git a/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/TaggedTextMerger.java b/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/TaggedTextMerger.java
index 0ae113ca4..b9cacb0db 100644
--- a/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/TaggedTextMerger.java
+++ b/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/TaggedTextMerger.java
@@ -6,7 +6,7 @@ import java.util.NoSuchElementException;
/**
* @author dstadnik
*/
-public class TaggedTextMerger extends TextMerger {
+public class TaggedTextMerger {
private String beginTag;
diff --git a/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/TextMerger.java b/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/TextMerger.java
index b70cd5189..4f784ad83 100644
--- a/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/TextMerger.java
+++ b/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/TextMerger.java
@@ -1,31 +1,40 @@
+/*
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Dmitri Stadnik (Borland) - initial API and implementation
+ */
package org.eclipse.gmf.internal.common.codegen;
/**
* @author dstadnik
*/
-public abstract class TextMerger {
-
- protected static final String TAG = "gmf generator persistent region"; //$NON-NLS-1$
-
- protected static final String BEGIN_TAG = TAG + " begin"; //$NON-NLS-1$
-
- protected static final String END_TAG = TAG + " end"; //$NON-NLS-1$
-
- // private static final TextMerger PRP = new TaggedTextMerger("# " + BEGIN_TAG, "# " + END_TAG); //$NON-NLS-1$ //$NON-NLS-2$
+public class TextMerger {
+ public String mergeJava(String oldText, String newText) {
+ return newText;
+ }
- private static final TextMerger PRP = new PropertiesTextMerger();
+ public String mergeProperties(String oldText, String newText) {
+ return newText;
+ }
- private static final TextMerger XML = new TaggedTextMerger("<!-- " + BEGIN_TAG + " -->", "<!-- " + END_TAG + " -->"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ public String mergeXML(String oldText, String newText) {
+ return newText;
+ }
- public static TextMerger getForFile(String name) {
- if (name.endsWith(".properties")) { //$NON-NLS-1$
- return PRP;
+ public String process(String fileNameExt, String oldText, String newText) {
+ if ("java".equals(fileNameExt)) {
+ return mergeJava(oldText, newText);
+ } else if ("xml".equals(fileNameExt)) {
+ return mergeXML(oldText, newText);
+ } else if ("properties".equals(fileNameExt)) {
+ return mergeProperties(oldText, newText);
}
- if (name.endsWith(".xml")) { //$NON-NLS-1$
- return XML;
- }
- return null;
+ return newText;
}
-
- public abstract String process(String oldText, String newText);
}
diff --git a/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/graphdef/codegen/StandaloneGenerator.java b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/graphdef/codegen/StandaloneGenerator.java
index 74fe79487..7ecaa2ad9 100644
--- a/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/graphdef/codegen/StandaloneGenerator.java
+++ b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/graphdef/codegen/StandaloneGenerator.java
@@ -11,17 +11,18 @@
*/
package org.eclipse.gmf.graphdef.codegen;
-import java.net.URL;
-
import org.eclipse.core.runtime.Path;
+import org.eclipse.emf.codegen.merge.java.JControlModel;
import org.eclipse.emf.codegen.util.CodeGenUtil;
import org.eclipse.gmf.common.UnexpectedBehaviourException;
import org.eclipse.gmf.common.codegen.ImportAssistant;
import org.eclipse.gmf.gmfgraph.Figure;
import org.eclipse.gmf.gmfgraph.util.FigureQualifiedNameSwitch;
+import org.eclipse.gmf.internal.common.codegen.DefaultTextMerger;
import org.eclipse.gmf.internal.common.codegen.DelegateImportManager;
import org.eclipse.gmf.internal.common.codegen.GeneratorBase;
import org.eclipse.gmf.internal.common.codegen.ImportUtil;
+import org.eclipse.gmf.internal.common.codegen.TextMerger;
public class StandaloneGenerator extends GeneratorBase {
private final Config myArgs;
@@ -145,8 +146,10 @@ public class StandaloneGenerator extends GeneratorBase {
//setupProgressMonitor("Generating GMFGraph plugin", 100);
}
- protected URL getJMergeControlFile() {
- return null;
+ @Override
+ protected TextMerger createMergeService() {
+ // jcontrol model that is not initialized can't merge, hence java merging won't happen
+ return new DefaultTextMerger(new JControlModel());
}
protected void customRun() throws InterruptedException, UnexpectedBehaviourException {
diff --git a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/AllTests.java b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/AllTests.java
index 3cb3f50e2..c061d4baa 100644
--- a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/AllTests.java
+++ b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/AllTests.java
@@ -19,7 +19,6 @@ import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
-import org.eclipse.gmf.internal.common.migrate.ModelLoadHelper;
import org.eclipse.gmf.runtime.emf.type.core.internal.EMFTypePlugin;
import org.eclipse.gmf.tests.gef.CompartmentPropertiesTest;
import org.eclipse.gmf.tests.gef.DiagramNodeTest;
@@ -58,6 +57,7 @@ import org.eclipse.gmf.tests.tr.HistoryTest;
import org.eclipse.gmf.tests.tr.LabelMappingTransformTest;
import org.eclipse.gmf.tests.tr.NamingStrategyTest;
import org.eclipse.gmf.tests.tr.PaletteTransformationTest;
+import org.eclipse.gmf.tests.tr.TestDefaultMergeService;
import org.eclipse.gmf.tests.tr.XmlTextMergerTest;
public class AllTests {
@@ -92,8 +92,8 @@ public class AllTests {
suite.addTest(feed(LabelMappingTransformTest.class, sessionSetup));
suite.addTest(feed(PaletteTransformationTest.class, sessionSetup));
suite.addTestSuite(HistoryTest.class);
- //suite.addTestSuite(PrpTextMergerTest.class); we use emf property merger
suite.addTestSuite(XmlTextMergerTest.class);
+ suite.addTestSuite(TestDefaultMergeService.class);
suite.addTest(feed(FigureCodegenTest.class, new FigureCodegenSetup()));
suite.addTest(feed(LabelSupportTest.class, new LabelSupportSetup()));
diff --git a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/PrpTextMergerTest.java b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/PrpTextMergerTest.java
deleted file mode 100644
index bcefdcd66..000000000
--- a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/PrpTextMergerTest.java
+++ /dev/null
@@ -1,51 +0,0 @@
-package org.eclipse.gmf.tests.tr;
-
-import org.eclipse.gmf.internal.common.codegen.TextMerger;
-
-/**
- * @author dstadnik
- */
-public class PrpTextMergerTest extends TextMergerTest {
-
- public PrpTextMergerTest(String name) {
- super(name);
- }
-
- protected TextMerger getTextMerger() {
- TextMerger merger = TextMerger.getForFile("plugin.properties");
- assertNotNull(merger);
- return merger;
- }
-
- protected String getOldHeader() {
- return "prop1=x\n";
- }
-
- protected String getOldPart1() {
- return "prop2=y\n";
- }
-
- protected String getOldTail() {
- return "prop3=z\n";
- }
-
- protected String getNewHeader() {
- return "propx=1\n";
- }
-
- protected String getNewPart1() {
- return "propy=2\n";
- }
-
- protected String getNewTail() {
- return "propz=3\n";
- }
-
- protected String getRegion1() {
- return "region1=aaa\n";
- }
-
- protected String getRegion2() {
- return "region21=bbb\nregion22=ccc\n";
- }
-}
diff --git a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/TextMergerTest.java b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/TaggedTextMergerTest.java
index 3c8f2be66..dc9752cb6 100644
--- a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/TextMergerTest.java
+++ b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/TaggedTextMergerTest.java
@@ -3,18 +3,17 @@ package org.eclipse.gmf.tests.tr;
import junit.framework.TestCase;
import org.eclipse.gmf.internal.common.codegen.TaggedTextMerger;
-import org.eclipse.gmf.internal.common.codegen.TextMerger;
/**
* @author dstadnik
*/
-public abstract class TextMergerTest extends TestCase {
+public abstract class TaggedTextMergerTest extends TestCase {
- public TextMergerTest(String name) {
+ public TaggedTextMergerTest(String name) {
super(name);
}
- protected abstract TextMerger getTextMerger();
+ protected abstract TaggedTextMerger getTextMerger();
protected abstract String getOldHeader();
@@ -33,7 +32,7 @@ public abstract class TextMergerTest extends TestCase {
protected abstract String getRegion2();
public void testOneRegionMerge() throws Exception {
- TaggedTextMerger merger = (TaggedTextMerger) getTextMerger();
+ TaggedTextMerger merger = getTextMerger();
String oldText = getOldHeader() + merger.getBeginTag() + getRegion1() + merger.getEndTag() + getOldTail();
String newText = getNewHeader() + merger.getBeginTag() + merger.getEndTag() + getNewTail();
String mergedText = merger.process(oldText, newText);
@@ -42,7 +41,7 @@ public abstract class TextMergerTest extends TestCase {
}
public void testTwoRegionsMerge() throws Exception {
- TaggedTextMerger merger = (TaggedTextMerger) getTextMerger();
+ TaggedTextMerger merger = getTextMerger();
String oldText = getOldHeader() + merger.getBeginTag() + getRegion1() + merger.getEndTag() + getOldPart1() + merger.getBeginTag() + getRegion2() + merger.getEndTag() + getOldTail();
String newText = getNewHeader() + getNewPart1() + merger.getBeginTag() + merger.getEndTag() + getNewTail();
String mergedText = merger.process(oldText, newText);
@@ -51,14 +50,14 @@ public abstract class TextMergerTest extends TestCase {
}
public void testIdentityMerge() throws Exception {
- TaggedTextMerger merger = (TaggedTextMerger) getTextMerger();
+ TaggedTextMerger merger = getTextMerger();
String newText = getNewHeader() + getNewPart1() + merger.getBeginTag() + merger.getEndTag() + getNewTail();
String mergedText = merger.process(newText, newText);
assertEquals(mergedText, newText);
}
public void testAnchorTextPreservation() throws Exception {
- TaggedTextMerger merger = (TaggedTextMerger) getTextMerger();
+ TaggedTextMerger merger = getTextMerger();
String oldText = getOldHeader() + getOldTail();
String newText = getNewHeader() + merger.getBeginTag() + "\n" + merger.getEndTag() + getNewTail();
String mergedText = merger.process(oldText, newText);
diff --git a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/TestDefaultMergeService.java b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/TestDefaultMergeService.java
new file mode 100644
index 000000000..a3c101c9e
--- /dev/null
+++ b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/TestDefaultMergeService.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Artem Tikhomirov (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.tests.tr;
+
+import java.io.ByteArrayInputStream;
+import java.util.Properties;
+
+import org.eclipse.emf.codegen.merge.java.JControlModel;
+import org.eclipse.gmf.internal.common.codegen.DefaultTextMerger;
+
+import junit.framework.TestCase;
+
+/**
+ * @author artem
+ */
+public class TestDefaultMergeService extends TestCase {
+
+ private DefaultTextMerger myMergeService;
+ private final String myBeginTagXML;
+ private final String myEndTagXML;
+
+ public TestDefaultMergeService(String name) {
+ super(name);
+ final String[] r = new String[2];
+ new DefaultTextMerger(new JControlModel()) {
+ {
+ r[0] = BEGIN_TAG;
+ r[1] = END_TAG;
+ }
+ };
+ // Copy from DefaultTextMerger cons
+ myBeginTagXML = "<!-- " + r[0] + " -->";
+ myEndTagXML = "<!-- " + r[1] + " -->";
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ myMergeService = new DefaultTextMerger(new JControlModel());
+ }
+
+ public void testXML() {
+ final String pieceToKeep = "<b>keep this</b>";
+ final String xmlOld = "<p>aaa" + myBeginTagXML + pieceToKeep + myEndTagXML + "<i>bbb</i>ccc</p>";
+ final String xmlNew = "<p>bbb" + myBeginTagXML + myEndTagXML + "<i>bbb</i><b>e</b>ccc</p>";
+ String result = myMergeService.mergeXML(xmlOld, xmlNew);
+ assertNotNull(result);
+ assertEquals("<p>bbb" + myBeginTagXML + pieceToKeep + myEndTagXML + "<i>bbb</i><b>e</b>ccc</p>", result);
+ }
+
+ public void testProperties() throws Exception {
+ final String oldContent = "prop1=value1\n" + "prop2=value2\n" + "#propRemoved=\n" + "propAdded=newValue\n" + "prop3=value3\n";
+ final String newContent = "prop1=value1\n" + "prop2=modifiedValue2\n" + "propRemoved=someValue\n" + "prop3=value3\n";
+ String merged = myMergeService.mergeProperties(oldContent, newContent);
+ assertNotNull(merged);
+ Properties result = new Properties();
+ result.load(new ByteArrayInputStream(merged.getBytes()));
+ assertEquals(4, result.size());
+ assertEquals("value1", result.getProperty("prop1"));
+ assertEquals("value2", result.getProperty("prop2"));
+ assertEquals("value3", result.getProperty("prop3"));
+ // check removed property not appear
+ assertNull(result.getProperty("propRemoved"));
+ // check added property kept
+ assertEquals("newValue", result.getProperty("propAdded"));
+ }
+
+ public void testJava() {
+ assertEquals("merger with uninitialized control model can't perform merge", getJavaNewText(), myMergeService.mergeJava(getJavaOldText(), getJavaNewText()));
+
+ }
+
+ public void testProcessJava() {
+ final String javaResult = myMergeService.process("java", getJavaOldText(), getJavaNewText());
+ assertEquals(myMergeService.mergeJava(getJavaOldText(), getJavaNewText()), javaResult);
+ }
+
+ public void testProcessXml() {
+ final String xmlOld = "<p>aaa<i>bbb</i>ccc</p>";
+ final String xmlNew = "<p>ddd<b>e</b>ccc</p>";
+ final String xmlResult = myMergeService.process("xml", xmlOld, xmlNew);
+ assertEquals(myMergeService.mergeXML(xmlOld, xmlNew), xmlResult);
+ }
+
+ public void testProcessProperties() {
+ final String propOld = "p1=v1\np2=v2\n#p3=\n";
+ final String propNew = "p1=v1\np2=nv\np3=v3\n";
+ final String propResult = myMergeService.process("properties", propOld, propNew);
+ assertEquals(myMergeService.mergeProperties(propOld, propNew), propResult);
+ }
+
+ private String getJavaOldText() {
+ return "class A {}";
+ }
+
+ private String getJavaNewText() {
+ return "class D {int c;}";
+ }
+}
diff --git a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/XmlTextMergerTest.java b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/XmlTextMergerTest.java
index 3cf193a31..650606e81 100644
--- a/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/XmlTextMergerTest.java
+++ b/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/tr/XmlTextMergerTest.java
@@ -1,20 +1,26 @@
package org.eclipse.gmf.tests.tr;
-import org.eclipse.gmf.internal.common.codegen.TextMerger;
+import org.eclipse.gmf.internal.common.codegen.TaggedTextMerger;
/**
* @author dstadnik
*/
-public class XmlTextMergerTest extends TextMergerTest {
+public class XmlTextMergerTest extends TaggedTextMergerTest {
+
+ private TaggedTextMerger myTaggedTextMerger;
public XmlTextMergerTest(String name) {
super(name);
}
- protected TextMerger getTextMerger() {
- TextMerger merger = TextMerger.getForFile("plugin.xml");
- assertNotNull(merger);
- return merger;
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ myTaggedTextMerger = new TaggedTextMerger("<!-- aaa -->", "<!-- bbb -->");
+ }
+
+ protected TaggedTextMerger getTextMerger() {
+ return myTaggedTextMerger;
}
protected String getOldHeader() {

Back to the top