KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > tools > ProjectCreator


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.tools;
17
18 import com.google.gwt.user.tools.util.ArgHandlerEclipse;
19 import com.google.gwt.user.tools.util.ArgHandlerIgnore;
20 import com.google.gwt.user.tools.util.ArgHandlerOverwrite;
21 import com.google.gwt.util.tools.ArgHandlerOutDir;
22 import com.google.gwt.util.tools.ArgHandlerString;
23 import com.google.gwt.util.tools.ToolBase;
24 import com.google.gwt.util.tools.Utility;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * Creates a new GWT project.
33  */

34 public final class ProjectCreator extends ToolBase {
35
36   private static final String JavaDoc PACKAGE_PATH;
37
38   static {
39     String JavaDoc path = ProjectCreator.class.getName();
40     path = path.substring(0, path.lastIndexOf('.') + 1);
41     PACKAGE_PATH = path.replace('.', '/');
42   }
43
44   public static void main(String JavaDoc[] args) {
45     ProjectCreator creator = new ProjectCreator();
46     if (creator.processArgs(args)) {
47       if (creator.run()) {
48         return;
49       }
50     }
51
52     System.exit(1);
53   }
54
55   /**
56    * @param eclipse The name of project to create.
57    * @param ant The name of an ant file to create.
58    * @param outDir The directory to write into.
59    * @param overwrite Overwrite an existing files if they exist.
60    * @param ignore Ignore existing files if they exist.
61    * @throws IOException
62    */

63   static void createProject(String JavaDoc eclipse, String JavaDoc ant, File JavaDoc outDir,
64       boolean overwrite, boolean ignore) throws IOException JavaDoc {
65
66     // Figure out the installation directory
67
String JavaDoc installPath = Utility.getInstallPath();
68
69     // Create a map of replacements.
70
//
71
Map JavaDoc replacements = new HashMap JavaDoc();
72     replacements.put("@gwtUserPath", installPath + '/' + "gwt-user.jar");
73
74     Utility.getDirectory(outDir, "src", true);
75     Utility.getDirectory(outDir, "test", true);
76
77     if (ant != null) {
78       // Create an ant build file
79
replacements.put("@projectName", ant);
80       File JavaDoc antXML = Utility.createNormalFile(outDir, ant + ".ant.xml",
81           overwrite, ignore);
82       if (antXML != null) {
83         String JavaDoc out = Utility.getFileFromClassPath(PACKAGE_PATH
84             + "project.ant.xmlsrc");
85         Utility.writeTemplateFile(antXML, out, replacements);
86       }
87     }
88
89     if (eclipse != null) {
90       // Create an eclipse project file
91
replacements.put("@projectName", eclipse);
92       File JavaDoc dotProject = Utility.createNormalFile(outDir, ".project", overwrite,
93           ignore);
94       if (dotProject != null) {
95         String JavaDoc out = Utility.getFileFromClassPath(PACKAGE_PATH + ".projectsrc");
96         Utility.writeTemplateFile(dotProject, out, replacements);
97       }
98
99       // Create an eclipse classpath file
100
File JavaDoc dotClasspath = Utility.createNormalFile(outDir, ".classpath",
101           overwrite, ignore);
102       if (dotClasspath != null) {
103         String JavaDoc out = Utility.getFileFromClassPath(PACKAGE_PATH
104             + ".classpathsrc");
105         Utility.writeTemplateFile(dotClasspath, out, replacements);
106       }
107     }
108   }
109
110   private String JavaDoc ant = null;
111
112   private String JavaDoc eclipse = null;
113
114   private boolean ignore = false;
115   private File JavaDoc outDir = null;
116   private boolean overwrite = false;
117
118   protected ProjectCreator() {
119
120     registerHandler(new ArgHandlerString() {
121
122       public String JavaDoc getPurpose() {
123         return "Generate an Ant buildfile to compile source (.ant.xml will be appended)";
124       }
125
126       public String JavaDoc getTag() {
127         return "-ant";
128       }
129
130       public String JavaDoc[] getTagArgs() {
131         return new String JavaDoc[] {"projectName"};
132       }
133
134       public boolean setString(String JavaDoc str) {
135         ant = str;
136         return true;
137       }
138
139     });
140
141     registerHandler(new ArgHandlerEclipse() {
142       public String JavaDoc getPurpose() {
143         return "Generate an eclipse project";
144       }
145
146       public boolean setString(String JavaDoc str) {
147         eclipse = str;
148         return true;
149       }
150     });
151
152     registerHandler(new ArgHandlerOutDir() {
153       public void setDir(File JavaDoc dir) {
154         outDir = dir;
155       }
156     });
157
158     registerHandler(new ArgHandlerOverwrite() {
159       public boolean setFlag() {
160         if (ignore) {
161           System.err.println("-overwrite cannot be used with -ignore.");
162           return false;
163         }
164         overwrite = true;
165         return true;
166       }
167     });
168
169     registerHandler(new ArgHandlerIgnore() {
170       public boolean setFlag() {
171         if (overwrite) {
172           System.err.println("-ignore cannot be used with -overwrite.");
173           return false;
174         }
175         ignore = true;
176         return true;
177       }
178     });
179   }
180
181   protected boolean run() {
182     try {
183       if (ant == null && eclipse == null) {
184         System.err.println("Please specify either -ant or -eclipse.");
185         printHelp();
186         return false;
187       }
188       createProject(eclipse, ant, outDir, overwrite, ignore);
189       return true;
190     } catch (IOException JavaDoc e) {
191       System.err.println(e.getClass().getName() + ": " + e.getMessage());
192       return false;
193     }
194   }
195 }
196
Popular Tags