package uebung08.doc;

public class CountWordsVisitor implements Visitor<Integer>{
    
    public Integer visit(Image image) {
        return 0;
    }

    public Integer visit(Paragraph paragraph) {
        int wordcount = 0;
        boolean isInWord = false;


        for (int i = 0; i < paragraph.getText().length(); i++) {
            if(Character.isAlphabetic(paragraph.getText().charAt(i))){
                if (!isInWord) {
                    wordcount++;
                    isInWord = true;
                }
            } else {
                isInWord = false;
            }
        }

        return wordcount;
    }

    public Integer visit(Section section) {
        int count = 0;
        for (TextComponent textComponent : section.getContent()) {
            count += textComponent.accept(this);
        }

        return count;
    }

}
