4dc37fc53140f68c91f1b0a2925628f362e8398d
[kmrcl.git] / xml-utils.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          xml-utils.lisp
6 ;;;; Purpose:       XML utilities
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id: xml-utils.lisp,v 1.9 2003/06/07 03:51:42 kevin Exp $
11 ;;;;
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; KMRCL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (in-package #:kmrcl)
20
21
22 (defun wrap-with-xml (str entity)
23   "Returns string of xml header along with entity tag start/end with str contents"
24   (format nil "<?xml version=\"1.0\" standalone=\"yes\"?>~%~%<~a>~%~a~%</~a>~%" 
25           str entity entity))
26
27
28 ;;; XML Extraction Functions
29
30 #|
31 #+allegro (require :pxml)
32 #+allegro
33 (defun parse-xml-no-ws (str)
34   "Return list structure of XML string with removing whitespace strings"
35   (remove-tree-if #'string-ws? (parse-xml str)))
36 |#
37
38 (defun positions-xml-tag-contents-old (tag xmlstr &optional (start-xmlstr 0) (end-xmlstr nil))
39   "Returns three values: the start and end positions of contents between
40  the xml tags and the position following the close of the end tag."
41   (let ((done nil)
42         (pos start-xmlstr)
43         (taglen (length tag))
44         (startpos nil)
45         (endpos nil)
46         (nextpos nil))
47     (unless end-xmlstr
48       (setq end-xmlstr (length xmlstr)))
49     (while (not done)
50            (let ((bracketpos (position #\< xmlstr :start pos :end end-xmlstr)))
51              (if bracketpos
52                  (let* ((starttag (1+ bracketpos))
53                         (endtag (+ starttag taglen)))
54                    (if (and (< endtag end-xmlstr)
55                             (string= tag xmlstr :start2 starttag :end2 endtag))
56                        (let* ((char-after-tag (char xmlstr endtag)))
57                          (declare (character char-after-tag))
58                          (if (or (char= #\> char-after-tag) (char= #\space char-after-tag))
59                              (progn
60                                (if (char= #\> char-after-tag) 
61                                    (setq startpos (1+ endtag))
62                                  (setq startpos (1+ (position #\> xmlstr :start (1+ endtag)))))
63                                (setq endpos (search (format nil "</~a>" tag) xmlstr
64                                                     :start2 startpos :end2 end-xmlstr))
65                                (setq done t)
66                                (if (and startpos endpos)
67                                    (progn
68                                      (setq nextpos (+ endpos taglen 3))
69                                      (setq pos nextpos))
70                                  (setf startpos nil
71                                        endpos nil)))
72                            (setq pos (1+ endtag))))
73                      (setq pos (1+ starttag)))
74                    (when (> pos end-xmlstr)
75                      (setq done t)))
76                (setq done t))))
77     (values startpos endpos nextpos)))
78
79 (defun fast-string-search (substr str substr-length startpos endpos)
80   (declare (simple-string substr str)
81            (fixnum substr-length startpos endpos)
82            (optimize (speed 3) (space 0) (safety 0)))
83   (do* ((pos startpos (1+ pos))
84         (lastpos (- endpos substr-length)))
85        ((> pos lastpos) nil)
86     (declare (fixnum pos lastpos))
87     (do ((i 0 (1+ i)))
88         ((= i substr-length)
89          (return-from fast-string-search pos))
90       (declare (fixnum i))
91       (unless (char= (schar str (+ i pos)) (schar substr i))
92         (return nil)))))
93
94 (defun find-start-tag (tag taglen xmlstr start-pos end-xmlstr)
95   (let ((bracketpos (seaposition-char #\< xmlstr start-pos end-xmlstr)))
96     (when bracketpos
97       (let* ((starttag (1+ bracketpos))
98              (endtag (+ starttag taglen)))
99         (if (and (< endtag end-xmlstr)
100                  (string= tag xmlstr :start2 starttag :end2 endtag))
101             (let* ((char-after-tag (char xmlstr endtag)))
102               (declare (character char-after-tag))
103               (if (or (char= #\> char-after-tag)
104                       (char= #\space char-after-tag))
105                   (progn
106                     (if (char= #\> char-after-tag) 
107                         (setq startpos (1+ endtag))
108                       (setq startpos (1+ (position-char #\> xmlstr (1+ endtag) end-xmlstr))))
109                     ))))))))
110
111 (defun positions-xml-tag-contents (tag xmlstr &optional (start-xmlstr 0)
112                                        (end-xmlstr (length xmlstr)))
113   "Returns three values: the start and end positions of contents between
114  the xml tags and the position following the close of the end tag."
115   (let ((done nil)
116         (pos start-xmlstr)
117         (taglen (length tag))
118         (startpos nil)
119         (endpos nil)
120         (nextpos nil))
121     (while (not done)
122            (let ((bracketpos (position-char #\< xmlstr pos end-xmlstr)))
123              (unless bracketpos
124                (return-from positions-xml-tag-contents
125                  (values nil nil nil)))
126              (let* ((starttag (1+ bracketpos))
127                     (endtag (+ starttag taglen)))
128                (if (and (< endtag end-xmlstr)
129                         (string= tag xmlstr :start2 starttag :end2 endtag))
130                    (let* ((char-after-tag (char xmlstr endtag)))
131                      (declare (character char-after-tag))
132                      (if (or (char= #\> char-after-tag)
133                              (char= #\space char-after-tag))
134                          (progn
135                            (if (char= #\> char-after-tag) 
136                                (setq startpos (1+ endtag))
137                              (setq startpos (1+ (position-char #\> xmlstr (1+ endtag) end-xmlstr))))
138                            (setq endpos (search (format nil "</~a>" tag) xmlstr
139                                                 :start2 startpos :end2 end-xmlstr))
140                            (if (and startpos endpos)
141                                (progn
142                                  (setq nextpos (+ endpos taglen 3))
143                                  (setq pos nextpos))
144                              (setf startpos nil
145                                    endpos nil))
146                            (setq done t))
147                        (setq pos (1+ endtag))))
148                  (setq pos (1+ starttag)))
149                (when (> pos end-xmlstr)
150                  (setq done t))))))
151     (values startpos endpos nextpos)))
152
153
154 (defun xml-tag-contents-old (tag xmlstr &optional (start-xmlstr 0) (end-xmlstr nil))
155   "Returns two values: the string between XML start and end tag 
156 and position of character following end tag."
157   (multiple-value-bind 
158       (startpos endpos nextpos) 
159       (positions-xml-tag-contents-old tag xmlstr start-xmlstr end-xmlstr)
160     (if (and startpos endpos)
161         (values (subseq xmlstr startpos endpos) nextpos)
162       (values nil nil))))
163
164 (defun xml-tag-contents (tag xmlstr &optional (start-xmlstr 0) (end-xmlstr nil))
165   "Returns two values: the string between XML start and end tag 
166 and position of character following end tag."
167   (multiple-value-bind 
168       (startpos endpos nextpos) 
169       (positions-xml-tag-contents tag xmlstr start-xmlstr end-xmlstr)
170     (if (and startpos endpos)
171         (values (subseq xmlstr startpos endpos) nextpos)
172       (values nil nil))))
173
174 (defun xml-cdata (str)
175   (concatenate 'string "<![CDATA[" str "]]>"))
176
177 (defun write-xml-cdata (str s)
178   (declare (simple-string str) (optimize (speed 3) (safety 0) (space 0)))
179   (do ((len (length str))
180        (i 0 (1+ i)))
181       ((= i len) str)
182     (declare (fixnum i len))
183     (let ((c (schar str i)))
184       (case c
185         (#\< (write-string "&lt;" s))
186         (#\& (write-string "&amp;" s))
187         (t   (write-char c s))))))
188