1 package org.eclipse.emf.examples.jet.article2.codegen; 2 3 4 import java.io.ByteArrayInputStream ; 5 import java.io.InputStream ; 6 7 import org.eclipse.core.resources.IContainer; 8 import org.eclipse.core.resources.IFile; 9 import org.eclipse.core.runtime.CoreException; 10 import org.eclipse.core.runtime.IPath; 11 import org.eclipse.core.runtime.IProgressMonitor; 12 import org.eclipse.core.runtime.NullProgressMonitor; 13 import org.eclipse.core.runtime.Path; 14 import org.eclipse.core.runtime.SubProgressMonitor; 15 16 import org.eclipse.emf.codegen.CodeGen; 17 import org.eclipse.emf.codegen.jet.JETEmitter; 18 import org.eclipse.emf.codegen.jet.JETException; 19 import org.eclipse.emf.codegen.jmerge.JControlModel; 20 import org.eclipse.emf.codegen.jmerge.JMerger; 21 22 23 29 public class JETGateway 30 { 31 32 private Config mConfig = null; 33 34 public JETGateway(Config config) 35 { 36 mConfig = config; 37 } 38 39 59 public String generate(IProgressMonitor monitor) throws CoreException 60 { 61 monitor = createIfNull(monitor); 62 63 Config config = getConfig(); 64 JETEmitter emitter = new JETEmitter(config.getTemplateFullUri(), getClass().getClassLoader()); 65 emitter.addVariable(config.getClasspathVariable(), config.getPluginId()); 66 67 IProgressMonitor sub = new SubProgressMonitor(monitor, 1); 68 String result = emitter.generate(sub, new Object []{ config.getModel() }); 69 monitor.worked(1); 70 return result; 71 } 72 73 91 public String merge(IProgressMonitor monitor, String emitterResult) throws CoreException 92 { 93 monitor = createIfNull(monitor); 94 95 Config config = getConfig(); 96 IContainer container = findOrCreateContainer(monitor, config.getTargetFolder(), config.getPackageName()); 97 if (container == null) 98 { 99 throw new JETException("Cound not find or create container for package " + config.getPackageName() + " in " 100 + config.getTargetFolder()); 101 } 102 103 IFile targetFile = container.getFile(new Path(config.getTargetFile())); 104 if (!targetFile.exists()) 105 { 106 monitor.worked(1); 107 return emitterResult; 108 } 109 110 JMerger jMerger = new JMerger(); 111 jMerger.setControlModel(new JControlModel(config.getMergeXmlFullUri())); 112 jMerger.setSourceCompilationUnit(jMerger.createCompilationUnitForContents(emitterResult)); 113 114 jMerger.setTargetCompilationUnit(jMerger.createCompilationUnitForInputStream(targetFile.getContents(true))); 115 String oldContents = jMerger.getTargetCompilationUnit().getContents(); 116 117 jMerger.merge(); 118 monitor.worked(1); 119 120 String result = jMerger.getTargetCompilationUnit().getContents(); 121 if (oldContents.equals(result)) 122 { 123 return result; 124 } 125 126 if (!targetFile.isReadOnly()) 127 { 128 return result; 129 } 130 131 if (targetFile.getWorkspace().validateEdit(new IFile []{ targetFile }, new SubProgressMonitor(monitor, 1)).isOK()) 134 { 135 136 jMerger.setTargetCompilationUnit(jMerger.createCompilationUnitForInputStream(targetFile.getContents(true))); 137 jMerger.remerge(); 138 return jMerger.getTargetCompilationUnit().getContents(); 139 } 140 return result; 141 } 142 143 156 public IFile save(IProgressMonitor monitor, byte[] contents) throws CoreException 157 { 158 monitor = createIfNull(monitor); 159 160 Config config = getConfig(); 161 IContainer container = findOrCreateContainer(monitor, config.getTargetFolder(), config.getPackageName()); 162 if (container == null) 163 { 164 throw new JETException("Cound not find or create container for package " + config.getPackageName() + " in " 165 + config.getTargetFolder()); 166 } 167 IFile targetFile = container.getFile(new Path(config.getTargetFile())); 168 IFile result = getWritableTargetFile(targetFile, container, config.getTargetFile()); 169 170 InputStream newContents = new ByteArrayInputStream (contents); 171 if (result.exists()) 172 { 173 result.setContents(newContents, true, true, new SubProgressMonitor(monitor, 1)); 174 } 175 else 176 { 177 result.create(newContents, true, new SubProgressMonitor(monitor, 1)); 178 } 179 return result; 180 } 181 182 190 private IProgressMonitor createIfNull(IProgressMonitor monitor) 191 { 192 if (monitor == null) 193 { 194 return new NullProgressMonitor(); 195 } 196 return monitor; 197 } 198 199 private IContainer findOrCreateContainer(IProgressMonitor progressMonitor, String targetDirectory, String packageName) throws CoreException 200 { 201 202 IPath outputPath = new Path(targetDirectory + "/" + packageName.replace('.', '/')); 203 progressMonitor.beginTask("", 4); 204 205 IProgressMonitor sub = new SubProgressMonitor(progressMonitor, 1); 206 IPath localLocation = null; IContainer container = CodeGen.findOrCreateContainer(outputPath, true, localLocation, sub); 208 return container; 209 } 210 211 228 private IFile getWritableTargetFile(IFile targetFile, IContainer container, String fileName) 229 { 230 if (targetFile.isReadOnly()) 231 { 232 if (getConfig().isForceOverwrite()) 233 { 234 targetFile.setReadOnly(false); 235 } 236 else 237 { 238 targetFile = container.getFile(new Path("." + fileName + ".new")); 239 } 240 } 241 return targetFile; 242 } 243 244 249 protected Config getConfig() 250 { 251 return mConfig; 252 } 253 } | Popular Tags |