OSDN Git Service

Merge branch 'mapsapi' of github.com:libgdx/libgdx into mapsapi
[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         public static String getIosPrjPath (ProjectUpdateConfiguration cfg) {
70                 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixIos + "/", true);
71         }
72
73         // -------------------------------------------------------------------------
74         // Classpath
75         // -------------------------------------------------------------------------
76
77         public static boolean isFileRuntimeJar(String filename) {
78                 if (!filename.endsWith(".jar")) return false;
79                 String name = FilenameUtils.getBaseName(filename);
80                 if (endsWith(name, "-source", "-sources", "-src")) return false;
81                 return true;
82         }
83
84         public static boolean endsWith(String str, String... ends) {
85                 for (String end : ends) if (str.endsWith(end)) return true;
86                 return false;
87         }
88
89         public static String getSource(List<String> files, String file) {
90                 String path = FilenameUtils.getFullPath(file);
91                 String name = FilenameUtils.getBaseName(file);
92                 String ext = FilenameUtils.getExtension(file);
93
94                 if (files.contains(path + name + "-source." + ext)) return path + name + "-source." + ext;
95                 if (files.contains(path + name + "-sources." + ext)) return path + name + "-sources." + ext;
96                 if (files.contains(path + name + "-src." + ext)) return path + name + "-src." + ext;
97                 return null;
98         }
99
100         public static List<ClasspathEntry> getCoreClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
101                 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
102
103                 for (String library : cfg.libraries) {
104                         LibraryDef def = libs.getDef(library);
105
106                         for (String file : def.libsCommon) {
107                                 if (!isFileRuntimeJar(file)) continue;
108                                 String source = getSource(def.libsCommon, file);
109                                 classpath.add(new ClasspathEntry("libs/", file, source, true));
110                         }
111                 }
112
113                 return classpath;
114         }
115
116         public static List<ClasspathEntry> getAndroidClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
117                 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
118                 if (!cfg.isAndroidIncluded) return classpath;
119
120                 String corePrjName = getCorePrjName(cfg);
121
122                 for (String library : cfg.libraries) {
123                         LibraryDef def = libs.getDef(library);
124
125                         for (String file : def.libsCommon) {
126                                 if (!isFileRuntimeJar(file)) continue;
127                                 String source = getSource(def.libsCommon, file);
128                                 classpath.add(new ClasspathEntry("/" + corePrjName + "/libs/", file, source, true));
129                         }
130
131                         for (String file : def.libsAndroid) {
132                                 if (!isFileRuntimeJar(file)) continue;
133                                 String source = getSource(def.libsAndroid, file);
134                                 classpath.add(new ClasspathEntry("libs/", file, source, true));
135                         }
136                 }
137
138                 return classpath;
139         }
140
141         public static List<ClasspathEntry> getDesktopClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
142                 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
143                 if (!cfg.isDesktopIncluded) return classpath;
144
145                 for (String library : cfg.libraries) {
146                         LibraryDef def = libs.getDef(library);
147
148                         for (String file : def.libsDesktop) {
149                                 if (!isFileRuntimeJar(file)) continue;
150                                 String source = getSource(def.libsDesktop, file);
151                                 classpath.add(new ClasspathEntry("libs/", file, source, false));
152                         }
153                 }
154
155                 return classpath;
156         }
157
158         public static List<ClasspathEntry> getHtmlClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
159                 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
160                 if (!cfg.isHtmlIncluded) return classpath;
161
162                 String corePrjName = getCorePrjName(cfg);
163
164                 for (String library : cfg.libraries) {
165                         LibraryDef def = libs.getDef(library);
166
167                         for (String file : def.libsCommon) {
168                                 if (!isFileRuntimeJar(file)) continue;
169                                 String source = getSource(def.libsCommon, file);
170                                 classpath.add(new ClasspathEntry("/" + corePrjName + "/libs/", file, source, false));
171                                 if (source != null) classpath.add(new ClasspathEntry("/" + corePrjName + "/libs/", source, null, false));
172                         }
173
174                         for (String file : def.libsHtml) {
175                                 if (!isFileRuntimeJar(file)) continue;
176                                 String source = getSource(def.libsHtml, file);
177                                 classpath.add(new ClasspathEntry("war/WEB-INF/lib/", file, source, false));
178                                 if (source != null) classpath.add(new ClasspathEntry("war/WEB-INF/lib/", source, null, false));
179                         }
180                 }
181
182                 return classpath;
183         }
184
185         public static List<GwtModule> getGwtModules(BaseProjectConfiguration cfg, LibraryManager libs) {
186                 List<GwtModule> gwtModules = new ArrayList<GwtModule>();
187                 if (!cfg.isHtmlIncluded) return gwtModules;
188
189                 for (String library : cfg.libraries) {
190                         LibraryDef def = libs.getDef(library);
191
192                         if (def.gwtModuleName != null) {
193                                 gwtModules.add(new GwtModule(def.gwtModuleName));
194                         }
195                 }
196
197                 return gwtModules;
198         }
199
200         public static List<ClasspathEntry> getClasspathEntries(File classpathFile) {
201                 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
202                 if (!classpathFile.isFile()) return classpath;
203
204                 try {
205                         Document doc = XmlUtils.createParser().parse(classpathFile);
206                         NodeList nodes = (NodeList) XmlUtils.xpath("classpath/classpathentry[@kind='lib' and @path]", doc, XPathConstants.NODESET);
207
208                         for (int i=0; i<nodes.getLength(); i++) {
209                                 String path = nodes.item(i).getAttributes().getNamedItem("path").getNodeValue();
210                                 String sourcepath = nodes.item(i).getAttributes().getNamedItem("sourcepath") != null
211                                         ? nodes.item(i).getAttributes().getNamedItem("sourcepath").getNodeValue() : null;
212                                 boolean exported = nodes.item(i).getAttributes().getNamedItem("exported") != null
213                                         ? nodes.item(i).getAttributes().getNamedItem("exported").getNodeValue().equalsIgnoreCase("true") : false;
214
215                                 classpath.add(new ClasspathEntry(path, sourcepath, exported));
216                         }
217
218                 } catch (SAXException ex) {
219                 } catch (IOException ex) {
220                 }
221
222                 return classpath;
223         }
224
225         public static List<GwtModule> getGwtModules(File modulesFile) {
226                 List<GwtModule> modules = new ArrayList<GwtModule>();
227                 if (!modulesFile.isFile()) return modules;
228
229                 try {
230                         Document doc = XmlUtils.createParser().parse(modulesFile);
231                         NodeList nodes = (NodeList) XmlUtils.xpath("module/inherits[@name]", doc, XPathConstants.NODESET);
232
233                         for (int i=0; i<nodes.getLength(); i++) {
234                                 String name = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
235                                 modules.add(new GwtModule(name));
236                         }
237
238                 } catch (SAXException ex) {
239                 } catch (IOException ex) {
240                 }
241
242                 return modules;
243         }
244
245         public static class ClasspathEntry implements Comparable<ClasspathEntry> {
246                 public String path;
247                 public String sourcepath;
248                 public boolean exported;
249                 public boolean overwritten = false;
250                 public boolean added = false;
251
252                 public ClasspathEntry(String path, String sourcepath, boolean exported) {
253                         this.path = path;
254                         this.sourcepath = sourcepath;
255                         this.exported = exported;
256                 }
257
258                 public ClasspathEntry(String preffix, String path, String sourcepath, boolean exported) {
259                         this.path = preffix + path;
260                         this.sourcepath = sourcepath != null ? preffix + sourcepath : null;
261                         this.exported = exported;
262                 }
263
264                 public void testOverwritten(List<ClasspathEntry> entries) {
265                         for (ClasspathEntry e : entries) if (e.path.equals(path)) {overwritten = true; return;}
266                 }
267
268                 public boolean testAdded(List<ClasspathEntry> entries) {
269                         for (ClasspathEntry e : entries) if (e.path.equals(path)) return false;
270                         added = true;
271                         return true;
272                 }
273
274                 @Override
275                 public int compareTo(ClasspathEntry o) {
276                         if (path.startsWith("/") && !o.path.startsWith("/")) return 1;
277                         if (!path.startsWith("/") && o.path.startsWith("/")) return -1;
278                         return path.compareTo(o.path);
279                 }
280         }
281
282         public static class GwtModule implements Comparable<GwtModule> {
283                 public String name;
284                 public boolean overwritten = false;
285                 public boolean added = false;
286
287                 public GwtModule(String name) {
288                         this.name = name;
289                 }
290
291                 public void testOverwritten(List<GwtModule> entries) {
292                         for (GwtModule e : entries) if (e.name.equals(name)) {overwritten = true; return;}
293                 }
294
295                 public boolean testAdded(List<GwtModule> entries) {
296                         for (GwtModule e : entries)
297                                 if (e.name.equals(name))
298                                         return false;
299                         added = true;
300                         return true;
301                 }
302
303                 @Override
304                 public int compareTo(GwtModule o) {
305                         return name.compareTo(o.name);
306                 }
307         }
308 }