use of org.eclipse.emf.codegen.jmerge.JMerger in project mdw-designer by CenturyLinkCloud.
the class JetAccess method merge.
/**
* Merges the specified emitterResult with the contents of an existing file
* (existing file is not modified). The location of the file to merge with
* is determined by finding or creating the container (folder) for the
* Config's package in the Config's target folder. The name of the file to
* merge with is the Config's target file.
*
* @param monitor
* the progress monitor to use (may be null)
* @param emitterResult
* generated content to merge with the existing content
* @return result of merging the generated contents with the existing file
* @throws CoreException
* if an error occurs accessing the contents of the file
*/
public String merge(IProgressMonitor monitor, String emitterResult) throws CoreException, JETException {
monitor = createIfNull(monitor);
JetConfig config = getConfig();
IContainer container = findOrCreateContainer(monitor, config.getTargetFolder(), config.getPackageName());
if (container == null) {
throw new JETException("Cound not find or create container for package " + config.getPackageName() + " in " + config.getTargetFolder());
}
IFile targetFile = container.getFile(new Path(config.getTargetFile()));
if (!targetFile.exists()) {
monitor.worked(1);
return emitterResult;
}
JMerger jMerger = new JMerger();
jMerger.setControlModel(new JControlModel(config.getMergeXmlFullUri()));
jMerger.setSourceCompilationUnit(jMerger.createCompilationUnitForContents(emitterResult));
jMerger.setTargetCompilationUnit(jMerger.createCompilationUnitForInputStream(targetFile.getContents(true)));
String oldContents = jMerger.getTargetCompilationUnit().getContents();
jMerger.merge();
monitor.worked(1);
String result = jMerger.getTargetCompilationUnit().getContents();
if (oldContents.equals(result)) {
return result;
}
if (!targetFile.isReadOnly()) {
return result;
}
// by a VCM component (here we ask permission to change the file)
if (targetFile.getWorkspace().validateEdit(new IFile[] { targetFile }, new SubProgressMonitor(monitor, 1)).isOK()) {
jMerger.setTargetCompilationUnit(jMerger.createCompilationUnitForInputStream(targetFile.getContents(true)));
jMerger.remerge();
return jMerger.getTargetCompilationUnit().getContents();
}
return result;
}
Aggregations