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 public static String getRobovomPrjPath (ProjectUpdateConfiguration cfg) {
74 return FilenameUtils.normalize(cfg.destinationPath + "/" + cfg.projectName + cfg.suffixRobovm + "/", true);
77 // -------------------------------------------------------------------------
79 // -------------------------------------------------------------------------
81 public static boolean isFileRuntimeJar(String filename) {
82 if (!filename.endsWith(".jar")) return false;
83 String name = FilenameUtils.getBaseName(filename);
84 if (endsWith(name, "-source", "-sources", "-src")) return false;
88 public static boolean endsWith(String str, String... ends) {
89 for (String end : ends) if (str.endsWith(end)) return true;
93 public static String getSource(List<String> files, String file) {
94 String path = FilenameUtils.getFullPath(file);
95 String name = FilenameUtils.getBaseName(file);
96 String ext = FilenameUtils.getExtension(file);
98 if (files.contains(path + name + "-source." + ext)) return path + name + "-source." + ext;
99 if (files.contains(path + name + "-sources." + ext)) return path + name + "-sources." + ext;
100 if (files.contains(path + name + "-src." + ext)) return path + name + "-src." + ext;
104 public static List<ClasspathEntry> getCoreClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
105 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
107 for (String library : cfg.libraries) {
108 LibraryDef def = libs.getDef(library);
110 for (String file : def.libsCommon) {
111 if (!isFileRuntimeJar(file)) continue;
112 String source = getSource(def.libsCommon, file);
113 classpath.add(new ClasspathEntry("libs/", file, source, true));
120 public static List<ClasspathEntry> getAndroidClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
121 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
122 if (!cfg.isAndroidIncluded) return classpath;
124 String corePrjName = getCorePrjName(cfg);
126 for (String library : cfg.libraries) {
127 LibraryDef def = libs.getDef(library);
129 for (String file : def.libsCommon) {
130 if (!isFileRuntimeJar(file)) continue;
131 String source = getSource(def.libsCommon, file);
132 classpath.add(new ClasspathEntry("/" + corePrjName + "/libs/", file, source, true));
135 for (String file : def.libsAndroid) {
136 if (!isFileRuntimeJar(file)) continue;
137 String source = getSource(def.libsAndroid, file);
138 classpath.add(new ClasspathEntry("libs/", file, source, true));
145 public static List<ClasspathEntry> getDesktopClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
146 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
147 if (!cfg.isDesktopIncluded) return classpath;
149 for (String library : cfg.libraries) {
150 LibraryDef def = libs.getDef(library);
152 for (String file : def.libsDesktop) {
153 if (!isFileRuntimeJar(file)) continue;
154 String source = getSource(def.libsDesktop, file);
155 classpath.add(new ClasspathEntry("libs/", file, source, false));
162 public static List<ClasspathEntry> getHtmlClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
163 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
164 if (!cfg.isHtmlIncluded) return classpath;
166 String corePrjName = getCorePrjName(cfg);
168 for (String library : cfg.libraries) {
169 LibraryDef def = libs.getDef(library);
171 for (String file : def.libsCommon) {
172 if (!isFileRuntimeJar(file)) continue;
173 String source = getSource(def.libsCommon, file);
174 classpath.add(new ClasspathEntry("/" + corePrjName + "/libs/", file, source, false));
175 if (source != null) classpath.add(new ClasspathEntry("/" + corePrjName + "/libs/", source, null, false));
178 for (String file : def.libsHtml) {
179 if (!isFileRuntimeJar(file)) continue;
180 String source = getSource(def.libsHtml, file);
181 classpath.add(new ClasspathEntry("war/WEB-INF/lib/", file, source, false));
182 if (source != null) classpath.add(new ClasspathEntry("war/WEB-INF/lib/", source, null, false));
189 public static List<GwtModule> getGwtModules(BaseProjectConfiguration cfg, LibraryManager libs) {
190 List<GwtModule> gwtModules = new ArrayList<GwtModule>();
191 if (!cfg.isHtmlIncluded) return gwtModules;
193 for (String library : cfg.libraries) {
194 LibraryDef def = libs.getDef(library);
196 if (def.gwtModuleName != null) {
197 gwtModules.add(new GwtModule(def.gwtModuleName));
204 public static List<ClasspathEntry> getClasspathEntries(File classpathFile) {
205 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
206 if (!classpathFile.isFile()) return classpath;
209 Document doc = XmlUtils.createParser().parse(classpathFile);
210 NodeList nodes = (NodeList) XmlUtils.xpath("classpath/classpathentry[@kind='lib' and @path]", doc, XPathConstants.NODESET);
212 for (int i=0; i<nodes.getLength(); i++) {
213 String path = nodes.item(i).getAttributes().getNamedItem("path").getNodeValue();
214 String sourcepath = nodes.item(i).getAttributes().getNamedItem("sourcepath") != null
215 ? nodes.item(i).getAttributes().getNamedItem("sourcepath").getNodeValue() : null;
216 boolean exported = nodes.item(i).getAttributes().getNamedItem("exported") != null
217 ? nodes.item(i).getAttributes().getNamedItem("exported").getNodeValue().equalsIgnoreCase("true") : false;
219 classpath.add(new ClasspathEntry(path, sourcepath, exported));
222 } catch (SAXException ex) {
223 } catch (IOException ex) {
229 public static List<GwtModule> getGwtModules(File modulesFile) {
230 List<GwtModule> modules = new ArrayList<GwtModule>();
231 if (!modulesFile.isFile()) return modules;
234 Document doc = XmlUtils.createParser().parse(modulesFile);
235 NodeList nodes = (NodeList) XmlUtils.xpath("module/inherits[@name]", doc, XPathConstants.NODESET);
237 for (int i=0; i<nodes.getLength(); i++) {
238 String name = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
239 modules.add(new GwtModule(name));
242 } catch (SAXException ex) {
243 } catch (IOException ex) {
249 public static class ClasspathEntry implements Comparable<ClasspathEntry> {
251 public String sourcepath;
252 public boolean exported;
253 public boolean overwritten = false;
254 public boolean added = false;
256 public ClasspathEntry(String path, String sourcepath, boolean exported) {
258 this.sourcepath = sourcepath;
259 this.exported = exported;
262 public ClasspathEntry(String preffix, String path, String sourcepath, boolean exported) {
263 this.path = preffix + path;
264 this.sourcepath = sourcepath != null ? preffix + sourcepath : null;
265 this.exported = exported;
268 public void testOverwritten(List<ClasspathEntry> entries) {
269 for (ClasspathEntry e : entries) if (e.path.equals(path)) {overwritten = true; return;}
272 public boolean testAdded(List<ClasspathEntry> entries) {
273 for (ClasspathEntry e : entries) if (e.path.equals(path)) return false;
279 public int compareTo(ClasspathEntry o) {
280 if (path.startsWith("/") && !o.path.startsWith("/")) return 1;
281 if (!path.startsWith("/") && o.path.startsWith("/")) return -1;
282 return path.compareTo(o.path);
286 public static class GwtModule implements Comparable<GwtModule> {
288 public boolean overwritten = false;
289 public boolean added = false;
291 public GwtModule(String name) {
295 public void testOverwritten(List<GwtModule> entries) {
296 for (GwtModule e : entries) if (e.name.equals(name)) {overwritten = true; return;}
299 public boolean testAdded(List<GwtModule> entries) {
300 for (GwtModule e : entries)
301 if (e.name.equals(name))
308 public int compareTo(GwtModule o) {
309 return name.compareTo(o.name);