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
= 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
;
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 static void start_handler(void *userdata
, const char *name
, const char **attrs
)
125 apr_xml_parser
*parser
= userdata
;
133 /* punt once we find an error */
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) */
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
;
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
;
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
;
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
;
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
187 if (attr
->value
[0] == '\0') {
188 parser
->error
= APR_XML_NS_ERROR_INVALID_DECL
;
193 else if (*prefix
!= '\0') {
194 /* advance "prev" since "attr" is still present */
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 */
212 elem
->attr
= attr
->next
;
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 */
224 elem
->attr
= attr
->next
;
226 prev
->next
= attr
->next
;
228 /* Note: prev will not be advanced since we just removed "attr" */
231 /* advance "prev" since "attr" is still present */
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);
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
;
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
;
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);
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
;
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
;
302 static void end_handler(void *userdata
, const char *name
)
304 apr_xml_parser
*parser
= userdata
;
306 /* punt once we find an error */
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
;
318 apr_text_header
*hdr
;
321 /* punt once we find an error */
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
;
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
);
350 APU_DECLARE(apr_xml_parser
*) apr_xml_parser_create(apr_pool_t
*pool
)
352 apr_xml_parser
*parser
= apr_pcalloc(pool
, sizeof(*parser
));
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
);
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
);
378 static apr_status_t
do_parse(apr_xml_parser
*parser
,
379 const char *data
, apr_size_t len
,
382 if (parser
->xp
== NULL
) {
383 parser
->error
= APR_XML_ERROR_PARSE_DONE
;
386 int rv
= XML_Parse(parser
->xp
, data
, (int)len
, is_final
);
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
,
402 return do_parse(parser
, data
, len
, 0 /* is_final */);
405 APU_DECLARE(apr_status_t
) apr_xml_parser_done(apr_xml_parser
*parser
,
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
);
422 APU_DECLARE(char *) apr_xml_parser_geterror(apr_xml_parser
*parser
,
424 apr_size_t errbufsize
)
426 int error
= parser
->error
;
429 /* clear our record of an error */
437 case APR_XML_NS_ERROR_UNKNOWN_PREFIX
:
438 msg
= "An undefined namespace prefix was used.";
441 case APR_XML_NS_ERROR_INVALID_DECL
:
442 msg
= "A namespace prefix was defined with an empty URI.";
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
);
451 case APR_XML_ERROR_PARSE_DONE
:
452 msg
= "The parser is not active.";
456 msg
= "There was an unknown error within the XML body.";
460 (void) apr_cpystrn(errbuf
, msg
, errbufsize
);
464 APU_DECLARE(apr_status_t
) apr_xml_parse_file(apr_pool_t
*p
,
465 apr_xml_parser
**parser
,
468 apr_size_t buffer_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 ;( */
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
) {
491 length
= buffer_length
;
492 rv
= apr_file_read(xmlfd
, buffer
, &length
);
497 rv
= apr_xml_parser_done(*parser
, ppdoc
);
502 APU_DECLARE(void) apr_text_append(apr_pool_t
* p
, apr_text_header
*hdr
,
505 apr_text
*t
= apr_palloc(p
, sizeof(*t
));
510 if (hdr
->first
== NULL
) {
511 /* no text elements yet */
512 hdr
->first
= hdr
->last
= t
;
515 /* append to the last text element */
522 /* ---------------------------------------------------------------
524 ** XML UTILITY FUNCTIONS
528 ** apr_xml_quote_string: quote an XML string
530 ** Replace '<', '>', and '&' with '<', '>', and '&'.
531 ** If quotes is true, then replace '"' with '"'.
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
,
541 apr_size_t extra
= 0;
546 for (scan
= s
; (c
= *scan
) != '\0'; ++scan
, ++len
) {
547 if (c
== '<' || c
== '>')
548 extra
+= 3; /* < or > */
550 extra
+= 4; /* & */
551 else if (quotes
&& c
== '"')
552 extra
+= 5; /* " */
559 qstr
= apr_palloc(p
, len
+ extra
+ 1);
560 for (scan
= s
, qscan
= qstr
; (c
= *scan
) != '\0'; ++scan
) {
580 else if (quotes
&& c
== '"') {
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
)
607 for (; t
; t
= t
->next
)
608 size
+= strlen(t
->text
);
612 static apr_size_t
elem_size(const apr_xml_elem
*elem
, int style
,
613 apr_array_header_t
*namespaces
, int *ns_map
)
617 if (style
== APR_XML_X2T_FULL
|| style
== APR_XML_X2T_FULL_NS_LANG
) {
618 const apr_xml_attr
*attr
;
622 if (style
== APR_XML_X2T_FULL_NS_LANG
) {
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;
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 "/" */
659 * two of above plus "/":
660 * <ns%d:%s> ... </ns%d:%s>
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;
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;
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
));
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
);
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
;
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
);
736 ns
= ns_map
? ns_map
[elem
->ns
] : elem
->ns
;
737 len
= sprintf(s
, "<ns%d:%s", ns
, elem
->name
);
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
);
745 ns
= ns_map
? ns_map
[attr
->ns
] : attr
->ns
;
746 len
= sprintf(s
, " ns%d:%s=\"%s\"", ns
, attr
->name
, attr
->value
);
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
);
760 /* add namespace definitions, if required */
761 if (style
== APR_XML_X2T_FULL_NS_LANG
) {
764 for (i
= namespaces
->nelts
; i
--;) {
765 len
= sprintf(s
, " xmlns:ns%d=\"%s\"", i
,
766 APR_XML_GET_URI_ITEM(namespaces
, i
));
771 /* no more to do. close it up and go. */
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
);
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
);
803 ns
= ns_map
? ns_map
[elem
->ns
] : elem
->ns
;
804 len
= sprintf(s
, "</ns%d:%s>", ns
, elem
->name
);
812 APU_DECLARE(void) apr_xml_quote_elem(apr_pool_t
*p
, apr_xml_elem
*elem
)
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
;
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
;
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
;
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
;
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
,
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
);
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
,
884 /* never insert an empty URI; this index is always APR_XML_NS_NONE */
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)
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
)
906 apr_size_t inbytes_left
, outbytes_left
;
909 inbytes_left
= outbytes_left
= strlen(e
->name
);
910 status
= apr_xlate_conv_buffer(convset
, e
->name
, &inbytes_left
, (char *) e
->name
, &outbytes_left
);
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
);
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
);
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
);
937 inbytes_left
= outbytes_left
= strlen(a
->value
);
938 status
= apr_xlate_conv_buffer(convset
, a
->value
, &inbytes_left
, (char *) a
->value
, &outbytes_left
);
944 for (ec
= e
->first_child
; ec
!= NULL
; ec
= ec
->next
) {
945 status
= apr_xml_parser_convert_elem(ec
, convset
);
953 /* convert the whole document to EBCDIC */
954 APU_DECLARE(apr_status_t
) apr_xml_parser_convert_doc(apr_pool_t
*pool
,
956 apr_xlate_t
*convset
)
959 /* Don't convert the namespaces: they are constant! */
960 if (pdoc
->namespaces
!= NULL
) {
962 apr_array_header_t
*namespaces
;
963 namespaces
= apr_array_make(pool
, pdoc
->namespaces
->nelts
, sizeof(const char *));
964 if (namespaces
== NULL
)
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
);
972 inbytes_left
= outbytes_left
= strlen(ptr
);
973 status
= apr_xlate_conv_buffer(convset
, ptr
, &inbytes_left
, ptr
, &outbytes_left
);
977 apr_xml_insert_uri(namespaces
, ptr
);
979 pdoc
->namespaces
= namespaces
;
981 return apr_xml_parser_convert_elem(pdoc
->root
, convset
);