Skip to content

Commit 6fce23d

Browse files
[JXR-164] Use file name without path for page title
1 parent 253bc32 commit 6fce23d

File tree

5 files changed

+99
-22
lines changed

5 files changed

+99
-22
lines changed

maven-jxr/src/main/java/org/apache/maven/jxr/JavaCodeTransform.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ private void appendHeader( PrintWriter out )
332332
}
333333
else
334334
{
335-
out.print( this.getCurrentFilename() );
335+
out.print( javaFile.getFilename() );
336336
}
337337
out.print( ' ' );
338338
}
@@ -983,7 +983,7 @@ private String getFileOverview()
983983
{
984984
overview.append( "<div id=\"overview\">" );
985985
// get the URI to get Javadoc info.
986-
Path javadocURI = javadocLinkDir;
986+
Path javadocURI;
987987

988988
try
989989
{
@@ -992,10 +992,16 @@ private String getFileOverview()
992992
javadocURI = javadocLinkDir.resolve( jf.getPackageType().getName().replace( '.', '/' ) )
993993
;
994994
// Use the name of the file instead of the class to handle inner classes properly
995+
String fileName;
995996
if ( jf.getClassType() != null && jf.getClassType().getFilename() != null )
996997
{
997-
javadocURI = javadocURI.resolve( jf.getClassType().getFilename() + ".html" );
998+
fileName = jf.getClassType().getFilename();
998999
}
1000+
else
1001+
{
1002+
fileName = jf.getFilename();
1003+
}
1004+
javadocURI = javadocURI.resolve( fileName + ".html" );
9991005

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

maven-jxr/src/main/java/org/apache/maven/jxr/pacman/JavaFile.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ public abstract class JavaFile
4242

4343
private final Path path;
4444

45+
private String filename;
46+
4547
private final String encoding;
4648

4749
protected JavaFile( Path path, String encoding )
4850
{
4951
this.path = path;
5052
this.encoding = encoding;
53+
this.filename = getFilenameWithoutPathOrExtension( path );
5154
}
5255

5356
/**
@@ -134,11 +137,35 @@ public Path getPath()
134137
return this.path;
135138
}
136139

140+
/**
141+
* File name without path and extension.
142+
*/
143+
public String getFilename()
144+
{
145+
return filename;
146+
}
147+
137148
/**
138149
* Gets the encoding attribute of the JavaFile object
139150
*/
140151
public String getEncoding()
141152
{
142153
return this.encoding;
143154
}
155+
156+
/**
157+
* Remove the path and the ".java" extension from a filename.
158+
*/
159+
protected static String getFilenameWithoutPathOrExtension( Path path )
160+
{
161+
String newFilename = path.getFileName().toString();
162+
// Remove the ".java" extension from the filename, if it exists
163+
int extensionIndex = newFilename.lastIndexOf( ".java" );
164+
if ( extensionIndex >= 0 )
165+
{
166+
newFilename = newFilename.substring( 0, extensionIndex );
167+
}
168+
return newFilename;
169+
}
170+
144171
}

maven-jxr/src/main/java/org/apache/maven/jxr/pacman/JavaFileImpl.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,6 @@ and packages that are imported with this (ex "test.*") will be
147147
}
148148
}
149149

150-
/**
151-
* Remove the path and the ".java" extension from a filename.
152-
*/
153-
private static String getFilenameWithoutPathOrExtension( Path path )
154-
{
155-
String newFilename = path.getFileName().toString();
156-
// Remove the ".java" extension from the filename, if it exists
157-
int extensionIndex = newFilename.lastIndexOf( ".java" );
158-
if ( extensionIndex >= 0 )
159-
{
160-
newFilename = newFilename.substring( 0, extensionIndex );
161-
}
162-
return newFilename;
163-
}
164-
165150
/**
166151
* Get a StreamTokenizer for this file.
167152
*/

maven-jxr/src/test/java/org/apache/maven/jxr/JavaCodeTransformTest.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.apache.maven.jxr;
22

3-
import static org.junit.Assert.assertTrue;
4-
53
/*
64
* Licensed to the Apache Software Foundation (ASF) under one
75
* or more contributor license agreements. See the NOTICE file
@@ -32,6 +30,8 @@
3230
import org.junit.Before;
3331
import org.junit.Test;
3432

33+
import static org.junit.Assert.assertTrue;
34+
3535
/**
3636
* JUnit test for {@link JavaCodeTransform}.
3737
*/
@@ -64,8 +64,14 @@ public void testTransform()
6464
multiline comment text
6565
6666
*/ codeTransform.transform( sourceFile, Paths.get( "target/JavaCodeTransformTest.html" ) // additional comment
67-
, Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "." ), "", "" );
67+
, Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "./javadocs-test" ), "", "" );
6868
assertTrue( /**/ Files.exists( Paths.get( "target/JavaCodeTransformTest.html" ) ) );
69+
70+
byte[] bytes = Files.readAllBytes( Paths.get( "target/JavaCodeTransformTest.html" ) );
71+
String content = new String( bytes, StandardCharsets.ISO_8859_1 );
72+
assertTrue( content.contains( "<title>JavaCodeTransformTest xref</title>" ) );
73+
assertTrue( content.contains( "<a href=\"./javadocs-test/org/apache/maven/jxr/JavaCodeTransformTest.html\">"
74+
+ "View Javadoc</a>" ) );
6975
}
7076

7177
/**
@@ -79,8 +85,13 @@ public void testTransformWithEmptyClassFile()
7985
assertTrue( Files.exists( sourceFile ) );
8086

8187
codeTransform.transform( sourceFile, Paths.get( "target/EmptyClass.html" )
82-
, Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "." ), "", "" );
88+
, Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "javadocs" ), "", "" );
8389
assertTrue( Files.exists( Paths.get( "target/EmptyClass.html" ) ) );
90+
91+
byte[] bytes = Files.readAllBytes( Paths.get( "target/EmptyClass.html" ) );
92+
String content = new String( bytes, StandardCharsets.ISO_8859_1 );
93+
assertTrue( content.contains( "<title>EmptyClass xref</title>" ) );
94+
assertTrue( content.contains( "<a href=\"javadocs/EmptyClass.html\">View Javadoc</a>" ) );
8495
}
8596

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

111122
}
123+
124+
/**
125+
* Test what happens with unknown java type.
126+
*/
127+
@Test
128+
public void testTransformWithUnknownJavaType()
129+
throws Exception
130+
{
131+
Path sourceFile = Paths.get( "src/test/resources/UnknownType.java" );
132+
assertTrue( Files.exists( sourceFile ) );
133+
134+
codeTransform.transform( sourceFile, Paths.get( "target/UnknownType.html" )
135+
, Locale.ENGLISH, "ISO-8859-1", "ISO-8859-1", Paths.get( "javadocs" ), "", "" );
136+
assertTrue( Files.exists( Paths.get( "target/UnknownType.html" ) ) );
137+
138+
byte[] bytes = Files.readAllBytes( Paths.get( "target/UnknownType.html" ) );
139+
String content = new String( bytes, StandardCharsets.ISO_8859_1 );
140+
assertTrue( content.contains( "<title>UnknownType xref</title>" ) );
141+
assertTrue( content.contains( "<a href=\"javadocs/example/UnknownType.html\">View Javadoc</a>" ) );
142+
}
143+
112144
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package example;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
/**
23+
* This is a sample class with unknown type.
24+
*/
25+
public unknown UnknownType
26+
{
27+
}

0 commit comments

Comments
 (0)