/xmlbench/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/xmlbench/trunk
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
;#! /usr/bin/clisp -C -q -q
; Should be called from root account once to generate binaries for libraries
 
(load #p"/usr/share/common-lisp/source/asdf/asdf.lisp")
(push #p"/usr/share/common-lisp/systems/" asdf:*central-registry*)
(asdf:oos 'asdf:load-op :cl-libxml2)
(asdf:oos 'asdf:load-op :iterate)

(defvar *xpath_expr* "//*[(number(@*) or number(text())) and (count(../child::*)>3)]")
(defvar *attrs_xe* (xpath:compile-expression "./@*"))

(defun my-getenv (name &optional default)
    #+CMU
    (let ((x (assoc name ext:*environment-list*
                    :test #'string=)))
      (if x (cdr x) default))
    #-CMU
    (or
     #+Allegro (sys:getenv name)
     #+CLISP (ext:getenv name)
     #+ECL (si:getenv name)
     #+SBCL (sb-unix::posix-getenv name)
     #+LISPWORKS (lispworks:environment-variable name)
     default))

(defmacro doc_search (doc) 
    `(xpath:xpath-object-value 
	(xpath:eval-expression (xtree:root ,doc) 
	    (xpath:compile-expression *xpath_expr*))))

(defmacro get_attrs (node)
    `(xpath:xpath-object-value 
	(xpath:eval-expression ,node *attrs_xe*)))

(defmacro read_text_value (node) 
    "Unconditional read from text node"
    `(let ((mystr (xtree:text-content (xtree:first-child ,node))))
	(if (string= mystr #\Newline) nil (read-from-string mystr))
    ))
    
(defmacro get_text_value (node)
    `(if (and (xtree:first-child ,node) (eq :XML-TEXT-NODE (xtree:node-type (xtree:first-child ,node))))
	(let ((myval (read_text_value ,node))) 
	    (if (numberp myval) myval nil)) nil))
;	    (if (numberp (read_text_value ,node)) (read_text_value ,node) nil) nil))


(defmacro get_attr_value (anode)
    `(get_text_value ,anode))

(defmacro get_attrs_value (node)
    `(let ((res nil) (attrset (get_attrs ,node))) 
	(loop for i from 0 to (- (xpath:node-set-length attrset) 1) do 
	    (setf res (get_attr_value (xpath:node-set-at attrset i)))
	    (if res (return))
	)
	(if res res 0)
    ))
    
(defmacro get_value (node) "Read the node value"
    `(let ((text_val (get_text_value ,node)))
	(if text_val text_val (get_attrs_value ,node))))
;    `(if (get_text_value ,node) (get_text_value ,node) (get_attrs_value ,node)))


(defun parse_file (fn)
    (setf *sum* 0)
    (xtree:with-parse-document (doc (pathname fn)) 
	(if (my-getenv "walk_tree")
	    (let ((nodeset (doc_search doc)))
		(loop for i from 0 to (- (xpath:node-set-length nodeset) 1) do 
		    (setf *sum* (+ *sum* (get_value (xpath:node-set-at nodeset i))))
		)
;		(print *sum*)
	    ))
    ))
;	(xtree:serialize doc *standard-output*)

(defun parse_iteration (fn i)
    (if fn
	(parse_file fn)
	(parse_file (concatenate 'string "../xml.tmp/" (write-to-string i) ".xml"))))


(defvar iterations NIL)
(defvar xmlfn NIL)

;sbcl
;(setf *ARGS* *posix-argv*)

(if (> (length *ARGS*) 0)
    (setf iterations (parse-integer (first *ARGS*)))
    (setf iterations 0))

(if (> (length *ARGS*) 1)
    (setf xmlfn (second *ARGS*))
    (setf xmlfn NIL))

(parse_iteration xmlfn 0)
;(pprint (list "Number of Iterations" iterations))
(if (> iterations 0)
    (time
	(dotimes (i iterations) 
	    (parse_iteration xmlfn (+ i 1))))
    (dotimes (i iterations) 
	(parse_iteration xmlfn (+ i 1))))


(quit)