annotate src/xml.c @ 110302:fd8902911ce3

Add support for the libxml2 library. This adds the html-parse-string and xml-parse-string functions in the new file src/xml.c, as well as autoconf detection of the library.
author Lars Magne Ingebrigtsen <larsi@gnus.org>
date Fri, 10 Sep 2010 18:44:35 +0200
parents
children fdcc2e819a9b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
110302
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
1 /* Interface to libxml2.
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
2 Copyright (C) 2010 Free Software Foundation, Inc.
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
3
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
4 This file is part of GNU Emacs.
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
5
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
6 GNU Emacs is free software: you can redistribute it and/or modify
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
8 the Free Software Foundation, either version 3 of the License, or
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
9 (at your option) any later version.
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
10
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
11 GNU Emacs is distributed in the hope that it will be useful,
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
14 GNU General Public License for more details.
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
15
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
18
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
19 #include <config.h>
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
20
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
21 #ifdef HAVE_LIBXML2
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
22
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
23 #include <setjmp.h>
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
24 #include <libxml/tree.h>
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
25 #include <libxml/parser.h>
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
26 #include <libxml/HTMLparser.h>
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
27
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
28 #include "lisp.h"
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
29 #include "buffer.h"
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
30
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
31 Lisp_Object make_dom (xmlNode *node)
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
32 {
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
33 if (node->type == XML_ELEMENT_NODE) {
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
34 Lisp_Object result = Fcons (intern (node->name), Qnil);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
35 xmlNode *child;
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
36 xmlAttr *property;
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
37
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
38 /* First add the attributes. */
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
39 property = node->properties;
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
40 while (property != NULL) {
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
41 if (property->children &&
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
42 property->children->content) {
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
43 char *pname = xmalloc (strlen (property->name) + 2);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
44 *pname = ':';
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
45 strcpy(pname + 1, property->name);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
46 result = Fcons (Fcons (intern (pname),
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
47 build_string(property->children->content)),
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
48 result);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
49 xfree (pname);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
50 }
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
51 property = property->next;
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
52 }
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
53 /* Then add the children of the node. */
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
54 child = node->children;
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
55 while (child != NULL) {
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
56 result = Fcons (make_dom (child), result);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
57 child = child->next;
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
58 }
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
59 return Fnreverse (result);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
60 } else if (node->type == XML_TEXT_NODE) {
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
61 Lisp_Object content = Qnil;
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
62
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
63 if (node->content)
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
64 content = build_string (node->content);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
65
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
66 return Fcons (intern (node->name), content);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
67 } else
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
68 return Qnil;
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
69 }
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
70
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
71 static Lisp_Object
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
72 parse_buffer (Lisp_Object string, Lisp_Object base_url, int htmlp)
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
73 {
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
74 xmlDoc *doc;
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
75 xmlNode *node;
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
76 Lisp_Object result;
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
77 int ibeg, iend;
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
78 char *burl = "";
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
79
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
80 LIBXML_TEST_VERSION;
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
81
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
82 CHECK_STRING (string);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
83
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
84 if (! NILP (base_url)) {
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
85 CHECK_STRING (base_url);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
86 burl = SDATA (base_url);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
87 }
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
88
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
89 if (htmlp)
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
90 doc = htmlReadMemory (SDATA (string), SBYTES (string), burl, "utf-8",
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
91 HTML_PARSE_RECOVER|HTML_PARSE_NONET|
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
92 HTML_PARSE_NOWARNING|HTML_PARSE_NOERROR);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
93 else
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
94 doc = xmlReadMemory (SDATA (string), SBYTES (string), burl, "utf-8",
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
95 XML_PARSE_NONET|XML_PARSE_NOWARNING|
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
96 XML_PARSE_NOERROR);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
97
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
98 if (doc != NULL) {
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
99 node = xmlDocGetRootElement (doc);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
100 if (node != NULL)
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
101 result = make_dom (node);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
102
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
103 xmlFreeDoc (doc);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
104 xmlCleanupParser ();
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
105 }
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
106
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
107 return result;
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
108 }
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
109
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
110 DEFUN ("html-parse-string", Fhtml_parse_string, Shtml_parse_string,
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
111 0, 2, 0,
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
112 doc: /* Parse the string as an HTML document and return the parse tree.*/)
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
113 (Lisp_Object string, Lisp_Object base_url)
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
114 {
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
115 return parse_buffer (string, base_url, 1);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
116 }
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
117
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
118 DEFUN ("xml-parse-string", Fxml_parse_string, Sxml_parse_string,
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
119 0, 2, 0,
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
120 doc: /* Parse the string as an XML document and return the parse tree.*/)
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
121 (Lisp_Object string, Lisp_Object base_url)
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
122 {
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
123 return parse_buffer (string, base_url, 0);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
124 }
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
125
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
126
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
127 /***********************************************************************
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
128 Initialization
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
129 ***********************************************************************/
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
130 void
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
131 syms_of_xml (void)
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
132 {
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
133 defsubr (&Shtml_parse_string);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
134 defsubr (&Sxml_parse_string);
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
135 }
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
136
fd8902911ce3 Add support for the libxml2 library.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
diff changeset
137 #endif /* HAVE_LIBXML2 */