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;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
23 import javax.xml.xpath.XPathConstants;
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;
30 import aurelienribon.utils.XmlUtils;
33 * @author Aurelien Ribon | http://www.aurelienribon.com/
36 // -------------------------------------------------------------------------
37 // Project Configuration
38 // -------------------------------------------------------------------------
40 public static String getCorePrjName(BaseProjectConfiguration cfg) {
41 return cfg.projectName + cfg.suffixCommon;
44 public static String getDesktopPrjName(BaseProjectConfiguration cfg) {
45 return cfg.projectName + cfg.suffixDesktop;
48 public static String getAndroidPrjName(BaseProjectConfiguration cfg) {
49 return cfg.projectName + cfg.suffixAndroid;
52 public static String getHtmlPrjName(BaseProjectConfiguration cfg) {
53 return cfg.projectName + cfg.suffixHtml;
56 public static String getCorePrjPath(BaseProjectConfiguration cfg) {
57 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixCommon + "/", true);
60 public static String getDesktopPrjPath(BaseProjectConfiguration cfg) {
61 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixDesktop + "/", true);
64 public static String getAndroidPrjPath(BaseProjectConfiguration cfg) {
65 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixAndroid + "/", true);
68 public static String getHtmlPrjPath(BaseProjectConfiguration cfg) {
69 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixHtml + "/", true);
72 public static String getRobovmPrjPath (ProjectUpdateConfiguration cfg) {
73 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixRobovm + "/", true);
76 // -------------------------------------------------------------------------
78 // -------------------------------------------------------------------------
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;
87 public static boolean endsWith(String str, String... ends) {
88 for (String end : ends) if (str.endsWith(end)) return true;
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);
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;
103 public static List<ClasspathEntry> getCoreClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
104 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
106 for (String library : cfg.libraries) {
107 LibraryDef def = libs.getDef(library);
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));
119 public static List<ClasspathEntry> getAndroidClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
120 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
121 if (!cfg.isAndroidIncluded) return classpath;
123 String corePrjName = getCorePrjName(cfg);
125 for (String library : cfg.libraries) {
126 LibraryDef def = libs.getDef(library);
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));
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));
144 public static List<ClasspathEntry> getDesktopClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
145 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
146 if (!cfg.isDesktopIncluded) return classpath;
148 for (String library : cfg.libraries) {
149 LibraryDef def = libs.getDef(library);
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));
161 public static List<ClasspathEntry> getHtmlClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
162 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
163 if (!cfg.isHtmlIncluded) return classpath;
165 String corePrjName = getCorePrjName(cfg);
167 for (String library : cfg.libraries) {
168 LibraryDef def = libs.getDef(library);
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));
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));
188 public static List<GwtModule> getGwtModules(BaseProjectConfiguration cfg, LibraryManager libs) {
189 List<GwtModule> gwtModules = new ArrayList<GwtModule>();
190 if (!cfg.isHtmlIncluded) return gwtModules;
192 for (String library : cfg.libraries) {
193 LibraryDef def = libs.getDef(library);
195 if (def.gwtModuleName != null) {
196 gwtModules.add(new GwtModule(def.gwtModuleName));
203 public static List<ClasspathEntry> getClasspathEntries(File classpathFile) {
204 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
205 if (!classpathFile.isFile()) return classpath;
208 Document doc = XmlUtils.createParser().parse(classpathFile);
209 NodeList nodes = (NodeList) XmlUtils.xpath("classpath/classpathentry[@kind='lib' and @path]", doc, XPathConstants.NODESET);
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;
218 classpath.add(new ClasspathEntry(path, sourcepath, exported));
221 } catch (SAXException ex) {
222 } catch (IOException ex) {
228 public static List<GwtModule> getGwtModules(File modulesFile) {
229 List<GwtModule> modules = new ArrayList<GwtModule>();
230 if (!modulesFile.isFile()) return modules;
233 Document doc = XmlUtils.createParser().parse(modulesFile);
234 NodeList nodes = (NodeList) XmlUtils.xpath("module/inherits[@name]", doc, XPathConstants.NODESET);
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));
241 } catch (SAXException ex) {
242 } catch (IOException ex) {
248 public static class ClasspathEntry implements Comparable<ClasspathEntry> {
250 public String sourcepath;
251 public boolean exported;
252 public boolean overwritten = false;
253 public boolean added = false;
255 public ClasspathEntry(String path, String sourcepath, boolean exported) {
257 this.sourcepath = sourcepath;
258 this.exported = exported;
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;
267 public void testOverwritten(List<ClasspathEntry> entries) {
268 for (ClasspathEntry e : entries) if (e.path.equals(path)) {overwritten = true; return;}
271 public boolean testAdded(List<ClasspathEntry> entries) {
272 for (ClasspathEntry e : entries) if (e.path.equals(path)) return false;
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);
285 public static class GwtModule implements Comparable<GwtModule> {
287 public boolean overwritten = false;
288 public boolean added = false;
290 public GwtModule(String name) {
294 public void testOverwritten(List<GwtModule> entries) {
295 for (GwtModule e : entries) if (e.name.equals(name)) {overwritten = true; return;}
298 public boolean testAdded(List<GwtModule> entries) {
299 for (GwtModule e : entries)
300 if (e.name.equals(name))
307 public int compareTo(GwtModule o) {
308 return name.compareTo(o.name);