-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHypertree.java
More file actions
100 lines (79 loc) · 1.87 KB
/
Copy pathHypertree.java
File metadata and controls
100 lines (79 loc) · 1.87 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
import org.w3c.dom.*;
class Hypertree {
Node root;
int indent;
Hypertree(Node root, int indent) {
this.root = root;
this.indent = indent;
}
public Hypertree prime()
{
NodeList children = root.getChildNodes();
Node first = children.item(0);
this.root = first;
return this;
}
public Hypertree head(int n)
{
NodeList children = root.getChildNodes();
int i = children.getLength();
while (i > n) {
root.removeChild(children.item(children.getLength()-1));
i = children.getLength();
}
return this;
}
public Hypertree tail(int n)
{
NodeList children = root.getChildNodes();
int i = children.getLength();
while (i > n) {
root.removeChild(children.item(0));
i = children.getLength();
}
return this;
}
public Hypertree simpleTree(int n)
{
head(n);
tail(1);
return this;
}
public void print()
{
printIndent(indent);
System.out.print("[Tag: "+root.getNodeName());
NamedNodeMap attributes = root.getAttributes();
if (attributes != null) {
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
System.out.print(", "+attribute.getNodeName()+": "+attribute.getNodeValue());
}
}
NodeList children = root.getChildNodes();
if (children.getLength() == 1) {
Node child = children.item(0);
if (child.getNodeType() == Node.TEXT_NODE) {
String value = child.getNodeValue().replaceAll("[\n\r]", "");
if (value.length() != 0) {
System.out.print(", Value: "+value);
}
}
}
System.out.print(" ]\n");
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
short type = child.getNodeType();
if (type == Node.ELEMENT_NODE) {
Hypertree tree = new Hypertree(child, indent + 1);
tree.print();
}
}
}
private void printIndent(int indent)
{
for (int i = 0; i < indent; i++) {
System.out.print(" ");
}
}
}