1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: xml-utils.lisp
6 ;;;; Purpose: XML utilities
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; $Id: xml-utils.lisp,v 1.9 2003/06/07 03:51:42 kevin Exp $
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
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 ;;;; *************************************************************************
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>~%"
28 ;;; XML Extraction Functions
31 #+allegro (require :pxml)
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)))
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."
48 (setq end-xmlstr (length xmlstr)))
50 (let ((bracketpos (position #\< xmlstr :start pos :end end-xmlstr)))
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))
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))
66 (if (and startpos endpos)
68 (setq nextpos (+ endpos taglen 3))
72 (setq pos (1+ endtag))))
73 (setq pos (1+ starttag)))
74 (when (> pos end-xmlstr)
77 (values startpos endpos nextpos)))
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)))
86 (declare (fixnum pos lastpos))
89 (return-from fast-string-search pos))
91 (unless (char= (schar str (+ i pos)) (schar substr i))
94 (defun find-start-tag (tag taglen xmlstr start-pos end-xmlstr)
95 (let ((bracketpos (seaposition-char #\< xmlstr start-pos end-xmlstr)))
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))
106 (if (char= #\> char-after-tag)
107 (setq startpos (1+ endtag))
108 (setq startpos (1+ (position-char #\> xmlstr (1+ endtag) end-xmlstr))))
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."
117 (taglen (length tag))
122 (let ((bracketpos (position-char #\< xmlstr pos end-xmlstr)))
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))
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)
142 (setq nextpos (+ endpos taglen 3))
147 (setq pos (1+ endtag))))
148 (setq pos (1+ starttag)))
149 (when (> pos end-xmlstr)
151 (values startpos endpos nextpos)))
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."
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)
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."
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)
174 (defun xml-cdata (str)
175 (concatenate 'string "<![CDATA[" str "]]>"))
177 (defun write-xml-cdata (str s)
178 (declare (simple-string str) (optimize (speed 3) (safety 0) (space 0)))
179 (do ((len (length str))
182 (declare (fixnum i len))
183 (let ((c (schar str i)))
185 (#\< (write-string "<" s))
186 (#\& (write-string "&" s))
187 (t (write-char c s))))))