Search in sources :

Example 11 with JETException

use of org.eclipse.emf.codegen.jet.JETException in project mdw-designer by CenturyLinkCloud.

the class JetAccess method append.

/**
 * Appends jet-generated content to the end of the JetAccess save file.
 *
 * @param monitor
 * @param contents
 * @return
 * @throws IOException
 * @throws CoreException
 */
public IFile append(IProgressMonitor monitor, byte[] contents) throws IOException, 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()));
    IFile result = getWritableTargetFile(targetFile, container, config.getTargetFile());
    InputStream newContents = new ByteArrayInputStream(contents);
    if (result.exists()) {
        newContents = appendFile(result, new String(contents));
        result.setContents(newContents, true, true, new SubProgressMonitor(monitor, 2));
    } else {
        result.create(newContents, true, new SubProgressMonitor(monitor, 1));
    }
    return result;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IContainer(org.eclipse.core.resources.IContainer) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) JETException(org.eclipse.emf.codegen.jet.JETException)

Example 12 with JETException

use of org.eclipse.emf.codegen.jet.JETException 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)

Example 13 with JETException

use of org.eclipse.emf.codegen.jet.JETException in project mdw-designer by CenturyLinkCloud.

the class JavaSource method getDefaultContent.

@Override
public String getDefaultContent() {
    try {
        // initialize from template
        String templateUri = Platform.getBundle(MdwPlugin.getPluginId()).getEntry("/").toString() + "/templates/source/JavaSource.javajet";
        JETEmitter emitter = new JETEmitter(templateUri, getClass().getClassLoader());
        emitter.addVariable(null, MdwPlugin.getPluginId());
        Map<String, Object> map = new HashMap<String, Object>();
        JavaCode javaCode = new JavaCode(getJavaPackageName(), getJavaClassName());
        map.put("model", javaCode);
        map.put("settings", MdwPlugin.getSettings());
        return emitter.generate(new NullProgressMonitor(), new Object[] { map });
    } catch (JETException ex) {
        PluginMessages.uiError(ex, "Generate Java", getProject());
        return null;
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) HashMap(java.util.HashMap) JavaCode(com.centurylink.mdw.plugin.codegen.meta.JavaCode) JETEmitter(org.eclipse.emf.codegen.jet.JETEmitter) JETException(org.eclipse.emf.codegen.jet.JETException)

Aggregations

JETException (org.eclipse.emf.codegen.jet.JETException)13 CoreException (org.eclipse.core.runtime.CoreException)5 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)5 IContainer (org.eclipse.core.resources.IContainer)4 IFile (org.eclipse.core.resources.IFile)4 IPath (org.eclipse.core.runtime.IPath)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 Path (org.eclipse.core.runtime.Path)4 JetBean (org.talend.designer.codegen.config.JetBean)4 CodeGeneratorException (org.talend.designer.codegen.exception.CodeGeneratorException)4 JetProxy (org.talend.designer.codegen.proxy.JetProxy)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 JETEmitter (org.eclipse.emf.codegen.jet.JETEmitter)3 CodeGeneratorArgument (org.talend.designer.codegen.config.CodeGeneratorArgument)3 IComponentFileNaming (org.talend.core.model.components.IComponentFileNaming)2 LightJetBean (org.talend.designer.codegen.config.LightJetBean)2 TalendJetEmitter (org.talend.designer.codegen.config.TalendJetEmitter)2 JavaCode (com.centurylink.mdw.plugin.codegen.meta.JavaCode)1