Merge from emacs-23
[bpt/emacs.git] / lisp / xml.el
index a55a906..edcc13d 100644 (file)
@@ -1,7 +1,7 @@
 ;;; xml.el --- XML parser
 
 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
-;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+;;   2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
 
 ;; Author: Emmanuel Briot  <briot@gnat.com>
 ;; Maintainer: Mark A. Hershberger <mah@everybody.org>
@@ -9,10 +9,10 @@
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -20,9 +20,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
@@ -323,18 +321,20 @@ If PARSE-NS is non-nil, then QNAMES are expanded."
                (progn
                  (forward-char -1)
                  (setq result (xml-parse-tag parse-dtd parse-ns))
-                 (if (and xml result (not xml-sub-parser))
-                     ;;  translation of rule [1] of XML specifications
-                     (error "XML: (Not Well-Formed) Only one root tag allowed")
-                   (cond
-                    ((null result))
-                    ((and (listp (car result))
-                          parse-dtd)
-                     (setq dtd (car result))
-                     (if (cdr result)  ; possible leading comment
-                         (add-to-list 'xml (cdr result))))
-                    (t
-                     (add-to-list 'xml result)))))
+                 (cond
+                  ((null result)
+                   ;; Not looking at an xml start tag.
+                   (forward-char 1))
+                  ((and xml (not xml-sub-parser))
+                   ;; Translation of rule [1] of XML specifications
+                   (error "XML: (Not Well-Formed) Only one root tag allowed"))
+                  ((and (listp (car result))
+                        parse-dtd)
+                   (setq dtd (car result))
+                   (if (cdr result)    ; possible leading comment
+                       (add-to-list 'xml (cdr result))))
+                  (t
+                   (add-to-list 'xml result))))
              (goto-char (point-max))))
          (if parse-dtd
              (cons dtd (nreverse xml))
@@ -496,9 +496,7 @@ Returns one of:
 (defun xml-parse-string ()
   "Parse the next whatever.  Could be a string, or an element."
   (let* ((pos (point))
-        (string (progn (if (search-forward "<" nil t)
-                           (forward-char -1)
-                         (goto-char (point-max)))
+        (string (progn (skip-chars-forward "^<")
                        (buffer-substring-no-properties pos (point)))))
     ;; Clean up the string.  As per XML specifications, the XML
     ;; processor should always pass the whole string to the
@@ -827,6 +825,25 @@ This follows the rule [28] in the XML specifications."
                              "")
                   (substring string point))))))
 
+(defun xml-substitute-numeric-entities (string)
+  "Substitute SGML numeric entities by their respective utf characters.
+This function replaces numeric entities in the input STRING and
+returns the modified string.  For example \"&#42;\" gets replaced
+by \"*\"."
+  (if (and string (stringp string))
+      (let ((start 0))
+        (while (string-match "&#\\([0-9]+\\);" string start)
+          (condition-case nil
+              (setq string (replace-match
+                            (string (read (substring string
+                                                     (match-beginning 1)
+                                                     (match-end 1))))
+                            nil nil string))
+            (error nil))
+          (setq start (1+ (match-beginning 0))))
+        string)
+    nil))
+
 ;;*******************************************************************
 ;;**
 ;;**  Printing a tree.
@@ -844,6 +861,18 @@ The first line is indented with the optional INDENT-STRING."
 
 (defalias 'xml-print 'xml-debug-print)
 
+(defun xml-escape-string (string)
+  "Return the string with entity substitutions made from
+xml-entity-alist."
+  (mapconcat (lambda (byte)
+               (let ((char (char-to-string byte)))
+                 (if (rassoc char xml-entity-alist)
+                     (concat "&" (car (rassoc char xml-entity-alist)) ";")
+                   char)))
+             ;; This differs from the non-unicode branch.  Just
+             ;; grabbing the string works here.
+             string ""))
+
 (defun xml-debug-print-internal (xml indent-string)
   "Outputs the XML tree in the current buffer.
 The first line is indented with INDENT-STRING."
@@ -854,7 +883,8 @@ The first line is indented with INDENT-STRING."
     ;;  output the attribute list
     (setq attlist (xml-node-attributes tree))
     (while attlist
-      (insert ?\  (symbol-name (caar attlist)) "=\"" (cdar attlist) ?\")
+      (insert ?\  (symbol-name (caar attlist)) "=\""
+              (xml-escape-string (cdar attlist)) ?\")
       (setq attlist (cdr attlist)))
 
     (setq tree (xml-node-children tree))
@@ -869,7 +899,8 @@ The first line is indented with INDENT-STRING."
         ((listp node)
          (insert ?\n)
          (xml-debug-print-internal node (concat indent-string "  ")))
-        ((stringp node) (insert node))
+        ((stringp node)
+          (insert (xml-escape-string node)))
         (t
          (error "Invalid XML tree"))))