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
|
module tango_dom;
//import std.stdio;
import tango.io.Stdout;
import tango.text.xml.SaxParser;
import Float = tango.text.convert.Float;
import tools;
private class ContentHandler(Ch = char) : SaxHandler!(Ch) {
public void startElement(Ch[] uri, Ch[] localName, Ch[] qName, Attribute!(Ch)[] atts) {
}
public void endElement(Ch[] uri, Ch[] localName, Ch[] qName) {
}
public void characters(Ch[] ch) {
// Stdout.formatln(ch);
}
}
private class CountHandler(Ch = char) : SaxHandler!(Ch) {
public double res_total;
double res_attr[MAX_LEVELS+1];
double res_sum[MAX_LEVELS+1];
int res_count[MAX_LEVELS+1];
int level = 0;
public void startDocument() {
level = 0;
res_total = 0;
res_count[0] = 0;
}
public void startElement(Ch[] uri, Ch[] localName, Ch[] qName, Attribute!(Ch)[] atts) {
int i;
double res = 0;
if (atts) {
foreach (cur_attr; atts) {
auto value = cur_attr.value;
if (value) {
try {
res = Float.toFloat(value);
if (res) break;
} catch {
}
}
}
}
// printf("%i %lf\n", level, res);
res_attr[level] = res;
if (++level == MAX_LEVELS) {
Stdout.formatln("XML structure is too deep, only {} levels are supported\n", MAX_LEVELS);
throw new Exception("XML structure is too deep");
}
res_count[level]=0;
}
public void endElement(Ch[] uri, Ch[] localName, Ch[] qName) {
if (res_count[level] > 3) {
res_total += res_sum[level];
}
--level;
if (res_count[level]) {
res_sum[level]+=res_attr[level];
} else {
res_sum[level]=res_attr[level];
}
++res_count[level];
}
public void characters(Ch[] ch) {
if (ch) {
try {
double res = Float.toFloat(ch);
if (res) res_attr[level-1] = res;
} catch {
}
}
// Stdout.formatln(ch);
}
}
SaxParser!(char) parser = null;
SaxHandler!(char) handler = null;
extern(C) void initXML(TestData *td) {
parser = new SaxParser!(char);
if (get_walk_mode()) {
handler = new CountHandler!(char);
} else {
handler = new ContentHandler!(char);
}
parser.setSaxHandler(handler);
}
extern(C) void releaseXML(TestData *td) {
}
extern(C) void parseXML(TestData *td, uint iter) {
parser.setContent(td.xml[0..td.xmllen]);
parser.parse();
if (get_walk_mode()) {
// Stdout.formatln("Sum {}", (cast(CountHandler!(char))handler).res_total);
}
parser.reset();
}
void main(char[][] args)
{
DTest(args);
}
|