Skip to content

Commit

Permalink
fix: support duplicated namespaces (#3233)
Browse files Browse the repository at this point in the history
  • Loading branch information
iBotPeaches committed Jul 30, 2023
1 parent 33ca292 commit 57ef8a2
Showing 1 changed file with 18 additions and 4 deletions.
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

0 comments on commit 57ef8a2

Please sign in to comment.