Title: [115] trunk/maven2-plugin: applied patch vfrom XDOCLET-59

Diff

Modified: trunk/maven2-plugin/pom.xml (114 => 115)

--- trunk/maven2-plugin/pom.xml	2008-08-04 14:02:39 UTC (rev 114)
+++ trunk/maven2-plugin/pom.xml	2008-09-20 13:43:11 UTC (rev 115)
@@ -27,7 +27,7 @@
             </roles>
         </developer>
         <developer>
-            <name>Gregory Josef</name>
+            <name>Gregory Joseph</name>
             <roles>
                 <role>Developer</role>
             </roles>
@@ -76,6 +76,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.maven.shared</groupId>
+            <artifactId>maven-common-artifact-filters</artifactId>
+            <version>1.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.maven.shared</groupId>
             <artifactId>maven-plugin-testing-harness</artifactId>
             <version>1.0-beta-1</version>
             <scope>test</scope>

Modified: trunk/maven2-plugin/src/main/java/org/codehaus/xdoclet/Maven2SourceProvider.java (114 => 115)

--- trunk/maven2-plugin/src/main/java/org/codehaus/xdoclet/Maven2SourceProvider.java	2008-08-04 14:02:39 UTC (rev 114)
+++ trunk/maven2-plugin/src/main/java/org/codehaus/xdoclet/Maven2SourceProvider.java	2008-09-20 13:43:11 UTC (rev 115)
@@ -1,34 +1,43 @@
 package org.codehaus.xdoclet;
 
 import org.codehaus.plexus.util.DirectoryScanner;
+import org.codehaus.plexus.util.SelectorUtils;
 import org.xdoclet.JavaSourceProvider;
 
 import java.io.File;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
+import java.net.URL;
+import java.util.*;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 
 /**
  * @author Espen Amble Kolstad
  * @author gjoseph
- *
  * @version $Revision$
  */
 public class Maven2SourceProvider implements JavaSourceProvider {
     private static final String[] EMPTY_STRING_ARRAY = new String[0];
 
     private final String encoding;
+    /**
+     * A list of String paths relative to the basedir.
+     */
     private final List compileSourceRoots;
+    /**
+     * A List of URLs also containing sources to be parsed.
+     */
+    private final List sourceJars;
+
     private final String[] includes;
     private final String[] excludes;
 
-    public Maven2SourceProvider(Config config, List compileSourceRoots) {
+    public Maven2SourceProvider(Config config, List compileSourceRoots, List sourceJars) {
         this.encoding = config.getEncoding();
         this.compileSourceRoots = compileSourceRoots;
-        this.includes = toStringArray(config.getIncludes());
-        this.excludes = toStringArray(config.getExcludes());
+        this.sourceJars = sourceJars;
+        this.includes = Util.toTrimmedStringArray(config.getIncludes());
+        this.excludes = Util.toTrimmedStringArray(config.getExcludes());
     }
 
     public Collection getURLs() throws IOException {
@@ -49,17 +58,53 @@
                 urls.add(file.toURL());
             }
         }
+        // open all jars and add each and every matching entry
+        final Iterator itSrcJars = sourceJars.iterator();
+        while (itSrcJars.hasNext()) {
+            final File file = (File) itSrcJars.next();
+            final URL fileUrl = file.toURI().toURL();
+            final String urlPrefix = "jar:" + fileUrl.toExternalForm() + "!/";
+            final JarFile jarFile = new JarFile(file);
+            final Enumeration entries = jarFile.entries();
+            while (entries.hasMoreElements()) {
+                JarEntry entry = (JarEntry) entries.nextElement();
+                final String entryName = entry.getName();
+                if (isIncluded(entryName) && !isExcluded(entryName)) {
+                    final URL url = "" URL(urlPrefix + entryName);
+                    urls.add(url);
+                }
+            }
+        }
         return urls;
     }
 
