apr_xml.c (302408) | apr_xml.c (362181) |
---|---|
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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 --- 72 unchanged lines hidden (view full) --- 81{ 82 apr_xml_elem *elem = parser->cur_elem; 83 84 /* 85 ** Walk up the tree, looking for a namespace scope that defines this 86 ** prefix. 87 */ 88 for (; elem; elem = elem->parent) { | 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 --- 72 unchanged lines hidden (view full) --- 81{ 82 apr_xml_elem *elem = parser->cur_elem; 83 84 /* 85 ** Walk up the tree, looking for a namespace scope that defines this 86 ** prefix. 87 */ 88 for (; elem; elem = elem->parent) { |
89 apr_xml_ns_scope *ns_scope = elem->ns_scope; | 89 apr_xml_ns_scope *ns_scope; |
90 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) { 94 /* 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 --- 17 unchanged lines hidden (view full) --- 115 if (*prefix == '\0') { 116 return APR_XML_NS_NONE; 117 } 118 119 /* not found */ 120 return APR_XML_NS_ERROR_UNKNOWN_PREFIX; 121} 122 | 90 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) { 94 /* 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 --- 17 unchanged lines hidden (view full) --- 115 if (*prefix == '\0') { 116 return APR_XML_NS_NONE; 117 } 118 119 /* not found */ 120 return APR_XML_NS_ERROR_UNKNOWN_PREFIX; 121} 122 |
123/* return original prefix given ns index */ 124static const char * find_prefix_name(const apr_xml_elem *elem, int ns, int parent) 125{ 126 /* 127 ** Walk up the tree, looking for a namespace scope that defines this 128 ** prefix. 129 */ 130 for (; elem; elem = parent ? elem->parent : NULL) { 131 apr_xml_ns_scope *ns_scope = elem->ns_scope; 132 133 for (; ns_scope; ns_scope = ns_scope->next) { 134 if (ns_scope->ns == ns) 135 return ns_scope->prefix; 136 } 137 } 138 /* not found */ 139 return ""; 140} 141 142 |
|
123static void start_handler(void *userdata, const char *name, const char **attrs) 124{ 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; --- 510 unchanged lines hidden (view full) --- 641 return size; 642} 643 644static apr_size_t elem_size(const apr_xml_elem *elem, int style, 645 apr_array_header_t *namespaces, int *ns_map) 646{ 647 apr_size_t size; 648 | 143static void start_handler(void *userdata, const char *name, const char **attrs) 144{ 145 apr_xml_parser *parser = userdata; 146 apr_xml_elem *elem; 147 apr_xml_attr *attr; 148 apr_xml_attr *prev; 149 char *colon; 150 const char *quoted; --- 510 unchanged lines hidden (view full) --- 661 return size; 662} 663 664static apr_size_t elem_size(const apr_xml_elem *elem, int style, 665 apr_array_header_t *namespaces, int *ns_map) 666{ 667 apr_size_t size; 668 |
649 if (style == APR_XML_X2T_FULL || style == APR_XML_X2T_FULL_NS_LANG) { | 669 if (style == APR_XML_X2T_FULL || style == APR_XML_X2T_FULL_NS_LANG || 670 style == APR_XML_X2T_PARSED) { |
650 const apr_xml_attr *attr; 651 652 size = 0; 653 654 if (style == APR_XML_X2T_FULL_NS_LANG) { 655 int i; 656 657 /* --- 7 unchanged lines hidden (view full) --- 665 strlen(APR_XML_GET_URI_ITEM(namespaces, i)) + 1); 666 } 667 668 if (elem->lang != NULL) { 669 /* compute size of: ' xml:lang="%s"' */ 670 size += 11 + strlen(elem->lang) + 1; 671 } 672 } | 671 const apr_xml_attr *attr; 672 673 size = 0; 674 675 if (style == APR_XML_X2T_FULL_NS_LANG) { 676 int i; 677 678 /* --- 7 unchanged lines hidden (view full) --- 686 strlen(APR_XML_GET_URI_ITEM(namespaces, i)) + 1); 687 } 688 689 if (elem->lang != NULL) { 690 /* compute size of: ' xml:lang="%s"' */ 691 size += 11 + strlen(elem->lang) + 1; 692 } 693 } |
694 else if (style == APR_XML_X2T_PARSED) { 695 apr_xml_ns_scope *ns_scope = elem->ns_scope; |
|
673 | 696 |
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)); 701 } 702 703 if (elem->lang != NULL) { 704 /* compute size of: ' xml:lang="%s"' */ 705 size += 11 + strlen(elem->lang) + 1; 706 } 707 } 708 |
|
674 if (elem->ns == APR_XML_NS_NONE) { 675 /* compute size of: <%s> */ 676 size += 1 + strlen(elem->name) + 1; 677 } | 709 if (elem->ns == APR_XML_NS_NONE) { 710 /* compute size of: <%s> */ 711 size += 1 + strlen(elem->name) + 1; 712 } |
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); 716 } |
|
678 else { 679 int ns = ns_map ? ns_map[elem->ns] : elem->ns; 680 681 /* compute size of: <ns%d:%s> */ 682 size += 3 + APR_XML_NS_LEN(ns) + 1 + strlen(elem->name) + 1; 683 } 684 685 if (APR_XML_ELEM_IS_EMPTY(elem)) { --- 9 unchanged lines hidden (view full) --- 695 size = 2 * size + 1; 696 } 697 698 for (attr = elem->attr; attr; attr = attr->next) { 699 if (attr->ns == APR_XML_NS_NONE) { 700 /* compute size of: ' %s="%s"' */ 701 size += 1 + strlen(attr->name) + 2 + strlen(attr->value) + 1; 702 } | 717 else { 718 int ns = ns_map ? ns_map[elem->ns] : elem->ns; 719 720 /* compute size of: <ns%d:%s> */ 721 size += 3 + APR_XML_NS_LEN(ns) + 1 + strlen(elem->name) + 1; 722 } 723 724 if (APR_XML_ELEM_IS_EMPTY(elem)) { --- 9 unchanged lines hidden (view full) --- 734 size = 2 * size + 1; 735 } 736 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; 741 } |
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); 745 } |
|
703 else { 704 /* compute size of: ' ns%d:%s="%s"' */ 705 int ns = ns_map ? ns_map[attr->ns] : attr->ns; 706 size += 3 + APR_XML_NS_LEN(ns) + 1 + strlen(attr->name) + 2 + strlen(attr->value) + 1; 707 } 708 } 709 710 /* --- 17 unchanged lines hidden (view full) --- 728 } 729 else 730 size = 0; 731 732 size += text_size(elem->first_cdata.first); 733 734 for (elem = elem->first_child; elem; elem = elem->next) { 735 /* the size of the child element plus the CDATA that follows it */ | 746 else { 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; 750 } 751 } 752 753 /* --- 17 unchanged lines hidden (view full) --- 771 } 772 else 773 size = 0; 774 775 size += text_size(elem->first_cdata.first); 776 777 for (elem = elem->first_child; elem; elem = elem->next) { 778 /* the size of the child element plus the CDATA that follows it */ |
736 size += (elem_size(elem, APR_XML_X2T_FULL, NULL, ns_map) + | 779 size += (elem_size(elem, style == APR_XML_X2T_PARSED ? APR_XML_X2T_PARSED : APR_XML_X2T_FULL, NULL, ns_map) + |
737 text_size(elem->following_cdata.first)); 738 } 739 740 return size; 741} 742 743static char *write_text(char *s, const apr_text *t) 744{ --- 7 unchanged lines hidden (view full) --- 752 753static char *write_elem(char *s, const apr_xml_elem *elem, int style, 754 apr_array_header_t *namespaces, int *ns_map) 755{ 756 const apr_xml_elem *child; 757 apr_size_t len; 758 int ns; 759 | 780 text_size(elem->following_cdata.first)); 781 } 782 783 return size; 784} 785 786static char *write_text(char *s, const apr_text *t) 787{ --- 7 unchanged lines hidden (view full) --- 795 796static char *write_elem(char *s, const apr_xml_elem *elem, int style, 797 apr_array_header_t *namespaces, int *ns_map) 798{ 799 const apr_xml_elem *child; 800 apr_size_t len; 801 int ns; 802 |
760 if (style == APR_XML_X2T_FULL || style == APR_XML_X2T_FULL_NS_LANG) { | 803 if (style == APR_XML_X2T_FULL || style == APR_XML_X2T_FULL_NS_LANG || 804 style == APR_XML_X2T_PARSED) { |
761 int empty = APR_XML_ELEM_IS_EMPTY(elem); 762 const apr_xml_attr *attr; 763 | 805 int empty = APR_XML_ELEM_IS_EMPTY(elem); 806 const apr_xml_attr *attr; 807 |
764 if (elem->ns == APR_XML_NS_NONE) { | 808 if (elem->ns == APR_XML_NS_NONE) |
765 len = sprintf(s, "<%s", elem->name); | 809 len = sprintf(s, "<%s", elem->name); |
766 } | 810 else if (style == APR_XML_X2T_PARSED) 811 len = sprintf(s, "<%s:%s", find_prefix_name(elem, elem->ns, 1), elem->name); |
767 else { 768 ns = ns_map ? ns_map[elem->ns] : elem->ns; 769 len = sprintf(s, "<ns%d:%s", ns, elem->name); 770 } 771 s += len; 772 773 for (attr = elem->attr; attr; attr = attr->next) { 774 if (attr->ns == APR_XML_NS_NONE) 775 len = sprintf(s, " %s=\"%s\"", attr->name, attr->value); | 812 else { 813 ns = ns_map ? ns_map[elem->ns] : elem->ns; 814 len = sprintf(s, "<ns%d:%s", ns, elem->name); 815 } 816 s += len; 817 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); |
776 else { 777 ns = ns_map ? ns_map[attr->ns] : attr->ns; 778 len = sprintf(s, " ns%d:%s=\"%s\"", ns, attr->name, attr->value); 779 } | 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); 824 else { 825 ns = ns_map ? ns_map[attr->ns] : attr->ns; 826 len = sprintf(s, " ns%d:%s=\"%s\"", ns, attr->name, attr->value); 827 } |
780 s += len; 781 } 782 783 /* add the xml:lang value if necessary */ 784 if (elem->lang != NULL && 785 (style == APR_XML_X2T_FULL_NS_LANG || 786 elem->parent == NULL || 787 elem->lang != elem->parent->lang)) { --- 6 unchanged lines hidden (view full) --- 794 int i; 795 796 for (i = namespaces->nelts; i--;) { 797 len = sprintf(s, " xmlns:ns%d=\"%s\"", i, 798 APR_XML_GET_URI_ITEM(namespaces, i)); 799 s += len; 800 } 801 } | 828 s += len; 829 } 830 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)) { --- 6 unchanged lines hidden (view full) --- 842 int i; 843 844 for (i = namespaces->nelts; i--;) { 845 len = sprintf(s, " xmlns:ns%d=\"%s\"", i, 846 APR_XML_GET_URI_ITEM(namespaces, i)); 847 s += len; 848 } 849 } |
850 else if (style == APR_XML_X2T_PARSED) { 851 apr_xml_ns_scope *ns_scope = elem->ns_scope; |
|
802 | 852 |
853 for (; ns_scope; ns_scope = ns_scope->next) { 854 const char *prefix = find_prefix_name(elem, ns_scope->ns, 0); 855 856 len = sprintf(s, " xmlns%s%s=\"%s\"", 857 *prefix ? ":" : "", *prefix ? prefix : "", 858 APR_XML_GET_URI_ITEM(namespaces, ns_scope->ns)); 859 s += len; 860 } 861 } 862 |
|
803 /* no more to do. close it up and go. */ 804 if (empty) { 805 *s++ = '/'; 806 *s++ = '>'; 807 return s; 808 } 809 810 /* just close it */ --- 7 unchanged lines hidden (view full) --- 818 s += len; 819 } 820 *s++ = '\0'; 821 } 822 823 s = write_text(s, elem->first_cdata.first); 824 825 for (child = elem->first_child; child; child = child->next) { | 863 /* no more to do. close it up and go. */ 864 if (empty) { 865 *s++ = '/'; 866 *s++ = '>'; 867 return s; 868 } 869 870 /* just close it */ --- 7 unchanged lines hidden (view full) --- 878 s += len; 879 } 880 *s++ = '\0'; 881 } 882 883 s = write_text(s, elem->first_cdata.first); 884 885 for (child = elem->first_child; child; child = child->next) { |
826 s = write_elem(s, child, APR_XML_X2T_FULL, NULL, ns_map); | 886 s = write_elem(s, child, 887 style == APR_XML_X2T_PARSED ? APR_XML_X2T_PARSED : APR_XML_X2T_FULL, 888 NULL, ns_map); |
827 s = write_text(s, child->following_cdata.first); 828 } 829 | 889 s = write_text(s, child->following_cdata.first); 890 } 891 |
830 if (style == APR_XML_X2T_FULL || style == APR_XML_X2T_FULL_NS_LANG) { 831 if (elem->ns == APR_XML_NS_NONE) { | 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) |
832 len = sprintf(s, "</%s>", elem->name); | 894 len = sprintf(s, "</%s>", elem->name); |
833 } | 895 else if (style == APR_XML_X2T_PARSED) 896 len = sprintf(s, "</%s:%s>", find_prefix_name(elem, elem->ns, 1), elem->name); |
834 else { 835 ns = ns_map ? ns_map[elem->ns] : elem->ns; 836 len = sprintf(s, "</ns%d:%s>", ns, elem->name); 837 } 838 s += len; 839 } 840 841 return s; --- 174 unchanged lines hidden --- | 897 else { 898 ns = ns_map ? ns_map[elem->ns] : elem->ns; 899 len = sprintf(s, "</ns%d:%s>", ns, elem->name); 900 } 901 s += len; 902 } 903 904 return s; --- 174 unchanged lines hidden --- |