View Javadoc

1   /*
2   Karma core - Core of the Karma application
3   Copyright (C) 2004  Toolforge <www.toolforge.nl>
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19  package nl.toolforge.karma.core.scm.maven;
20  
21  import nl.toolforge.karma.core.KarmaException;
22  import org.apache.commons.digester.Digester;
23  import org.xml.sax.SAXException;
24  
25  import java.io.File;
26  import java.io.FileInputStream;
27  import java.io.FileNotFoundException;
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.util.List;
31  
32  
33  /***
34   * This class is a lightweight reader for Maven project.xml's. Using the Maven stuff itself sucks, as it
35   * is very heavy stuff for the purpose of reading the project's dependencies only.
36   *
37   * @author D.A. Smedes
38   */
39  public class MavenDependencyReader {
40  
41    // todo name the correct maven dependency
42    //
43    /***
44     * Parses a Maven project.xml file and stores all <code>&lt;dependency&gt;</code>-elements in a <code>List</code>.
45     *
46     * @param dependencyFileIs
47     * @return A <code>List</code>, containing <code>org.apache.maven.project.Dependency</code> instances.
48     */
49   public List parse(InputStream dependencyFileIs) throws KarmaException {
50  
51      Digester digester = new Digester();
52  
53      digester.addObjectCreate("project", "java.template.ArrayList");
54      digester.addObjectCreate("project/dependencies/dependency", "org.apache.maven.project.Dependency");
55  
56      digester.addCallMethod("project/dependencies/dependency/Id", "setId", 1);
57      digester.addCallParam("project/dependencies/dependency/Id", 0);
58      digester.addCallMethod("project/dependencies/dependency/groupId", "setGroupId", 1);
59      digester.addCallParam("project/dependencies/dependency/groupId", 0);
60      digester.addCallMethod("project/dependencies/dependency/artifactId", "setArtifactId", 1);
61      digester.addCallParam("project/dependencies/dependency/artifactId", 0);
62      digester.addCallMethod("project/dependencies/dependency/version", "setVersion", 1);
63      digester.addCallParam("project/dependencies/dependency/version", 0);
64      digester.addCallMethod("project/dependencies/dependency/jar", "setJar", 1);
65      digester.addCallParam("project/dependencies/dependency/jar", 0);
66  
67      // Call List.add(Dependency)
68      //
69      digester.addSetNext("project/dependencies/dependency", "add", "org.apache.maven.project.Dependency");
70  
71      List deps = null;
72      try {
73        deps = (List) digester.parse(dependencyFileIs);
74      } catch (IOException e) {
75        e.printStackTrace();
76  //      throw new KarmaException(KarmaException.LAZY_BASTARD);
77      } catch (SAXException e) {
78        e.printStackTrace();
79  //      throw new KarmaException(KarmaException.LAZY_BASTARD);
80      }
81  
82      return deps;
83    }
84  
85    public List parse(File projectXmlFile) throws KarmaException {
86      try {
87        return parse(new FileInputStream(projectXmlFile));
88      } catch (FileNotFoundException e) {
89        throw new KarmaException(KarmaException.NO_MAVEN_PROJECT_XML);
90      }
91    }
92  }
93