1 ;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
3 ;;; Copyright (c) 2007,2008 David Lichteblau, Ivan Shvedunov.
4 ;;; All rights reserved.
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
13 ;;; * Redistributions in binary form must reproduce the above
14 ;;; copyright notice, this list of conditions and the following
15 ;;; disclaimer in the documentation and/or other materials
16 ;;; provided with the distribution.
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 (in-package :xuriella
)
32 (defun xsl-number-value (y)
33 (let ((x (xpath:number-value y
)))
37 (>= x
(expt 2 31)) ;-(
39 (xpath:string-value x
)
40 (round (xpath::xnum-round x
)))))
42 (define-instruction xsl
:number
(args env
)
43 (destructuring-bind (&key level count from value format lang letter-value
44 grouping-separator grouping-size
)
46 (let ((count (and count
(xpath:make-pattern-matcher
* count env
)))
47 (from (and from
(xpath:make-pattern-matcher
* from env
)))
48 (value (and value
(compile-xpath value env
)))
49 (format (compile-avt (or format
"1") env
))
50 (lang (compile-avt (or lang
"") env
))
51 (letter-value (compile-avt (or letter-value
"foo") env
))
53 (and grouping-separator
(compile-avt grouping-separator env
)))
54 (grouping-size (and grouping-size
(compile-avt grouping-size env
))))
56 (let ((value (when value
57 (xsl-number-value (funcall value ctx
))))
58 (format (funcall format ctx
))
59 (lang (funcall lang ctx
))
60 (letter-value (funcall letter-value ctx
))
61 (grouping-separator (when grouping-separator
62 (funcall grouping-separator ctx
)))
63 (grouping-size (when grouping-size
65 (funcall grouping-size ctx
)))))
72 (compute-number-list (or level
"single")
73 (xpath::context-node ctx
)
82 (defun pattern-thunk-matches-p (pattern-thunk node
)
83 (xpath:matching-value pattern-thunk node
))
85 (defun ancestors-using-count-and-from (node count from
)
88 (funcall (xpath::axis-function
:ancestor-or-self
) node
))))
89 (remove-if-not (lambda (ancestor)
90 (pattern-thunk-matches-p count ancestor
))
94 when
(pattern-thunk-matches-p from a
)
97 finally
(return ancestors
))
100 (defun node-position-among-siblings (node count
)
102 (count-if (lambda (sibling)
103 (pattern-thunk-matches-p count sibling
))
105 (funcall (xpath::axis-function
:preceding-sibling
) node
)))))
107 (defun node-type (node)
108 (dolist (type '(:element
113 :processing-instruction
115 (when (xpath-protocol:node-type-p node type
)
118 (defun compute-number-list (level node count from
)
121 (let ((uri (xpath-protocol:namespace-uri node
))
122 (lname (xpath-protocol:local-name node
))
123 (node-type (node-type node
)))
124 (lambda (pattern-node)
125 (if (if (eq node-type
:element
)
126 (and (xpath-protocol:node-type-p pattern-node
:element
)
127 (equal (xpath-protocol:namespace-uri pattern-node
)
129 (equal (xpath-protocol:local-name pattern-node
)
131 (xpath-protocol:node-type-p pattern-node node-type
))
135 ((equal level
"single")
136 (let ((ancestor (car (ancestors-using-count-and-from node count from
))))
138 (list (node-position-among-siblings ancestor count
))
140 ((equal level
"multiple")
141 (mapcar (lambda (ancestor)
142 (node-position-among-siblings ancestor count
))
144 (ancestors-using-count-and-from node count from
))))
146 (destructuring-bind (root)
147 (xpath::force
(funcall (xpath::axis-function
:root
) node
))
148 (let ((nodes (xpath::force
150 (xpath::subpipe-before
152 (funcall (xpath::axis-function
:descendant-or-self
) root
))
156 for
(current . rest
) on nodes
157 when
(pattern-thunk-matches-p from current
)
163 count
(pattern-thunk-matches-p count n
))))))
165 (xslt-error "invalid number level: ~A" level
))))
167 (xpath::deflexer
(format-lexer :ignore-whitespace nil
)
168 ;; zzz just enough unicode "support" here to pass the tests
169 (#.
(format nil
"([a-zA-Z0-9~A]+)" (code-char 945)) (x) (values :format x
))
170 (#.
(format nil
"([^a-zA-Z0-9~A]+)" (code-char 945)) (x) (values :text x
)))
172 (defun format-number-token (str n
)
176 ;; zzz just enough unicode "support" here to pass the tests
177 (equal str
#.
(string (code-char 945))))
178 (let ((start (char-code (elt str
0)))
179 (greekp (equal str
#.
(string (code-char 945)))))
181 (xslt-error "cannot format zero"))
183 (with-output-to-string (r)
186 for
(rest digit
) = (multiple-value-list
192 (write-char (code-char (+ start digit
)) r
))
194 (write-char (code-char (+ start digit
)) r
)
199 (format nil
"~(~@R~)" n
)))
203 (format nil
"~@R" n
)))
205 (unless (cl-ppcre:all-matches
"^0*1$" str
)
206 ;; unsupported format
208 (format nil
"~v,'0D" (length str
) n
))))
210 (defun group-numbers (str separator size stream
)
213 for i from
(1- (length str
)) downto
0
215 (write-char c stream
)
216 (when (and (plusp size
)
217 (and (zerop (mod i size
)) (plusp i
)))
218 (write-string separator stream
))))
220 ;;; fixme: unicode support
221 (defun format-number-list
222 (list format lang letter-value grouping-separator grouping-size
)
223 (declare (ignore lang letter-value
))
224 (if (some #'xpath
::nan-p list
)
226 (multiple-value-bind (prefix pairs suffix
)
227 (parse-number-format format
)
228 (with-output-to-string (s)
229 (write-string prefix s
)
231 for
(separator . subformat
) in pairs
232 for n in
(remove 0 list
)
233 for formatted
= (format-number-token subformat n
)
236 (write-string separator s
))
237 (if (and grouping-separator
239 (group-numbers formatted
243 (write-string formatted s
)))
244 (write-string suffix s
)))))
246 (defun parse-number-format (format)
247 (let ((lexer (format-lexer format
))
253 (multiple-value-bind (type str
) (funcall lexer
)
259 (setf current-text str
)
262 (push (cons (if conses
263 (or current-text
".")
267 (setf current-text nil
)))))
270 (setf suffix current-text
))
272 (setf suffix prefix
)))
274 (setf conses
(list (cons nil
"1"))))
275 (let* ((tail-cons (car conses
))
276 (tail (if (car tail-cons
)
278 (push (cons "." (cdr tail-cons
)) conses
))))
279 (setf conses
(nreverse conses
))
280 (setf (cdr tail
) tail
))
281 (values prefix conses suffix
)))