1 17 package org.eclipse.emf.codegen; 18 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.File ; 23 import java.io.InputStream ; 24 import java.io.PrintStream ; 25 import java.util.Collection ; 26 import java.util.List ; 27 28 import org.eclipse.core.resources.IContainer; 29 import org.eclipse.core.resources.IFile; 30 import org.eclipse.core.resources.IProjectDescription; 31 import org.eclipse.core.resources.IWorkspace; 32 import org.eclipse.core.resources.IWorkspaceRunnable; 33 import org.eclipse.core.resources.ResourcesPlugin; 34 import org.eclipse.core.runtime.CoreException; 35 import org.eclipse.core.runtime.IPath; 36 import org.eclipse.core.runtime.IPlatformRunnable; 37 import org.eclipse.core.runtime.IProgressMonitor; 38 import org.eclipse.core.runtime.Path; 39 import org.eclipse.core.runtime.SubProgressMonitor; 40 41 import org.eclipse.emf.codegen.jet.JETCompiler; 42 import org.eclipse.emf.codegen.jet.JETException; 43 import org.eclipse.emf.codegen.jmerge.JControlModel; 44 import org.eclipse.emf.codegen.jmerge.JMerger; 45 import org.eclipse.emf.codegen.util.CodeGenUtil; 46 47 48 53 public class CodeGen implements IPlatformRunnable 54 { 55 59 public static class StreamProgressMonitor extends CodeGenUtil.StreamProgressMonitor 60 { 61 public StreamProgressMonitor(PrintStream printStream) 62 { 63 super(printStream); 64 } 65 } 66 67 70 public static IContainer findOrCreateContainer 71 (IPath path, boolean forceRefresh, IPath localLocation, IProgressMonitor progressMonitor) throws CoreException 72 { 73 return CodeGenUtil.findOrCreateContainer(path, forceRefresh, localLocation, progressMonitor); 74 } 75 76 79 public static IContainer findOrCreateContainer 80 (IPath path, boolean forceRefresh, IProjectDescription projectDescription, IProgressMonitor progressMonitor) throws CoreException 81 { 82 return CodeGenUtil.findOrCreateContainer(path, forceRefresh, projectDescription, progressMonitor); 83 } 84 85 88 public static List getClasspathPaths(String pluginID) throws JETException 89 { 90 return CodeGenUtil.getClasspathPaths(pluginID); 91 } 92 93 96 public static void addClasspathEntries(Collection classpathEntries, String variableName, String pluginID) throws JETException 97 { 98 CodeGenUtil.addClasspathEntries(classpathEntries, variableName, pluginID); 99 } 100 101 104 public static void addClasspathEntries(Collection classpathEntries, String pluginID) throws Exception 105 { 106 CodeGenUtil.addClasspathEntries(classpathEntries, pluginID); 107 } 108 109 112 public Object run(Object object) 113 { 114 try 115 { 116 final String [] arguments = (String [])object; 117 final IWorkspace workspace = ResourcesPlugin.getWorkspace(); 118 IWorkspaceRunnable runnable = 119 new IWorkspaceRunnable() 120 { 121 public void run(IProgressMonitor progressMonitor) throws CoreException 122 { 123 try 124 { 125 String templateFile = arguments[0]; 126 File file = new File (templateFile); 127 if (file.exists()) 128 { 129 templateFile = file.getAbsoluteFile().toURL().toString(); 130 } 131 IPath targetPath = new Path(new File (arguments[1]).getAbsolutePath()); 132 progressMonitor.beginTask("", 5); 133 progressMonitor.subTask 134 (CodeGenPlugin.getPlugin().getString("_UI_CompilingTemplate_message", new Object [] { templateFile })); 135 136 JControlModel jControlModel = arguments.length > 2 ? new JControlModel(arguments[2]) : null; 137 progressMonitor.worked(1); 138 139 progressMonitor.subTask 140 (CodeGenPlugin.getPlugin().getString("_UI_ParsingTemplate_message", new Object [] { templateFile })); 141 JETCompiler jetCompiler = new JETCompiler(templateFile); 142 jetCompiler.parse(); 143 progressMonitor.worked(1); 144 145 progressMonitor.subTask 146 (CodeGenPlugin.getPlugin().getString("_UI_GeneratingJava_message", new Object [] { templateFile })); 147 ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); 148 jetCompiler.generate(outputStream); 149 InputStream contents = new ByteArrayInputStream (outputStream.toByteArray()); 150 progressMonitor.worked(1); 151 152 IPath projectTargetPath = new Path("/Result/" + jetCompiler.getSkeleton().getPackageName().replace('.','/')); 153 154 IContainer container = 155 CodeGenUtil.findOrCreateContainer(projectTargetPath, true, targetPath, new SubProgressMonitor(progressMonitor, 1)); 156 IFile targetFile = container.getFile(new Path(jetCompiler.getSkeleton().getClassName() + ".java")); 157 158 progressMonitor.subTask 159 (CodeGenPlugin.getPlugin().getString("_UI_Updating_message", new Object [] { targetFile.getLocation() })); 160 if (targetFile.exists()) 161 { 162 if (jControlModel != null) 163 { 164 JMerger jMerger = new JMerger(); 165 jMerger.setControlModel(jControlModel); 166 jMerger.setSourceCompilationUnit(jMerger.createCompilationUnitForContents(outputStream.toString())); 167 jMerger.setTargetCompilationUnit(jMerger.createCompilationUnitForURI(targetPath.toString())); 168 jMerger.merge(); 169 170 InputStream mergedContents = new ByteArrayInputStream (jMerger.getTargetCompilationUnitContents().getBytes()); 171 targetFile.setContents(mergedContents, true, true, new SubProgressMonitor(progressMonitor, 1)); 172 } 173 else 174 { 175 targetFile.setContents(contents, true, true, new SubProgressMonitor(progressMonitor, 1)); 176 } 177 } 178 else 179 { 180 targetFile.create(contents, true, new SubProgressMonitor(progressMonitor, 1)); 181 } 182 } 183 catch (java.net.MalformedURLException exception) 184 { 185 System.err.println(CodeGenPlugin.getPlugin().getString("_UI_BadURL_message", new Object [] { arguments[0] })); 186 exception.printStackTrace(); 187 } 188 finally 189 { 190 progressMonitor.done(); 191 } 192 } 193 }; 194 workspace.run(runnable, new CodeGenUtil.StreamProgressMonitor(System.out)); 195 return new Integer (0); 196 } 197 catch (Exception exception) 198 { 199 System.err.println(CodeGenPlugin.getPlugin().getString("_UI_UsageArguments_message")); 200 exception.printStackTrace(); 201 return new Integer (1); 202 } 203 } 204 } 205 | Popular Tags |