| 701 | |
| 702 | |
| 703 | void XMLWriter::declareNamespaces(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const Attributes& attributes) |
| 704 | { |
| 705 | std::map<XMLString, std::set<XMLString>> usedNamespaces; |
| 706 | bool defaultNameSpaceUsed = false; |
| 707 | XMLString defaultNamespaceURI = _namespaces.getURI(XMLString()); |
| 708 | XMLString local; |
| 709 | XMLString prefix; |
| 710 | XMLString elementNamespaceURI = namespaceURI; |
| 711 | Name::split(qname, prefix, local); |
| 712 | if (elementNamespaceURI.empty()) |
| 713 | elementNamespaceURI = _namespaces.getURI(prefix); |
| 714 | if (!elementNamespaceURI.empty()) |
| 715 | { |
| 716 | usedNamespaces[prefix].insert(elementNamespaceURI); |
| 717 | if (!defaultNamespaceURI.empty() && elementNamespaceURI == defaultNamespaceURI) |
| 718 | defaultNameSpaceUsed = true; |
| 719 | } |
| 720 | for (int i = 0; i < attributes.getLength(); i++) |
| 721 | { |
| 722 | XMLString attributeNamespaceURI = attributes.getURI(i); |
| 723 | XMLString attributeLocalName = attributes.getLocalName(i); |
| 724 | XMLString attributeQName = attributes.getQName(i); |
| 725 | |
| 726 | XMLString attributePrefix; |
| 727 | XMLString attributeLocal; |
| 728 | Name::split(attributeQName, attributePrefix, attributeLocal); |
| 729 | if (attributeNamespaceURI.empty()) |
| 730 | attributeNamespaceURI = _namespaces.getURI(prefix); |
| 731 | if (!attributeNamespaceURI.empty()) |
| 732 | { |
| 733 | usedNamespaces[attributePrefix].insert(attributeNamespaceURI); |
| 734 | defaultNameSpaceUsed = defaultNameSpaceUsed || (!defaultNamespaceURI.empty() && attributeNamespaceURI == defaultNamespaceURI); |
| 735 | } |
| 736 | } |
| 737 | for (const auto& p: usedNamespaces) |
| 738 | { |
| 739 | const std::set<XMLString> namespaceURIs = p.second; |
| 740 | for (const auto& nsURI: namespaceURIs) |
| 741 | { |
| 742 | XMLString prefix = p.first; |
| 743 | if (prefix.empty()) |
| 744 | prefix = _namespaces.getPrefix(nsURI); |
| 745 | if (prefix.empty() && !_namespaces.isMapped(nsURI)) |
| 746 | { |
| 747 | if (defaultNameSpaceUsed) |
| 748 | { |
| 749 | if (nsURI != defaultNamespaceURI) |
| 750 | prefix = uniquePrefix(); |
| 751 | } |
| 752 | else |
| 753 | { |
| 754 | defaultNamespaceURI = nsURI; |
| 755 | defaultNameSpaceUsed = true; |
| 756 | } |
| 757 | |
| 758 | } |
| 759 | const XMLString& uri = _namespaces.getURI(prefix); |
| 760 | if ((uri.empty() || uri != nsURI) && !nsURI.empty()) |