Package Torello.HTML.Tools.NewsSite
Class ToHTML
- java.lang.Object
-
- Torello.HTML.Tools.NewsSite.ToHTML
-
public class ToHTML extends java.lang.Object
ToHTML - Documentation.
This class provides only one method. The method converts theSerialized Object
HTML data-files that have been generated by theScrapeArticles.download(...)
method intotext/HTML
snippets ('.html'
files)
Hi-Lited Source-Code:
- View Here: Torello/HTML/Tools/NewsSite/ToHTML.java
- Open New Browser-Tab: Torello/HTML/Tools/NewsSite/ToHTML.java
Stateless Class:
This class neither contains any program-state, nor can it be instantiated.
The
The
@StaticFunctional
Annotation may also be called 'The Spaghetti Report'- 1 Constructor(s), 1 declared private, zero-argument constructor
- 1 Method(s), 1 declared static
- 1 Field(s), 1 declared static, 1 declared final
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method static void
convert(String inputDir, String outputDir, boolean cleanIt, HTMLModifier modifyOrRetrieve, Appendable log)
-
-
-
Method Detail
-
convert
public static void convert(java.lang.String inputDir, java.lang.String outputDir, boolean cleanIt, HTMLModifier modifyOrRetrieve, java.lang.Appendable log) throws java.io.IOException
This method is a 'convenience' method that depicts how to convert the data-files that are generated by theScrapeArticles.download(...)
method into partial HTML files whose images have been localized. This method performs two primary operations:- Retrieves
'.vdat'
files from the directory where thedownload(...)
method from this class left the web-site article page data-files. Uses standard java object de-serialization to load the HTML page-Vector<HTMLNode>
into memory, and saves this files as standard.html
text-files
- Invokes the
ImageScraper.localizeImages
method to download any images that are present on the web-page to the local directory, and replaces the HTML<IMG SRC=...>
links with the downloaded-image local file-name.
- Parameters:
inputDir
- This parameter should contain the name of the directory that used with the methoddownload(...)
from this class. This directory must exist and it must contain the'.dat'
files generated by thedownload
method.outputDir
- This parameter should contain the name of the directory where the expanded and de-serialized'.html'
files will be stored, along with their downloaded images.cleanIt
- When this parameter is set to TRUE, then the some HTML information will be stipped from the HTML that is saved to disk. This can be a great benefit if these sections make reading the output HTML more readable, without loss of information. Set this parameter to FALSE to skip this 'cleaning' step. These HTML Elements are removed if requested:<SCRIPT>...</SCRIPT>
blocks are removed<STYLE>...</STYLE>
blocks are removed'class=...'
and'id=...'
HTML Element Attributes are stripped
modifyOrRetrieve
- ThisFunctional Interface
allows a user to pass a method or a lambda-expression that performs customized "Clean Up" of the NewspaperArticle's
. Customized clean up could be anything from removing advertisements to extracting the Author's Name and Article Data and placing it somewhere - up to and including removing links such as "Post to Twitter" or "Post to Facebook" thumbnails.
NULLABLE: This parameter may be null, and if it is it shall be ignored. Just to be frank, theArticleGet
that is used to retrieve theArticle
-body HTML could just as easily be used to perform any needed cleanup on the news-paper articles. Having an additional window here is only provided for convenience - although perhaps it makes this part of the class look more complicated than it needs to be.
NOTE: Once a good understanding of how the classes and methods in thepackage HTML.NodeSearch
is attained, using those methods to move and update or modify HTML becomes second-nature. Cleaning up large numbers of newspaper articles to get rid of the "View Related Articles" links-portion of the page, or banners at the top that say "Send Via E-Mail" and "Pin to Pinterest" takes on the order of 5 lines of code.
ALSO: Another good use for thisFunctional Interface
would be to extract data that is inside HTML<SCRIPT> ... </SCRIPT>
tags. There might be additional images or article "Meta Data" (author, title, date, reporter-name, etc..) that the programmer might consider important - and would need to be parsed using aJSON
parser which is freely available for download on the internet as well. Sun / Oracle provides two good versions of aJSON Parser
- one underAndroid Developer
and one in the Standard JDK filed under thejavax.json
packages.log
- Output text is sent to this log. This parameter may be null, and if it is, it shall be ignored. If this program is running on UNIX, color-codes will be included in the log data. This parameter expects an implementation of Java'sinterface java.lang.Appendable
which allows for a wide range of options when logging intermediate messages.Class or Interface Instance Use & Purpose 'System.out'
Sends text to the standard-out terminal Torello.Java.StorageWriter
Sends text to System.out
, and saves it, internally.FileWriter, PrintWriter, StringWriter
General purpose java text-output classes FileOutputStream, PrintStream
More general-purpose java text-output classes
IMPORTANT: Theinterface Appendable
requires that the check exceptionIOException
must be caught when using itsappend(CharSequence)
methods.- Throws:
java.io.IOException
- If there any I/O Exceptions when writing image files to the file-system, then this exception will throw.- Code:
- Exact Method Body:
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
if (log !=null) log.append( "\n" + C.BRED + "*****************************************************************************************\n" + "*****************************************************************************************\n" + C.RESET + " Converting Vector<HTMLNode> to '.html' files, and downloading Pictures." + C.BRED + "\n" + "*****************************************************************************************\n" + "*****************************************************************************************\n" + C.RESET + '\n' ); if (! outputDir.endsWith(File.separator)) outputDir = outputDir + File.separator; // Uses the FileNode class to build an iterator of all '.dat' files that are found in the // 'inputDir' directory-parameter. Iterator<FileNode> iter = FileNode .createRoot(inputDir) .loadTree() .getDirContentsFiles (RetTypeChoice.ITERATOR, (FileNode fn) -> fn.name.endsWith(".dat")); // Iterate through each of the data-files. while (iter.hasNext()) try { // Retrieve next article, using the iterator FileNode fn = iter.next(); // Load the instance of 'Article' into memory, using Object De-Serialization Article page = FileRW.readObjectFromFileNOCNFE(fn.toString(), Article.class, true); // If there are customized modifications to the page (or retrieval operations) // that were requested, they are done here. if (modifyOrRetrieve != null) { // Retrieves the section-number and article-number from file-name Matcher m = P1.matcher(fn.toString()); // These will be set to -1, and if the directoryName/fileName did not use the // standard "factory-generated" file-save, then these will STILL BE -1 when // passed to the modifier lambda. int sectionNum = -1; int articleNum = -1; if (m.find()) { sectionNum = Integer.parseInt(m.group(1)); articleNum = Integer.parseInt(m.group(2)); } // pass the articleBody (and it's URL and filename) to the customized // HTML Modifier provided by the user who called this method modifyOrRetrieve.modifyOrRetrieve (page.articleBody, page.url, sectionNum, articleNum); } // We need to build a "Sub-Directory" name for the HTML page where the download // images will be stored int dotPos = fn.name.lastIndexOf("."); String outDirName = outputDir + fn.name.substring(0, dotPos).replace("\\.", "/") + '/'; // Make sure the subdirectory exists. new File(outDirName).mkdirs(); // This process may be skipped, but it makes the output HTML much cleaner and more // readable for most Internet News Web-Sites. Both <SCRIPT>, <!-- --> elements are removed // Also, any "class" or "id" fields are eliminated. This "cleaning" can be easily skipped if (cleanIt) { Util.removeScriptNodeBlocks(page.articleBody); Util.removeStyleNodeBlocks(page.articleBody); Util.removeAllCommentNodes(page.articleBody); Attributes.remove(page.articleBody, "class", "id"); } if (log != null) log.append("Writing Page: " + C.BGREEN + fn.name + C.RESET + '\n'); // 'Localize' any images available. 'localizing' an HTML web-page means downloading // the image data, and saving it to disk. AdditionalParameters ap = new AdditionalParameters(); ImageScraper.localizeImages(page.articleBody, page.url, log, ap, outDirName); // If there were any images available, they were downloaded and localized. The // Write the (updated) HTML to an '.html' text-file. FileRW.writeFile(Util.pageToString(page.articleBody), outDirName + "index.html"); } // NOTE: The "ImageScraper" spawns a (very) small "monitor thread" that ensures that // downloading does not "hang" the system by aborting image-downloads that take longer // than 10 seconds. It is necessary to shut-down these threads on system exit, because // if they are not shutdown, when a java program terminates, the operating system that // the program is using (the terminal window) will appear to "hang" or "freeze" until // the extra-thread is shut-down by the JVM. This delay can be upwards of 30 seconds. catch (IOException ioe) { ImageScraper.shutdownTOThreads(); throw ioe; } catch (Exception e) { ImageScraper.shutdownTOThreads(); throw new IOException( "There was a problem converting the html pages. See exception.getCause() for more details.", e ); } // Exit the method. Again, shutdown the Time-Out "monitor" thread. ImageScraper.shutdownTOThreads();
- Retrieves
-
-