Don't configure MySQL if the driver isn't there.
[apr-util.git] / xml / apr_xml.c
blob73543555ff800ced98d133e83753884ab0c72fe9
1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
2 * applicable.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * 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 #ifdef APR_HAVE_OLD_EXPAT
29 #include "xmlparse.h"
30 #else
31 #include "expat.h"
32 #endif
34 #define DEBUG_CR "\r\n"
36 static const char APR_KW_xmlns[] = { 0x78, 0x6D, 0x6C, 0x6E, 0x73, '\0' };
37 static const char APR_KW_xmlns_lang[] = { 0x78, 0x6D, 0x6C, 0x3A, 0x6C, 0x61, 0x6E, 0x67, '\0' };
38 static const char APR_KW_DAV[] = { 0x44, 0x41, 0x56, 0x3A, '\0' };
40 /* errors related to namespace processing */
41 #define APR_XML_NS_ERROR_UNKNOWN_PREFIX (-1000)
42 #define APR_XML_NS_ERROR_INVALID_DECL (-1001)
44 /* test for a namespace prefix that begins with [Xx][Mm][Ll] */
45 #define APR_XML_NS_IS_RESERVED(name) \
46 ( (name[0] == 0x58 || name[0] == 0x78) && \
47 (name[1] == 0x4D || name[1] == 0x6D) && \
48 (name[2] == 0x4C || name[2] == 0x6C) )
51 /* the real (internal) definition of the parser context */
52 struct apr_xml_parser {
53 apr_xml_doc *doc; /* the doc we're parsing */
54 apr_pool_t *p; /* the pool we allocate from */
55 apr_xml_elem *cur_elem; /* current element */
57 int error; /* an error has occurred */
58 #define APR_XML_ERROR_EXPAT 1
59 #define APR_XML_ERROR_PARSE_DONE 2
60 /* also: public APR_XML_NS_ERROR_* values (if any) */
62 XML_Parser xp; /* the actual (Expat) XML parser */
63 enum XML_Error xp_err; /* stored Expat error code */
66 /* struct for scoping namespace declarations */
67 typedef struct apr_xml_ns_scope {
68 const char *prefix; /* prefix used for this ns */
69 int ns; /* index into namespace table */
70 int emptyURI; /* the namespace URI is the empty string */
71 struct apr_xml_ns_scope *next; /* next scoped namespace */
72 } apr_xml_ns_scope;
75 /* return namespace table index for a given prefix */
76 static int find_prefix(apr_xml_parser *parser, const char *prefix)
78 apr_xml_elem *elem = parser->cur_elem;
81 ** Walk up the tree, looking for a namespace scope that defines this
82 ** prefix.
84 for (; elem; elem = elem->parent) {
85 apr_xml_ns_scope *ns_scope = elem->ns_scope;
87 for (ns_scope = elem->ns_scope; ns_scope; ns_scope = ns_scope->next) {
88 if (strcmp(prefix, ns_scope->prefix) == 0) {
89 if (ns_scope->emptyURI) {
91 ** It is possible to set the default namespace to an
92 ** empty URI string; this resets the default namespace
93 ** to mean "no namespace." We just found the prefix
94 ** refers to an empty URI, so return "no namespace."
96 return APR_XML_NS_NONE;
99 return ns_scope->ns;
105 * If the prefix is empty (""), this means that a prefix was not
106 * specified in the element/attribute. The search that was performed
107 * just above did not locate a default namespace URI (which is stored
108 * into ns_scope with an empty prefix). This means the element/attribute
109 * has "no namespace". We have a reserved value for this.
111 if (*prefix == '\0') {
112 return APR_XML_NS_NONE;
115 /* not found */
116 return APR_XML_NS_ERROR_UNKNOWN_PREFIX;
119 static void start_handler(void *userdata, const char *name, const char **attrs)
121 apr_xml_parser *parser = userdata;
122 apr_xml_elem *elem;
123 apr_xml_attr *attr;
124 apr_xml_attr *prev;
125 char *colon;
126 const char *quoted;
127 char *elem_name;
129 /* punt once we find an error */
130 if (parser->error)
131 return;
133 elem = apr_pcalloc(parser->p, sizeof(*elem));
135 /* prep the element */
136 elem->name = elem_name = apr_pstrdup(parser->p, name);
138 /* fill in the attributes (note: ends up in reverse order) */
139 while (*attrs) {
140 attr = apr_palloc(parser->p, sizeof(*attr));
141 attr->name = apr_pstrdup(parser->p, *attrs++);
142 attr->value = apr_pstrdup(parser->p, *attrs++);
143 attr->next = elem->attr;
144 elem->attr = attr;
147 /* hook the element into the tree */
148 if (parser->cur_elem == NULL) {
149 /* no current element; this also becomes the root */
150 parser->cur_elem = parser->doc->root = elem;
152 else {
153 /* this element appeared within the current elem */
154 elem->parent = parser->cur_elem;
156 /* set up the child/sibling links */
157 if (elem->parent->last_child == NULL) {
158 /* no first child either */
159 elem->parent->first_child = elem->parent->last_child = elem;
161 else {
162 /* hook onto the end of the parent's children */
163 elem->parent->last_child->next = elem;
164 elem->parent->last_child = elem;
167 /* this element is now the current element */
168 parser->cur_elem = elem;
171 /* scan the attributes for namespace declarations */
172 for (prev = NULL, attr = elem->attr;
173 attr;
174 attr = attr->next) {
175 if (strncmp(attr->name, APR_KW_xmlns, 5) == 0) {
176 const char *prefix = &attr->name[5];
177 apr_xml_ns_scope *ns_scope;
179 /* test for xmlns:foo= form and xmlns= form */
180 if (*prefix == 0x3A) {
181 /* a namespace prefix declaration must have a
182 non-empty value. */
183 if (attr->value[0] == '\0') {
184 parser->error = APR_XML_NS_ERROR_INVALID_DECL;
185 return;
187 ++prefix;
189 else if (*prefix != '\0') {
190 /* advance "prev" since "attr" is still present */
191 prev = attr;
192 continue;
195 /* quote the URI before we ever start working with it */
196 quoted = apr_xml_quote_string(parser->p, attr->value, 1);
198 /* build and insert the new scope */
199 ns_scope = apr_pcalloc(parser->p, sizeof(*ns_scope));
200 ns_scope->prefix = prefix;
201 ns_scope->ns = apr_xml_insert_uri(parser->doc->namespaces, quoted);
202 ns_scope->emptyURI = *quoted == '\0';
203 ns_scope->next = elem->ns_scope;
204 elem->ns_scope = ns_scope;
206 /* remove this attribute from the element */
207 if (prev == NULL)
208 elem->attr = attr->next;
209 else
210 prev->next = attr->next;
212 /* Note: prev will not be advanced since we just removed "attr" */
214 else if (strcmp(attr->name, APR_KW_xmlns_lang) == 0) {
215 /* save away the language (in quoted form) */
216 elem->lang = apr_xml_quote_string(parser->p, attr->value, 1);
218 /* remove this attribute from the element */
219 if (prev == NULL)
220 elem->attr = attr->next;
221 else
222 prev->next = attr->next;
224 /* Note: prev will not be advanced since we just removed "attr" */
226 else {
227 /* advance "prev" since "attr" is still present */
228 prev = attr;
233 ** If an xml:lang attribute didn't exist (lang==NULL), then copy the
234 ** language from the parent element (if present).
236 ** NOTE: elem_size() *depends* upon this pointer equality.
238 if (elem->lang == NULL && elem->parent != NULL)
239 elem->lang = elem->parent->lang;
241 /* adjust the element's namespace */
242 colon = strchr(elem_name, 0x3A);
243 if (colon == NULL) {
245 * The element is using the default namespace, which will always
246 * be found. Either it will be "no namespace", or a default
247 * namespace URI has been specified at some point.
249 elem->ns = find_prefix(parser, "");
251 else if (APR_XML_NS_IS_RESERVED(elem->name)) {
252 elem->ns = APR_XML_NS_NONE;
254 else {
255 *colon = '\0';
256 elem->ns = find_prefix(parser, elem->name);
257 elem->name = colon + 1;
259 if (APR_XML_NS_IS_ERROR(elem->ns)) {
260 parser->error = elem->ns;
261 return;
265 /* adjust all remaining attributes' namespaces */
266 for (attr = elem->attr; attr; attr = attr->next) {
268 * apr_xml_attr defines this as "const" but we dup'd it, so we
269 * know that we can change it. a bit hacky, but the existing
270 * structure def is best.
272 char *attr_name = (char *)attr->name;
274 colon = strchr(attr_name, 0x3A);
275 if (colon == NULL) {
277 * Attributes do NOT use the default namespace. Therefore,
278 * we place them into the "no namespace" category.
280 attr->ns = APR_XML_NS_NONE;
282 else if (APR_XML_NS_IS_RESERVED(attr->name)) {
283 attr->ns = APR_XML_NS_NONE;
285 else {
286 *colon = '\0';
287 attr->ns = find_prefix(parser, attr->name);
288 attr->name = colon + 1;
290 if (APR_XML_NS_IS_ERROR(attr->ns)) {
291 parser->error = attr->ns;
292 return;
298 static void end_handler(void *userdata, const char *name)
300 apr_xml_parser *parser = userdata;
302 /* punt once we find an error */
303 if (parser->error)
304 return;
306 /* pop up one level */
307 parser->cur_elem = parser->cur_elem->parent;
310 static void cdata_handler(void *userdata, const char *data, int len)
312 apr_xml_parser *parser = userdata;
313 apr_xml_elem *elem;
314 apr_text_header *hdr;
315 const char *s;
317 /* punt once we find an error */
318 if (parser->error)
319 return;
321 elem = parser->cur_elem;
322 s = apr_pstrndup(parser->p, data, len);
324 if (elem->last_child == NULL) {
325 /* no children yet. this cdata follows the start tag */
326 hdr = &elem->first_cdata;
328 else {
329 /* child elements exist. this cdata follows the last child. */
330 hdr = &elem->last_child->following_cdata;
333 apr_text_append(parser->p, hdr, s);
336 static apr_status_t cleanup_parser(void *ctx)
338 apr_xml_parser *parser = ctx;
340 XML_ParserFree(parser->xp);
341 parser->xp = NULL;
343 return APR_SUCCESS;
346 APU_DECLARE(apr_xml_parser *) apr_xml_parser_create(apr_pool_t *pool)
348 apr_xml_parser *parser = apr_pcalloc(pool, sizeof(*parser));
350 parser->p = pool;
351 parser->doc = apr_pcalloc(pool, sizeof(*parser->doc));
353 parser->doc->namespaces = apr_array_make(pool, 5, sizeof(const char *));
355 /* ### is there a way to avoid hard-coding this? */
356 apr_xml_insert_uri(parser->doc->namespaces, APR_KW_DAV);
358 parser->xp = XML_ParserCreate(NULL);
359 if (parser->xp == NULL) {
360 (*apr_pool_abort_get(pool))(APR_ENOMEM);
361 return NULL;
364 apr_pool_cleanup_register(pool, parser, cleanup_parser,
365 apr_pool_cleanup_null);
367 XML_SetUserData(parser->xp, parser);
368 XML_SetElementHandler(parser->xp, start_handler, end_handler);
369 XML_SetCharacterDataHandler(parser->xp, cdata_handler);
371 return parser;
374 static apr_status_t do_parse(apr_xml_parser *parser,
375 const char *data, apr_size_t len,
376 int is_final)
378 if (parser->xp == NULL) {
379 parser->error = APR_XML_ERROR_PARSE_DONE;
381 else {
382 int rv = XML_Parse(parser->xp, data, len, is_final);
384 if (rv == 0) {
385 parser->error = APR_XML_ERROR_EXPAT;
386 parser->xp_err = XML_GetErrorCode(parser->xp);
390 /* ### better error code? */
391 return parser->error ? APR_EGENERAL : APR_SUCCESS;
394 APU_DECLARE(apr_status_t) apr_xml_parser_feed(apr_xml_parser *parser,
395 const char *data,
396 apr_size_t len)
398 return do_parse(parser, data, len, 0 /* is_final */);
401 APU_DECLARE(apr_status_t) apr_xml_parser_done(apr_xml_parser *parser,
402 apr_xml_doc **pdoc)
404 char end;
405 apr_status_t status = do_parse(parser, &end, 0, 1 /* is_final */);
407 /* get rid of the parser */
408 (void) apr_pool_cleanup_run(parser->p, parser, cleanup_parser);
410 if (status)
411 return status;
413 if (pdoc != NULL)
414 *pdoc = parser->doc;
415 return APR_SUCCESS;
418 APU_DECLARE(char *) apr_xml_parser_geterror(apr_xml_parser *parser,
419 char *errbuf,
420 apr_size_t errbufsize)
422 int error = parser->error;
423 const char *msg;
425 /* clear our record of an error */
426 parser->error = 0;
428 switch (error) {
429 case 0:
430 msg = "No error.";
431 break;
433 case APR_XML_NS_ERROR_UNKNOWN_PREFIX:
434 msg = "An undefined namespace prefix was used.";
435 break;
437 case APR_XML_NS_ERROR_INVALID_DECL:
438 msg = "A namespace prefix was defined with an empty URI.";
439 break;
441 case APR_XML_ERROR_EXPAT:
442 (void) apr_snprintf(errbuf, errbufsize,
443 "XML parser error code: %s (%d)",
444 XML_ErrorString(parser->xp_err), parser->xp_err);
445 return errbuf;
447 case APR_XML_ERROR_PARSE_DONE:
448 msg = "The parser is not active.";
449 break;
451 default:
452 msg = "There was an unknown error within the XML body.";
453 break;
456 (void) apr_cpystrn(errbuf, msg, errbufsize);
457 return errbuf;
460 APU_DECLARE(apr_status_t) apr_xml_parse_file(apr_pool_t *p,
461 apr_xml_parser **parser,
462 apr_xml_doc **ppdoc,
463 apr_file_t *xmlfd,
464 apr_size_t buffer_length)
466 apr_status_t rv;
467 char *buffer;
468 apr_size_t length;
470 *parser = apr_xml_parser_create(p);
471 if (*parser == NULL) {
472 /* FIXME: returning an error code would be nice,
473 * but we dont get one ;( */
474 return APR_EGENERAL;
476 buffer = apr_palloc(p, buffer_length);
477 length = buffer_length;
479 rv = apr_file_read(xmlfd, buffer, &length);
481 while (rv == APR_SUCCESS) {
482 rv = apr_xml_parser_feed(*parser, buffer, length);
483 if (rv != APR_SUCCESS) {
484 return rv;
487 length = buffer_length;
488 rv = apr_file_read(xmlfd, buffer, &length);
490 if (rv != APR_EOF) {
491 return rv;
493 rv = apr_xml_parser_done(*parser, ppdoc);
494 *parser = NULL;
495 return rv;
498 APU_DECLARE(void) apr_text_append(apr_pool_t * p, apr_text_header *hdr,
499 const char *text)
501 apr_text *t = apr_palloc(p, sizeof(*t));
503 t->text = text;
504 t->next = NULL;
506 if (hdr->first == NULL) {
507 /* no text elements yet */
508 hdr->first = hdr->last = t;
510 else {
511 /* append to the last text element */
512 hdr->last->next = t;
513 hdr->last = t;
518 /* ---------------------------------------------------------------
520 ** XML UTILITY FUNCTIONS
524 ** apr_xml_quote_string: quote an XML string
526 ** Replace '<', '>', and '&' with '&lt;', '&gt;', and '&amp;'.
527 ** If quotes is true, then replace '"' with '&quot;'.
529 ** quotes is typically set to true for XML strings that will occur within
530 ** double quotes -- attribute values.
532 APU_DECLARE(const char *) apr_xml_quote_string(apr_pool_t *p, const char *s,
533 int quotes)
535 const char *scan;
536 apr_size_t len = 0;
537 apr_size_t extra = 0;
538 char *qstr;
539 char *qscan;
540 char c;
542 for (scan = s; (c = *scan) != '\0'; ++scan, ++len) {
543 if (c == '<' || c == '>')
544 extra += 3; /* &lt; or &gt; */
545 else if (c == '&')
546 extra += 4; /* &amp; */
547 else if (quotes && c == '"')
548 extra += 5; /* &quot; */
551 /* nothing to do? */
552 if (extra == 0)
553 return s;
555 qstr = apr_palloc(p, len + extra + 1);
556 for (scan = s, qscan = qstr; (c = *scan) != '\0'; ++scan) {
557 if (c == '<') {
558 *qscan++ = '&';
559 *qscan++ = 'l';
560 *qscan++ = 't';
561 *qscan++ = ';';
563 else if (c == '>') {
564 *qscan++ = '&';
565 *qscan++ = 'g';
566 *qscan++ = 't';
567 *qscan++ = ';';
569 else if (c == '&') {
570 *qscan++ = '&';
571 *qscan++ = 'a';
572 *qscan++ = 'm';
573 *qscan++ = 'p';
574 *qscan++ = ';';
576 else if (quotes && c == '"') {
577 *qscan++ = '&';
578 *qscan++ = 'q';
579 *qscan++ = 'u';
580 *qscan++ = 'o';
581 *qscan++ = 't';
582 *qscan++ = ';';
584 else {
585 *qscan++ = c;
589 *qscan = '\0';
590 return qstr;
593 /* how many characters for the given integer? */
594 #define APR_XML_NS_LEN(ns) ((ns) < 10 ? 1 : (ns) < 100 ? 2 : (ns) < 1000 ? 3 : \
595 (ns) < 10000 ? 4 : (ns) < 100000 ? 5 : \
596 (ns) < 1000000 ? 6 : (ns) < 10000000 ? 7 : \
597 (ns) < 100000000 ? 8 : (ns) < 1000000000 ? 9 : 10)
599 static apr_size_t text_size(const apr_text *t)
601 apr_size_t size = 0;
603 for (; t; t = t->next)
604 size += strlen(t->text);
605 return size;
608 static apr_size_t elem_size(const apr_xml_elem *elem, int style,
609 apr_array_header_t *namespaces, int *ns_map)
611 apr_size_t size;
613 if (style == APR_XML_X2T_FULL || style == APR_XML_X2T_FULL_NS_LANG) {
614 const apr_xml_attr *attr;
616 size = 0;
618 if (style == APR_XML_X2T_FULL_NS_LANG) {
619 int i;
622 ** The outer element will contain xmlns:ns%d="%s" attributes
623 ** and an xml:lang attribute, if applicable.
626 for (i = namespaces->nelts; i--;) {
627 /* compute size of: ' xmlns:ns%d="%s"' */
628 size += (9 + APR_XML_NS_LEN(i) + 2 +
629 strlen(APR_XML_GET_URI_ITEM(namespaces, i)) + 1);
632 if (elem->lang != NULL) {
633 /* compute size of: ' xml:lang="%s"' */
634 size += 11 + strlen(elem->lang) + 1;
638 if (elem->ns == APR_XML_NS_NONE) {
639 /* compute size of: <%s> */
640 size += 1 + strlen(elem->name) + 1;
642 else {
643 int ns = ns_map ? ns_map[elem->ns] : elem->ns;
645 /* compute size of: <ns%d:%s> */
646 size += 3 + APR_XML_NS_LEN(ns) + 1 + strlen(elem->name) + 1;
649 if (APR_XML_ELEM_IS_EMPTY(elem)) {
650 /* insert a closing "/" */
651 size += 1;
653 else {
655 * two of above plus "/":
656 * <ns%d:%s> ... </ns%d:%s>
657 * OR <%s> ... </%s>
659 size = 2 * size + 1;
662 for (attr = elem->attr; attr; attr = attr->next) {
663 if (attr->ns == APR_XML_NS_NONE) {
664 /* compute size of: ' %s="%s"' */
665 size += 1 + strlen(attr->name) + 2 + strlen(attr->value) + 1;
667 else {
668 /* compute size of: ' ns%d:%s="%s"' */
669 size += 3 + APR_XML_NS_LEN(attr->ns) + 1 + strlen(attr->name) + 2 + strlen(attr->value) + 1;
674 ** If the element has an xml:lang value that is *different* from
675 ** its parent, then add the thing in: ' xml:lang="%s"'.
677 ** NOTE: we take advantage of the pointer equality established by
678 ** the parsing for "inheriting" the xml:lang values from parents.
680 if (elem->lang != NULL &&
681 (elem->parent == NULL || elem->lang != elem->parent->lang)) {
682 size += 11 + strlen(elem->lang) + 1;
685 else if (style == APR_XML_X2T_LANG_INNER) {
687 * This style prepends the xml:lang value plus a null terminator.
688 * If a lang value is not present, then we insert a null term.
690 size = elem->lang ? strlen(elem->lang) + 1 : 1;
692 else
693 size = 0;
695 size += text_size(elem->first_cdata.first);
697 for (elem = elem->first_child; elem; elem = elem->next) {
698 /* the size of the child element plus the CDATA that follows it */
699 size += (elem_size(elem, APR_XML_X2T_FULL, NULL, ns_map) +
700 text_size(elem->following_cdata.first));
703 return size;
706 static char *write_text(char *s, const apr_text *t)
708 for (; t; t = t->next) {
709 apr_size_t len = strlen(t->text);
710 memcpy(s, t->text, len);
711 s += len;
713 return s;
716 static char *write_elem(char *s, const apr_xml_elem *elem, int style,
717 apr_array_header_t *namespaces, int *ns_map)
719 const apr_xml_elem *child;
720 apr_size_t len;
721 int ns;
723 if (style == APR_XML_X2T_FULL || style == APR_XML_X2T_FULL_NS_LANG) {
724 int empty = APR_XML_ELEM_IS_EMPTY(elem);
725 const apr_xml_attr *attr;
727 if (elem->ns == APR_XML_NS_NONE) {
728 len = sprintf(s, "<%s", elem->name);
730 else {
731 ns = ns_map ? ns_map[elem->ns] : elem->ns;
732 len = sprintf(s, "<ns%d:%s", ns, elem->name);
734 s += len;
736 for (attr = elem->attr; attr; attr = attr->next) {
737 if (attr->ns == APR_XML_NS_NONE)
738 len = sprintf(s, " %s=\"%s\"", attr->name, attr->value);
739 else
740 len = sprintf(s, " ns%d:%s=\"%s\"", attr->ns, attr->name, attr->value);
741 s += len;
744 /* add the xml:lang value if necessary */
745 if (elem->lang != NULL &&
746 (style == APR_XML_X2T_FULL_NS_LANG ||
747 elem->parent == NULL ||
748 elem->lang != elem->parent->lang)) {
749 len = sprintf(s, " xml:lang=\"%s\"", elem->lang);
750 s += len;
753 /* add namespace definitions, if required */
754 if (style == APR_XML_X2T_FULL_NS_LANG) {
755 int i;
757 for (i = namespaces->nelts; i--;) {
758 len = sprintf(s, " xmlns:ns%d=\"%s\"", i,
759 APR_XML_GET_URI_ITEM(namespaces, i));
760 s += len;
764 /* no more to do. close it up and go. */
765 if (empty) {
766 *s++ = '/';
767 *s++ = '>';
768 return s;
771 /* just close it */
772 *s++ = '>';
774 else if (style == APR_XML_X2T_LANG_INNER) {
775 /* prepend the xml:lang value */
776 if (elem->lang != NULL) {
777 len = strlen(elem->lang);
778 memcpy(s, elem->lang, len);
779 s += len;
781 *s++ = '\0';
784 s = write_text(s, elem->first_cdata.first);
786 for (child = elem->first_child; child; child = child->next) {
787 s = write_elem(s, child, APR_XML_X2T_FULL, NULL, ns_map);
788 s = write_text(s, child->following_cdata.first);
791 if (style == APR_XML_X2T_FULL || style == APR_XML_X2T_FULL_NS_LANG) {
792 if (elem->ns == APR_XML_NS_NONE) {
793 len = sprintf(s, "</%s>", elem->name);
795 else {
796 ns = ns_map ? ns_map[elem->ns] : elem->ns;
797 len = sprintf(s, "</ns%d:%s>", ns, elem->name);
799 s += len;
802 return s;
805 APU_DECLARE(void) apr_xml_quote_elem(apr_pool_t *p, apr_xml_elem *elem)
807 apr_text *scan_txt;
808 apr_xml_attr *scan_attr;
809 apr_xml_elem *scan_elem;
811 /* convert the element's text */
812 for (scan_txt = elem->first_cdata.first;
813 scan_txt != NULL;
814 scan_txt = scan_txt->next) {
815 scan_txt->text = apr_xml_quote_string(p, scan_txt->text, 0);
817 for (scan_txt = elem->following_cdata.first;
818 scan_txt != NULL;
819 scan_txt = scan_txt->next) {
820 scan_txt->text = apr_xml_quote_string(p, scan_txt->text, 0);
823 /* convert the attribute values */
824 for (scan_attr = elem->attr;
825 scan_attr != NULL;
826 scan_attr = scan_attr->next) {
827 scan_attr->value = apr_xml_quote_string(p, scan_attr->value, 1);
830 /* convert the child elements */
831 for (scan_elem = elem->first_child;
832 scan_elem != NULL;
833 scan_elem = scan_elem->next) {
834 apr_xml_quote_elem(p, scan_elem);
838 /* convert an element to a text string */
839 APU_DECLARE(void) apr_xml_to_text(apr_pool_t * p, const apr_xml_elem *elem,
840 int style, apr_array_header_t *namespaces,
841 int *ns_map, const char **pbuf,
842 apr_size_t *psize)
844 /* get the exact size, plus a null terminator */
845 apr_size_t size = elem_size(elem, style, namespaces, ns_map) + 1;
846 char *s = apr_palloc(p, size);
848 (void) write_elem(s, elem, style, namespaces, ns_map);
849 s[size - 1] = '\0';
851 *pbuf = s;
852 if (psize)
853 *psize = size;
856 APU_DECLARE(const char *) apr_xml_empty_elem(apr_pool_t * p,
857 const apr_xml_elem *elem)
859 if (elem->ns == APR_XML_NS_NONE) {
861 * The prefix (xml...) is already within the prop name, or
862 * the element simply has no prefix.
864 return apr_psprintf(p, "<%s/>" DEBUG_CR, elem->name);
867 return apr_psprintf(p, "<ns%d:%s/>" DEBUG_CR, elem->ns, elem->name);
870 /* return the URI's (existing) index, or insert it and return a new index */
871 APU_DECLARE(int) apr_xml_insert_uri(apr_array_header_t *uri_array,
872 const char *uri)
874 int i;
875 const char **pelt;
877 /* never insert an empty URI; this index is always APR_XML_NS_NONE */
878 if (*uri == '\0')
879 return APR_XML_NS_NONE;
881 for (i = uri_array->nelts; i--;) {
882 if (strcmp(uri, APR_XML_GET_URI_ITEM(uri_array, i)) == 0)
883 return i;
886 pelt = apr_array_push(uri_array);
887 *pelt = uri; /* assume uri is const or in a pool */
888 return uri_array->nelts - 1;
891 /* convert the element to EBCDIC */
892 #if APR_CHARSET_EBCDIC
893 static apr_status_t apr_xml_parser_convert_elem(apr_xml_elem *e,
894 apr_xlate_t *convset)
896 apr_xml_attr *a;
897 apr_xml_elem *ec;
898 apr_text *t;
899 apr_size_t inbytes_left, outbytes_left;
900 apr_status_t status;
902 inbytes_left = outbytes_left = strlen(e->name);
903 status = apr_xlate_conv_buffer(convset, e->name, &inbytes_left, (char *) e->name, &outbytes_left);
904 if (status) {
905 return status;
908 for (t = e->first_cdata.first; t != NULL; t = t->next) {
909 inbytes_left = outbytes_left = strlen(t->text);
910 status = apr_xlate_conv_buffer(convset, t->text, &inbytes_left, (char *) t->text, &outbytes_left);
911 if (status) {
912 return status;
916 for (t = e->following_cdata.first; t != NULL; t = t->next) {
917 inbytes_left = outbytes_left = strlen(t->text);
918 status = apr_xlate_conv_buffer(convset, t->text, &inbytes_left, (char *) t->text, &outbytes_left);
919 if (status) {
920 return status;
924 for (a = e->attr; a != NULL; a = a->next) {
925 inbytes_left = outbytes_left = strlen(a->name);
926 status = apr_xlate_conv_buffer(convset, a->name, &inbytes_left, (char *) a->name, &outbytes_left);
927 if (status) {
928 return status;
930 inbytes_left = outbytes_left = strlen(a->value);
931 status = apr_xlate_conv_buffer(convset, a->value, &inbytes_left, (char *) a->value, &outbytes_left);
932 if (status) {
933 return status;
937 for (ec = e->first_child; ec != NULL; ec = ec->next) {
938 status = apr_xml_parser_convert_elem(ec, convset);
939 if (status) {
940 return status;
943 return APR_SUCCESS;
946 /* convert the whole document to EBCDIC */
947 APU_DECLARE(apr_status_t) apr_xml_parser_convert_doc(apr_pool_t *pool,
948 apr_xml_doc *pdoc,
949 apr_xlate_t *convset)
951 apr_status_t status;
952 /* Don't convert the namespaces: they are constant! */
953 if (pdoc->namespaces != NULL) {
954 int i;
955 apr_array_header_t *namespaces;
956 namespaces = apr_array_make(pool, pdoc->namespaces->nelts, sizeof(const char *));
957 if (namespaces == NULL)
958 return APR_ENOMEM;
959 for (i = 0; i < pdoc->namespaces->nelts; i++) {
960 apr_size_t inbytes_left, outbytes_left;
961 char *ptr = (char *) APR_XML_GET_URI_ITEM(pdoc->namespaces, i);
962 ptr = apr_pstrdup(pool, ptr);
963 if ( ptr == NULL)
964 return APR_ENOMEM;
965 inbytes_left = outbytes_left = strlen(ptr);
966 status = apr_xlate_conv_buffer(convset, ptr, &inbytes_left, ptr, &outbytes_left);
967 if (status) {
968 return status;
970 apr_xml_insert_uri(namespaces, ptr);
972 pdoc->namespaces = namespaces;
974 return apr_xml_parser_convert_elem(pdoc->root, convset);
976 #endif