KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > MavenPOMAdapter


1 /*
2  * Created on Aug 16, 2004
3  */

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 /**
15  * @author Eitan Suez
16  */

17 public class MavenPOMAdapter
18 {
19    
20    public static API read(File file, String JavaDoc sourcepath) throws MarshalException
21    {
22       // pseudocode: read xml file
23
// here's the mapping: (use dom4j, do it manually, easy enough)
24
// name: project/name
25
// summaryDescription: project/shortDescription
26
// description: project/description
27
// publisher: project/organization/name
28
// downloadUrl: project/url
29
// version: project/currentVersion
30
// releaseDate (approximated as): project/inceptionYear
31

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 JavaDoc 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          // now need to resolve and add the package names
50
String JavaDoc rootPkgName = xpath(doc, "//project/package");
51          String JavaDoc[] 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 JavaDoc ex)
60       {
61          ex.printStackTrace();
62          throw new MarshalException("Failed to unmarshal xml file");
63       }
64    }
65    
66    private static String JavaDoc xpath(Document doc, String JavaDoc expression)
67    {
68       Element element = (Element) doc.selectSingleNode(expression);
69       return element.getTextTrim();
70    }
71    
72    public static String JavaDoc[]
73             getPackageNamesFromSourcePath(String JavaDoc rootPackageName, String JavaDoc sourcepath)
74    {
75       Set packages = new HashSet();
76       Path path = new Path(new Project(), sourcepath);
77       String JavaDoc[] pathParts = path.list();
78       for (int i=0; i<pathParts.length; i++)
79       {
80          String JavaDoc[] 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 JavaDoc[]) packages.toArray(new String JavaDoc[packages.size()]);
87    }
88
89    public static String JavaDoc[] getPackageNames(String JavaDoc rootPackageName, String JavaDoc baseDir)
90    {
91       String JavaDoc input = rootPackageName.replace('.', '/') + "/**";
92       DirectoryScanner scanner = new DirectoryScanner();
93       scanner.setIncludes(new String JavaDoc[] {input});
94       
95       scanner.setBasedir(baseDir);
96       scanner.scan();
97       
98       String JavaDoc[] 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 JavaDoc[]) returnList.toArray(new String JavaDoc[returnList.size()]);
106    }
107 }
108
Popular Tags