1 4 package org.ashkelon; 5 6 import java.io.*; 7 import org.exolab.castor.xml.MarshalException; 8 import org.dom4j.*; 9 import org.dom4j.io.*; 10 import java.util.*; 11 import org.apache.tools.ant.*; 12 import org.apache.tools.ant.types.*; 13 14 17 public class MavenPOMAdapter 18 { 19 20 public static API read(File file, String sourcepath) throws MarshalException 21 { 22 32 try 33 { 34 SAXReader reader = new SAXReader(); 35 Document doc = reader.read(file); 36 API api = new API(); 37 api.setName(xpath(doc, "//project/name")); 38 api.setSummaryDescription(xpath(doc, "//project/shortDescription")); 39 api.setDescription(xpath(doc, "//project/description")); 40 api.setPublisher(xpath(doc, "//project/organization/name")); 41 api.setDownloadURL(xpath(doc, "//project/url")); 42 api.setVersion(xpath(doc, "//project/currentVersion")); 43 44 String year = xpath(doc, "//project/inceptionYear"); 45 Calendar cal = Calendar.getInstance(); 46 cal.set(Calendar.YEAR, Integer.parseInt(year)); 47 api.setReleaseDate(cal.getTime()); 48 49 String rootPkgName = xpath(doc, "//project/package"); 51 String [] packageNames = getPackageNamesFromSourcePath(rootPkgName, sourcepath); 52 for (int i=0; i<packageNames.length; i++) 53 { 54 api.addPackagename(packageNames[i]); 55 } 56 57 return api; 58 } 59 catch (Exception ex) 60 { 61 ex.printStackTrace(); 62 throw new MarshalException("Failed to unmarshal xml file"); 63 } 64 } 65 66 private static String xpath(Document doc, String expression) 67 { 68 Element element = (Element) doc.selectSingleNode(expression); 69 return element.getTextTrim(); 70 } 71 72 public static String [] 73 getPackageNamesFromSourcePath(String rootPackageName, String sourcepath) 74 { 75 Set packages = new HashSet(); 76 Path path = new Path(new Project(), sourcepath); 77 String [] pathParts = path.list(); 78 for (int i=0; i<pathParts.length; i++) 79 { 80 String [] packagenames = getPackageNames(rootPackageName, pathParts[i]); 81 for (int j=0; j<packagenames.length; j++) 82 { 83 packages.add(packagenames[j]); 84 } 85 } 86 return (String []) packages.toArray(new String [packages.size()]); 87 } 88 89 public static String [] getPackageNames(String rootPackageName, String baseDir) 90 { 91 String input = rootPackageName.replace('.', '/') + "/**"; 92 DirectoryScanner scanner = new DirectoryScanner(); 93 scanner.setIncludes(new String [] {input}); 94 95 scanner.setBasedir(baseDir); 96 scanner.scan(); 97 98 String [] dirs = scanner.getIncludedDirectories(); 99 List returnList = new ArrayList(dirs.length); 100 for (int i=0; i<dirs.length; i++) 101 { 102 if (dirs[i].endsWith("/CVS")) continue; 103 returnList.add(dirs[i].replace('/', '.')); 104 } 105 return (String []) returnList.toArray(new String [returnList.size()]); 106 } 107 } 108 | Popular Tags |