aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/BrowserApp.java
blob: 520cac8126ae8179caf2628eee06019ffb76513b (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package ui;

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

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

/**
 * The console interface to Apus.
 */
public class BrowserApp {
    private Scanner input;
    private static final String border = "===============================================";
    private String pathString;
    private ArrayList<Node> parsed;
    private ArrayDeque<String> tabs;

    /**
     * EFFECTS: Renders an arbitrary HTML page and arbitrary HTML input.
     */
    public BrowserApp() {
        println("apus: currently a barebones html/css renderer");
        this.input = new Scanner(System.in);
        this.tabs = new ArrayDeque<>();
        mainLoop();
    }

    /**
     * EFFECTS: Runs the main loop
     */
    private void mainLoop() {
        while (true) {
            try {
                Path path = Paths.get(pathString);
                String file = new String(Files.readAllBytes(path));
                HtmlParser parser = new HtmlParser();
                parsed = parser.parseHtml(file);
                println(border);
                renderHtml(parsed);
                println(border);
                println("Page rendered. Input additional commands if desired.");
                println("Impemented commands: newuri, newtab, nexttab, quit");
                handleInput(this.input.next());
                println(border);
            } catch (Exception e) {
                println("Reading from the file failed with " + e.toString());
                println("Please try again.");
            }
        }
    }

    /**
     * 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());
            }
        }
    }

    /**
     * EFFECTS: Handles user input after rendering an initial site
     */
    private void handleInput(String input) {
        switch (input) {
            case "newuri":
                println("please provide a path to a file (examples located in data/*):");
                pathString = this.input.next();
                break;
            case "newtab":
                this.tabs.add(pathString);
                println("please provide a path to a file (examples located in data/*):");
                pathString = this.input.next();
                break;
            case "nexttab":
                this.tabs.add(pathString);
                pathString = this.tabs.removeFirst();
                break;
            case "quit":
                System.exit(0);
                break;
            default:
                println("Sorry, I didn't quite get that. Please try again.");
                break;
        }
    }

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

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

}