KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > codegen > CodeGen


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: CodeGen.java,v 1.7 2005/06/08 06:15:57 nickb Exp $
16  */

17 package org.eclipse.emf.codegen;
18
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.PrintStream JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.List JavaDoc;
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 /**
49  * This class implements some reusable static utility methods.
50  * It also implements the method {@link #run},
51  * which is called just like main during headless workbench invocation.
52  */

53 public class CodeGen implements IPlatformRunnable
54 {
55   /**
56    * This is a progress monitor that prints the progress information to a stream.
57    * @deprecated As of EMF 2.1.0, moved to {@link CodeGenUtil.StreamProgressMonitor CodeGenUtil}.
58    */

59   public static class StreamProgressMonitor extends CodeGenUtil.StreamProgressMonitor
60   {
61     public StreamProgressMonitor(PrintStream JavaDoc printStream)
62     {
63       super(printStream);
64     }
65   }
66
67   /**
68    * @deprecated As of EMF 2.1.0, moved to {@link CodeGenUtil#findOrCreateContainer(IPath, boolean, IPath, IProgressMonitor) CodeGenUtil}.
69    */

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   /**
77    * @deprecated As of EMF 2.1.0, moved to {@link CodeGenUtil#findOrCreateContainer(IPath, boolean, IProjectDescription, IProgressMonitor) CodeGenUtil}.
78    */

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   /**
86    * @deprecated As of EMF 2.1.0, moved to {@link CodeGenUtil#getClasspathPaths CodeGenUtil}.
87    */

88   public static List JavaDoc getClasspathPaths(String JavaDoc pluginID) throws JETException
89   {
90     return CodeGenUtil.getClasspathPaths(pluginID);
91   }
92
93   /**
94    * @deprecated As of EMF 2.1.0, moved to {@link CodeGenUtil#addClasspathEntries(Collection, String, String) CodeGenUtil}.
95    */

96   public static void addClasspathEntries(Collection JavaDoc classpathEntries, String JavaDoc variableName, String JavaDoc pluginID) throws JETException
97   {
98     CodeGenUtil.addClasspathEntries(classpathEntries, variableName, pluginID);
99   }
100
101   /**
102    * @deprecated As of EMF 2.1.0, moved to {@link CodeGenUtil#addClasspathEntries(Collection, String) CodeGenUtil}.
103    */

104   public static void addClasspathEntries(Collection JavaDoc classpathEntries, String JavaDoc pluginID) throws Exception JavaDoc
105   {
106     CodeGenUtil.addClasspathEntries(classpathEntries, pluginID);
107   }
108
109   /**
110    * This is called with the command line arguments of a headless workbench invocation.
111    */

112   public Object JavaDoc run(Object JavaDoc object)
113   {
114     try
115     {
116       final String JavaDoc[] arguments = (String JavaDoc[])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 JavaDoc templateFile = arguments[0];
126               File JavaDoc file = new File JavaDoc(templateFile);
127               if (file.exists())
128               {
129                 templateFile = file.getAbsoluteFile().toURL().toString();
130               }
131               IPath targetPath = new Path(new File JavaDoc(arguments[1]).getAbsolutePath());
132               progressMonitor.beginTask("", 5);
133               progressMonitor.subTask
134                 (CodeGenPlugin.getPlugin().getString("_UI_CompilingTemplate_message", new Object JavaDoc [] { 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 JavaDoc [] { 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 JavaDoc [] { templateFile }));
147               ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc();
148               jetCompiler.generate(outputStream);
149               InputStream JavaDoc contents = new ByteArrayInputStream JavaDoc(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 JavaDoc [] { 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 JavaDoc mergedContents = new ByteArrayInputStream JavaDoc(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 JavaDoc exception)
184             {
185               System.err.println(CodeGenPlugin.getPlugin().getString("_UI_BadURL_message", new Object JavaDoc [] { 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 JavaDoc(0);
196     }
197     catch (Exception JavaDoc exception)
198     {
199       System.err.println(CodeGenPlugin.getPlugin().getString("_UI_UsageArguments_message"));
200       exception.printStackTrace();
201       return new Integer JavaDoc(1);
202     }
203   }
204 }
205
Popular Tags