r5062: return from san diego
[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.8 2003/06/06 21:59:30 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 find-start-tag (tag taglen xmlstr start-pos end-xmlstr)
80   (let ((bracketpos (position-char #\< xmlstr start-pos end-xmlstr)))
81     (when bracketpos
82       (let* ((starttag (1+ bracketpos))
83              (endtag (+ starttag taglen)))
84         (if (and (< endtag end-xmlstr)
85                  (string= tag xmlstr :start2 starttag :end2 endtag))
86             (let* ((char-after-tag (char xmlstr endtag)))
87               (declare (character char-after-tag))
88               (if (or (char= #\> char-after-tag)
89                       (char= #\space char-after-tag))
90                   (progn
91                     (if (char= #\> char-after-tag) 
92                         (setq startpos (1+ endtag))
93                       (setq startpos (1+ (position-char #\> xmlstr (1+ endtag) end-xmlstr))))
94                     ))))))))
95
96 (defun positions-xml-tag-contents (tag xmlstr &optional (start-xmlstr 0)
97                                        (end-xmlstr (length xmlstr)))
98   "Returns three values: the start and end positions of contents between
99  the xml tags and the position following the close of the end tag."
100   (let ((done nil)
101         (pos start-xmlstr)
102         (taglen (length tag))
103         (startpos nil)
104         (endpos nil)
105         (nextpos nil))
106     (while (not done)
107            (let ((bracketpos (position-char #\< xmlstr pos end-xmlstr)))
108              (unless bracketpos
109                (return-from positions-xml-tag-contents
110                  (values nil nil nil)))
111              (let* ((starttag (1+ bracketpos))
112                     (endtag (+ starttag taglen)))
113                (if (and (< endtag end-xmlstr)
114                         (string= tag xmlstr :start2 starttag :end2 endtag))
115                    (let* ((char-after-tag (char xmlstr endtag)))
116                      (declare (character char-after-tag))
117                      (if (or (char= #\> char-after-tag)
118                              (char= #\space char-after-tag))
119                          (progn
120                            (if (char= #\> char-after-tag) 
121                                (setq startpos (1+ endtag))
122                              (setq startpos (1+ (position-char #\> xmlstr (1+ endtag) end-xmlstr))))
123                            (setq endpos (search (format nil "</~a>" tag) xmlstr
124                                                 :start2 startpos :end2 end-xmlstr))
125                            (if (and startpos endpos)
126                                (progn
127                                  (setq nextpos (+ endpos taglen 3))
128                                  (setq pos nextpos))
129                              (setf startpos nil
130                                    endpos nil))
131                            (setq done t))
132                        (setq pos (1+ endtag))))
133                  (setq pos (1+ starttag)))
134                (when (> pos end-xmlstr)
135                  (setq done t))))))
136     (values startpos endpos nextpos)))
137
138
139 (defun xml-tag-contents-old (tag xmlstr &optional (start-xmlstr 0) (end-xmlstr nil))
140   "Returns two values: the string between XML start and end tag 
141 and position of character following end tag."
142   (multiple-value-bind 
143       (startpos endpos nextpos) 
144       (positions-xml-tag-contents-old tag xmlstr start-xmlstr end-xmlstr)
145     (if (and startpos endpos)
146         (values (subseq xmlstr startpos endpos) nextpos)
147       (values nil nil))))
148
149 (defun xml-tag-contents (tag xmlstr &optional (start-xmlstr 0) (end-xmlstr nil))
150   "Returns two values: the string between XML start and end tag 
151 and position of character following end tag."
152   (multiple-value-bind 
153       (startpos endpos nextpos) 
154       (positions-xml-tag-contents tag xmlstr start-xmlstr end-xmlstr)
155     (if (and startpos endpos)
156         (values (subseq xmlstr startpos endpos) nextpos)
157       (values nil nil))))
158
159 (defun xml-cdata (str)
160   (concatenate 'string "<![CDATA[" str "]]>"))
161
162 (defun write-xml-cdata (str s)
163   (declare (simple-string str) (optimize (speed 3) (safety 0) (space 0)))
164   (do ((len (length str))
165        (i 0 (1+ i)))
166       ((= i len) str)
167     (declare (fixnum i len))
168     (let ((c (schar str i)))
169       (case c
170         (#\< (write-string "&lt;" s))
171         (#\& (write-string "&amp;" s))
172         (t   (write-char c s))))))
173