-    private String[] toStringArray(String commaSeparated) {
-        if (commaSeparated == null || commaSeparated.length() == 0) {
-            return EMPTY_STRING_ARRAY;
+    public String getEncoding() {
+        return encoding;
+    }
+
+
+    private final static boolean caseSensitive = true;
+
+    // copied from plexus-util DirectoryScanner:
+    protected boolean isIncluded(String name) {
+        for (int i = 0; i < includes.length; i++) {
+            if (matchPath(includes[i], name, caseSensitive)) {
+                return true;
+            }
         }
-        return commaSeparated.split(",", 0);
+        return false;
     }
 
-    public String getEncoding() {
-        return encoding;
+    protected boolean isExcluded(String name) {
+        for (int i = 0; i < excludes.length; i++) {
+            if (matchPath(excludes[i], name, caseSensitive)) {
+                return true;
+            }
+        }
+        return false;
     }
+
+    protected static boolean matchPath(String pattern, String str, boolean isCaseSensitive) {
+        return SelectorUtils.matchPath(pattern, str, isCaseSensitive);
+    }
 }
\ No newline at end of file

Added: trunk/maven2-plugin/src/main/java/org/codehaus/xdoclet/Util.java (0 => 115)

--- trunk/maven2-plugin/src/main/java/org/codehaus/xdoclet/Util.java	                        (rev 0)
+++ trunk/maven2-plugin/src/main/java/org/codehaus/xdoclet/Util.java	2008-09-20 13:43:11 UTC (rev 115)
@@ -0,0 +1,32 @@
+package org.codehaus.xdoclet;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @author gjoseph
+ * @version $Revision: $ ($Author: $)
+ */
+public class Util {
+
+    static String[] toTrimmedStringArray(String commaSeparated) {
+        final List list = toTrimmedList(commaSeparated);
+        return (String[]) list.toArray(new String[list.size()]);
+    }
+
+    static List toTrimmedList(String commaSeparated) {
+        if (commaSeparated == null || commaSeparated.length() == 0) {
+            return Collections.EMPTY_LIST;
+        }
+        final List list = new ArrayList();
+        final String[] strings = commaSeparated.split(",", 0);
+        for (int i = 0; i < strings.length; i++) {
+            final String string = strings[i].trim();
+            if (string.length() > 0) {
+                list.add(string);
+            }
+        }
+        return list;
+    }
+}

Modified: trunk/maven2-plugin/src/main/java/org/codehaus/xdoclet/XDocletMojo.java (114 => 115)

--- trunk/maven2-plugin/src/main/java/org/codehaus/xdoclet/XDocletMojo.java	2008-08-04 14:02:39 UTC (rev 114)
+++ trunk/maven2-plugin/src/main/java/org/codehaus/xdoclet/XDocletMojo.java	2008-09-20 13:43:11 UTC (rev 115)
@@ -1,10 +1,18 @@
 package org.codehaus.xdoclet;
 
 import org.apache.maven.model.Resource;
+import org.apache.maven.model.Dependency;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.artifact.resolver.ArtifactResolutionException;
+import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
 import org.generama.JellyTemplateEngine;
 import org.generama.VelocityTemplateEngine;
 import org.generama.defaults.FileWriterMapper;
@@ -32,6 +40,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.ArrayList;
 
 /**
  * @author Espen Amble Kolstad
@@ -45,16 +54,18 @@
 public class XDocletMojo extends AbstractMojo {
 	private static class PluginContainerComposer implements ContainerComposer {
 		private final List compileSourceRoots;
+		private final List dependenciesSourcesURLs;
 
 		private final Config config;
 
 		private final Map defaultPluginProps;
 
 		public PluginContainerComposer(Config config, Map defaultPluginProps,
-				List compileSourceRoots) {
+				List compileSourceRoots, List dependenciesSourcesURLs) {
 			this.config = config;
 			this.defaultPluginProps = defaultPluginProps;
 			this.compileSourceRoots = compileSourceRoots;
+			this.dependenciesSourcesURLs = dependenciesSourcesURLs;
 		}
 
 		public void composeContainer(MutablePicoContainer pico,
@@ -68,7 +79,7 @@
 			pico.registerComponentImplementation(VelocityTemplateEngine.class);
 
 			Maven2SourceProvider sourceProvider = new Maven2SourceProvider(
-					config, compileSourceRoots);
+					config, compileSourceRoots, dependenciesSourcesURLs);
 			pico.registerComponentInstance(sourceProvider);
 
 			// register the plugin itself
@@ -113,7 +124,7 @@
 
 	/**
 	 * A list of config for XDoclet.
-	 * 
+	 *
 	 * @parameter
 	 * @required
 	 */
@@ -130,7 +141,77 @@
 	 */
 	private MavenProject project;
 
+	/**
+	 * Local maven repository.
+	 *
+	 * @parameter _expression_="${localRepository}"
+	 * @required
+	 * @readonly
+	 */
+	private ArtifactRepository localRepository;
+
+	/**
+	 * Artifact factory, needed to download source jars for inclusion in classpath.
+	 *
+	 * @component role="org.apache.maven.artifact.factory.ArtifactFactory"
+	 * @required
+	 * @readonly
+	 */
+	private ArtifactFactory artifactFactory;
+
+	/**
+	 * Artifact resolver, needed to download source jars for inclusion in classpath.
+	 *
+	 * @component role="org.apache.maven.artifact.resolver.ArtifactResolver"
+	 * @required
+	 * @readonly
+	 */
+	private ArtifactResolver artifactResolver;
+
+	/**
+	 * A comma separated list of patterns for which source jars artifacts should be passed to QDox for parsing.
+	 * i.e "org.apache:*,com:*,net.sourceforge.myproject:myartifact" or even "foo.bar:baz*" are supported.
+	 * The version can't be specified here and will be deducted from the project's dependencies.
+	 * If you don't want to <strong>generate</strong> code or descriptors for these artifacts, you'll need
+	 * to add <code>&lt;restrictedpath&gt;file://${settings.localRepository}&lt;/restrictedpath&gt;</code> to the
+	 * appropriate components(xdoclet plugins).
+	 * @parameter
+	 */
+	private String sourceArtifacts;
+
 	public void execute() throws MojoExecutionException, MojoFailureException {
+		final List dependenciesSourcesURLs = new ArrayList();
+
+		final List sourceDependencyIncludes = Util.toTrimmedList(sourceArtifacts);
+		final PatternIncludesArtifactFilter filter = new PatternIncludesArtifactFilter(sourceDependencyIncludes, true);
+
+		if (!sourceDependencyIncludes.isEmpty()) {
+			final List remoteArtifactRepositories = project.getRemoteArtifactRepositories();
+			final List deps = project.getDependencies();
+
+			final Iterator itDeps = deps.iterator();
+			while (itDeps.hasNext()) {
+				final Dependency dep = (Dependency) itDeps.next();
+				final String artifactId = dep.getArtifactId();
+				final String groupId = dep.getGroupId();
+				final String version = dep.getVersion();
+				final Artifact a = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, "java-source", "sources");
+				try {
+					if (filter.include(a)) {
+						artifactResolver.resolve(a, remoteArtifactRepositories, localRepository);
+						if (a.getFile() == null) {
+							throw new ArtifactNotFoundException("Can't resolve", a);
+						}
+						dependenciesSourcesURLs.add(a.getFile());
+					}
+				} catch (ArtifactNotFoundException e) {
+					throw new MojoExecutionException("Source artifact for " + groupId + ":" + artifactId + ":" + version + " was not found : " + e.getMessage());
+				} catch (ArtifactResolutionException e) {
+					getLog().warn("Could not download source artifact for " + groupId + ":" + artifactId + ":" + version + " : " + e.getMessage());
+				}
+			}
+		}
+
 		final Iterator it = configs.iterator();
 		while (it.hasNext()) {
 			final Config config = (Config) it.next();
@@ -142,7 +223,7 @@
 			final Map defaultPluginProps = Collections.singletonMap("destdir",
 					outputPath);
 			final ContainerComposer containerComposer = new PluginContainerComposer(
-					config, defaultPluginProps, project.getCompileSourceRoots());
+					config, defaultPluginProps, project.getCompileSourceRoots(), dependenciesSourcesURLs);
 			final ContainerBuilder containerBuilder = new PluginLifecycleContainerBuilder(
 					containerComposer);
 			try {


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to