r3555: *** empty log message ***
[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.6 2002/12/04 16:49:23 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 (declaim (optimize (speed 3) (safety 2) (compilation-speed 0) (debug 3)))
21
22
23 (defun wrap-with-xml (str entity)
24   "Returns string of xml header along with entity tag start/end with str contents"
25   (format nil "<?xml version=\"1.0\" standalone=\"yes\"?>~%~%<~a>~%~a~%</~a>~%" 
26           str entity entity))
27
28
29 ;;; XML Extraction Functions
30
31 #|
32 #+allegro (require :pxml)
33 #+allegro
34 (defun parse-xml-no-ws (str)
35   "Return list structure of XML string with removing whitespace strings"
36   (remove-tree-if #'string-ws? (parse-xml str)))
37 |#
38
39 (defun positions-xml-tag-contents (tag xmlstr &optional (start-xmlstr 0) (end-xmlstr nil))
40   "Returns three values: the start and end positions of contents between
41  the xml tags and the position following the close of the end tag."
42   (let ((done nil)
43         (pos start-xmlstr)
44         (taglen (length tag))
45         (startpos nil)
46         (endpos nil)
47         (nextpos nil))
48     (unless end-xmlstr
49       (setq end-xmlstr (length xmlstr)))
50     (while (not done)
51            (let ((bracketpos (position #\< xmlstr :start pos :end end-xmlstr)))
52              (if bracketpos
53                  (let* ((starttag (1+ bracketpos))
54                         (endtag (+ starttag taglen)))
55                    (if (and (< endtag end-xmlstr)
56                             (string= tag xmlstr :start2 starttag :end2 endtag))
57                        (let* ((char-after-tag (char xmlstr endtag)))
58                          (declare (character char-after-tag))
59                          (if (or (char= #\> char-after-tag) (char= #\space char-after-tag))
60                              (progn
61                                (if (char= #\> char-after-tag) 
62                                    (setq startpos (1+ endtag))
63                                  (setq startpos (1+ (position #\> xmlstr :start (1+ endtag)))))
64                                (setq endpos (search (format nil "</~a>" tag) xmlstr
65                                                     :start2 startpos :end2 end-xmlstr))
66                                (setq done t)
67                                (if (and startpos endpos)
68                                    (progn
69                                      (setq nextpos (+ endpos taglen 3))
70                                      (setq pos nextpos))
71                                  (setf startpos nil
72                                        endpos nil)))
73                            (setq pos (1+ endtag))))
74                      (setq pos (1+ starttag)))
75                    (when (> pos end-xmlstr)
76                      (setq done t)))
77                (setq done t))))
78     (values startpos endpos nextpos)))
79
80
81 (defun xml-tag-contents (tag xmlstr &optional (start-xmlstr 0) (end-xmlstr nil))
82   "Returns two values: the string between XML start and end tag 
83 and position of character following end tag."
84   (multiple-value-bind 
85       (startpos endpos nextpos) 
86       (positions-xml-tag-contents tag xmlstr start-xmlstr end-xmlstr)
87     (if (and startpos endpos)
88         (values (subseq xmlstr startpos endpos) nextpos)
89       (values nil nil))))
90
91 (defun xml-cdata (str)
92   (concatenate 'string "<![CDATA[" str "]]>"))
93