-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.java
More file actions
419 lines (394 loc) · 11.7 KB
/
Tree.java
File metadata and controls
419 lines (394 loc) · 11.7 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package structures;
import java.util.*;
/**
* This class implements an HTML DOM Tree. Each node of the tree is a TagNode, with fields for
* tag/text, first child and sibling.
*
*/
public class Tree {
/**
* Root node
*/
TagNode root=null;
/**
* Scanner used to read input HTML file when building the tree
*/
Scanner sc;
/**
* Initializes this tree object with scanner for input HTML file
*
* @param sc Scanner for input HTML file
*/
public Tree(Scanner sc) {
this.sc = sc;
root = null;
}
/**
* Builds the DOM tree from input HTML file, through scanner passed
* in to the constructor and stored in the sc field of this object.
*
* The root of the tree that is built is referenced by the root field of this object.
*/
public void build() {
/** COMPLETE THIS METHOD **/
if(!(sc.hasNextLine())){
return;
}
root = new TagNode(removeBrackets(sc.nextLine()), null, null);
Stack<TagNode> stk = new Stack<TagNode>();
stk.push(root);
while(sc.hasNext()) {
String tagOrtext = sc.nextLine();
//System.out.println(1 + tagOrtext);
if(tagOrtext.charAt(0) == '<' && tagOrtext.charAt(1) == '/') {
//System.out.println(tagOrtext + " : closing tag");
stk.pop();
}else if(tagOrtext.charAt(0) == '<' && tagOrtext.charAt(1) != '/'){
TagNode temp = new TagNode(removeBrackets(tagOrtext), null, null);
//System.out.println(tagOrtext + " : tag");
if(stk.peek().firstChild == null) {
stk.peek().firstChild = temp;
}else {
TagNode current = stk.peek().firstChild;
while(current.sibling != null) {
current = current.sibling;
}
current.sibling = temp;
}
stk.push(temp);
}else{
//System.out.println(tagOrtext);
TagNode temptext = new TagNode(tagOrtext, null, null);
if(stk.peek().firstChild == null) {
stk.peek().firstChild = temptext;
}else {
TagNode current = stk.peek().firstChild;
while(current.sibling != null) {
current = current.sibling;
}
current.sibling = temptext;
}
}
}
}
private String removeBrackets(String s) {
return s.substring(1, s.length() - 1);
}
/**
* Replaces all occurrences of an old tag in the DOM tree with a new tag
*
* @param oldTag Old tag
* @param newTag Replacement tag
*/
public void replaceTag(String oldTag, String newTag) {
/** COMPLETE THIS METHOD **/
helperReplaceTag(this.root, oldTag, newTag);
}
private void helperReplaceTag(TagNode newRoot, String oldTag, String newTag) {
if(newRoot == null) {
return;
}
if(newRoot.tag.equals(oldTag)) {
newRoot.tag = newTag;
}
if(newRoot.firstChild != null && newRoot.sibling != null) {
helperReplaceTag(newRoot.firstChild, oldTag, newTag);
helperReplaceTag(newRoot.sibling , oldTag, newTag);
}else if(newRoot.firstChild != null) {
helperReplaceTag(newRoot.firstChild , oldTag, newTag);
}else if(newRoot.sibling != null) {
helperReplaceTag(newRoot.sibling , oldTag, newTag);
}
}
/**
* Boldfaces every column of the given row of the table in the DOM tree. The boldface (b)
* tag appears directly under the td tag of every column of this row.
*
* @param row Row to bold, first row is numbered 1 (not 0).
*/
public void boldRow(int row) {
/** COMPLETE THIS METHOD **/
boldface(row, this.root);
}
private void boldface(int row, TagNode iteration) {
if(iteration == null) {
return;
}
if(iteration.tag.equals("table")) {
//System.out.println("Hello sir I am the goat");
int count = 0;
TagNode current = iteration.firstChild;
boolean check = false;
while(current != null) {
//System.out.println("I am while loop");
if(current.tag.equals("tr")) {
count++;
}
if(count == row) {
check = true;
break;
}
current = current.sibling;
//System.out.println(current.tag);
}
if(check) {
//System.out.println("Hello sir: " + current.tag);
TagNode current2 = current.firstChild;
while(current2 != null) {
if(current2.tag.equals("td")) {
TagNode temp = new TagNode("b", current2.firstChild, null);
current2.firstChild = temp;
}
current2 = current2.sibling;
}
}else {
return;
}
}
boldface(row, iteration.firstChild);
boldface(row, iteration.sibling);
}
/**
* Remove all occurrences of a tag from the DOM tree. If the tag is p, em, or b, all occurrences of the tag
* are removed. If the tag is ol or ul, then All occurrences of such a tag are removed from the tree, and,
* in addition, all the li tags immediately under the removed tag are converted to p tags.
*
* @param tag Tag to be removed, can be p, em, b, ol, or ul
*/
public void removeTag(String tag) {
/** COMPLETE THIS METHOD **/
if(tag == null || tag.equals("")) {
return;
}
int num = numberOfTags(tag, this.root);
if(tag.equals("p") || tag.equals("em") || tag.equals("b") || tag.equals("ol") || tag.equals("ul")) {
for(int i = 0; i < num ; i++) {
tagRemoval(tag, this.root.firstChild, this.root);
}
}
}
private void tagRemoval(String tag, TagNode target, TagNode parent) {
if(parent == null || target == null) {
return;
}
if(target.tag.equals(tag)) {
if(target.tag.equals("ul") || target.tag.equals("ol")) {
TagNode current = target.firstChild;
while(current != null) {
if(current.tag.equals("li")) {
current.tag = "p";
}
current = current.sibling;
}
}
if(target == parent.firstChild) {
parent.firstChild = target.firstChild;
TagNode current = target.firstChild;
while(current.sibling != null) {
current = current.sibling;
}
current.sibling = target.sibling;
}else if(target == parent.sibling){
TagNode current = target.firstChild;
while(current.sibling != null) {
current = current.sibling;
}
current.sibling = target.sibling;
parent.sibling = target.firstChild;
}
return;
}
parent = target;
tagRemoval(tag, target.firstChild, parent);
tagRemoval(tag, target.sibling, parent);
}
private int numberOfTags(String tag, TagNode root) {
if(root == null) {
return 0;
}
if(root.tag.equals(tag)) {
return 1 + numberOfTags(tag, root.firstChild)+ numberOfTags(tag, root.sibling);
}else {
return 0 + numberOfTags(tag, root.firstChild)+ numberOfTags(tag, root.sibling);
}
}
/**
* Adds a tag around all occurrences of a word in the DOM tree.
*
* @param word Word around which tag is to be added
* @param tag Tag to be added
*/
public void addTag(String word, String tag) {
/** COMPLETE THIS METHOD **/
if(word.equals("") || word == null || tag == null || tag.equals("")) {
return;
}
if(tag.equals("em") || tag.equals("b")) {
addTagRec(word, tag, this.root);
}
}
private void addTagRec(String word, String tag, TagNode root) {
if(root == null) {
return;
}
TagNode trackRoot = root;
TagNode trackIt = root.sibling;
if(root.firstChild != null && root.firstChild.firstChild == null) {
TagNode parent = root;
TagNode saveSibling = root.firstChild.sibling;
root = recAddTag(root.firstChild.tag, tag, word);
parent.firstChild = root;
TagNode prev = null;
TagNode current = root;
while(current.sibling != null) {
prev = current;
current = current.sibling;
}
if(prev != null && prev.sibling != null && prev.sibling.tag == null) {
prev.sibling = saveSibling;
root = prev;
}else {
current.sibling = saveSibling;
root = current;
}
addTagRec(word, tag, parent.sibling);
}
if(trackIt != null && trackIt.firstChild == null) {
TagNode parent = trackRoot;
TagNode saveSibling = trackIt.sibling;
trackIt = recAddTag(trackIt.tag, tag, word);
parent.sibling = trackIt;
TagNode prev = null;
TagNode current = trackIt;
while(current.sibling != null) {
prev = current;
current = current.sibling;
}
if(prev != null && prev.sibling != null && prev.sibling.tag == null) {
prev.sibling = saveSibling;
trackRoot = prev;
}else {
current.sibling = saveSibling;
trackRoot = current;
}
}
addTagRec(word, tag, root.firstChild);
addTagRec(word, tag, root.sibling);
}
private boolean checkPunc(char ch){
return ((ch == '!') || (ch == ':') || (ch == ',') || (ch == '.') || (ch == '?')|| (ch == ';'));
}
private boolean indexC(String str, String target, int index) {
if(str.length() == index + target.length() || ((str.length() > index + target.length())
&& (str.charAt(index + target.length()) == ' '))) {
if(index > 0 && str.charAt(index - 1) == ' ') {
return true;
}else if(index == 0) {
return true;
}
}
if((str.length() >= index + target.length() + 1 && checkPunc(str.charAt(index + target.length())))
&& ((str.length() >= index + target.length() + 2 &&
str.charAt(index + target.length() + 1) == ' ') || str.length() == index + target.length() + 1)) {
if(index > 0 && str.charAt(index - 1) == ' ') {
return true;
}else if(index == 0) {
return true;
}
}
return false;
}
private TagNode recAddTag(String str, String tag, String target) {
if(str.equals("") || str == null) {
return new TagNode(null, null, null);
}
int index = str.toLowerCase().indexOf(target.toLowerCase());
boolean check = indexC(str, target, index);
boolean checkIn = true;
while( (index == 0 || (index > 0 && (str.charAt(index - 1) != ' ' || checkIn))) && check == false) {
index = str.toLowerCase().indexOf(target.toLowerCase(), index + target.length());
check = indexC(str, target, index);
if(check == false) {
checkIn = true;
}
}
if(index == 0) {
int punI = target.length();
if(str.length() > punI && (str.charAt(punI) == '.' || str.charAt(punI) == '!' ||
str.charAt(punI) == ',' || str.charAt(punI) == ':' || str.charAt(punI) == ';' || str.charAt(punI) == '?')) {
TagNode newTag = new TagNode(tag, null, null);
newTag.firstChild = new TagNode(str.substring(index, punI + 1), null, null);
newTag.sibling = recAddTag(str.substring(punI + 1), tag, target);
return newTag;
}else{
TagNode newTag = new TagNode(tag, null, null);
newTag.firstChild = new TagNode(str.substring(index, punI), null, null);
if(punI == str.length()) {
//System.out.println("==");
newTag.sibling = recAddTag("", tag, target);
}else {
newTag.sibling = recAddTag(str.substring(punI), tag, target);
}
return newTag;
}
}else if(index == -1) {
TagNode newTag = new TagNode(str, null, null);
return newTag;
}else {
TagNode newTag = new TagNode(str.substring(0, index), null, null);
newTag.sibling = recAddTag(str.substring(index), tag, target);
return newTag;
}
}
/**
* Gets the HTML represented by this DOM tree. The returned string includes
* new lines, so that when it is printed, it will be identical to the
* input file from which the DOM tree was built.
*
* @return HTML string, including new lines.
*/
public String getHTML() {
StringBuilder sb = new StringBuilder();
getHTML(root, sb);
return sb.toString();
}
private void getHTML(TagNode root, StringBuilder sb) {
for (TagNode ptr=root; ptr != null;ptr=ptr.sibling) {
if (ptr.firstChild == null) {
sb.append(ptr.tag);
sb.append("\n");
} else {
sb.append("<");
sb.append(ptr.tag);
sb.append(">\n");
getHTML(ptr.firstChild, sb);
sb.append("</");
sb.append(ptr.tag);
sb.append(">\n");
}
}
}
/**
* Prints the DOM tree.
*
*/
public void print() {
print(root, 1);
}
private void print(TagNode root, int level) {
for (TagNode ptr=root; ptr != null;ptr=ptr.sibling) {
for (int i=0; i < level-1; i++) {
System.out.print(" ");
};
if (root != this.root) {
System.out.print("|----");
} else {
System.out.print(" ");
}
System.out.println(ptr.tag);
if (ptr.firstChild != null) {
print(ptr.firstChild, level+1);
}
}
}
}