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.8 2003/06/06 21:59:30 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 find-start-tag (tag taglen xmlstr start-pos end-xmlstr)
80 (let ((bracketpos (position-char #\< xmlstr start-pos end-xmlstr)))
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))
91 (if (char= #\> char-after-tag)
92 (setq startpos (1+ endtag))
93 (setq startpos (1+ (position-char #\> xmlstr (1+ endtag) end-xmlstr))))
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."
102 (taglen (length tag))
107 (let ((bracketpos (position-char #\< xmlstr pos end-xmlstr)))
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))
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)
127 (setq nextpos (+ endpos taglen 3))
132 (setq pos (1+ endtag))))
133 (setq pos (1+ starttag)))
134 (when (> pos end-xmlstr)
136 (values startpos endpos nextpos)))
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."
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)
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."
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)
159 (defun xml-cdata (str)
160 (concatenate 'string "<![CDATA[" str "]]>"))
162 (defun write-xml-cdata (str s)
163 (declare (simple-string str) (optimize (speed 3) (safety 0) (space 0)))
164 (do ((len (length str))
167 (declare (fixnum i len))
168 (let ((c (schar str i)))
170 (#\< (write-string "<" s))
171 (#\& (write-string "&" s))
172 (t (write-char c s))))))