You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2019/07/05 13:24:27 UTC
[maven-jxr] 01/01: [JXR-145] Support custom codetransformers
This is an automated email from the ASF dual-hosted git repository.
rfscholte pushed a commit to branch JXR-145
in repository https://gitbox.apache.org/repos/asf/maven-jxr.git
commit ee3219f57550a5f81e97f43ac12d59698f9a0375
Author: rfscholte <rf...@apache.org>
AuthorDate: Fri Jul 5 15:24:17 2019 +0200
[JXR-145] Support custom codetransformers
---
.../apache/maven/plugin/jxr/AbstractJxrReport.java | 1 -
.../org/apache/maven/plugin/jxr/JxrReport.html | 2 +-
.../src/main/java/org/apache/maven/jxr/JXR.java | 119 +++++++++------------
.../org/apache/maven/jxr/JavaCodeTransform.java | 97 ++++++++---------
.../apache/maven/jxr/JavaCodeTransformTest.java | 8 +-
5 files changed, 101 insertions(+), 126 deletions(-)
diff --git a/maven-jxr-plugin/src/main/java/org/apache/maven/plugin/jxr/AbstractJxrReport.java b/maven-jxr-plugin/src/main/java/org/apache/maven/plugin/jxr/AbstractJxrReport.java
index 2f6586a..678a0e8 100644
--- a/maven-jxr-plugin/src/main/java/org/apache/maven/plugin/jxr/AbstractJxrReport.java
+++ b/maven-jxr-plugin/src/main/java/org/apache/maven/plugin/jxr/AbstractJxrReport.java
@@ -277,7 +277,6 @@ public abstract class AbstractJxrReport
jxr.setInputEncoding( inputEncoding );
jxr.setLocale( locale );
jxr.setOutputEncoding( getOutputEncoding() );
- jxr.setRevision( "HEAD" );
jxr.setJavadocLinkDir( getJavadocLocation() );
// Set include/exclude patterns on the jxr instance
if ( excludes != null && !excludes.isEmpty() )
diff --git a/maven-jxr-plugin/src/site/resources/example/xref/org/apache/maven/plugin/jxr/JxrReport.html b/maven-jxr-plugin/src/site/resources/example/xref/org/apache/maven/plugin/jxr/JxrReport.html
index 2e228e7..6adf638 100644
--- a/maven-jxr-plugin/src/site/resources/example/xref/org/apache/maven/plugin/jxr/JxrReport.html
+++ b/maven-jxr-plugin/src/site/resources/example/xref/org/apache/maven/plugin/jxr/JxrReport.html
@@ -394,7 +394,7 @@ under the License.
<a name="366" href="#366">366</a> jxr.setLocale( locale );
<a name="367" href="#367">367</a> jxr.setLog( <strong>new</strong> PluginLogAdapter( getLog() ) );
<a name="368" href="#368">368</a> jxr.setOutputEncoding( outputEncoding );
-<a name="369" href="#369">369</a> jxr.setRevision( <span class="string">"HEAD"</span> );
+<a name="369" href="#369">369</a>
<a name="370" href="#370">370</a>
<a name="371" href="#371">371</a> jxr.xref( sourceDirs, templateDir, windowTitle, docTitle, bottom );
<a name="372" href="#372">372</a>
diff --git a/maven-jxr/src/main/java/org/apache/maven/jxr/JXR.java b/maven-jxr/src/main/java/org/apache/maven/jxr/JXR.java
index a719ff0..54bb576 100644
--- a/maven-jxr/src/main/java/org/apache/maven/jxr/JXR.java
+++ b/maven-jxr/src/main/java/org/apache/maven/jxr/JXR.java
@@ -27,8 +27,12 @@ import org.codehaus.plexus.logging.AbstractLogEnabled;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
import javax.inject.Inject;
import javax.inject.Named;
@@ -52,14 +56,9 @@ public class JXR extends AbstractLogEnabled
* the eye!" :)
*/
@Inject
- private JavaCodeTransform transformer;
+ private Map<String, CodeTransformer> transformers;
/**
- * The default list of include patterns to use.
- */
- private static final String[] DEFAULT_INCLUDES = { "**/*.java" };
-
- /**
* Path to destination.
*/
private Path destDir;
@@ -75,21 +74,15 @@ public class JXR extends AbstractLogEnabled
*/
private Path javadocLinkDir;
-
- /**
- * The revision of the module currently being processed.
- */
- private String revision;
-
/**
* The list of exclude patterns to use.
*/
- private String[] excludes = null;
+ private String[] excludes;
/**
* The list of include patterns to use.
*/
- private String[] includes = DEFAULT_INCLUDES;
+ private String[] includes;
/**
* Now that we have instantiated everything. Process this JXR task.
@@ -102,53 +95,67 @@ public class JXR extends AbstractLogEnabled
public void processPath( PackageManager packageManager, Path sourceDir, String bottom )
throws IOException
{
+
DirectoryScanner ds = new DirectoryScanner();
// I'm not sure why we don't use the directoryScanner in packageManager,
// but since we don't we need to set includes/excludes here as well
ds.setExcludes( excludes );
- ds.setIncludes( includes );
+ if ( includes != null )
+ {
+ ds.setIncludes( includes );
+ }
+ else
+ {
+ Set<String> transformerIncludes = new HashSet<>();
+ for ( CodeTransformer transformer: transformers.values() )
+ {
+ transformerIncludes.addAll( transformer.getDefaultIncludes() );
+ }
+ ds.setIncludes( transformerIncludes.toArray( new String[0] ) );
+ }
ds.addDefaultExcludes();
ds.setBasedir( sourceDir.toString() );
ds.scan();
//now get the list of included files
-
String[] files = ds.getIncludedFiles();
-
+
+ Map<String, CodeTransformer> transformerForExtension = new HashMap<>();
+
for ( String file : files )
{
Path sourceFile = sourceDir.resolve( file );
+
+ String fileExtension = getExtension( sourceFile );
- if ( isJavaFile( sourceFile.toString() ) )
+ CodeTransformer transformer = transformerForExtension.get( fileExtension );
+ if ( !transformerForExtension.containsKey( fileExtension ) )
+ {
+ for ( CodeTransformer ct : transformers.values() )
+ {
+ if ( ct.canTransform( fileExtension ) )
+ {
+ transformer = ct;
+ break;
+ }
+ }
+ transformerForExtension.put( fileExtension, transformer );
+ }
+
+ if ( transformer != null )
{
- String newFileName = file.replaceFirst( ".java$", ".html" );
+ String newFileName = file.replaceFirst( fileExtension + '$', ".html" );
- transform( sourceFile, this.destDir.resolve( newFileName ), bottom );
+ transform( transformer, sourceFile, this.destDir.resolve( newFileName ), bottom );
}
}
}
-
- /**
- * Check to see if the file is a Java source file.
- *
- * @param filename The name of the file to check
- * @return <code>true</code> if the file is a Java file
- */
- public static boolean isJavaFile( String filename )
- {
- return filename.endsWith( ".java" );
- }
-
- /**
- * Check to see if the file is an HTML file.
- *
- * @param filename The name of the file to check
- * @return <code>true</code> if the file is an HTML file
- */
- public static boolean isHtmlFile( String filename )
+
+ private String getExtension( Path file )
{
- return filename.endsWith( ".html" );
+ String fileName = file.getFileName().toString();
+ return fileName.substring( fileName.indexOf( '.' ) );
}
/**
@@ -193,22 +200,6 @@ public class JXR extends AbstractLogEnabled
}
/**
- * @param transformer
- */
- public void setTransformer( JavaCodeTransform transformer )
- {
- this.transformer = transformer;
- }
-
- /**
- * @param revision
- */
- public void setRevision( String revision )
- {
- this.revision = revision;
- }
-
- /**
* @param sourceDirs
* @param templateDir
* @param windowTitle
@@ -260,15 +251,14 @@ public class JXR extends AbstractLogEnabled
* @param bottom The bottom footer text just as in the package pages
* @throws IOException Thrown if the transform can't happen for some reason.
*/
- private void transform( Path sourceFile, Path destFile, String bottom )
+ private void transform( CodeTransformer transformer, Path sourceFile, Path destFile, String bottom )
throws IOException
{
getLogger().debug( sourceFile + " -> " + destFile );
// get a relative link to the javadocs
Path javadoc = javadocLinkDir != null ? getRelativeLink( destFile.getParent(), javadocLinkDir ) : null;
- transformer.transform( sourceFile, destFile, locale, inputEncoding, outputEncoding, javadoc,
- this.revision, bottom );
+ transformer.transform( sourceFile, destFile, locale, inputEncoding, outputEncoding, javadoc, bottom );
}
/**
@@ -294,17 +284,8 @@ public class JXR extends AbstractLogEnabled
this.excludes = excludes;
}
-
public void setIncludes( String[] includes )
{
- if ( includes == null )
- {
- // We should not include non-java files, so we use a sensible default pattern
- this.includes = DEFAULT_INCLUDES;
- }
- else
- {
- this.includes = includes;
- }
+ this.includes = includes;
}
}
diff --git a/maven-jxr/src/main/java/org/apache/maven/jxr/JavaCodeTransform.java b/maven-jxr/src/main/java/org/apache/maven/jxr/JavaCodeTransform.java
index 106280f..fb856c6 100644
--- a/maven-jxr/src/main/java/org/apache/maven/jxr/JavaCodeTransform.java
+++ b/maven-jxr/src/main/java/org/apache/maven/jxr/JavaCodeTransform.java
@@ -1,5 +1,33 @@
package org.apache.maven.jxr;
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Reader;
+import java.io.Serializable;
+import java.io.Writer;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.inject.Singleton;
+
/*
* CodeViewer.java
* CoolServlets.com
@@ -40,34 +68,6 @@ import org.apache.maven.jxr.pacman.PackageType;
import org.apache.maven.jxr.util.SimpleWordTokenizer;
import org.apache.maven.jxr.util.StringEntry;
-import java.io.BufferedReader;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.OutputStreamWriter;
-import java.io.PrintWriter;
-import java.io.Reader;
-import java.io.Serializable;
-import java.io.Writer;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Hashtable;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Set;
-
-import javax.inject.Inject;
-import javax.inject.Named;
-import javax.inject.Singleton;
-
/**
* Syntax highlights java by turning it into html. A codeviewer object is created and then keeps state as lines are
* passed in. Each line passed in as java test, is returned as syntax highlighted html text. Users of the class can set
@@ -95,10 +95,10 @@ import javax.inject.Singleton;
* importFilter
* </pre>
*/
-@Named
+@Named( "java" )
@Singleton
public class JavaCodeTransform
- implements Serializable
+ implements Serializable, CodeTransformer
{
// ----------------------------------------------------------------------
// public fields
@@ -191,11 +191,6 @@ public class JavaCodeTransform
private Path currentFilename = null;
/**
- * The current CVS revision of the currently transformed document
- */
- private String revision = null;
-
- /**
* The input encoding
*/
private String inputEncoding = null;
@@ -285,6 +280,18 @@ public class JavaCodeTransform
reservedWords.put( "implements", "implements" );
}
+ @Override
+ public boolean canTransform( String fileExtension )
+ {
+ return ".java".equals( fileExtension );
+ }
+
+ @Override
+ public Set<String> getDefaultIncludes()
+ {
+ return Collections.singleton( "**/*.java" );
+ }
+
// ----------------------------------------------------------------------
// public methods
// ----------------------------------------------------------------------
@@ -393,19 +400,17 @@ public class JavaCodeTransform
* @param inputEncoding String
* @param outputEncoding String
* @param javadocLinkDir String
- * @param revision String
* @param bottom string
* @throws IOException
*/
private void transform( Reader sourceReader, Writer destWriter, Locale locale, String inputEncoding,
- String outputEncoding, Path javadocLinkDir, String revision, String bottom )
+ String outputEncoding, Path javadocLinkDir, String bottom )
throws IOException
{
this.locale = locale;
this.inputEncoding = inputEncoding;
this.outputEncoding = outputEncoding;
this.javadocLinkDir = javadocLinkDir;
- this.revision = revision;
BufferedReader in = new BufferedReader( sourceReader );
@@ -443,12 +448,12 @@ public class JavaCodeTransform
* @param inputEncoding String
* @param outputEncoding String
* @param javadocLinkDir String
- * @param revision String
* @param bottom TODO
* @throws IOException
*/
+ @Override
public final void transform( Path sourcefile, Path destfile, Locale locale, String inputEncoding,
- String outputEncoding, Path javadocLinkDir, String revision, String bottom )
+ String outputEncoding, Path javadocLinkDir, String bottom )
throws IOException
{
this.setCurrentFilename( sourcefile );
@@ -458,7 +463,7 @@ public class JavaCodeTransform
try ( Reader fr = getReader( sourcefile, inputEncoding ); Writer fw = getWriter( destfile, outputEncoding ) )
{
- transform( fr, fw, locale, inputEncoding, outputEncoding, javadocLinkDir, revision, bottom );
+ transform( fr, fw, locale, inputEncoding, outputEncoding, javadocLinkDir, bottom );
}
catch ( RuntimeException e )
{
@@ -606,16 +611,6 @@ public class JavaCodeTransform
}
/**
- * The current revision of the CVS module
- *
- * @return String
- */
- public final String getRevision()
- {
- return this.revision;
- }
-
- /**
* Cross Reference the given line with JXR returning the new content.
*
* @param line String
diff --git a/maven-jxr/src/test/java/org/apache/maven/jxr/JavaCodeTransformTest.java b/maven-jxr/src/test/java/org/apache/maven/jxr/JavaCodeTransformTest.java
index 1b1f37c..52e9d86 100644
--- a/maven-jxr/src/test/java/org/apache/maven/jxr/JavaCodeTransformTest.java
+++ b/maven-jxr/src/test/java/org/apache/maven/jxr/JavaCodeTransformTest.java
@@ -34,7 +34,7 @@ import org.codehaus.plexus.PlexusTestCase;
public class JavaCodeTransformTest extends PlexusTestCase
{
/** JavaCodeTransform object under test */
- private JavaCodeTransform codeTransform;
+ private CodeTransformer codeTransform;
@Override
protected void customizeContainerConfiguration( ContainerConfiguration configuration )
@@ -65,7 +65,7 @@ public class JavaCodeTransformTest extends PlexusTestCase
multiline comment text
*/ codeTransform.transform( sourceFile, Paths.get( "target/JavaCodeTransformTest.html" ) // additional comment
- , Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "." ), "", "" );
+ , Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "." ), "" );
assertTrue( /**/ Files.exists( Paths.get( "target/JavaCodeTransformTest.html" ) ) );
}
@@ -79,7 +79,7 @@ public class JavaCodeTransformTest extends PlexusTestCase
assertTrue( Files.exists( sourceFile ) );
codeTransform.transform( sourceFile, Paths.get( "target/EmptyClass.html" )
- , Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "." ), "", "" );
+ , Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "." ), "" );
assertTrue( Files.exists( Paths.get( "target/EmptyClass.html" ) ) );
}
@@ -93,7 +93,7 @@ public class JavaCodeTransformTest extends PlexusTestCase
assertTrue( Files.exists( sourceFile ) );
codeTransform.transform( sourceFile, Paths.get( "target/ClassWithLink.html" )
- , Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "." ), "", "" );
+ , Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "." ), "" );
assertTrue( Files.exists( Paths.get( "target/ClassWithLink.html" ) ) );
byte[] bytes = Files.readAllBytes( Paths.get( "target/ClassWithLink.html" ) );