/xmlbench/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/xmlbench/trunk

« back to all changes in this revision

Viewing changes to parse/tango-sax.d

  • Committer: Suren A. Chilingaryan
  • Date: 2009-10-01 15:56:11 UTC
  • Revision ID: csa@dside.dyndns.org-20091001155611-9g3f1yad2w195t4g
Implement DOM walking mode for parsing benchmark in fast parsers

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
//import std.stdio;
4
4
import tango.io.Stdout;
5
5
import tango.text.xml.SaxParser;
 
6
import Float = tango.text.convert.Float;
6
7
 
7
8
import tools;
8
9
 
19
20
    }
20
21
}
21
22
 
 
23
private class CountHandler(Ch = char) : SaxHandler!(Ch) {
 
24
 public double res_total;
 
25
 double res_attr[MAX_LEVELS+1];
 
26
 double res_sum[MAX_LEVELS+1];
 
27
 int res_count[MAX_LEVELS+1];
 
28
 int level = 0;
 
29
 
 
30
 public void startDocument() {
 
31
    level = 0;
 
32
    res_total = 0;
 
33
    res_count[0] = 0;
 
34
 }
 
35
    
 
36
 public void startElement(Ch[] uri, Ch[] localName, Ch[] qName, Attribute!(Ch)[] atts) {
 
37
    int i;
 
38
    double res = 0;
 
39
 
 
40
    if (atts) {
 
41
        foreach (cur_attr; atts) {
 
42
            auto value = cur_attr.value;
 
43
            if (value) {
 
44
                try {
 
45
                    res = Float.toFloat(value);
 
46
                    if (res) break;
 
47
                } catch {
 
48
                }
 
49
            }
 
50
        }
 
51
    }
 
52
 
 
53
//    printf("%i %lf\n", level, res);
 
54
    res_attr[level] = res;
 
55
    if (++level == MAX_LEVELS) {
 
56
        Stdout.formatln("XML structure is too deep, only %i levels are supported\n", MAX_LEVELS);
 
57
        throw new Exception("XML structure is too deep");
 
58
    }
 
59
    res_count[level]=0;
 
60
 }
 
61
 
 
62
 public void endElement(Ch[] uri, Ch[] localName, Ch[] qName) {
 
63
    if (res_count[level] > 3) {
 
64
        res_total += res_sum[level];
 
65
    }
 
66
    
 
67
    --level;
 
68
    if (res_count[level]) {
 
69
        res_sum[level]+=res_attr[level];
 
70
    } else {
 
71
        res_sum[level]=res_attr[level];
 
72
    }
 
73
    ++res_count[level];
 
74
 }
 
75
 
 
76
 public void characters(Ch[] ch) {
 
77
    if (ch) {
 
78
        try {
 
79
            double res = Float.toFloat(ch);
 
80
            if (res) res_attr[level-1] = res;
 
81
        } catch {
 
82
        }
 
83
    }
 
84
 
 
85
//      Stdout.formatln(ch);
 
86
 }
 
87
}
 
88
 
22
89
 
23
90
SaxParser!(char) parser = null;
24
 
ContentHandler!(char) handler = null;
 
91
SaxHandler!(char) handler = null;
25
92
 
26
93
extern(C) void initXML(TestData *td) {
27
 
    handler = new ContentHandler!(char);
28
94
    parser = new SaxParser!(char);
 
95
 
 
96
    if (get_walk_mode()) {
 
97
        handler = new CountHandler!(char);
 
98
    } else {
 
99
        handler = new ContentHandler!(char);
 
100
    }
 
101
 
29
102
    parser.setSaxHandler(handler);
30
103
 
31
104
}
37
110
extern(C) void parseXML(TestData *td, uint iter) {
38
111
    parser.setContent(td.xml[0..td.xmllen]);
39
112
    parser.parse();
 
113
 
 
114
    if (get_walk_mode()) {
 
115
//      Stdout.formatln("Sum {}", (cast(CountHandler!(char))handler).res_total);
 
116
    }
 
117
    
40
118
    parser.reset();
41
119
}
42
120