Created
March 15, 2016 21:03
-
-
Save jmini/7358488073cb12e09a80 to your computer and use it in GitHub Desktop.
Java Main class to verify that local HTML links point to existing files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.File; | |
| import java.io.FileFilter; | |
| import java.io.FilenameFilter; | |
| import org.jsoup.Jsoup; | |
| import org.jsoup.nodes.Document; | |
| import org.jsoup.nodes.Element; | |
| import org.jsoup.select.Elements; | |
| import com.google.common.base.Charsets; | |
| import com.google.common.io.Files; | |
| /** | |
| * @author jbr | |
| */ | |
| public class CheckLink { | |
| private static final FileFilter DIRECTORY_FILEFILTER = new FileFilter() { | |
| @Override | |
| public boolean accept(File f) { | |
| return f.isDirectory(); | |
| } | |
| }; | |
| private static final FilenameFilter HTML_FILENAMEFILTER = new FilenameFilter() { | |
| @Override | |
| public boolean accept(File dir, String name) { | |
| return name.endsWith(".html"); | |
| } | |
| }; | |
| public static void main(String[] args) throws Exception { | |
| File root = new File("C:/Users/jbr/git/n4js"); | |
| handleFolder(root); | |
| } | |
| /** | |
| * @param f | |
| * @throws Exception | |
| */ | |
| private static void checkLinks(File file) throws Exception { | |
| String html = Files.toString(file, Charsets.UTF_8); | |
| Document doc = Jsoup.parse(html); | |
| Elements aElements = doc.getElementsByTag("a"); | |
| for (Element e : aElements) { | |
| String href = e.attr("href"); | |
| if (shouldFixLink(href)) { | |
| if (!href.startsWith("#")) { | |
| int i = href.lastIndexOf("#"); | |
| String path; | |
| if (i > 0) { | |
| path = href.substring(0, i); | |
| } | |
| else { | |
| path = href; | |
| } | |
| File otherFile = new File(file.getParentFile(), path); | |
| if (!otherFile.exists()) { | |
| System.out.println("In file " + file.getAbsolutePath() + " there is a link to '" + href + "' that can not be found"); | |
| } | |
| } | |
| } | |
| } | |
| System.out.println("Checked " + file.getAbsolutePath() + "."); | |
| } | |
| static boolean shouldFixLink(String href) { | |
| return href != null && !href.isEmpty() && !href.matches("(mailto\\:|[a-zA-Z]+\\://|//).+"); | |
| } | |
| /** | |
| * @param root | |
| * @throws Exception | |
| */ | |
| private static void handleFolder(File folder) throws Exception { | |
| File[] files = folder.listFiles(HTML_FILENAMEFILTER); | |
| for (File f : files) { | |
| checkLinks(f); | |
| } | |
| File[] folders = folder.listFiles(DIRECTORY_FILEFILTER); | |
| if (folders != null) { | |
| for (File f : folders) { | |
| handleFolder(f); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment