Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support duplicated namespaces #3233

Merged
merged 1 commit into from
Jul 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import org.xmlpull.v1.XmlSerializer;

import java.io.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
* Implementation of XmlSerializer interface from XmlPull V1 API. This
Expand Down Expand Up @@ -646,23 +648,35 @@ protected void closeStartTag() throws IOException {
}

protected void writeNamespaceDeclarations() throws IOException {
Set<String> uniqueNamespaces = new HashSet<>();
for (int i = elNamespaceCount[depth - 1]; i < namespaceEnd; i++) {
if (doIndent && namespaceUri[i].length() > 40) {
String prefix = namespacePrefix[i];
String uri = namespaceUri[i];

// Some applications as seen in #2664 have duplicated namespaces.
// AOSP doesn't care, but the parser does. So we filter them out.
if (uniqueNamespaces.contains(prefix + uri)) {
continue;
}

if (doIndent && uri.length() > 40) {
writeIndent();
out.write(" ");
}
if (namespacePrefix[i] != "") {
if (prefix != "") {
out.write(" xmlns:");
out.write(namespacePrefix[i]);
out.write(prefix);
out.write('=');
} else {
out.write(" xmlns=");
}
out.write(attributeUseApostrophe ? '\'' : '"');

// NOTE: escaping of namespace value the same way as attributes!!!!
writeAttributeValue(namespaceUri[i], out);
writeAttributeValue(uri, out);
out.write(attributeUseApostrophe ? '\'' : '"');

uniqueNamespaces.add(prefix + uri);
}
}

Expand Down