/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/sun_dom2.java

  • Committer: Suren A. Chilingaryan
  • Date: 2009-10-01 16:30:16 UTC
  • Revision ID: csa@dside.dyndns.org-20091001163016-qum8v7u9726vfwi8
DOM Walking for deferred mode parsers

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
import javax.xml.parsers.DocumentBuilderFactory; 
5
5
import javax.xml.parsers.DocumentBuilder; 
6
6
 
 
7
import org.w3c.dom.Document;
 
8
import org.w3c.dom.NamedNodeMap;
 
9
import org.w3c.dom.Node;
 
10
import org.w3c.dom.Text;
 
11
import java.lang.Float;
 
12
 
7
13
public class sun_dom2 extends bench {
8
 
 
9
 
    DocumentBuilder parser;
10
 
    DocumentBuilderFactory factory;
11
 
    
12
 
    public void InitXML() {
 
14
 int walk_mode;
 
15
 DocumentBuilder parser;
 
16
 DocumentBuilderFactory factory;
 
17
 
 
18
 double walker_process_node(Node cur_node) {
 
19
    double res;
 
20
    Node child;
 
21
    
 
22
    child = cur_node.getFirstChild();
 
23
    if ((child!=null)&&(child.getNodeType() == Node.TEXT_NODE)) {
 
24
        try {
 
25
            res = Float.parseFloat(child.getNodeValue());
 
26
            if (res!=0) return res;
 
27
        } catch(Exception e) {
 
28
        }
 
29
    } 
 
30
 
 
31
    NamedNodeMap attrMap = cur_node.getAttributes();
 
32
    int length = attrMap.getLength();
 
33
 
 
34
    for (int i = 0; i < length; i++) {
 
35
        try {
 
36
            res = Float.parseFloat(attrMap.item(i).getNodeValue());
 
37
            if (res!=0) return res;
 
38
        } catch (Exception e) {
 
39
        }
 
40
    }
 
41
 
 
42
    return 0;
 
43
 }
 
44
 
 
45
 
 
46
 double walker(Node node) {
 
47
    int i = 0;
 
48
    double res = 0;
 
49
    Node cur_node;
 
50
 
 
51
    for (cur_node = node; cur_node!=null; cur_node = cur_node.getNextSibling()) {
 
52
        if((cur_node.getNodeType() == Node.ELEMENT_NODE)&&(++i>3)) break;
 
53
    }
 
54
    
 
55
    if (i<=3) i = 0;
 
56
 
 
57
    for (cur_node = node; cur_node!=null; cur_node = cur_node.getNextSibling()) {
 
58
        if (cur_node.getNodeType() == Node.ELEMENT_NODE) {
 
59
            if (i>0) {
 
60
                res += walker_process_node(cur_node);
 
61
            }
 
62
            res+=walker(cur_node.getFirstChild());
 
63
        }
 
64
    }
 
65
    return res;
 
66
 }
 
67
    
 
68
 public void InitXML() {
13
69
        factory = DocumentBuilderFactory.newInstance();
14
70
        factory.setNamespaceAware(true);
15
71
        factory.setValidating(false);
20
76
            } else {
21
77
                factory.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
22
78
            }
 
79
 
 
80
            if (System.getenv("walk_tree")!=null) {
 
81
                walk_mode = 1;
 
82
            } else {
 
83
                walk_mode = 0;
 
84
            }
23
85
        } catch (Throwable err) {
24
86
            // ignoring missing option
25
87
        }
30
92
        catch (Throwable err) {
31
93
            err.printStackTrace ();
32
94
        }
33
 
    }
 
95
 }
34
96
 
35
 
    public void ParseXML(FileInputStream is, int iters, int iter) {
 
97
 public void ParseXML(FileInputStream is, int iters, int iter) {
36
98
        try {
37
 
            parser.parse(is);           
 
99
            Document doc = parser.parse(is);
 
100
            if (walk_mode!=0) {
 
101
                double res = walker(doc.getFirstChild());
 
102
//              System.out.println("Sum: " + res);
 
103
            }           
38
104
        }
39
105
        catch (Exception e)
40
106
        {
41
107
            System.out.println(e.toString());
42
108
        }
43
 
    }
 
109
 }
44
110
  
45
 
    static public void main(String argv[]) throws IOException {
 
111
 static public void main(String argv[]) throws IOException {
46
112
        bench mybench = new sun_dom2();
47
113
        mybench.Bench(argv);
48
 
    }
 
114
 }
49
115
}