Interface ScrapedArticleReceiver

  • Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @FunctionalInterface
    public interface ScrapedArticleReceiver
    ScrapedArticleReceiver - Documentation.

    This interface is a FunctionalInterface, meaning it has only one method which must be written to satisfy the requirements of implementing the interface. The method in this FunctionalInterface is here to receive Article's that have been downloaded by the class ScrapeArticles - allowing the programmer to decide where, how and when to save those Article's.

    NOTE: There is a simple, standardized factory-builder method in this interface that may be used if the programmer is satisfied with saving downloaded Article classes to a simple directory on the file-system.



    • Method Detail

      • receive

        void receive​(Article articleBody,
                     int sectionURLNum,
                     int articleNum)
              throws ReceiveException
        FUNCTIONAL-INTERFACE METHOD: This is the method that must be fulfilled to meet the requirements of this FunctionalInterface
        Parameters:
        articleBody - After an article has been downloaded by the ScrapeArticles class, it will build an instance of class Article and pass it to this class. It is the programmer's responsibility to ultimately decide what to do with news articles after they have been successfully scraped and parsed.
        sectionURLNum - This is a convenience parameter that informs the implementing-interface from which URL Section of the News Web-Sites main-page that this article is being downloaded.

        NOTE: Review the class ScrapeURLs to read more about "Section URL's." The number of "Section URL's" for a news web-site scrape is just the length of the Vector<URL> sectionURL's
        articleNum - This parameter informs the implementing-interface which article number is being downloaded. Each section-URL will have a number of different articles in the section. These numbers can be used to create unique file-names, for instance.
        Throws:
        ReceiveException - This exception may be thrown by the lambda-expression or class instance that implements this FunctionalInterface. It is not mandatory that this exception be used.
      • saveToFS

        static ScrapedArticleReceiver saveToFS​(java.lang.String dirNameStr)
        saveToFS => Save To File-System

        This is a static factory-builder method that will produce a 'ScrapedArticleReceiver' that simply saves downloaded articles to a directory on the file-system. The user, here, merely needs to provide a directory-name using parameter String dirNameStr.

        NOTE: This is the most simple way to create an instance of this class.
        Parameters:
        dirNameStr - This is a directory on the file-system that will be used to save articles that are received directly to the file-system.
        Throws:
        WritableDirectoryException - This method shall check whether it is possible to write to the provided directory name.
        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
         WritableDirectoryException.check(dirNameStr);
        
         // Make sure that the directory name-string ends with the system File-Separator
         // character.  This '/' for UNIX and '\' for MS-DOS.
         final String finalDirNameStr = dirNameStr.endsWith(File.separator) 
                                             ? dirNameStr
                                             : dirNameStr + File.separator;
        
         // Create an instance of this functional-interface using a lambda-expression.
         // NOTE: This is literally just saving an object to a file using object-serialization.
         //       The exception catching / throwing is just to produce standardized error messages
         //       back to the user, if an exception occurs when saving files.
         return (Article articleBody, int sectionURLNum, int articleNum) ->
         {
            String outFileName = finalDirNameStr +
                                     StringParse.zeroPad(sectionURLNum) + '.' +
                                     StringParse.zeroPad10e4(articleNum) + ".dat";
        
             try
                 { FileRW.writeObjectToFile(articleBody, outFileName, true); }
             catch (IOException ioe)
             {
                 throw new ReceiveException(
                     "There is a java.io.IOException when attempting to write a downloaded article to the " +
                     "file-system.  Section-URL [" + sectionURLNum + "], Article Number [" + articleNum + "]." +
                     "Could not save file:\n" +
                     outFileName + "\n" + 
                     "to the file-system.  Please review this exception's getCause() for more details.",
                     ioe, sectionURLNum, articleNum
                 );
             }
             catch (Exception ex)
             {
                 throw new ReceiveException(
                     "There is an unknown exception when attempting to write a downloaded article to the " +
                     "file-system.  Section-URL [" + sectionURLNum + "], Article Number [" + articleNum + "]." +
                     "Could not save file:\n" +
                     outFileName + "\n" + 
                     "to the file-system.  Please review this exception's getCause() for more details.",
                     ex, sectionURLNum, articleNum
                 );
             }
         };