OSDN Git Service

fixed up setup ui
[mikumikustudio/libgdx-mikumikustudio.git] / extensions / gdx-setup-ui / src / aurelienribon / gdxsetupui / Helper.java
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package aurelienribon.gdxsetupui;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import javax.xml.xpath.XPathConstants;
24
25 import org.apache.commons.io.FilenameUtils;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.NodeList;
28 import org.xml.sax.SAXException;
29
30 import aurelienribon.utils.XmlUtils;
31
32 /**
33  * @author Aurelien Ribon | http://www.aurelienribon.com/
34  */
35 public class Helper {
36         // -------------------------------------------------------------------------
37         // Project Configuration
38         // -------------------------------------------------------------------------
39
40         public static String getCorePrjName(BaseProjectConfiguration cfg) {
41                 return cfg.projectName + cfg.suffixCommon;
42         }
43
44         public static String getDesktopPrjName(BaseProjectConfiguration cfg) {
45                 return cfg.projectName + cfg.suffixDesktop;
46         }
47
48         public static String getAndroidPrjName(BaseProjectConfiguration cfg) {
49                 return cfg.projectName + cfg.suffixAndroid;
50         }
51
52         public static String getHtmlPrjName(BaseProjectConfiguration cfg) {
53                 return cfg.projectName + cfg.suffixHtml;
54         }
55
56         public static String getCorePrjPath(BaseProjectConfiguration cfg) {
57                 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixCommon + "/", true);
58         }
59
60         public static String getDesktopPrjPath(BaseProjectConfiguration cfg) {
61                 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixDesktop + "/", true);
62         }
63
64         public static String getAndroidPrjPath(BaseProjectConfiguration cfg) {
65                 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixAndroid + "/", true);
66         }
67
68         public static String getHtmlPrjPath(BaseProjectConfiguration cfg) {
69                 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixHtml + "/", true);
70         }
71         
72         public static String getRobovmPrjPath (ProjectUpdateConfiguration cfg) {
73                 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixRobovm + "/", true);
74         }
75
76         // -------------------------------------------------------------------------
77         // Classpath
78         // -------------------------------------------------------------------------
79
80         public static boolean isFileRuntimeJar(String filename) {
81                 if (!filename.endsWith(".jar")) return false;
82                 String name = FilenameUtils.getBaseName(filename);
83                 if (endsWith(name, "-source", "-sources", "-src")) return false;
84                 return true;
85         }
86
87         public static boolean endsWith(String str, String... ends) {
88                 for (String end : ends) if (str.endsWith(end)) return true;
89                 return false;
90         }
91
92         public static String getSource(List<String> files, String file) {
93                 String path = FilenameUtils.getFullPath(file);
94                 String name = FilenameUtils.getBaseName(file);
95                 String ext = FilenameUtils.getExtension(file);
96
97                 if (files.contains(path + name + "-source." + ext)) return path + name + "-source." + ext;
98                 if (files.contains(path + name + "-sources." + ext)) return path + name + "-sources." + ext;
99                 if (files.contains(path + name + "-src." + ext)) return path + name + "-src." + ext;
100                 return null;
101         }
102
103         public static List<ClasspathEntry> getCoreClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
104                 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
105
106                 for (String library : cfg.libraries) {
107                         LibraryDef def = libs.getDef(library);
108
109                         for (String file : def.libsCommon) {
110                                 if (!isFileRuntimeJar(file)) continue;
111                                 String source = getSource(def.libsCommon, file);
112                                 classpath.add(new ClasspathEntry("libs/", file, source, true));
113                         }
114                 }
115
116                 return classpath;
117         }
118
119         public static List<ClasspathEntry> getAndroidClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
120                 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
121                 if (!cfg.isAndroidIncluded) return classpath;
122
123                 String corePrjName = getCorePrjName(cfg);
124
125                 for (String library : cfg.libraries) {
126                         LibraryDef def = libs.getDef(library);
127
128                         for (String file : def.libsCommon) {
129                                 if (!isFileRuntimeJar(file)) continue;
130                                 String source = getSource(def.libsCommon, file);
131                                 classpath.add(new ClasspathEntry("/" + corePrjName + "/libs/", file, source, true));
132                         }
133
134                         for (String file : def.libsAndroid) {
135                                 if (!isFileRuntimeJar(file)) continue;
136                                 String source = getSource(def.libsAndroid, file);
137                                 classpath.add(new ClasspathEntry("libs/", file, source, true));
138                         }
139                 }
140
141                 return classpath;
142         }
143
144         public static List<ClasspathEntry> getDesktopClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
145                 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
146                 if (!cfg.isDesktopIncluded) return classpath;
147
148                 for (String library : cfg.libraries) {
149                         LibraryDef def = libs.getDef(library);
150
151                         for (String file : def.libsDesktop) {
152                                 if (!isFileRuntimeJar(file)) continue;
153                                 String source = getSource(def.libsDesktop, file);
154                                 classpath.add(new ClasspathEntry("libs/", file, source, false));
155                         }
156                 }
157
158                 return classpath;
159         }
160
161         public static List<ClasspathEntry> getHtmlClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
162                 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
163                 if (!cfg.isHtmlIncluded) return classpath;
164
165                 String corePrjName = getCorePrjName(cfg);
166
167                 for (String library : cfg.libraries) {
168                         LibraryDef def = libs.getDef(library);
169
170                         for (String file : def.libsCommon) {
171                                 if (!isFileRuntimeJar(file)) continue;
172                                 String source = getSource(def.libsCommon, file);
173                                 classpath.add(new ClasspathEntry("/" + corePrjName + "/libs/", file, source, false));
174                                 if (source != null) classpath.add(new ClasspathEntry("/" + corePrjName + "/libs/", source, null, false));
175                         }
176
177                         for (String file : def.libsHtml) {
178                                 if (!isFileRuntimeJar(file)) continue;
179                                 String source = getSource(def.libsHtml, file);
180                                 classpath.add(new ClasspathEntry("war/WEB-INF/lib/", file, source, false));
181                                 if (source != null) classpath.add(new ClasspathEntry("war/WEB-INF/lib/", source, null, false));
182                         }
183                 }
184
185                 return classpath;
186         }
187
188         public static List<GwtModule> getGwtModules(BaseProjectConfiguration cfg, LibraryManager libs) {
189                 List<GwtModule> gwtModules = new ArrayList<GwtModule>();
190                 if (!cfg.isHtmlIncluded) return gwtModules;
191
192                 for (String library : cfg.libraries) {
193                         LibraryDef def = libs.getDef(library);
194
195                         if (def.gwtModuleName != null) {
196                                 gwtModules.add(new GwtModule(def.gwtModuleName));
197                         }
198                 }
199
200                 return gwtModules;
201         }
202
203         public static List<ClasspathEntry> getClasspathEntries(File classpathFile) {
204                 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
205                 if (!classpathFile.isFile()) return classpath;
206
207                 try {
208                         Document doc = XmlUtils.createParser().parse(classpathFile);
209                         NodeList nodes = (NodeList) XmlUtils.xpath("classpath/classpathentry[@kind='lib' and @path]", doc, XPathConstants.NODESET);
210
211                         for (int i=0; i<nodes.getLength(); i++) {
212                                 String path = nodes.item(i).getAttributes().getNamedItem("path").getNodeValue();
213                                 String sourcepath = nodes.item(i).getAttributes().getNamedItem("sourcepath") != null
214                                         ? nodes.item(i).getAttributes().getNamedItem("sourcepath").getNodeValue() : null;
215                                 boolean exported = nodes.item(i).getAttributes().getNamedItem("exported") != null
216                                         ? nodes.item(i).getAttributes().getNamedItem("exported").getNodeValue().equalsIgnoreCase("true") : false;
217
218                                 classpath.add(new ClasspathEntry(path, sourcepath, exported));
219                         }
220
221                 } catch (SAXException ex) {
222                 } catch (IOException ex) {
223                 }
224
225                 return classpath;
226         }
227
228         public static List<GwtModule> getGwtModules(File modulesFile) {
229                 List<GwtModule> modules = new ArrayList<GwtModule>();
230                 if (!modulesFile.isFile()) return modules;
231
232                 try {
233                         Document doc = XmlUtils.createParser().parse(modulesFile);
234                         NodeList nodes = (NodeList) XmlUtils.xpath("module/inherits[@name]", doc, XPathConstants.NODESET);
235
236                         for (int i=0; i<nodes.getLength(); i++) {
237                                 String name = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
238                                 modules.add(new GwtModule(name));
239                         }
240
241                 } catch (SAXException ex) {
242                 } catch (IOException ex) {
243                 }
244
245                 return modules;
246         }
247
248         public static class ClasspathEntry implements Comparable<ClasspathEntry> {
249                 public String path;
250                 public String sourcepath;
251                 public boolean exported;
252                 public boolean overwritten = false;
253                 public boolean added = false;
254
255                 public ClasspathEntry(String path, String sourcepath, boolean exported) {
256                         this.path = path;
257                         this.sourcepath = sourcepath;
258                         this.exported = exported;
259                 }
260
261                 public ClasspathEntry(String preffix, String path, String sourcepath, boolean exported) {
262                         this.path = preffix + path;
263                         this.sourcepath = sourcepath != null ? preffix + sourcepath : null;
264                         this.exported = exported;
265                 }
266
267                 public void testOverwritten(List<ClasspathEntry> entries) {
268                         for (ClasspathEntry e : entries) if (e.path.equals(path)) {overwritten = true; return;}
269                 }
270
271                 public boolean testAdded(List<ClasspathEntry> entries) {
272                         for (ClasspathEntry e : entries) if (e.path.equals(path)) return false;
273                         added = true;
274                         return true;
275                 }
276
277                 @Override
278                 public int compareTo(ClasspathEntry o) {
279                         if (path.startsWith("/") && !o.path.startsWith("/")) return 1;
280                         if (!path.startsWith("/") && o.path.startsWith("/")) return -1;
281                         return path.compareTo(o.path);
282                 }
283         }
284
285         public static class GwtModule implements Comparable<GwtModule> {
286                 public String name;
287                 public boolean overwritten = false;
288                 public boolean added = false;
289
290                 public GwtModule(String name) {
291                         this.name = name;
292                 }
293
294                 public void testOverwritten(List<GwtModule> entries) {
295                         for (GwtModule e : entries) if (e.name.equals(name)) {overwritten = true; return;}
296                 }
297
298                 public boolean testAdded(List<GwtModule> entries) {
299                         for (GwtModule e : entries)
300                                 if (e.name.equals(name))
301                                         return false;
302                         added = true;
303                         return true;
304                 }
305
306                 @Override
307                 public int compareTo(GwtModule o) {
308                         return name.compareTo(o.name);
309                 }
310         }
311 }