diff options
| author | Joakim Verona | 2010-10-18 22:05:07 +0200 |
|---|---|---|
| committer | Joakim Verona | 2010-10-18 22:05:07 +0200 |
| commit | 13cfe8df462ab8da9f0028e16cc84dcaceaca3d1 (patch) | |
| tree | 723f254768f9e503504ab4c8b68801f80a56591a /src/xml.c | |
| parent | 35f4b80a934b299b3b18e62f5db44f64c240e65b (diff) | |
| parent | e48eb34332dc91de823314090451459ba2ffacbf (diff) | |
| download | emacs-13cfe8df462ab8da9f0028e16cc84dcaceaca3d1.tar.gz emacs-13cfe8df462ab8da9f0028e16cc84dcaceaca3d1.zip | |
merge from upstream
Diffstat (limited to 'src/xml.c')
| -rw-r--r-- | src/xml.c | 153 |
1 files changed, 85 insertions, 68 deletions
| @@ -30,101 +30,118 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ | |||
| 30 | 30 | ||
| 31 | Lisp_Object make_dom (xmlNode *node) | 31 | Lisp_Object make_dom (xmlNode *node) |
| 32 | { | 32 | { |
| 33 | if (node->type == XML_ELEMENT_NODE) { | 33 | if (node->type == XML_ELEMENT_NODE) |
| 34 | Lisp_Object result = Fcons (intern (node->name), Qnil); | 34 | { |
| 35 | xmlNode *child; | 35 | Lisp_Object result = Fcons (intern (node->name), Qnil); |
| 36 | xmlAttr *property; | 36 | xmlNode *child; |
| 37 | 37 | xmlAttr *property; | |
| 38 | /* First add the attributes. */ | 38 | Lisp_Object plist = Qnil; |
| 39 | property = node->properties; | 39 | |
| 40 | while (property != NULL) { | 40 | /* First add the attributes. */ |
| 41 | if (property->children && | 41 | property = node->properties; |
| 42 | property->children->content) { | 42 | while (property != NULL) |
| 43 | char *pname = xmalloc (strlen (property->name) + 2); | 43 | { |
| 44 | *pname = ':'; | 44 | if (property->children && |
| 45 | strcpy(pname + 1, property->name); | 45 | property->children->content) |
| 46 | result = Fcons (Fcons (intern (pname), | 46 | { |
| 47 | build_string(property->children->content)), | 47 | plist = Fcons (Fcons (intern (property->name), |
| 48 | result); | 48 | build_string (property->children->content)), |
| 49 | xfree (pname); | 49 | plist); |
| 50 | } | 50 | } |
| 51 | property = property->next; | 51 | property = property->next; |
| 52 | } | ||
| 53 | result = Fcons (Fnreverse (plist), result); | ||
| 54 | |||
| 55 | /* Then add the children of the node. */ | ||
| 56 | child = node->children; | ||
| 57 | while (child != NULL) | ||
| 58 | { | ||
| 59 | result = Fcons (make_dom (child), result); | ||
| 60 | child = child->next; | ||
| 61 | } | ||
| 62 | |||
| 63 | return Fnreverse (result); | ||
| 52 | } | 64 | } |
| 53 | /* Then add the children of the node. */ | 65 | else if (node->type == XML_TEXT_NODE) |
| 54 | child = node->children; | 66 | { |
| 55 | while (child != NULL) { | 67 | if (node->content) |
| 56 | result = Fcons (make_dom (child), result); | 68 | return build_string (node->content); |
| 57 | child = child->next; | 69 | else |
| 70 | return Qnil; | ||
| 58 | } | 71 | } |
| 59 | return Fnreverse (result); | 72 | else |
| 60 | } else if (node->type == XML_TEXT_NODE) { | ||
| 61 | Lisp_Object content = Qnil; | ||
| 62 | |||
| 63 | if (node->content) | ||
| 64 | content = build_string (node->content); | ||
| 65 | |||
| 66 | return Fcons (intern (node->name), content); | ||
| 67 | } else | ||
| 68 | return Qnil; | 73 | return Qnil; |
| 69 | } | 74 | } |
| 70 | 75 | ||
| 71 | static Lisp_Object | 76 | static Lisp_Object |
| 72 | parse_buffer (Lisp_Object string, Lisp_Object base_url, int htmlp) | 77 | parse_region (Lisp_Object start, Lisp_Object end, Lisp_Object base_url, int htmlp) |
| 73 | { | 78 | { |
| 74 | xmlDoc *doc; | 79 | xmlDoc *doc; |
| 75 | xmlNode *node; | 80 | xmlNode *node; |
| 76 | Lisp_Object result; | 81 | Lisp_Object result = Qnil; |
| 77 | int ibeg, iend; | 82 | const char *burl = ""; |
| 78 | char *burl = ""; | 83 | EMACS_INT bytes; |
| 84 | EMACS_INT istart, iend; | ||
| 79 | 85 | ||
| 80 | LIBXML_TEST_VERSION; | 86 | LIBXML_TEST_VERSION; |
| 81 | 87 | ||
| 82 | CHECK_STRING (string); | 88 | validate_region (&start, &end); |
| 83 | 89 | ||
| 84 | if (! NILP (base_url)) { | 90 | istart = XINT (start); |
| 85 | CHECK_STRING (base_url); | 91 | iend = XINT (end); |
| 86 | burl = SDATA (base_url); | 92 | |
| 87 | } | 93 | if (istart < GPT && GPT < iend) |
| 94 | move_gap (iend); | ||
| 95 | |||
| 96 | if (! NILP (base_url)) | ||
| 97 | { | ||
| 98 | CHECK_STRING (base_url); | ||
| 99 | burl = SDATA (base_url); | ||
| 100 | } | ||
| 101 | |||
| 102 | bytes = CHAR_TO_BYTE (iend) - CHAR_TO_BYTE (istart); | ||
| 88 | 103 | ||
| 89 | if (htmlp) | 104 | if (htmlp) |
| 90 | doc = htmlReadMemory (SDATA (string), SBYTES (string), burl, "utf-8", | 105 | doc = htmlReadMemory (BYTE_POS_ADDR (CHAR_TO_BYTE (istart)), |
| 106 | bytes, burl, "utf-8", | ||
| 91 | HTML_PARSE_RECOVER|HTML_PARSE_NONET| | 107 | HTML_PARSE_RECOVER|HTML_PARSE_NONET| |
| 92 | HTML_PARSE_NOWARNING|HTML_PARSE_NOERROR); | 108 | HTML_PARSE_NOWARNING|HTML_PARSE_NOERROR); |
| 93 | else | 109 | else |
| 94 | doc = xmlReadMemory (SDATA (string), SBYTES (string), burl, "utf-8", | 110 | doc = xmlReadMemory (BYTE_POS_ADDR (CHAR_TO_BYTE (istart)), |
| 111 | bytes, burl, "utf-8", | ||
| 95 | XML_PARSE_NONET|XML_PARSE_NOWARNING| | 112 | XML_PARSE_NONET|XML_PARSE_NOWARNING| |
| 96 | XML_PARSE_NOERROR); | 113 | XML_PARSE_NOERROR); |
| 97 | 114 | ||
| 98 | if (doc != NULL) { | 115 | if (doc != NULL) |
| 99 | node = xmlDocGetRootElement (doc); | 116 | { |
| 100 | if (node != NULL) | 117 | node = xmlDocGetRootElement (doc); |
| 101 | result = make_dom (node); | 118 | if (node != NULL) |
| 102 | 119 | result = make_dom (node); | |
| 103 | xmlFreeDoc (doc); | 120 | xmlFreeDoc (doc); |
| 104 | xmlCleanupParser (); | 121 | xmlCleanupParser (); |
| 105 | } | 122 | } |
| 106 | 123 | ||
| 107 | return result; | 124 | return result; |
| 108 | } | 125 | } |
| 109 | 126 | ||
| 110 | DEFUN ("html-parse-string", Fhtml_parse_string, Shtml_parse_string, | 127 | DEFUN ("libxml-parse-html-region", Flibxml_parse_html_region, |
| 111 | 0, 2, 0, | 128 | Slibxml_parse_html_region, |
| 112 | doc: /* Parse the string as an HTML document and return the parse tree. | 129 | 2, 3, 0, |
| 113 | If BASE-URL is non-nil, it will be used to expand relative URLs in | 130 | doc: /* Parse the region as an HTML document and return the parse tree. |
| 114 | the HTML document.*/) | 131 | If BASE-URL is non-nil, it is used to expand relative URLs. */) |
| 115 | (Lisp_Object string, Lisp_Object base_url) | 132 | (Lisp_Object start, Lisp_Object end, Lisp_Object base_url) |
| 116 | { | 133 | { |
| 117 | return parse_buffer (string, base_url, 1); | 134 | return parse_region (start, end, base_url, 1); |
| 118 | } | 135 | } |
| 119 | 136 | ||
| 120 | DEFUN ("xml-parse-string", Fxml_parse_string, Sxml_parse_string, | 137 | DEFUN ("libxml-parse-xml-region", Flibxml_parse_xml_region, |
| 121 | 0, 2, 0, | 138 | Slibxml_parse_xml_region, |
| 122 | doc: /* Parse the string as an XML document and return the parse tree. | 139 | 2, 3, 0, |
| 123 | If BASE-URL is non-nil, it will be used to expand relative URLs in | 140 | doc: /* Parse the region as an XML document and return the parse tree. |
| 124 | the XML document.*/) | 141 | If BASE-URL is non-nil, it is used to expand relative URLs. */) |
| 125 | (Lisp_Object string, Lisp_Object base_url) | 142 | (Lisp_Object start, Lisp_Object end, Lisp_Object base_url) |
| 126 | { | 143 | { |
| 127 | return parse_buffer (string, base_url, 0); | 144 | return parse_region (start, end, base_url, 0); |
| 128 | } | 145 | } |
| 129 | 146 | ||
| 130 | 147 | ||
| @@ -134,8 +151,8 @@ the XML document.*/) | |||
| 134 | void | 151 | void |
| 135 | syms_of_xml (void) | 152 | syms_of_xml (void) |
| 136 | { | 153 | { |
| 137 | defsubr (&Shtml_parse_string); | 154 | defsubr (&Slibxml_parse_html_region); |
| 138 | defsubr (&Sxml_parse_string); | 155 | defsubr (&Slibxml_parse_xml_region); |
| 139 | } | 156 | } |
| 140 | 157 | ||
| 141 | #endif /* HAVE_LIBXML2 */ | 158 | #endif /* HAVE_LIBXML2 */ |