Skip to content

Commit

Permalink
[JXR-164] Use file name without path for page title
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Mar 10, 2022
1 parent 253bc32 commit 6fce23d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ private void appendHeader( PrintWriter out )
}
else
{
out.print( this.getCurrentFilename() );
out.print( javaFile.getFilename() );
}
out.print( ' ' );
}
Expand Down Expand Up @@ -983,7 +983,7 @@ private String getFileOverview()
{
overview.append( "<div id=\"overview\">" );
// get the URI to get Javadoc info.
Path javadocURI = javadocLinkDir;
Path javadocURI;

try
{
Expand All @@ -992,10 +992,16 @@ private String getFileOverview()
javadocURI = javadocLinkDir.resolve( jf.getPackageType().getName().replace( '.', '/' ) )
;
// Use the name of the file instead of the class to handle inner classes properly
String fileName;
if ( jf.getClassType() != null && jf.getClassType().getFilename() != null )
{
javadocURI = javadocURI.resolve( jf.getClassType().getFilename() + ".html" );
fileName = jf.getClassType().getFilename();
}
else
{
fileName = jf.getFilename();
}
javadocURI = javadocURI.resolve( fileName + ".html" );

String javadocHREF = "<a href=\"" + javadocURI.toString().replace( '\\', '/' ) + "\">View Javadoc</a>";

Expand Down
27 changes: 27 additions & 0 deletions maven-jxr/src/main/java/org/apache/maven/jxr/pacman/JavaFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ public abstract class JavaFile

private final Path path;

private String filename;

private final String encoding;

protected JavaFile( Path path, String encoding )
{
this.path = path;
this.encoding = encoding;
this.filename = getFilenameWithoutPathOrExtension( path );
}

/**
Expand Down Expand Up @@ -134,11 +137,35 @@ public Path getPath()
return this.path;
}

/**
* File name without path and extension.
*/
public String getFilename()
{
return filename;
}

/**
* Gets the encoding attribute of the JavaFile object
*/
public String getEncoding()
{
return this.encoding;
}

/**
* Remove the path and the ".java" extension from a filename.
*/
protected static String getFilenameWithoutPathOrExtension( Path path )
{
String newFilename = path.getFileName().toString();
// Remove the ".java" extension from the filename, if it exists
int extensionIndex = newFilename.lastIndexOf( ".java" );
if ( extensionIndex >= 0 )
{
newFilename = newFilename.substring( 0, extensionIndex );
}
return newFilename;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,6 @@ and packages that are imported with this (ex "test.*") will be
}
}

/**
* Remove the path and the ".java" extension from a filename.
*/
private static String getFilenameWithoutPathOrExtension( Path path )
{
String newFilename = path.getFileName().toString();
// Remove the ".java" extension from the filename, if it exists
int extensionIndex = newFilename.lastIndexOf( ".java" );
if ( extensionIndex >= 0 )
{
newFilename = newFilename.substring( 0, extensionIndex );
}
return newFilename;
}

/**
* Get a StreamTokenizer for this file.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.apache.maven.jxr;

import static org.junit.Assert.assertTrue;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
Expand Down Expand Up @@ -32,6 +30,8 @@
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

/**
* JUnit test for {@link JavaCodeTransform}.
*/
Expand Down Expand Up @@ -64,8 +64,14 @@ public void testTransform()
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( "./javadocs-test" ), "", "" );
assertTrue( /**/ Files.exists( Paths.get( "target/JavaCodeTransformTest.html" ) ) );

byte[] bytes = Files.readAllBytes( Paths.get( "target/JavaCodeTransformTest.html" ) );
String content = new String( bytes, StandardCharsets.ISO_8859_1 );
assertTrue( content.contains( "<title>JavaCodeTransformTest xref</title>" ) );
assertTrue( content.contains( "<a href=\"./javadocs-test/org/apache/maven/jxr/JavaCodeTransformTest.html\">"
+ "View Javadoc</a>" ) );
}

/**
Expand All @@ -79,8 +85,13 @@ public void testTransformWithEmptyClassFile()
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( "javadocs" ), "", "" );
assertTrue( Files.exists( Paths.get( "target/EmptyClass.html" ) ) );

byte[] bytes = Files.readAllBytes( Paths.get( "target/EmptyClass.html" ) );
String content = new String( bytes, StandardCharsets.ISO_8859_1 );
assertTrue( content.contains( "<title>EmptyClass xref</title>" ) );
assertTrue( content.contains( "<a href=\"javadocs/EmptyClass.html\">View Javadoc</a>" ) );
}

/**
Expand Down Expand Up @@ -109,4 +120,25 @@ public void testLinkHandling()
"target=\"alexandria_uri\">https://www.apache.org/licenses/LICENSE-2.0</a></em>" ) );

}

/**
* Test what happens with unknown java type.
*/
@Test
public void testTransformWithUnknownJavaType()
throws Exception
{
Path sourceFile = Paths.get( "src/test/resources/UnknownType.java" );
assertTrue( Files.exists( sourceFile ) );

codeTransform.transform( sourceFile, Paths.get( "target/UnknownType.html" )
, Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "javadocs" ), "", "" );
assertTrue( Files.exists( Paths.get( "target/UnknownType.html" ) ) );

byte[] bytes = Files.readAllBytes( Paths.get( "target/UnknownType.html" ) );
String content = new String( bytes, StandardCharsets.ISO_8859_1 );
assertTrue( content.contains( "<title>UnknownType xref</title>" ) );
assertTrue( content.contains( "<a href=\"javadocs/example/UnknownType.html\">View Javadoc</a>" ) );
}

}
27 changes: 27 additions & 0 deletions maven-jxr/src/test/resources/UnknownType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package example;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
* This is a sample class with unknown type.
*/
public unknown UnknownType
{
}

0 comments on commit 6fce23d

Please sign in to comment.