aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/BrowserApp.java
blob: 521a28ffadfebfb22c442b8337a08d927fd8f48f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package ui;

import model.html.ElementNode;
import model.html.HtmlParser;
import model.html.TextNode;
import model.util.Node;

import java.nio.file.*;
import java.util.*;

/**
 * The console interface to Apus.
 */
public class BrowserApp {
    private Scanner input;

    /**
     * EFFECTS: Renders an arbitrary HTML page and arbitrary HTML input.
     */
    public BrowserApp() {
        println("apus: currently a barebones html/css renderer");
        println("please provide a path to a file (examples located in data/*):");

        String pathString = input.next();
        Path path = Path.of(pathString);
        try {
            String file = Files.readString(path);
            HtmlParser parser = new HtmlParser();
            renderHtml(parser.parseHtml(file));
            println("Page rendered. Input raw HTML to add Nodes.");
            parser = new HtmlParser();
            String rawHtml = input.next();
            renderHtml(parser.parseHtml(file + rawHtml));
        } catch (Exception e) {
            println("Reading from the file failed with " + e.toString());
        }
    }

    /**
     * EFFECTS: Barebones HTML rendering. Iterates through a list of Nodes and their children and prints any text.
     */
    private void renderHtml(ArrayList<Node> html) {
        for (Node node: html) {
            if (node instanceof TextNode) {
                println(node.getData());
            } else {
                renderHtml(((ElementNode) node).getChildren());
            }
        }
    }

    private void print(String toPrint) {
        System.out.print(toPrint);
    }

    private void println(String toPrint) {
        System.out.println(toPrint);
    }

}