ParseBodyStory.java

  1. /*******************************************************************************
  2.  * Copyright (C) 2020 Ram Sadasiv
  3.  *
  4.  * This program is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation, either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  ******************************************************************************/
  17. package io.outofprintmagazine.corpus.batch.impl.gutenberg;

  18. import java.io.IOException;

  19. import org.apache.commons.codec.digest.DigestUtils;
  20. import org.apache.logging.log4j.LogManager;
  21. import org.apache.logging.log4j.Logger;
  22. import org.jsoup.nodes.Document;
  23. import org.jsoup.nodes.Element;
  24. import org.jsoup.parser.Parser;
  25. import org.jsoup.select.Elements;

  26. import com.fasterxml.jackson.databind.node.ArrayNode;
  27. import com.fasterxml.jackson.databind.node.ObjectNode;

  28. import io.outofprintmagazine.corpus.batch.CorpusBatchStep;
  29. import io.outofprintmagazine.corpus.batch.ICorpusBatchStep;


  30. public class ParseBodyStory extends CorpusBatchStep implements ICorpusBatchStep {
  31.    
  32.     private static final Logger logger = LogManager.getLogger(ParseBodyStory.class);

  33.     @SuppressWarnings("unused")
  34.     private Logger getLogger() {
  35.         return logger;
  36.     }
  37.        
  38.     public ParseBodyStory() {
  39.         super();
  40.     }
  41.    
  42.     @Override
  43.     public ObjectNode getDefaultProperties() {
  44.         ObjectNode properties = getMapper().createObjectNode();
  45.         properties.put("selector", "body");
  46.         return properties;
  47.     }
  48.    
  49.     @Override
  50.     public ArrayNode runOne(ObjectNode inputStepItem) throws Exception {

  51.         ArrayNode retval = getMapper().createArrayNode();
  52.         ObjectNode outputStepItem = copyInputToOutput(inputStepItem);
  53.         Document doc = getJsoupDocumentFromStorageNormalized(inputStepItem);

  54.         boolean inStory = false;
  55.         StringBuffer buf = new StringBuffer();
  56.         Elements paragraphs = doc.selectFirst(
  57.             getData().getProperties().get("selector").asText()
  58.         ).children();

  59.         for (Element paragraph : paragraphs) {
  60.             if (paragraph.selectFirst(inputStepItem.get("oop_Text").asText()) != null) {
  61.                 inStory = true;

  62.             }  
  63.             else if (inStory && inputStepItem.get("oop_TextNext").asText().length() > 0 && paragraph.selectFirst(inputStepItem.get("oop_TextNext").asText()) != null) {
  64.                 try {
  65.                     setStorageLink(
  66.                             getStorage().storeScratchFileString(
  67.                                 getData().getCorpusId(),
  68.                                 getOutputScratchFilePath(
  69.                                         getTitle(inputStepItem),
  70.                                         "txt"
  71.                                 ),
  72.                                 buf.toString().trim()
  73.                             ),
  74.                             outputStepItem
  75.                     );
  76.                 }
  77.                 catch (IOException ioe) {
  78.                     setStorageLink(
  79.                             getStorage().storeScratchFileString(
  80.                                 getData().getCorpusId(),
  81.                                 getOutputScratchFilePath(
  82.                                         DigestUtils.md5Hex(
  83.                                                 getTitle(inputStepItem)
  84.                                         ),
  85.                                         "txt"
  86.                                 ),
  87.                                 buf.toString().trim()
  88.                             ),
  89.                             outputStepItem
  90.                     );
  91.                 }

  92.                 retval.add(outputStepItem);
  93.                 inStory = false;
  94.                 break;
  95.             }
  96.             else if (inStory) {
  97.                 if (paragraph.tagName().equalsIgnoreCase("p")) {
  98.                     buf.append(Parser.unescapeEntities(paragraph.text().trim(), false));
  99.                     buf.append('\n');
  100.                     buf.append('\n');
  101.                 }
  102.             }
  103.         }


  104.         if (buf.length() > 0 && inStory) {
  105.             try {
  106.                 setStorageLink(
  107.                         getStorage().storeScratchFileString(
  108.                             getData().getCorpusId(),
  109.                             getOutputScratchFilePath(
  110.                                     getTitle(inputStepItem),
  111.                                     "txt"
  112.                             ),
  113.                             buf.toString().trim()
  114.                         ),
  115.                         outputStepItem
  116.                 );
  117.             }
  118.             catch (IOException ioe) {
  119.                 setStorageLink(
  120.                         getStorage().storeScratchFileString(
  121.                             getData().getCorpusId(),
  122.                             getOutputScratchFilePath(
  123.                                     DigestUtils.md5Hex(
  124.                                             getTitle(inputStepItem)
  125.                                     ),
  126.                                     "txt"
  127.                             ),
  128.                             buf.toString().trim()
  129.                         ),
  130.                         outputStepItem
  131.                 );
  132.             }
  133.             retval.add(outputStepItem);
  134.         }
  135.         return retval;
  136.     }
  137. }