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.
18 #include "apr_strings.h"
20 #define APR_WANT_STDIO /* for sprintf() */
21 #define APR_WANT_STRFUNC
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>
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 */
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
88 for (; elem; elem = elem->parent) {
89 apr_xml_ns_scope *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;
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;
120 return APR_XML_NS_ERROR_UNKNOWN_PREFIX;
123 /* return original prefix given ns index */
124 static const char * find_prefix_name(const apr_xml_elem *elem, int ns, int parent)
127 ** Walk up the tree, looking for a namespace scope that defines this
130 for (; elem; elem = parent ? elem->parent : NULL) {
131 apr_xml_ns_scope *ns_scope = elem->ns_scope;
133 for (; ns_scope; ns_scope = ns_scope->next) {
134 if (ns_scope->ns == ns)
135 return ns_scope->prefix;
143 static void start_handler(void *userdata, const char *name, const char **attrs)
145 apr_xml_parser *parser = userdata;
153 /* punt once we find an error */
157 elem = apr_pcalloc(parser->p, sizeof(*elem));
159 /* prep the element */
160 elem->name = elem_name = apr_pstrdup(parser->p, name);
162 /* fill in the attributes (note: ends up in reverse order) */
164 attr = apr_palloc(parser->p, sizeof(*attr));
165 attr->name = apr_pstrdup(parser->p, *attrs++);
166 attr->value = apr_pstrdup(parser->p, *attrs++);
167 attr->next = elem->attr;
171 /* hook the element into the tree */
172 if (parser->cur_elem == NULL) {
173 /* no current element; this also becomes the root */
174 parser->cur_elem = parser->doc->root = elem;
177 /* this element appeared within the current elem */
178 elem->parent = parser->cur_elem;
180 /* set up the child/sibling links */
181 if (elem->parent->last_child == NULL) {
182 /* no first child either */
183 elem->parent->first_child = elem->parent->last_child = elem;
186 /* hook onto the end of the parent's children */
187 elem->parent->last_child->next = elem;
188 elem->parent->last_child = elem;
191 /* this element is now the current element */
192 parser->cur_elem = elem;
195 /* scan the attributes for namespace declarations */
196 for (prev = NULL, attr = elem->attr;
199 if (strncmp(attr->name, APR_KW_xmlns, 5) == 0) {
200 const char *prefix = &attr->name[5];
201 apr_xml_ns_scope *ns_scope;
203 /* test for xmlns:foo= form and xmlns= form */
204 if (*prefix == 0x3A) {
205 /* a namespace prefix declaration must have a
207 if (attr->value[0] == '\0') {
208 parser->error = APR_XML_NS_ERROR_INVALID_DECL;
213 else if (*prefix != '\0') {
214 /* advance "prev" since "attr" is still present */
219 /* quote the URI before we ever start working with it */
220 quoted = apr_xml_quote_string(parser->p, attr->value, 1);
222 /* build and insert the new scope */
223 ns_scope = apr_pcalloc(parser->p, sizeof(*ns_scope));
224 ns_scope->prefix = prefix;
225 ns_scope->ns = apr_xml_insert_uri(parser->doc->namespaces, quoted);
226 ns_scope->emptyURI = *quoted == '\0';
227 ns_scope->next = elem->ns_scope;
228 elem->ns_scope = ns_scope;
230 /* remove this attribute from the element */
232 elem->attr = attr->next;
234 prev->next = attr->next;
236 /* Note: prev will not be advanced since we just removed "attr" */
238 else if (strcmp(attr->name, APR_KW_xmlns_lang) == 0) {
239 /* save away the language (in quoted form) */
240 elem->lang = apr_xml_quote_string(parser->p, attr->value, 1);
242 /* remove this attribute from the element */
244 elem->attr = attr->next;
246 prev->next = attr->next;
248 /* Note: prev will not be advanced since we just removed "attr" */
251 /* advance "prev" since "attr" is still present */
257 ** If an xml:lang attribute didn't exist (lang==NULL), then copy the
258 ** language from the parent element (if present).
260 ** NOTE: elem_size() *depends* upon this pointer equality.
262 if (elem->lang == NULL && elem->parent != NULL)
263 elem->lang = elem->parent->lang;
265 /* adjust the element's namespace */
266 colon = strchr(elem_name, 0x3A);
269 * The element is using the default namespace, which will always
270 * be found. Either it will be "no namespace", or a default
271 * namespace URI has been specified at some point.
273 elem->ns = find_prefix(parser, "");
275 else if (APR_XML_NS_IS_RESERVED(elem->name)) {
276 elem->ns = APR_XML_NS_NONE;
280 elem->ns = find_prefix(parser, elem->name);
281 elem->name = colon + 1;
283 if (APR_XML_NS_IS_ERROR(elem->ns)) {
284 parser->error = elem->ns;
289 /* adjust all remaining attributes' namespaces */
290 for (attr = elem->attr; attr; attr = attr->next) {
292 * apr_xml_attr defines this as "const" but we dup'd it, so we
293 * know that we can change it. a bit hacky, but the existing
294 * structure def is best.
296 char *attr_name = (char *)attr->name;
298 colon = strchr(attr_name, 0x3A);
301 * Attributes do NOT use the default namespace. Therefore,
302 * we place them into the "no namespace" category.
304 attr->ns = APR_XML_NS_NONE;
306 else if (APR_XML_NS_IS_RESERVED(attr->name)) {
307 attr->ns = APR_XML_NS_NONE;
311 attr->ns = find_prefix(parser, attr->name);
312 attr->name = colon + 1;
314 if (APR_XML_NS_IS_ERROR(attr->ns)) {
315 parser->error = attr->ns;
322 static void end_handler(void *userdata, const char *name)
324 apr_xml_parser *parser = userdata;
326 /* punt once we find an error */
330 /* pop up one level */
331 parser->cur_elem = parser->cur_elem->parent;
334 static void cdata_handler(void *userdata, const char *data, int len)
336 apr_xml_parser *parser = userdata;
338 apr_text_header *hdr;
341 /* punt once we find an error */
345 elem = parser->cur_elem;
346 s = apr_pstrndup(parser->p, data, len);
348 if (elem->last_child == NULL) {
349 /* no children yet. this cdata follows the start tag */
350 hdr = &elem->first_cdata;
353 /* child elements exist. this cdata follows the last child. */
354 hdr = &elem->last_child->following_cdata;
357 apr_text_append(parser->p, hdr, s);
360 static apr_status_t cleanup_parser(void *ctx)
362 apr_xml_parser *parser = ctx;
364 XML_ParserFree(parser->xp);
370 #if XML_MAJOR_VERSION > 1
371 /* Stop the parser if an entity declaration is hit. */
372 static void entity_declaration(void *userData, const XML_Char *entityName,
373 int is_parameter_entity, const XML_Char *value,
374 int value_length, const XML_Char *base,
375 const XML_Char *systemId, const XML_Char *publicId,
376 const XML_Char *notationName)
378 apr_xml_parser *parser = userData;
380 XML_StopParser(parser->xp, XML_FALSE);
383 /* A noop default_handler. */
384 static void default_handler(void *userData, const XML_Char *s, int len)
389 APU_DECLARE(apr_xml_parser *) apr_xml_parser_create(apr_pool_t *pool)
391 apr_xml_parser *parser = apr_pcalloc(pool, sizeof(*parser));
394 parser->doc = apr_pcalloc(pool, sizeof(*parser->doc));
396 parser->doc->namespaces = apr_array_make(pool, 5, sizeof(const char *));
398 /* ### is there a way to avoid hard-coding this? */
399 apr_xml_insert_uri(parser->doc->namespaces, APR_KW_DAV);
401 parser->xp = XML_ParserCreate(NULL);
402 if (parser->xp == NULL) {
403 (*apr_pool_abort_get(pool))(APR_ENOMEM);
407 apr_pool_cleanup_register(pool, parser, cleanup_parser,
408 apr_pool_cleanup_null);
410 XML_SetUserData(parser->xp, parser);
411 XML_SetElementHandler(parser->xp, start_handler, end_handler);
412 XML_SetCharacterDataHandler(parser->xp, cdata_handler);
414 /* Prevent the "billion laughs" attack against expat by disabling
415 * internal entity expansion. With 2.x, forcibly stop the parser
416 * if an entity is declared - this is safer and a more obvious
417 * failure mode. With older versions, installing a noop
418 * DefaultHandler means that internal entities will be expanded as
419 * the empty string, which is also sufficient to prevent the
421 #if XML_MAJOR_VERSION > 1
422 XML_SetEntityDeclHandler(parser->xp, entity_declaration);
424 XML_SetDefaultHandler(parser->xp, default_handler);
430 static apr_status_t do_parse(apr_xml_parser *parser,
431 const char *data, apr_size_t len,
434 if (parser->xp == NULL) {
435 parser->error = APR_XML_ERROR_PARSE_DONE;
438 int rv = XML_Parse(parser->xp, data, (int)len, is_final);
441 parser->error = APR_XML_ERROR_EXPAT;
442 parser->xp_err = XML_GetErrorCode(parser->xp);
446 /* ### better error code? */
447 return parser->error ? APR_EGENERAL : APR_SUCCESS;
450 APU_DECLARE(apr_status_t) apr_xml_parser_feed(apr_xml_parser *parser,
454 return do_parse(parser, data, len, 0 /* is_final */);
457 APU_DECLARE(apr_status_t) apr_xml_parser_done(apr_xml_parser *parser,
461 apr_status_t status = do_parse(parser, &end, 0, 1 /* is_final */);
463 /* get rid of the parser */
464 (void) apr_pool_cleanup_run(parser->p, parser, cleanup_parser);
474 APU_DECLARE(char *) apr_xml_parser_geterror(apr_xml_parser *parser,
476 apr_size_t errbufsize)
478 int error = parser->error;
481 /* clear our record of an error */
489 case APR_XML_NS_ERROR_UNKNOWN_PREFIX:
490 msg = "An undefined namespace prefix was used.";
493 case APR_XML_NS_ERROR_INVALID_DECL:
494 msg = "A namespace prefix was defined with an empty URI.";
497 case APR_XML_ERROR_EXPAT:
498 (void) apr_snprintf(errbuf, errbufsize,
499 "XML parser error code: %s (%d)",
500 XML_ErrorString(parser->xp_err), parser->xp_err);
503 case APR_XML_ERROR_PARSE_DONE:
504 msg = "The parser is not active.";
508 msg = "There was an unknown error within the XML body.";
512 (void) apr_cpystrn(errbuf, msg, errbufsize);
516 APU_DECLARE(apr_status_t) apr_xml_parse_file(apr_pool_t *p,
517 apr_xml_parser **parser,
520 apr_size_t buffer_length)
526 *parser = apr_xml_parser_create(p);
527 if (*parser == NULL) {
528 /* FIXME: returning an error code would be nice,
529 * but we dont get one ;( */
532 buffer = apr_palloc(p, buffer_length);
533 length = buffer_length;
535 rv = apr_file_read(xmlfd, buffer, &length);
537 while (rv == APR_SUCCESS) {
538 rv = apr_xml_parser_feed(*parser, buffer, length);
539 if (rv != APR_SUCCESS) {
543 length = buffer_length;
544 rv = apr_file_read(xmlfd, buffer, &length);
549 rv = apr_xml_parser_done(*parser, ppdoc);
554 APU_DECLARE(void) apr_text_append(apr_pool_t * p, apr_text_header *hdr,
557 apr_text *t = apr_palloc(p, sizeof(*t));
562 if (hdr->first == NULL) {
563 /* no text elements yet */
564 hdr->first = hdr->last = t;
567 /* append to the last text element */
574 /* ---------------------------------------------------------------
576 ** XML UTILITY FUNCTIONS
580 ** apr_xml_quote_string: quote an XML string
582 ** Replace '<', '>', and '&' with '<', '>', and '&'.
583 ** If quotes is true, then replace '"' with '"'.
585 ** quotes is typically set to true for XML strings that will occur within
586 ** double quotes -- attribute values.
588 APU_DECLARE(const char *) apr_xml_quote_string(apr_pool_t *p, const char *s,
593 apr_size_t extra = 0;
598 for (scan = s; (c = *scan) != '\0'; ++scan, ++len) {
599 if (c == '<' || c == '>')
600 extra += 3; /* < or > */
602 extra += 4; /* & */
603 else if (quotes && c == '"')
604 extra += 5; /* " */
611 qstr = apr_palloc(p, len + extra + 1);
612 for (scan = s, qscan = qstr; (c = *scan) != '\0'; ++scan) {
632 else if (quotes && c == '"') {
649 /* how many characters for the given integer? */
650 #define APR_XML_NS_LEN(ns) ((ns) < 10 ? 1 : (ns) < 100 ? 2 : (ns) < 1000 ? 3 : \
651 (ns) < 10000 ? 4 : (ns) < 100000 ? 5 : \
652 (ns) < 1000000 ? 6 : (ns) < 10000000 ? 7 : \
653 (ns) < 100000000 ? 8 : (ns) < 1000000000 ? 9 : 10)
655 static apr_size_t text_size(const apr_text *t)
659 for (; t; t = t->next)
660 size += strlen(t->text);
664 static apr_size_t elem_size(const apr_xml_elem *elem, int style,
665 apr_array_header_t *namespaces, int *ns_map)
669 if (style == APR_XML_X2T_FULL || style == APR_XML_X2T_FULL_NS_LANG ||
670 style == APR_XML_X2T_PARSED) {
671 const apr_xml_attr *attr;
675 if (style == APR_XML_X2T_FULL_NS_LANG) {
679 ** The outer element will contain xmlns:ns%d="%s" attributes
680 ** and an xml:lang attribute, if applicable.
683 for (i = namespaces->nelts; i--;) {
684 /* compute size of: ' xmlns:ns%d="%s"' */
685 size += (9 + APR_XML_NS_LEN(i) + 2 +
686 strlen(APR_XML_GET_URI_ITEM(namespaces, i)) + 1);
689 if (elem->lang != NULL) {
690 /* compute size of: ' xml:lang="%s"' */
691 size += 11 + strlen(elem->lang) + 1;
694 else if (style == APR_XML_X2T_PARSED) {
695 apr_xml_ns_scope *ns_scope = elem->ns_scope;
697 /* compute size of: ' xmlns:%s="%s"' */
698 for (; ns_scope; ns_scope = ns_scope->next) {
699 size += 10 + strlen(find_prefix_name(elem, ns_scope->ns, 0)) +
700 strlen(APR_XML_GET_URI_ITEM(namespaces, ns_scope->ns));
703 if (elem->lang != NULL) {
704 /* compute size of: ' xml:lang="%s"' */
705 size += 11 + strlen(elem->lang) + 1;
709 if (elem->ns == APR_XML_NS_NONE) {
710 /* compute size of: <%s> */
711 size += 1 + strlen(elem->name) + 1;
713 else if (style == APR_XML_X2T_PARSED) {
714 /* compute size of: <%s:%s> */
715 size += 3 + strlen(find_prefix_name(elem, elem->ns, 1)) + strlen(elem->name);
718 int ns = ns_map ? ns_map[elem->ns] : elem->ns;
720 /* compute size of: <ns%d:%s> */
721 size += 3 + APR_XML_NS_LEN(ns) + 1 + strlen(elem->name) + 1;
724 if (APR_XML_ELEM_IS_EMPTY(elem)) {
725 /* insert a closing "/" */
730 * two of above plus "/":
731 * <ns%d:%s> ... </ns%d:%s>
737 for (attr = elem->attr; attr; attr = attr->next) {
738 if (attr->ns == APR_XML_NS_NONE) {
739 /* compute size of: ' %s="%s"' */
740 size += 1 + strlen(attr->name) + 2 + strlen(attr->value) + 1;
742 else if (style == APR_XML_X2T_PARSED) {
743 /* compute size of: ' %s:%s="%s"' */
744 size += 5 + strlen(find_prefix_name(elem, attr->ns, 1)) + strlen(attr->name) + strlen(attr->value);
747 /* compute size of: ' ns%d:%s="%s"' */
748 int ns = ns_map ? ns_map[attr->ns] : attr->ns;
749 size += 3 + APR_XML_NS_LEN(ns) + 1 + strlen(attr->name) + 2 + strlen(attr->value) + 1;
754 ** If the element has an xml:lang value that is *different* from
755 ** its parent, then add the thing in: ' xml:lang="%s"'.
757 ** NOTE: we take advantage of the pointer equality established by
758 ** the parsing for "inheriting" the xml:lang values from parents.
760 if (elem->lang != NULL &&
761 (elem->parent == NULL || elem->lang != elem->parent->lang)) {
762 size += 11 + strlen(elem->lang) + 1;
765 else if (style == APR_XML_X2T_LANG_INNER) {
767 * This style prepends the xml:lang value plus a null terminator.
768 * If a lang value is not present, then we insert a null term.
770 size = elem->lang ? strlen(elem->lang) + 1 : 1;
775 size += text_size(elem->first_cdata.first);
777 for (elem = elem->first_child; elem; elem = elem->next) {
778 /* the size of the child element plus the CDATA that follows it */
779 size += (elem_size(elem, style == APR_XML_X2T_PARSED ? APR_XML_X2T_PARSED : APR_XML_X2T_FULL, NULL, ns_map) +
780 text_size(elem->following_cdata.first));
786 static char *write_text(char *s, const apr_text *t)
788 for (; t; t = t->next) {
789 apr_size_t len = strlen(t->text);
790 memcpy(s, t->text, len);
796 static char *write_elem(char *s, const apr_xml_elem *elem, int style,
797 apr_array_header_t *namespaces, int *ns_map)
799 const apr_xml_elem *child;
803 if (style == APR_XML_X2T_FULL || style == APR_XML_X2T_FULL_NS_LANG ||
804 style == APR_XML_X2T_PARSED) {
805 int empty = APR_XML_ELEM_IS_EMPTY(elem);
806 const apr_xml_attr *attr;
808 if (elem->ns == APR_XML_NS_NONE)
809 len = sprintf(s, "<%s", elem->name);
810 else if (style == APR_XML_X2T_PARSED)
811 len = sprintf(s, "<%s:%s", find_prefix_name(elem, elem->ns, 1), elem->name);
813 ns = ns_map ? ns_map[elem->ns] : elem->ns;
814 len = sprintf(s, "<ns%d:%s", ns, elem->name);
818 for (attr = elem->attr; attr; attr = attr->next) {
819 if (attr->ns == APR_XML_NS_NONE)
820 len = sprintf(s, " %s=\"%s\"", attr->name, attr->value);
821 else if (style == APR_XML_X2T_PARSED)
822 len = sprintf(s, " %s:%s=\"%s\"",
823 find_prefix_name(elem, attr->ns, 1), attr->name, attr->value);
825 ns = ns_map ? ns_map[attr->ns] : attr->ns;
826 len = sprintf(s, " ns%d:%s=\"%s\"", ns, attr->name, attr->value);
831 /* add the xml:lang value if necessary */
832 if (elem->lang != NULL &&
833 (style == APR_XML_X2T_FULL_NS_LANG ||
834 elem->parent == NULL ||
835 elem->lang != elem->parent->lang)) {
836 len = sprintf(s, " xml:lang=\"%s\"", elem->lang);
840 /* add namespace definitions, if required */
841 if (style == APR_XML_X2T_FULL_NS_LANG) {
844 for (i = namespaces->nelts; i--;) {
845 len = sprintf(s, " xmlns:ns%d=\"%s\"", i,
846 APR_XML_GET_URI_ITEM(namespaces, i));
850 else if (style == APR_XML_X2T_PARSED) {
851 apr_xml_ns_scope *ns_scope = elem->ns_scope;
853 for (; ns_scope; ns_scope = ns_scope->next) {
854 const char *prefix = find_prefix_name(elem, ns_scope->ns, 0);
856 len = sprintf(s, " xmlns%s%s=\"%s\"",
857 *prefix ? ":" : "", *prefix ? prefix : "",
858 APR_XML_GET_URI_ITEM(namespaces, ns_scope->ns));
863 /* no more to do. close it up and go. */
873 else if (style == APR_XML_X2T_LANG_INNER) {
874 /* prepend the xml:lang value */
875 if (elem->lang != NULL) {
876 len = strlen(elem->lang);
877 memcpy(s, elem->lang, len);
883 s = write_text(s, elem->first_cdata.first);
885 for (child = elem->first_child; child; child = child->next) {
886 s = write_elem(s, child,
887 style == APR_XML_X2T_PARSED ? APR_XML_X2T_PARSED : APR_XML_X2T_FULL,
889 s = write_text(s, child->following_cdata.first);
892 if (style == APR_XML_X2T_FULL || style == APR_XML_X2T_FULL_NS_LANG || style == APR_XML_X2T_PARSED) {
893 if (elem->ns == APR_XML_NS_NONE)
894 len = sprintf(s, "</%s>", elem->name);
895 else if (style == APR_XML_X2T_PARSED)
896 len = sprintf(s, "</%s:%s>", find_prefix_name(elem, elem->ns, 1), elem->name);
898 ns = ns_map ? ns_map[elem->ns] : elem->ns;
899 len = sprintf(s, "</ns%d:%s>", ns, elem->name);
907 APU_DECLARE(void) apr_xml_quote_elem(apr_pool_t *p, apr_xml_elem *elem)
910 apr_xml_attr *scan_attr;
911 apr_xml_elem *scan_elem;
913 /* convert the element's text */
914 for (scan_txt = elem->first_cdata.first;
916 scan_txt = scan_txt->next) {
917 scan_txt->text = apr_xml_quote_string(p, scan_txt->text, 0);
919 for (scan_txt = elem->following_cdata.first;
921 scan_txt = scan_txt->next) {
922 scan_txt->text = apr_xml_quote_string(p, scan_txt->text, 0);
925 /* convert the attribute values */
926 for (scan_attr = elem->attr;
928 scan_attr = scan_attr->next) {
929 scan_attr->value = apr_xml_quote_string(p, scan_attr->value, 1);
932 /* convert the child elements */
933 for (scan_elem = elem->first_child;
935 scan_elem = scan_elem->next) {
936 apr_xml_quote_elem(p, scan_elem);
940 /* convert an element to a text string */
941 APU_DECLARE(void) apr_xml_to_text(apr_pool_t * p, const apr_xml_elem *elem,
942 int style, apr_array_header_t *namespaces,
943 int *ns_map, const char **pbuf,
946 /* get the exact size, plus a null terminator */
947 apr_size_t size = elem_size(elem, style, namespaces, ns_map) + 1;
948 char *s = apr_palloc(p, size);
950 (void) write_elem(s, elem, style, namespaces, ns_map);
958 APU_DECLARE(const char *) apr_xml_empty_elem(apr_pool_t * p,
959 const apr_xml_elem *elem)
961 if (elem->ns == APR_XML_NS_NONE) {
963 * The prefix (xml...) is already within the prop name, or
964 * the element simply has no prefix.
966 return apr_psprintf(p, "<%s/>" DEBUG_CR, elem->name);
969 return apr_psprintf(p, "<ns%d:%s/>" DEBUG_CR, elem->ns, elem->name);
972 /* return the URI's (existing) index, or insert it and return a new index */
973 APU_DECLARE(int) apr_xml_insert_uri(apr_array_header_t *uri_array,
979 /* never insert an empty URI; this index is always APR_XML_NS_NONE */
981 return APR_XML_NS_NONE;
983 for (i = uri_array->nelts; i--;) {
984 if (strcmp(uri, APR_XML_GET_URI_ITEM(uri_array, i)) == 0)
988 pelt = apr_array_push(uri_array);
989 *pelt = uri; /* assume uri is const or in a pool */
990 return uri_array->nelts - 1;
993 /* convert the element to EBCDIC */
994 #if APR_CHARSET_EBCDIC
995 static apr_status_t apr_xml_parser_convert_elem(apr_xml_elem *e,
996 apr_xlate_t *convset)
1001 apr_size_t inbytes_left, outbytes_left;
1002 apr_status_t status;
1004 inbytes_left = outbytes_left = strlen(e->name);
1005 status = apr_xlate_conv_buffer(convset, e->name, &inbytes_left, (char *) e->name, &outbytes_left);
1010 for (t = e->first_cdata.first; t != NULL; t = t->next) {
1011 inbytes_left = outbytes_left = strlen(t->text);
1012 status = apr_xlate_conv_buffer(convset, t->text, &inbytes_left, (char *) t->text, &outbytes_left);
1018 for (t = e->following_cdata.first; t != NULL; t = t->next) {
1019 inbytes_left = outbytes_left = strlen(t->text);
1020 status = apr_xlate_conv_buffer(convset, t->text, &inbytes_left, (char *) t->text, &outbytes_left);
1026 for (a = e->attr; a != NULL; a = a->next) {
1027 inbytes_left = outbytes_left = strlen(a->name);
1028 status = apr_xlate_conv_buffer(convset, a->name, &inbytes_left, (char *) a->name, &outbytes_left);
1032 inbytes_left = outbytes_left = strlen(a->value);
1033 status = apr_xlate_conv_buffer(convset, a->value, &inbytes_left, (char *) a->value, &outbytes_left);
1039 for (ec = e->first_child; ec != NULL; ec = ec->next) {
1040 status = apr_xml_parser_convert_elem(ec, convset);
1048 /* convert the whole document to EBCDIC */
1049 APU_DECLARE(apr_status_t) apr_xml_parser_convert_doc(apr_pool_t *pool,
1051 apr_xlate_t *convset)
1053 apr_status_t status;
1054 /* Don't convert the namespaces: they are constant! */
1055 if (pdoc->namespaces != NULL) {
1057 apr_array_header_t *namespaces;
1058 namespaces = apr_array_make(pool, pdoc->namespaces->nelts, sizeof(const char *));
1059 if (namespaces == NULL)
1061 for (i = 0; i < pdoc->namespaces->nelts; i++) {
1062 apr_size_t inbytes_left, outbytes_left;
1063 char *ptr = (char *) APR_XML_GET_URI_ITEM(pdoc->namespaces, i);
1064 ptr = apr_pstrdup(pool, ptr);
1067 inbytes_left = outbytes_left = strlen(ptr);
1068 status = apr_xlate_conv_buffer(convset, ptr, &inbytes_left, ptr, &outbytes_left);
1072 apr_xml_insert_uri(namespaces, ptr);
1074 pdoc->namespaces = namespaces;
1076 return apr_xml_parser_convert_elem(pdoc->root, convset);