<repo_name>ninjaja/task3_composite<file_sep>/src/com/htp/matrizaev/constant/Constant.java
package com.htp.matrizaev.constant;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Constant {

    public static final String FILENAME = "./input/input.txt";
    public static final String FILETESTNAME = "input/test.txt";
    public static final Logger LOGGER = LogManager.getLogger();
    public static final String PARAGRAPH_DELIMITER = "(?sm)^[^\\s]+.*?\\.\\s*$";
    public static final String SENTENCE_DELIMITER = "(?:[^!?.]|\\.(?=\\d))+[!?.]";
    public static final String LEXEME_DELIMITER = "\\s";
    public static final String SYMBOL_DELIMITER = "\\w";
}
<file_sep>/src/com/htp/matrizaev/reader/TextReader.java
package com.htp.matrizaev.reader;


import com.htp.matrizaev.exception.TextReaderException;
import org.apache.logging.log4j.*;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

import static com.htp.matrizaev.constant.Constant.LOGGER;


public class TextReader {

    public static String readText(String filePath) throws TextReaderException {

        if (filePath == null || filePath.isEmpty() || !new File(filePath).exists()) {
            throw new TextReaderException("Invalid file path or file does not exist");
        }

        StringBuilder builder = new StringBuilder();
        try (Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) {
            stream.forEach(s -> builder.append(s).append("\n"));
        } catch (IOException e) {
            throw new TextReaderException("Cannot read the file", e);
        }
        LOGGER.log(Level.INFO, "File has been read");
        return builder.toString();
    }
}

<file_sep>/src/com/htp/matrizaev/parser/TextParser.java
package com.htp.matrizaev.parser;

import com.htp.matrizaev.entity.Component;

public class TextParser implements MainParser {

    private ParagraphParser paragraphParser;
    private Component textComposite;

    public TextParser() {
        SymbolParser symbolParser = new SymbolParser();
        LexemeParser lexemeParser = new LexemeParser(symbolParser);
        SentenceParser sentenceParser = new SentenceParser(lexemeParser);
        this.paragraphParser = new ParagraphParser(sentenceParser);
    }

    public Component getTextComposite() {
        return textComposite;
    }

    @Override
    public void parse(Component textComposite, String text) {
        if (paragraphParser !=null){
            paragraphParser.parse(textComposite, text);
        }
        this.textComposite = textComposite;
    }
}
<file_sep>/src/com/htp/matrizaev/entity/Leaf.java
package com.htp.matrizaev.entity;


public class Leaf implements Component {

    private char leaf;
    private ComponentType componentType;

    public Leaf(char leaf, ComponentType componentType) {
        this.leaf = leaf;
        this.componentType = componentType;
    }

    @Override
    public void addComponent(Component component) {

    }

    @Override
    public ComponentType defineType() {
        return componentType;
    }

    @Override
    public String toString() {
        return String.valueOf(leaf);
    }
}
<file_sep>/src/com/htp/matrizaev/composer/Composer.java
package com.htp.matrizaev.composer;

import java.util.List;

public class Composer {

    List<String> listToCompose;

    public String composeText(List<String> list) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            builder.append(list.get(i));
        }
        return builder.toString();
    }

}
<file_sep>/src/com/htp/matrizaev/parser/MainParser.java
package com.htp.matrizaev.parser;

import com.htp.matrizaev.entity.Component;

public interface MainParser {

    void parse(Component component, String text);

}
<file_sep>/src/com/htp/matrizaev/test/Main.java
package com.htp.matrizaev.test;

import com.htp.matrizaev.entity.Component;
import com.htp.matrizaev.entity.ComponentType;
import com.htp.matrizaev.entity.Composite;
import com.htp.matrizaev.exception.TextReaderException;
import com.htp.matrizaev.parser.TextParser;
import com.htp.matrizaev.reader.TextReader;

import static com.htp.matrizaev.constant.Constant.FILENAME;

public class Main {

    public static void main(String[] args) throws TextReaderException {

        String text = TextReader.readText(FILENAME);
        Component composite = new Composite(ComponentType.TEXT);
        TextParser textParser = new TextParser();

        textParser.parse(composite, text);
        composite = textParser.getTextComposite();
        String result = composite.toString();

        System.out.println(result);
    }
}
<file_sep>/test/com/htp/matrizaev/entity/CompositeTest.java
package com.htp.matrizaev.entity;

import com.htp.matrizaev.exception.TextReaderException;
import com.htp.matrizaev.parser.TextParser;
import com.htp.matrizaev.reader.TextReader;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import static com.htp.matrizaev.constant.Constant.FILETESTNAME;
import static org.testng.Assert.*;

public class CompositeTest {

    private TextParser textParser;
    private Component textComposite;
    private String text;

    @BeforeTest
    public void setUp() {
        textParser = new TextParser();
        textComposite = new Composite(ComponentType.TEXT);
    }

    @AfterTest
    public void tearDown() {
        textParser = null;
        textComposite = null;
        text = null;
    }

    @Test
    public void test() {
        String expected = "    Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
        String actual = null;

        try {
            text = TextReader.readText(FILETESTNAME);
            textParser.parse(textComposite, text);
            textComposite = textParser.getTextComposite();
            actual = textComposite.toString();
        } catch (TextReaderException e) {
            fail();
        }
        Assert.assertEquals(actual, expected);
    }
}
