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
|
import java.io.*;
import java.util.*;
import java.text.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import java.lang.Float;
public class sun_dom2 extends bench {
int walk_mode;
DocumentBuilder parser;
DocumentBuilderFactory factory;
double walker_process_node(Node cur_node) {
double res;
Node child;
child = cur_node.getFirstChild();
if ((child!=null)&&(child.getNodeType() == Node.TEXT_NODE)) {
try {
res = Float.parseFloat(child.getNodeValue());
if (res!=0) return res;
} catch(Exception e) {
}
}
NamedNodeMap attrMap = cur_node.getAttributes();
int length = attrMap.getLength();
for (int i = 0; i < length; i++) {
try {
res = Float.parseFloat(attrMap.item(i).getNodeValue());
if (res!=0) return res;
} catch (Exception e) {
}
}
return 0;
}
double walker(Node node) {
int i = 0;
double res = 0;
Node cur_node;
for (cur_node = node; cur_node!=null; cur_node = cur_node.getNextSibling()) {
if((cur_node.getNodeType() == Node.ELEMENT_NODE)&&(++i>3)) break;
}
if (i<=3) i = 0;
for (cur_node = node; cur_node!=null; cur_node = cur_node.getNextSibling()) {
if (cur_node.getNodeType() == Node.ELEMENT_NODE) {
if (i>0) {
res += walker_process_node(cur_node);
}
res+=walker(cur_node.getFirstChild());
}
}
return res;
}
public void InitXML() {
factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
try {
if (System.getenv("allow_deferring")!=null) {
factory.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
} else {
factory.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
}
if (System.getenv("walk_tree")!=null) {
walk_mode = 1;
} else {
walk_mode = 0;
}
} catch (Throwable err) {
// ignoring missing option
}
try {
parser = factory.newDocumentBuilder();
}
catch (Throwable err) {
err.printStackTrace ();
}
}
public void ParseXML(FileInputStream is, int iters, int iter) {
try {
Document doc = parser.parse(is);
if (walk_mode!=0) {
double res = walker(doc.getFirstChild());
// System.out.println("Sum: " + res);
}
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
static public void main(String argv[]) throws IOException {
bench mybench = new sun_dom2();
mybench.Bench(argv);
}
}
|