r3474: *** 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.5 2002/11/25 07:45:36 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 1) (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 (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
80 (defun xml-tag-contents (tag xmlstr &optional (start-xmlstr 0) (end-xmlstr nil))
81   "Returns two values: the string between XML start and end tag 
82 and position of character following end tag."
83   (multiple-value-bind 
84       (startpos endpos nextpos) 
85       (positions-xml-tag-contents tag xmlstr start-xmlstr end-xmlstr)
86     (if (and startpos endpos)
87         (values (subseq xmlstr startpos endpos) nextpos)
88       (values nil nil))))
89
90 (defun xml-cdata (str)
91   (concatenate 'string "<![CDATA[" str "]]>"))
92