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 // -------------------------------------------------------------------------
71 // -------------------------------------------------------------------------
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;
80 public static boolean endsWith(String str, String... ends) {
81 for (String end : ends) if (str.endsWith(end)) return true;
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);
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;
96 public static List<ClasspathEntry> getCoreClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
97 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
99 for (String library : cfg.libraries) {
100 LibraryDef def = libs.getDef(library);
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));
112 public static List<ClasspathEntry> getAndroidClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
113 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
114 if (!cfg.isAndroidIncluded) return classpath;
116 String corePrjName = getCorePrjName(cfg);
118 for (String library : cfg.libraries) {
119 LibraryDef def = libs.getDef(library);
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));
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));
137 public static List<ClasspathEntry> getDesktopClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
138 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
139 if (!cfg.isDesktopIncluded) return classpath;
141 for (String library : cfg.libraries) {
142 LibraryDef def = libs.getDef(library);
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));
154 public static List<ClasspathEntry> getHtmlClasspathEntries(BaseProjectConfiguration cfg, LibraryManager libs) {
155 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
156 if (!cfg.isHtmlIncluded) return classpath;
158 String corePrjName = getCorePrjName(cfg);
160 for (String library : cfg.libraries) {
161 LibraryDef def = libs.getDef(library);
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));
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));
181 public static List<GwtModule> getGwtModules(BaseProjectConfiguration cfg, LibraryManager libs) {
182 List<GwtModule> gwtModules = new ArrayList<GwtModule>();
183 if (!cfg.isHtmlIncluded) return gwtModules;
185 for (String library : cfg.libraries) {
186 LibraryDef def = libs.getDef(library);
188 if (def.gwtModuleName != null) {
189 gwtModules.add(new GwtModule(def.gwtModuleName));
196 public static List<ClasspathEntry> getClasspathEntries(File classpathFile) {
197 List<ClasspathEntry> classpath = new ArrayList<ClasspathEntry>();
198 if (!classpathFile.isFile()) return classpath;
201 Document doc = XmlUtils.createParser().parse(classpathFile);
202 NodeList nodes = (NodeList) XmlUtils.xpath("classpath/classpathentry[@kind='lib' and @path]", doc, XPathConstants.NODESET);
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;
211 classpath.add(new ClasspathEntry(path, sourcepath, exported));
214 } catch (SAXException ex) {
215 } catch (IOException ex) {
221 public static List<GwtModule> getGwtModules(File modulesFile) {
222 List<GwtModule> modules = new ArrayList<GwtModule>();
223 if (!modulesFile.isFile()) return modules;
226 Document doc = XmlUtils.createParser().parse(modulesFile);
227 NodeList nodes = (NodeList) XmlUtils.xpath("module/inherits[@name]", doc, XPathConstants.NODESET);
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));
234 } catch (SAXException ex) {
235 } catch (IOException ex) {
241 public static class ClasspathEntry implements Comparable<ClasspathEntry> {
243 public String sourcepath;
244 public boolean exported;
245 public boolean overwritten = false;
246 public boolean added = false;
248 public ClasspathEntry(String path, String sourcepath, boolean exported) {
250 this.sourcepath = sourcepath;
251 this.exported = exported;
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;
260 public void testOverwritten(List<ClasspathEntry> entries) {
261 for (ClasspathEntry e : entries) if (e.path.equals(path)) {overwritten = true; return;}
264 public boolean testAdded(List<ClasspathEntry> entries) {
265 for (ClasspathEntry e : entries) if (e.path.equals(path)) return false;
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);
278 public static class GwtModule implements Comparable<GwtModule> {
280 public boolean overwritten = false;
281 public boolean added = false;
283 public GwtModule(String name) {
287 public void testOverwritten(List<GwtModule> entries) {
288 for (GwtModule e : entries) if (e.name.equals(name)) {overwritten = true; return;}
291 public boolean testAdded(List<GwtModule> entries) {
292 for (GwtModule e : entries)
293 if (e.name.equals(name))
300 public int compareTo(GwtModule o) {
301 return name.compareTo(o.name);