X

Geertjan's Blog

  • July 8, 2007

Link Checking and Iterating Through Folders

Geertjan Wielenga
Product Manager
My link check feature now works mostly in the way that I would expect it to. From the Tools menu on folder and file nodes, I can choose 'Link Check' and then the file, or all files within the folder, are checked. Broken links are written to the Output window and an annotation is attached to the line in the editor:

Above, the link check is done on the high-level 'docs' folder, which contains all the subfolders of my help system. In other words, I am now able to perform a link check once, for all HTML files in the system, instead of having to do a link check on lower-level folders or, even worse, on document-level only. This high-level link check feature will come in useful while I write help topics for the upcoming 6.0 release! Just need to add a progress bar, as a finishing touch.

The most interesting thing I learned was how to iterate through folders. In the method below, some FileObject is received. If it is a folder, the content is examined for other folders, in which case the found folders are returned to the same method. Once we get to a file, the file's MIME type is checked. If it is an HTML file, it is processed elsewhere for broken links. Here it is:

public void findAllHtmlFilesAndCheckThem(FileObject fo) {
if (fo.isFolder()) {
try {
//Get data folder:
DataFolder dFo = (DataFolder) DataObject.find(fo);
//Get a way to access the content of the folder:
DataObject[] dObjs = null;
dObjs = dFo.getChildren();
//Iterate through all the content:
for (int i = 0; i < dObjs.length; i++) {
//Get one object via its data object:
DataObject dataObject = dObjs[i];
//If the file object is a folder:
if (dataObject.getPrimaryFile().isFolder()) {
//Return the file object to the same method:
findAllHtmlFilesAndCheckThem(dataObject.getPrimaryFile());
//If we are not dealing with a folder, continue with the else:
} else {
//Now, after making sure that we're dealing with an HTML file,
//we send the file on for link checking:
if (dataObject.getPrimaryFile().getMIMEType().equals("text/html")) {
...do link check on HTML files...
}
}
}
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} catch (SAXException ex) {
Exceptions.printStackTrace(ex);
}
} else {
//If we're not dealing with a folder,
//we're dealing with a file, here
//we deal with the file:
if (fo.getMIMEType().equals("text/html")) {
...do link check on HTML files...
}
}
}

I think the above method could be considerably more compact, which is a fun activity to spend time on in between other things.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.