All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.ac.shef.dcs.sti.parser.table.TableParserReverbnation Maven / Gradle / Ivy

package uk.ac.shef.dcs.sti.parser.table;

import org.apache.any23.extractor.html.DomUtils;
import org.apache.any23.extractor.html.TagSoupParser;
import org.apache.commons.io.FileUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import uk.ac.shef.dcs.sti.STIException;
import uk.ac.shef.dcs.sti.core.model.Table;
import uk.ac.shef.dcs.sti.core.model.TContext;
import uk.ac.shef.dcs.sti.parser.table.creator.TableObjCreatorReverbnationList;
import uk.ac.shef.dcs.sti.parser.table.hodetector.TableHODetector;
import uk.ac.shef.dcs.sti.parser.table.hodetector.TableHODetectorByHTMLTag;
import uk.ac.shef.dcs.sti.parser.table.normalizer.TableNormalizer;
import uk.ac.shef.dcs.sti.parser.table.creator.TableObjCreator;
import uk.ac.shef.dcs.sti.parser.table.context.TableContextExtractorGeneric;
import uk.ac.shef.dcs.sti.parser.table.normalizer.TableNormalizerListTransformer;
import uk.ac.shef.dcs.sti.parser.table.validator.TableValidatorGeneric;
import uk.ac.shef.dcs.sti.parser.table.validator.TableValidator;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zqz
 * Date: 31/03/14
 * Time: 15:21
 * To change this template use File | Settings | File Templates.
 */
public class TableParserReverbnation extends TableParser {
    public TableParserReverbnation() {
        super(new TableNormalizerListTransformer(),
                new TableHODetectorByHTMLTag(),
                new TableObjCreatorReverbnationList(),
                new TableValidatorGeneric());
    }

    public TableParserReverbnation(TableNormalizer normalizer, TableHODetector detector, TableObjCreator creator, TableValidator... validators) {
        super(normalizer, detector, creator, validators);
    }

    @Override
    public List extract(String inFile, String sourceId) throws STIException {
        String input;
        try {
            input = FileUtils.readFileToString(new File(inFile));
        } catch (IOException e) {
            throw new STIException(e);
        }

        List
rs = new ArrayList<>(); parser = new TagSoupParser(new ByteArrayInputStream(input.getBytes()), sourceId,"UTF-8"); Document doc = null; try { doc = parser.getDOM(); } catch (IOException e) { return rs; } List tables = DomUtils.findAll(doc, "//UL[@class='profile_songs_container']"); List contexts = new ArrayList<>(); try { contexts = new TableContextExtractorGeneric().extract(new File(sourceId), doc); } catch (STIException e) { e.printStackTrace(); } int tableCount = 0; for (Node n : tables) { tableCount++; TContext[] contexts_array = new TContext[contexts.size()]; for (int i = 0; i < contexts.size(); i++) contexts_array[i] = contexts.get(i); Table table = extractTable(n, String.valueOf(tableCount), sourceId, contexts_array); if (table != null) rs.add(table); } return rs; } }