Fix bug #42294
[apr-util.git] / xml / apr_xml.c
blob83aff45208edcacaf957ea9633e47416c2c1ffa3
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "apr.h"
18 #include "apr_strings.h"
20 #define APR_WANT_STDIO /* for sprintf() */
21 #define APR_WANT_STRFUNC
22 #include "apr_want.h"
24 #include "apr_xml.h"
26 #include "apu_config.h"
28 #if defined(HAVE_XMLPARSE_XMLPARSE_H)
29 #include <xmlparse/xmlparse.h>
30 #elif defined(HAVE_XMLTOK_XMLPARSE_H)
31 #include <xmltok/xmlparse.h>
32 #elif defined(HAVE_XML_XMLPARSE_H)
33 #include <xml/xmlparse.h>
34 #else
35 #include <expat.h>
36 #endif
38 #define DEBUG_CR "\r\n"
40 static const char APR_KW_xmlns[] = { 0x78, 0x6D, 0x6C, 0x6E, 0x73, '\0' };
41 static const char APR_KW_xmlns_lang[] = { 0x78, 0x6D, 0x6C, 0x3A, 0x6C, 0x61, 0x6E, 0x67, '\0' };
42 static const char APR_KW_DAV[] = { 0x44, 0x41, 0x56, 0x3A, '\0' };
44 /* errors related to namespace processing */
45 #define APR_XML_NS_ERROR_UNKNOWN_PREFIX (-1000)
46 #define APR_XML_NS_ERROR_INVALID_DECL (-1001)
48 /* test for a namespace prefix that begins with [Xx][Mm][Ll] */
49 #define APR_XML_NS_IS_RESERVED(name) \
50 ( (name[0] == 0x58 || name[0] == 0x78) && \
51 (name[1] == 0x4D || name[1] == 0x6D) && \
52 (name[2] == 0x4C || name[2] == 0x6C) )
55 /* the real (internal) definition of the parser context */
56 struct apr_xml_parser {
57 apr_xml_doc *doc; /* the doc we're parsing */
58 apr_pool_t *p; /* the pool we allocate from */
59 apr_xml_elem *cur_elem; /* current element */
61 int error; /* an error has occurred */
62 #define APR_XML_ERROR_EXPAT 1
63 #define APR_XML_ERROR_PARSE_DONE 2
64 /* also: public APR_XML_NS_ERROR_* values (if any) */
66 XML_Parser xp; /* the actual (Expat) XML parser */
67 enum XML_Error xp_err; /* stored Expat error code */
70 /* struct for scoping namespace declarations */
71 typedef struct apr_xml_ns_scope {
72 const char *prefix; /* prefix used for this ns */
73 int ns; /* index into namespace table */
74 int emptyURI; /* the namespace URI is the empty string */
75 struct apr_xml_ns_scope *next; /* next scoped namespace */
76 } apr_xml_ns_scope;
79 /* return namespace table index for a given prefix */
80 static int find_prefix(apr_xml_parser *parser, const char *prefix)
82 apr_xml_elem *elem = parser->cur_elem;
85 ** Walk up the tree, looking for a namespace scope that defines this
86 ** prefix.
88 for (; elem; elem = elem->parent) {
89 apr_xml_ns_scope *ns_scope = elem->ns_scope;
91 for (ns_scope = elem->ns_scope; ns_scope; ns_scope = ns_scope->next) {
92 if (strcmp(prefix, ns_scope->prefix) == 0) {
93 if (ns_scope->emptyURI) {
95 ** It is possible to set the default namespace to an
96 ** empty URI string; this resets the default namespace
97 ** to mean "no namespace." We just found the prefix
98 ** refers to an empty URI, so return "no namespace."
100 return APR_XML_NS_NONE;
103 return ns_scope->ns;
109 * If the prefix is empty (""), this means that a prefix was not
110 * specified in the element/attribute. The search that was performed
111 * just above did not locate a default namespace URI (which is stored
112 * into ns_scope with an empty prefix). This means the element/attribute
113 * has "no namespace". We have a reserved value for this.
115 if (*prefix == '\0') {
116 return APR_XML_NS_NONE;
119 /* not found */
120 return APR_XML_NS_ERROR_UNKNOWN_PREFIX;
123 static void start_handler(void *userdata, const char *name, const char **attrs)
125 apr_xml_parser *parser = userdata;
126 apr_xml_elem *elem;
127 apr_xml_attr *attr;
128 apr_xml_attr *prev;
129 char *colon;
130 const char *quoted;
131 char *elem_name;
133 /* punt once we find an error */
134 if (parser->error)
135 return;
137 elem = apr_pcalloc(parser->p, sizeof(*elem));
139 /* prep the element */
140 elem->name = elem_name = apr_pstrdup(parser->p, name);
142 /* fill in the attributes (note: ends up in reverse order) */
143 while (*attrs) {
144 attr = apr_palloc(parser->p, sizeof(*attr));
145 attr->name = apr_pstrdup(parser->p, *attrs++);
146 attr->value = apr_pstrdup(parser->p, *attrs++);
147 attr->next = elem->attr;
148 elem->attr = attr;
151 /* hook the element into the tree */
152 if (parser->cur_elem == NULL) {
153 /* no current element; this also becomes the root */
154 parser->cur_elem = parser->doc->root = elem;
156 else {
157 /* this element appeared within the current elem */
158 elem->parent = parser->cur_elem;
160 /* set up the child/sibling links */
161 if (elem->parent->last_child == NULL) {
162 /* no first child either */
163 elem->parent->first_child = elem->parent->last_child = elem;
165 else {
166 /* hook onto the end of the parent's children */
167 elem->parent->last_child->next = elem;
168 elem->parent->last_child = elem;
171 /* this element is now the current element */
172 parser->cur_elem = elem;
175 /* scan the attributes for namespace declarations */
176 for (prev = NULL, attr = elem->attr;
177 attr;
178 attr = attr->next) {
179 if (strncmp(attr->name, APR_KW_xmlns, 5) == 0) {
180 const char *prefix = &attr->name[5];
181 apr_xml_ns_scope *ns_scope;
183 /* test for xmlns:foo= form and xmlns= form */
184 if (*prefix == 0x3A) {
185 /* a namespace prefix declaration must have a
186 non-empty value. */
187 if (attr->value[0] == '\0') {
188 parser->error = APR_XML_NS_ERROR_INVALID_DECL;
189 return;
191 ++prefix;
193 else if (*prefix != '\0') {
194 /* advance "prev" since "attr" is still present */
195 prev = attr;
196 continue;
199 /* quote the URI before we ever start working with it */
200 quoted = apr_xml_quote_string(parser->p, attr->value, 1);
202 /* build and insert the new scope */
203 ns_scope = apr_pcalloc(parser->p, sizeof(*ns_scope));
204 ns_scope->prefix = prefix;
205 ns_scope->ns = apr_xml_insert_uri(parser->doc->namespaces, quoted);
206 ns_scope->emptyURI = *quoted == '\0';
207 ns_scope->next = elem->ns_scope;
208 elem->ns_scope = ns_scope;
210 /* remove this attribute from the element */
211 if (prev == NULL)
212 elem->attr = attr->next;
213 else
214 prev->next = attr->next;
216 /* Note: prev will not be advanced since we just removed "attr" */
218 else if (strcmp(attr->name, APR_KW_xmlns_lang) == 0) {
219 /* save away the language (in quoted form) */
220 elem->lang = apr_xml_quote_string(parser->p, attr->value, 1);
222 /* remove this attribute from the element */
223 if (prev == NULL)
224 elem->attr = attr->next;
225 else
226 prev->next = attr->next;
228 /* Note: prev will not be advanced since we just removed "attr" */
230 else {
231 /* advance "prev" since "attr" is still present */
232 prev = attr;
237 ** If an xml:lang attribute didn't exist (lang==NULL), then copy the
238 ** language from the parent element (if present).
240 ** NOTE: elem_size() *depends* upon this pointer equality.
242 if (elem->lang == NULL && elem->parent != NULL)
243 elem->lang = elem->parent->lang;
245 /* adjust the element's namespace */
246 colon = strchr(elem_name, 0x3A);
247 if (colon == NULL) {
249 * The element is using the default namespace, which will always
250 * be found. Either it will be "no namespace", or a default
251 * namespace URI has been specified at some point.
253 elem->ns = find_prefix(parser, "");
255 else if (APR_XML_NS_IS_RESERVED(elem->name)) {
256 elem->ns = APR_XML_NS_NONE;
258 else {
259 *colon = '\0';
260 elem->ns = find_prefix(parser, elem->name);
261 elem->name = colon + 1;
263 if (APR_XML_NS_IS_ERROR(elem->ns)) {
264 parser->error = elem->ns;
265 return;
269 /* adjust all remaining attributes' namespaces */
270 for (attr = elem->attr; attr; attr = attr->next) {
272 * apr_xml_attr defines this as "const" but we dup'd it, so we
273 * know that we can change it. a bit hacky, but the existing
274 * structure def is best.
276 char *attr_name = (char *)attr->name;
278 colon = strchr(attr_name, 0x3A);
279 if (colon == NULL) {
281 * Attributes do NOT use the default namespace. Therefore,
282 * we place them into the "no namespace" category.
284 attr->ns = APR_XML_NS_NONE;
286 else if (APR_XML_NS_IS_RESERVED(attr->name)) {
287 attr->ns = APR_XML_NS_NONE;
289 else {
290 *colon = '\0';
291 attr->ns = find_prefix(parser, attr->name);
292 attr->name = colon + 1;
294 if (APR_XML_NS_IS_ERROR(attr->ns)) {
295 parser->error = attr->ns;
296 return;
302 static void end_handler(void *userdata, const char *name)
304 apr_xml_parser *parser = userdata;
306 /* punt once we find an error */
307 if (parser->error)
308 return;
310 /* pop up one level */
311 parser->cur_elem = parser->cur_elem->parent;
314 static void cdata_handler(void *userdata, const char *data, int len)
316 apr_xml_parser *parser = userdata;
317 apr_xml_elem *elem;
318 apr_text_header *hdr;
319 const char *s;
321 /* punt once we find an error */
322 if (parser->error)
323 return;
325 elem = parser->cur_elem;
326 s = apr_pstrndup(parser->p, data, len);
328 if (elem->last_child == NULL) {
329 /* no children yet. this cdata follows the start tag */
330 hdr = &elem->first_cdata;
332 else {
333 /* child elements exist. this cdata follows the last child. */
334 hdr = &elem->last_child->following_cdata;
337 apr_text_append(parser->p, hdr, s);
340 static apr_status_t cleanup_parser(void *ctx)
342 apr_xml_parser *parser = ctx;
344 XML_ParserFree(parser->xp);
345 parser->xp = NULL;
347 return APR_SUCCESS;
350 APU_DECLARE(apr_xml_parser *) apr_xml_parser_create(apr_pool_t *pool)
352 apr_xml_parser *parser = apr_pcalloc(pool, sizeof(*parser));
354 parser->p = pool;
355 parser->doc = apr_pcalloc(pool, sizeof(*parser->doc));
357 parser->doc->namespaces = apr_array_make(pool, 5, sizeof(const char *));
359 /* ### is there a way to avoid hard-coding this? */
360 apr_xml_insert_uri(parser->doc->namespaces, APR_KW_DAV);
362 parser->xp = XML_ParserCreate(NULL);
363 if (parser->xp == NULL) {
364 (*apr_pool_abort_get(pool))(APR_ENOMEM);
365 return NULL;
368 apr_pool_cleanup_register(pool, parser, cleanup_parser,
369 apr_pool_cleanup_null);
371 XML_SetUserData(parser->xp, parser);
372 XML_SetElementHandler(parser->xp, start_handler, end_handler);
373 XML_SetCharacterDataHandler(parser->xp, cdata_handler);
375 return parser;
378 static apr_status_t do_parse(apr_xml_parser *parser,
379 const char *data, apr_size_t len,
380 int is_final)
382 if (parser->xp == NULL) {
383 parser->error = APR_XML_ERROR_PARSE_DONE;
385 else {
386 int rv = XML_Parse(parser->xp, data, len, is_final);
388 if (rv == 0) {
389 parser->error = APR_XML_ERROR_EXPAT;
390 parser->xp_err = XML_GetErrorCode(parser->xp);
394 /* ### better error code? */
395 return parser->error ? APR_EGENERAL : APR_SUCCESS;
398 APU_DECLARE(apr_status_t) apr_xml_parser_feed(apr_xml_parser *parser,
399 const char *data,
400 apr_size_t len)
402 return do_parse(parser, data, len, 0 /* is_final */);
405 APU_DECLARE(apr_status_t) apr_xml_parser_done(apr_xml_parser *parser,
406 apr_xml_doc **pdoc)
408 char end;
409 apr_status_t status = do_parse(parser, &end, 0, 1 /* is_final */);
411 /* get rid of the parser */
412 (void) apr_pool_cleanup_run(parser->p, parser, cleanup_parser);
414 if (status)
415 return status;
417 if (pdoc != NULL)
418 *pdoc = parser->doc;
419 return APR_SUCCESS;
422 APU_DECLARE(char *) apr_xml_parser_geterror(apr_xml_parser *parser,
423 char *errbuf,
424 apr_size_t errbufsize)
426 int error = parser->error;
427 const char *msg;
429 /* clear our record of an error */
430 parser->error = 0;
432 switch (error) {
433 case 0:
434 msg = "No error.";
435 break;
437 case APR_XML_NS_ERROR_UNKNOWN_PREFIX:
438 msg = "An undefined namespace prefix was used.";
439 break;
441 case APR_XML_NS_ERROR_INVALID_DECL:
442 msg = "A namespace prefix was defined with an empty URI.";
443 break;
445 case APR_XML_ERROR_EXPAT:
446 (void) apr_snprintf(errbuf, errbufsize,
447 "XML parser error code: %s (%d)",
448 XML_ErrorString(parser->xp_err), parser->xp_err);
449 return errbuf;
451 case APR_XML_ERROR_PARSE_DONE:
452 msg = "The parser is not active.";
453 break;
455 default:
456 msg = "There was an unknown error within the XML body.";
457 break;
460 (void) apr_cpystrn(errbuf, msg, errbufsize);
461 return errbuf;
464 APU_DECLARE(apr_status_t) apr_xml_parse_file(apr_pool_t *p,
465 apr_xml_parser **parser,
466 apr_xml_doc **ppdoc,
467 apr_file_t *xmlfd,
468 apr_size_t buffer_length)
470 apr_status_t rv;
471 char *buffer;
472 apr_size_t length;
474 *parser = apr_xml_parser_create(p);
475 if (*parser == NULL) {
476 /* FIXME: returning an error code would be nice,
477 * but we dont get one ;( */
478 return APR_EGENERAL;
480 buffer = apr_palloc(p, buffer_length);
481 length = buffer_length;
483 rv = apr_file_read(xmlfd, buffer, &length);
485 while (rv == APR_SUCCESS) {
486 rv = apr_xml_parser_feed(*parser, buffer, length);
487 if (rv != APR_SUCCESS) {
488 return rv;
491 length = buffer_length;
492 rv = apr_file_read(xmlfd, buffer, &length);
494 if (rv != APR_EOF) {
495 return rv;
497 rv = apr_xml_parser_done(*parser, ppdoc);
498 *parser = NULL;
499 return rv;
502 APU_DECLARE(void) apr_text_append(apr_pool_t * p, apr_text_header *hdr,
503 const char *text)
505 apr_text *t = apr_palloc(p, sizeof(*t));
507 t->text = text;
508 t->next = NULL;
510 if (hdr->first == NULL) {
511 /* no text elements yet */
512 hdr->first = hdr->last = t;
514 else {
515 /* append to the last text element */
516 hdr->last->next = t;
517 hdr->last = t;
522 /* ---------------------------------------------------------------
524 ** XML UTILITY FUNCTIONS
528 ** apr_xml_quote_string: quote an XML string
530 ** Replace '<', '>', and '&' with '&lt;', '&gt;', and '&amp;'.
531 ** If quotes is true, then replace '"' with '&quot;'.
533 ** quotes is typically set to true for XML strings that will occur within
534 ** double quotes -- attribute values.
536 APU_DECLARE(const char *) apr_xml_quote_string(apr_pool_t *p, const char *s,
537 int quotes)
539 const char *scan;
540 apr_size_t len = 0;
541 apr_size_t extra = 0;
542 char *qstr;
543 char *qscan;
544 char c;
546 for (scan = s; (c = *scan) != '\0'; ++scan, ++len) {
547 if (c == '<' || c == '>')
548 extra += 3; /* &lt; or &gt; */
549 else if (c == '&')
550 extra += 4; /* &amp; */
551 else if (quotes && c == '"')
552 extra += 5; /* &quot; */
555 /* nothing to do? */
556 if (extra == 0)
557 return s;
559 qstr = apr_palloc(p, len + extra + 1);
560 for (scan = s, qscan = qstr; (c = *scan) != '\0'; ++scan) {
561 if (c == '<') {
562 *qscan++ = '&';
563 *qscan++ = 'l';
564 *qscan++ = 't';
565 *qscan++ = ';';
567 else if (c == '>') {
568 *qscan++ = '&';
569 *qscan++ = 'g';
570 *qscan++ = 't';
571 *qscan++ = ';';
573 else if (c == '&') {
574 *qscan++ = '&';
575 *qscan++ = 'a';
576 *qscan++ = 'm';
577 *qscan++ = 'p';
578 *qscan++ = ';';
580 else if (quotes && c == '"') {
581 *qscan++ = '&';
582 *qscan++ = 'q';
583 *qscan++ = 'u';
584 *qscan++ = 'o';
585 *qscan++ = 't';
586 *qscan++ = ';';
588 else {
589 *qscan++ = c;
593 *qscan = '\0';
594 return qstr;
597 /* how many characters for the given integer? */
598 #define APR_XML_NS_LEN(ns) ((ns) < 10 ? 1 : (ns) < 100 ? 2 : (ns) < 1000 ? 3 : \
599 (ns) < 10000 ? 4 : (ns) < 100000 ? 5 : \
600 (ns) < 1000000 ? 6 : (ns) < 10000000 ? 7 : \
601 (ns) < 100000000 ? 8 : (ns) < 1000000000 ? 9 : 10)
603 static apr_size_t text_size(const apr_text *t)
605 apr_size_t size = 0;
607 for (; t; t = t->next)
608 size += strlen(t->text);
609 return size;
612 static apr_size_t elem_size(const apr_xml_elem *elem, int style,
613 apr_array_header_t *namespaces, int *ns_map)
615 apr_size_t size;
617 if (style == APR_XML_X2T_FULL || style == APR_XML_X2T_FULL_NS_LANG) {
618 const apr_xml_attr *attr;
620 size = 0;
622 if (style == APR_XML_X2T_FULL_NS_LANG) {
623 int i;
626 ** The outer element will contain xmlns:ns%d="%s" attributes
627 ** and an xml:lang attribute, if applicable.
630 for (i = namespaces->nelts; i--;) {
631 /* compute size of: ' xmlns:ns%d="%s"' */
632 size += (9 + APR_XML_NS_LEN(i) + 2 +
633 strlen(APR_XML_GET_URI_ITEM(namespaces, i)) + 1);
636 if (elem->lang != NULL) {
637 /* compute size of: ' xml:lang="%s"' */
638 size += 11 + strlen(elem->lang) + 1;
642 if (elem->ns == APR_XML_NS_NONE) {
643 /* compute size of: <%s> */
644 size += 1 + strlen(elem->name) + 1;
646 else {
647 int ns = ns_map ? ns_map[elem->ns] : elem->ns;
649 /* compute size of: <ns%d:%s> */
650 size += 3 + APR_XML_NS_LEN(ns) + 1 + strlen(elem->name) + 1;
653 if (APR_XML_ELEM_IS_EMPTY(elem)) {
654 /* insert a closing "/" */
655 size += 1;
657 else {
659 * two of above plus "/":
660 * <ns%d:%s> ... </ns%d:%s>
661 * OR <%s> ... </%s>
663 size = 2 * size + 1;
666 for (attr = elem->attr; attr; attr = attr->next) {
667 if (attr->ns == APR_XML_NS_NONE) {
668 /* compute size of: ' %s="%s"' */
669 size += 1 + strlen(attr->name) + 2 + strlen(attr->value) + 1;
671 else {
672 /* compute size of: ' ns%d:%s="%s"' */
673 int ns = ns_map ? ns_map[attr->ns] : attr->ns;
674 size += 3 + APR_XML_NS_LEN(ns) + 1 + strlen(attr->name) + 2 + strlen(attr->value) + 1;
679 ** If the element has an xml:lang value that is *different* from
680 ** its parent, then add the thing in: ' xml:lang="%s"'.
682 ** NOTE: we take advantage of the pointer equality established by
683 ** the parsing for "inheriting" the xml:lang values from parents.
685 if (elem->lang != NULL &&
686 (elem->parent == NULL || elem->lang != elem->parent->lang)) {
687 size += 11 + strlen(elem->lang) + 1;
690 else if (style == APR_XML_X2T_LANG_INNER) {
692 * This style prepends the xml:lang value plus a null terminator.
693 * If a lang value is not present, then we insert a null term.
695 size = elem->lang ? strlen(elem->lang) + 1 : 1;
697 else
698 size = 0;
700 size += text_size(elem->first_cdata.first);
702 for (elem = elem->first_child; elem; elem = elem->next) {
703 /* the size of the child element plus the CDATA that follows it */
704 size += (elem_size(elem, APR_XML_X2T_FULL, NULL, ns_map) +
705 text_size(elem->following_cdata.first));
708 return size;
711 static char *write_text(char *s, const apr_text *t)
713 for (; t; t = t->next) {
714 apr_size_t len = strlen(t->text);
715 memcpy(s, t->text, len);
716 s += len;
718 return s;
721 static char *write_elem(char *s, const apr_xml_elem *elem, int style,
722 apr_array_header_t *namespaces, int *ns_map)
724 const apr_xml_elem *child;
725 apr_size_t len;
726 int ns;
728 if (style == APR_XML_X2T_FULL || style == APR_XML_X2T_FULL_NS_LANG) {
729 int empty = APR_XML_ELEM_IS_EMPTY(elem);
730 const apr_xml_attr *attr;
732 if (elem->ns == APR_XML_NS_NONE) {
733 len = sprintf(s, "<%s", elem->name);
735 else {
736 ns = ns_map ? ns_map[elem->ns] : elem->ns;
737 len = sprintf(s, "<ns%d:%s", ns, elem->name);
739 s += len;
741 for (attr = elem->attr; attr; attr = attr->next) {
742 if (attr->ns == APR_XML_NS_NONE)
743 len = sprintf(s, " %s=\"%s\"", attr->name, attr->value);
744 else {
745 ns = ns_map ? ns_map[attr->ns] : attr->ns;
746 len = sprintf(s, " ns%d:%s=\"%s\"", ns, attr->name, attr->value);
748 s += len;
751 /* add the xml:lang value if necessary */
752 if (elem->lang != NULL &&
753 (style == APR_XML_X2T_FULL_NS_LANG ||
754 elem->parent == NULL ||
755 elem->lang != elem->parent->lang)) {
756 len = sprintf(s, " xml:lang=\"%s\"", elem->lang);
757 s += len;
760 /* add namespace definitions, if required */
761 if (style == APR_XML_X2T_FULL_NS_LANG) {
762 int i;
764 for (i = namespaces->nelts; i--;) {
765 len = sprintf(s, " xmlns:ns%d=\"%s\"", i,
766 APR_XML_GET_URI_ITEM(namespaces, i));
767 s += len;
771 /* no more to do. close it up and go. */
772 if (empty) {
773 *s++ = '/';
774 *s++ = '>';
775 return s;
778 /* just close it */
779 *s++ = '>';
781 else if (style == APR_XML_X2T_LANG_INNER) {
782 /* prepend the xml:lang value */
783 if (elem->lang != NULL) {
784 len = strlen(elem->lang);
785 memcpy(s, elem->lang, len);
786 s += len;
788 *s++ = '\0';
791 s = write_text(s, elem->first_cdata.first);
793 for (child = elem->first_child; child; child = child->next) {
794 s = write_elem(s, child, APR_XML_X2T_FULL, NULL, ns_map);
795 s = write_text(s, child->following_cdata.first);
798 if (style == APR_XML_X2T_FULL || style == APR_XML_X2T_FULL_NS_LANG) {
799 if (elem->ns == APR_XML_NS_NONE) {
800 len = sprintf(s, "</%s>", elem->name);
802 else {
803 ns = ns_map ? ns_map[elem->ns] : elem->ns;
804 len = sprintf(s, "</ns%d:%s>", ns, elem->name);
806 s += len;
809 return s;
812 APU_DECLARE(void) apr_xml_quote_elem(apr_pool_t *p, apr_xml_elem *elem)
814 apr_text *scan_txt;
815 apr_xml_attr *scan_attr;
816 apr_xml_elem *scan_elem;
818 /* convert the element's text */
819 for (scan_txt = elem->first_cdata.first;
820 scan_txt != NULL;
821 scan_txt = scan_txt->next) {
822 scan_txt->text = apr_xml_quote_string(p, scan_txt->text, 0);
824 for (scan_txt = elem->following_cdata.first;
825 scan_txt != NULL;
826 scan_txt = scan_txt->next) {
827 scan_txt->text = apr_xml_quote_string(p, scan_txt->text, 0);
830 /* convert the attribute values */
831 for (scan_attr = elem->attr;
832 scan_attr != NULL;
833 scan_attr = scan_attr->next) {
834 scan_attr->value = apr_xml_quote_string(p, scan_attr->value, 1);
837 /* convert the child elements */
838 for (scan_elem = elem->first_child;
839 scan_elem != NULL;
840 scan_elem = scan_elem->next) {
841 apr_xml_quote_elem(p, scan_elem);
845 /* convert an element to a text string */
846 APU_DECLARE(void) apr_xml_to_text(apr_pool_t * p, const apr_xml_elem *elem,
847 int style, apr_array_header_t *namespaces,
848 int *ns_map, const char **pbuf,
849 apr_size_t *psize)
851 /* get the exact size, plus a null terminator */
852 apr_size_t size = elem_size(elem, style, namespaces, ns_map) + 1;
853 char *s = apr_palloc(p, size);
855 (void) write_elem(s, elem, style, namespaces, ns_map);
856 s[size - 1] = '\0';
858 *pbuf = s;
859 if (psize)
860 *psize = size;
863 APU_DECLARE(const char *) apr_xml_empty_elem(apr_pool_t * p,
864 const apr_xml_elem *elem)
866 if (elem->ns == APR_XML_NS_NONE) {
868 * The prefix (xml...) is already within the prop name, or
869 * the element simply has no prefix.
871 return apr_psprintf(p, "<%s/>" DEBUG_CR, elem->name);
874 return apr_psprintf(p, "<ns%d:%s/>" DEBUG_CR, elem->ns, elem->name);
877 /* return the URI's (existing) index, or insert it and return a new index */
878 APU_DECLARE(int) apr_xml_insert_uri(apr_array_header_t *uri_array,
879 const char *uri)
881 int i;
882 const char **pelt;
884 /* never insert an empty URI; this index is always APR_XML_NS_NONE */
885 if (*uri == '\0')
886 return APR_XML_NS_NONE;
888 for (i = uri_array->nelts; i--;) {
889 if (strcmp(uri, APR_XML_GET_URI_ITEM(uri_array, i)) == 0)
890 return i;
893 pelt = apr_array_push(uri_array);
894 *pelt = uri; /* assume uri is const or in a pool */
895 return uri_array->nelts - 1;
898 /* convert the element to EBCDIC */
899 #if APR_CHARSET_EBCDIC
900 static apr_status_t apr_xml_parser_convert_elem(apr_xml_elem *e,
901 apr_xlate_t *convset)
903 apr_xml_attr *a;
904 apr_xml_elem *ec;
905 apr_text *t;
906 apr_size_t inbytes_left, outbytes_left;
907 apr_status_t status;
909 inbytes_left = outbytes_left = strlen(e->name);
910 status = apr_xlate_conv_buffer(convset, e->name, &inbytes_left, (char *) e->name, &outbytes_left);
911 if (status) {
912 return status;
915 for (t = e->first_cdata.first; t != NULL; t = t->next) {
916 inbytes_left = outbytes_left = strlen(t->text);
917 status = apr_xlate_conv_buffer(convset, t->text, &inbytes_left, (char *) t->text, &outbytes_left);
918 if (status) {
919 return status;
923 for (t = e->following_cdata.first; t != NULL; t = t->next) {
924 inbytes_left = outbytes_left = strlen(t->text);
925 status = apr_xlate_conv_buffer(convset, t->text, &inbytes_left, (char *) t->text, &outbytes_left);
926 if (status) {
927 return status;
931 for (a = e->attr; a != NULL; a = a->next) {
932 inbytes_left = outbytes_left = strlen(a->name);
933 status = apr_xlate_conv_buffer(convset, a->name, &inbytes_left, (char *) a->name, &outbytes_left);
934 if (status) {
935 return status;
937 inbytes_left = outbytes_left = strlen(a->value);
938 status = apr_xlate_conv_buffer(convset, a->value, &inbytes_left, (char *) a->value, &outbytes_left);
939 if (status) {
940 return status;
944 for (ec = e->first_child; ec != NULL; ec = ec->next) {
945 status = apr_xml_parser_convert_elem(ec, convset);
946 if (status) {
947 return status;
950 return APR_SUCCESS;
953 /* convert the whole document to EBCDIC */
954 APU_DECLARE(apr_status_t) apr_xml_parser_convert_doc(apr_pool_t *pool,
955 apr_xml_doc *pdoc,
956 apr_xlate_t *convset)
958 apr_status_t status;
959 /* Don't convert the namespaces: they are constant! */
960 if (pdoc->namespaces != NULL) {
961 int i;
962 apr_array_header_t *namespaces;
963 namespaces = apr_array_make(pool, pdoc->namespaces->nelts, sizeof(const char *));
964 if (namespaces == NULL)
965 return APR_ENOMEM;
966 for (i = 0; i < pdoc->namespaces->nelts; i++) {
967 apr_size_t inbytes_left, outbytes_left;
968 char *ptr = (char *) APR_XML_GET_URI_ITEM(pdoc->namespaces, i);
969 ptr = apr_pstrdup(pool, ptr);
970 if ( ptr == NULL)
971 return APR_ENOMEM;
972 inbytes_left = outbytes_left = strlen(ptr);
973 status = apr_xlate_conv_buffer(convset, ptr, &inbytes_left, ptr, &outbytes_left);
974 if (status) {
975 return status;
977 apr_xml_insert_uri(namespaces, ptr);
979 pdoc->namespaces = namespaces;
981 return apr_xml_parser_convert_elem(pdoc->root, convset);
983 #endif