1 /*******************************************************************************
2 * Copyright 2011 See AUTHORS file.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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;
18 import aurelienribon.utils.XmlUtils;
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;
30 * @author Aurelien Ribon | http://www.aurelienribon.com/
33 // -------------------------------------------------------------------------
34 // Project Configuration
35 // -------------------------------------------------------------------------
37 public static String getCorePrjName(BaseProjectConfiguration cfg) {
38 return cfg.projectName + cfg.suffixCommon;
41 public static String getDesktopPrjName(BaseProjectConfiguration cfg) {
42 return cfg.projectName + cfg.suffixDesktop;
45 public static String getAndroidPrjName(BaseProjectConfiguration cfg) {
46 return cfg.projectName + cfg.suffixAndroid;
49 public static String getHtmlPrjName(BaseProjectConfiguration cfg) {
50 return cfg.projectName + cfg.suffixHtml;
53 public static String getCorePrjPath(BaseProjectConfiguration cfg) {
54 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixCommon + "/", true);
57 public static String getDesktopPrjPath(BaseProjectConfiguration cfg) {
58 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixDesktop + "/", true);
61 public static String getAndroidPrjPath(BaseProjectConfiguration cfg) {
62 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixAndroid + "/", true);
65 public static String getHtmlPrjPath(BaseProjectConfiguration cfg) {
66 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixHtml + "/", true);
69 public static String getIosPrjPath (ProjectUpdateConfiguration cfg) {
70 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixIos + "/", true);
73 // -------------------------------------------------------------------------
75 // -------------------------------------------------------------------------
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;
84 public static boolean endsWith(String str, String... ends) {
85 for (String end : ends) if (str.endsWith(end)) return true;
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);
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;
100 public static List<ClasspathEntry> getCoreClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
101 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
103 for (String library : cfg.libraries) {
104 LibraryDef def = libs.getDef(library);
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));
116 public static List<ClasspathEntry> getAndroidClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
117 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
118 if (!cfg.isAndroidIncluded) return classpath;
120 String corePrjName = getCorePrjName(cfg);
122 for (String library : cfg.libraries) {
123 LibraryDef def = libs.getDef(library);
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));
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));
141 public static List<ClasspathEntry> getDesktopClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
142 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
143 if (!cfg.isDesktopIncluded) return classpath;
145 for (String library : cfg.libraries) {
146 LibraryDef def = libs.getDef(library);
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));
158 public static List<ClasspathEntry> getHtmlClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
159 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
160 if (!cfg.isHtmlIncluded) return classpath;
162 String corePrjName = getCorePrjName(cfg);
164 for (String library : cfg.libraries) {
165 LibraryDef def = libs.getDef(library);
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));
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));
185 public static List<GwtModule> getGwtModules(BaseProjectConfiguration cfg, LibraryManager libs) {
186 List<GwtModule> gwtModules = new ArrayList<GwtModule>();
187 if (!cfg.isHtmlIncluded) return gwtModules;
189 for (String library : cfg.libraries) {
190 LibraryDef def = libs.getDef(library);
192 if (def.gwtModuleName != null) {
193 gwtModules.add(new GwtModule(def.gwtModuleName));
200 public static List<ClasspathEntry> getClasspathEntries(File classpathFile) {
201 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
202 if (!classpathFile.isFile()) return classpath;
205 Document doc = XmlUtils.createParser().parse(classpathFile);
206 NodeList nodes = (NodeList) XmlUtils.xpath("classpath/classpathentry[@kind='lib' and @path]", doc, XPathConstants.NODESET);
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;
215 classpath.add(new ClasspathEntry(path, sourcepath, exported));
218 } catch (SAXException ex) {
219 } catch (IOException ex) {
225 public static List<GwtModule> getGwtModules(File modulesFile) {
226 List<GwtModule> modules = new ArrayList<GwtModule>();
227 if (!modulesFile.isFile()) return modules;
230 Document doc = XmlUtils.createParser().parse(modulesFile);
231 NodeList nodes = (NodeList) XmlUtils.xpath("module/inherits[@name]", doc, XPathConstants.NODESET);
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));
238 } catch (SAXException ex) {
239 } catch (IOException ex) {
245 public static class ClasspathEntry implements Comparable<ClasspathEntry> {
247 public String sourcepath;
248 public boolean exported;
249 public boolean overwritten = false;
250 public boolean added = false;
252 public ClasspathEntry(String path, String sourcepath, boolean exported) {
254 this.sourcepath = sourcepath;
255 this.exported = exported;
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;
264 public void testOverwritten(List<ClasspathEntry> entries) {
265 for (ClasspathEntry e : entries) if (e.path.equals(path)) {overwritten = true; return;}
268 public boolean testAdded(List<ClasspathEntry> entries) {
269 for (ClasspathEntry e : entries) if (e.path.equals(path)) return false;
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);
282 public static class GwtModule implements Comparable<GwtModule> {
284 public boolean overwritten = false;
285 public boolean added = false;
287 public GwtModule(String name) {
291 public void testOverwritten(List<GwtModule> entries) {
292 for (GwtModule e : entries) if (e.name.equals(name)) {overwritten = true; return;}
295 public boolean testAdded(List<GwtModule> entries) {
296 for (GwtModule e : entries)
297 if (e.name.equals(name))
304 public int compareTo(GwtModule o) {
305 return name.compareTo(o.name);