Search in sources :

Example 1 with JMerger

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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) JMerger(org.eclipse.emf.codegen.jmerge.JMerger) IFile(org.eclipse.core.resources.IFile) JControlModel(org.eclipse.emf.codegen.jmerge.JControlModel) IContainer(org.eclipse.core.resources.IContainer) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) JETException(org.eclipse.emf.codegen.jet.JETException)

Aggregations

IContainer (org.eclipse.core.resources.IContainer)1 IFile (org.eclipse.core.resources.IFile)1 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)1 JETException (org.eclipse.emf.codegen.jet.JETException)1 JControlModel (org.eclipse.emf.codegen.jmerge.JControlModel)1 JMerger (org.eclipse.emf.codegen.jmerge.JMerger)1