OSDN Git Service

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