/
JavadocLinkGenerator.java
265 lines (250 loc) · 10.3 KB
/
JavadocLinkGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package org.apache.maven.tools.plugin.javadoc;
/*
* 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.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.languages.java.version.JavaVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Generates links for elements (packages, classes, fields, constructors, methods) in external
* and/or an internal (potentially not yet existing) javadoc site.
* The external site must be accessible for it to be considered due to the different fragment formats.
*/
public class JavadocLinkGenerator
{
/**
* Javadoc tool version ranges whose generated sites expose different link formats.
*
*/
public enum JavadocToolVersionRange
{
JDK7_OR_LOWER( null, JavaVersion.parse( "1.8" ) ),
JDK8_OR_9( JavaVersion.parse( "1.8" ), JavaVersion.parse( "10" ) ),
JDK10_OR_HIGHER( JavaVersion.parse( "10" ), null );
// upper bound is exclusive, lower bound inclusive (null means unlimited)
private final JavaVersion lowerBound;
private final JavaVersion upperBound;
JavadocToolVersionRange( JavaVersion lowerBound, JavaVersion upperBound )
{
this.lowerBound = lowerBound;
this.upperBound = upperBound;
}
static JavadocToolVersionRange findMatch( JavaVersion javadocVersion )
{
for ( JavadocToolVersionRange range : values() )
{
if ( ( range.lowerBound == null || javadocVersion.isAtLeast( range.lowerBound ) )
&& ( range.upperBound == null || javadocVersion.isBefore( range.upperBound ) ) )
{
return range;
}
}
throw new IllegalArgumentException( "Found no matching javadoc tool version range for " + javadocVersion );
}
}
private static final Logger LOG = LoggerFactory.getLogger( JavadocLinkGenerator.class );
private final List<JavadocSite> externalJavadocSites;
private final JavadocSite internalJavadocSite; // may be null
/**
* Constructor for an offline internal site only.
*
* @param internalJavadocSiteUrl the url of the javadoc generated website
* @param internalJavadocVersion the version of javadoc with which the internal site from
* {@code internalJavadocSiteUrl} has been generated
*/
public JavadocLinkGenerator( URI internalJavadocSiteUrl,
String internalJavadocVersion )
{
this( internalJavadocSiteUrl, internalJavadocVersion, Collections.emptyList(), null );
}
/**
* Constructor for online external sites only.
*
* @param externalJavadocSiteUrls
* @param settings
*/
public JavadocLinkGenerator( List<URI> externalJavadocSiteUrls, Settings settings )
{
this( null, null, externalJavadocSiteUrls, settings );
}
/**
* Constructor for both an internal (offline) and external (online) sites.
*
* @param internalJavadocSiteUrl
* @param internalJavadocVersion
* @param externalJavadocSiteUrls
* @param settings
*/
public JavadocLinkGenerator( URI internalJavadocSiteUrl,
String internalJavadocVersion,
List<URI> externalJavadocSiteUrls, Settings settings )
{
if ( internalJavadocSiteUrl != null )
{
// resolve version
JavaVersion javadocVersion = JavaVersion.parse( internalJavadocVersion );
internalJavadocSite = new JavadocSite( internalJavadocSiteUrl,
JavadocToolVersionRange.findMatch( javadocVersion ),
false );
}
else
{
internalJavadocSite = null;
}
if ( externalJavadocSiteUrls != null )
{
externalJavadocSites = new ArrayList<>( externalJavadocSiteUrls.size() );
for ( URI siteUrl : externalJavadocSiteUrls )
{
try
{
externalJavadocSites.add( new JavadocSite( siteUrl, settings ) );
}
catch ( IOException e )
{
LOG.warn( "Could not use {} as base URL: {}", siteUrl, e.getMessage(), e );
}
}
}
else
{
externalJavadocSites = Collections.emptyList();
}
if ( internalJavadocSite == null && externalJavadocSites.isEmpty() )
{
throw new IllegalArgumentException( "Either internal or at least one accessible external javadoc "
+ "URLs must be given!" );
}
}
/**
* Generates a (deep-)link to a HTML page in any of the sites given to the constructor.
* The link is not validated (i.e. might point to a non-existing page).
* Only uses the offline site for references returning {@code false} for
* {@link FullyQualifiedJavadocReference#isExternal()}.
* @param javadocReference
* @return the (deep-) link towards a javadoc page
* @throws IllegalArgumentException in case no javadoc link could be generated for the given reference
* @throws IllegalStateException in case no javadoc source sites have been configured
*/
public URI createLink( FullyQualifiedJavadocReference javadocReference )
{
if ( !javadocReference.isExternal() && internalJavadocSite != null )
{
return internalJavadocSite.createLink( javadocReference );
}
else
{
JavadocSite javadocSite = externalJavadocSites.stream()
.filter( base -> base.hasEntryFor( javadocReference.getModuleName(),
javadocReference.getPackageName() ) )
.findFirst().orElseThrow( () -> new IllegalArgumentException( "Found no javadoc site for "
+ javadocReference ) );
return javadocSite.createLink( javadocReference );
}
}
/**
* Generates a (deep-)link to a HTML page in any of the sites given to the constructor.
* The link is not validated (i.e. might point to a non-existing page).
* Preferably resolves from the online sites if they provide the given package.
* @param binaryName a binary name according to
* <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-13.html#jls-13.1">JLS 13.1</a>
* @return the (deep-) link towards a javadoc page
* @throws IllegalArgumentException in case no javadoc link could be generated for the given name
*/
public URI createLink( String binaryName )
{
Map.Entry<String, String> packageAndClassName = JavadocSite.getPackageAndClassName( binaryName );
// first check external links, otherwise assume internal link
JavadocSite javadocSite = externalJavadocSites.stream()
.filter( base -> base.hasEntryFor( Optional.empty(),
Optional.of( packageAndClassName.getKey() ) ) )
.findFirst().orElse( null );
if ( javadocSite == null )
{
if ( internalJavadocSite != null )
{
javadocSite = internalJavadocSite;
}
else
{
throw new IllegalArgumentException( "Found no javadoc site for " + binaryName );
}
}
return javadocSite.createLink( packageAndClassName.getKey(), packageAndClassName.getValue() );
}
public URI getInternalJavadocSiteBaseUrl()
{
if ( internalJavadocSite == null )
{
throw new IllegalStateException( "Could not get docroot of internal javadoc as it hasn't been set" );
}
return internalJavadocSite.getBaseUri();
}
/**
* Checks if a given link is valid. For absolute links uses the underling {@link java.net.HttpURLConnection},
* otherwise checks for existence of the file on the filesystem.
*
* @param url the url to check
* @param baseDirectory the base directory to which relative file URLs refer
* @return {@code true} in case the given link is valid otherwise {@code false}
*/
public static boolean isLinkValid( URI url, Path baseDirectory )
{
if ( url.isAbsolute() )
{
try ( BufferedReader reader = JavadocSite.getReader( url.toURL(), null ) )
{
if ( url.getFragment() != null )
{
Pattern pattern = JavadocSite.getAnchorPattern( url.getFragment() );
if ( reader.lines().noneMatch( pattern.asPredicate() ) )
{
return false;
}
}
}
catch ( IOException e )
{
return false;
}
return true;
}
else
{
Path file = baseDirectory.resolve( url.getPath() );
boolean exists = Files.exists( file );
if ( !exists )
{
LOG.debug( "Could not find file given through '{}' in resolved path '{}'", url, file );
}
return exists;
}
}
}