Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[maven] Fixing URL generation for Windows OS in the documentation gen…
…erator.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed May 2, 2017
1 parent 3df4293 commit a72b58c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
@@ -0,0 +1,89 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2017 the original authors or authors.
*
* Licensed 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.
*/

package io.sarl.maven.docs.bugs;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.arakhne.afc.vmutil.FileSystem;
import org.arakhne.afc.vmutil.URISchemeType;
import org.eclipse.xtext.xbase.lib.Pure;

/** Functions that are missed in {@link FileSystem}, but planned to be added.
*
* <p>Each function inside this class is marked with the AFC issue that is associated to.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
public final class FileSystemAddons {

private FileSystemAddons() {
//
}

/** Replies an URL for the given file and translate it into a
* resource URL if the given file is inside the classpath.
*
* @param file is the filename to translate.
* @param allowRelativePath indicate if a relative path is allowed in the returned URL.
* @return the URL which is corresponding to file, or <code>null</code> if
* the url cannot be computed.
* @see "https://github.com/gallandarakhneorg/afc/issues/173"
*/
@Pure
public static URL convertFileToURL(File file, boolean allowRelativePath) {
if (file == null) {
return null;
}
try {
File thefile = file;
if (FileSystem.isWindowsNativeFilename(file.toString())) {
thefile = FileSystem.normalizeWindowsNativeFilename(file.toString());
if (thefile == null) {
thefile = file;
}
}
final URL url;
if (thefile.isAbsolute() || !allowRelativePath) {
url = thefile.toURI().toURL();
} else {
final String[] elements = FileSystem.split(thefile);
final StringBuilder path = new StringBuilder();
for (final String element : elements) {
if (path.length() > 0) {
path.append(FileSystem.URL_PATH_SEPARATOR);
}
path.append(element);
}
url = new URL(URISchemeType.FILE.name().toLowerCase(), null, path.toString());
}
return FileSystem.toShortestURL(url);
} catch (MalformedURLException e) {
return null;
}
}

}
Expand Up @@ -56,6 +56,7 @@
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IntegerRange;

import io.sarl.maven.docs.bugs.FileSystemAddons;
import io.sarl.maven.docs.parser.AbstractMarkerLanguageParser;
import io.sarl.maven.docs.parser.DynamicValidationComponent;
import io.sarl.maven.docs.parser.DynamicValidationContext;
Expand Down Expand Up @@ -443,7 +444,7 @@ protected String transformLinks(String content) {
final String extension = FileSystem.extension(filename);
if (isMarkdownFileExtension(extension)) {
filename = FileSystem.replaceExtension(filename, ".html"); //$NON-NLS-1$
replacements.put(it.getUrl(), filename.toString());
replacements.put(it.getUrl(), FileSystemAddons.convertFileToURL(filename, true).getPath());
}
}
}));
Expand Down

0 comments on commit a72b58c

Please sign in to comment.