Package br.com.caelum.tubaina.builder

Source Code of br.com.caelum.tubaina.builder.BookPartsBuilder

package br.com.caelum.tubaina.builder;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import br.com.caelum.tubaina.BookPart;
import br.com.caelum.tubaina.Chapter;
import br.com.caelum.tubaina.SectionsManager;
import br.com.caelum.tubaina.chunk.IntroductionChunk;
import br.com.caelum.tubaina.resources.Resource;

public class BookPartsBuilder {

    private final Logger LOG = Logger.getLogger(BookPartsBuilder.class);

    List<BookPart> bookParts;

    private BookPartParametersExtractor extractor;

  private final SectionsManager sectionsManager;

    public BookPartsBuilder(SectionsManager sectionsManager) {
        this.sectionsManager = sectionsManager;
    bookParts = new ArrayList<BookPart>();
        Pattern.compile("(?i)(?s)(?m)^\\[part\\s+\"(.*?)\"(.*?)\\].*?");
        extractor = new BookPartParametersExtractor();
    }

    //TODO: why do we send introductionTex and introductionChunk?
    public BookPartsBuilder addPartFrom(String text) {
        if (containsPartTag(text)) {
            String bookPartTitle = extractPartBookTitle(text);
            List<Resource> partResources = new ArrayList<Resource>();

            String introductionText = extractIntroduction(text);
            String illustrationPath = extractIllustrationPath(text);
            IntroductionChunk introChunk = new IntroductionChunk(new ChunkSplitter(partResources,
                    "all", sectionsManager).splitChunks(introductionText));

            bookParts.add(new BookPart(bookPartTitle, true, introductionText, introChunk,
                    illustrationPath, partResources));
            LOG.info("Parsing part: " + bookPartTitle);
        }
        return this;
    }

    private String extractIntroduction(String text) {
        Pattern introductionPattern = Pattern.compile("(?i)(?s)\\[part.*?\\](.*?)(\\[chapter|\\z)");
        Matcher introductionMatcher = introductionPattern.matcher(text);

        String introduction = "";
        if (introductionMatcher.find())
            introduction = introductionMatcher.group(1);
        introduction = introduction.trim();
        return introduction;
    }

    public List<BookPart> build() {
        return bookParts;
    }

    public BookPartsBuilder addChaptersToLastAddedPart(List<Chapter> parsedChapters) {
        if (bookParts.isEmpty()) {
            bookParts.add(new BookPart("", false, "", null, "", new ArrayList<Resource>()));
        }
        int lastPartIndex = bookParts.size() - 1;
        bookParts.get(lastPartIndex).addAllChapters(parsedChapters);
        return this;
    }

    private String extractPartBookTitle(String text) {
        return extractor.extractTitleFrom(text);
    }

    private String extractIllustrationPath(String text) {
        return extractor.extractIllustrationPathFrom(text);
    }

    private boolean containsPartTag(String text) {
        return extractor.containsPartTag(text);
    }
}
TOP

Related Classes of br.com.caelum.tubaina.builder.BookPartsBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.