-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDOM.java
More file actions
88 lines (81 loc) · 2.42 KB
/
DOM.java
File metadata and controls
88 lines (81 loc) · 2.42 KB
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
package apps;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
//import java.util.StringTokenizer;
import structures.Tree;
public class DOM {
static Scanner stdin = new Scanner(System.in);
static String options = "hprbdaq";
static char getOption() {
System.out.print("\nChoose action: ");
System.out.print("(p)rint Tree, ");
System.out.print("(h)tml, ");
System.out.print("(r)eplace tag, ");
System.out.print("(b)oldface row, ");
System.out.print("(d)elete tag, ");
System.out.print("(a)dd tag, or ");
System.out.print("(q)uit? => ");
char response = stdin.nextLine().toLowerCase().charAt(0);
while (!options.contains(response+"")) {
System.out.print("\tYou must enter one of p, h, r, b, d, a, or q => ");
response = stdin.nextLine().toLowerCase().charAt(0);
}
return response;
}
/**
* @param args
*/
public static void main(String[] args)
throws IOException {
// TODO Auto-generated method stub
System.out.print("Enter HTML file name => ");
String htmlFile = stdin.nextLine();
Tree tree = new Tree(new Scanner(new File(htmlFile)));
tree.build();
char option;
while ((option = getOption()) != 'q') {
System.out.println();
if (option == 'h') {
System.out.print(tree.getHTML());
} else if (option == 'p') {
tree.print();
} else if (option == 'r') {
System.out.print("\tEnter old tag => ");
String oldTag = stdin.nextLine();
System.out.print("\tEnter new tag => ");
String newTag = stdin.nextLine();
tree.replaceTag(oldTag, newTag);
} else if (option == 'b') {
System.out.print("\tEnter row number (1..n) => ");
int row;
while (true) {
try {
row = Integer.parseInt(stdin.nextLine());
if (row > 0) {
break;
} else {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
System.out.print("\tYou must enter a positive integer => ");
}
}
try {
tree.boldRow(row);
} catch (IllegalArgumentException iae) {
System.out.println("\tTable does not have row " + row);
}
} else if (option == 'd') {
System.out.print("\tEnter tag to remove => ");
tree.removeTag(stdin.nextLine().trim());
} else if (option == 'a') {
System.out.print("\tEnter text to tag => ");
String text = stdin.nextLine().trim();
System.out.print("\tEnter tag => ");
String tag = stdin.nextLine().trim();
tree.addTag(text, tag);
}
}
}
}