1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-25 21:42:07 +01:00

Remove duplicate code in XmlStringBuilder

This commit is contained in:
Florian Schmaus 2019-09-04 09:47:29 +02:00
parent 85ef149c83
commit 935465a11b

View file

@ -591,21 +591,10 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
* @throws IOException if an I/O error occured. * @throws IOException if an I/O error occured.
*/ */
public void write(Writer writer, String enclosingNamespace) throws IOException { public void write(Writer writer, String enclosingNamespace) throws IOException {
for (CharSequence csq : sb.getAsList()) { XmlEnvironment enclosingXmlEnvironment = XmlEnvironment.builder()
if (csq instanceof XmlStringBuilder) { .withNamespace(enclosingNamespace)
((XmlStringBuilder) csq).write(writer, enclosingNamespace); .build();
} appendXmlTo(writer, enclosingXmlEnvironment);
else if (csq instanceof XmlNsAttribute) {
XmlNsAttribute xmlNsAttribute = (XmlNsAttribute) csq;
if (!xmlNsAttribute.value.equals(enclosingNamespace)) {
writer.write(xmlNsAttribute.toString());
enclosingNamespace = xmlNsAttribute.value;
}
}
else {
writer.write(csq.toString());
}
}
} }
public Iterator<CharSequence> getCharSequenceIterator() { public Iterator<CharSequence> getCharSequenceIterator() {
@ -615,14 +604,19 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
@Override @Override
public CharSequence toXML(XmlEnvironment enclosingXmlEnvironment) { public CharSequence toXML(XmlEnvironment enclosingXmlEnvironment) {
StringBuilder res = new StringBuilder(); StringBuilder res = new StringBuilder();
appendXmlTo(res, enclosingXmlEnvironment); try {
appendXmlTo(res, enclosingXmlEnvironment);
} catch (IOException e) {
// Should never happen.
throw new AssertionError(e);
}
return res; return res;
} }
private void appendXmlTo(StringBuilder res, XmlEnvironment enclosingXmlEnvironment) { private void appendXmlTo(Appendable appendable, XmlEnvironment enclosingXmlEnvironment) throws IOException {
for (CharSequence csq : sb.getAsList()) { for (CharSequence csq : sb.getAsList()) {
if (csq instanceof XmlStringBuilder) { if (csq instanceof XmlStringBuilder) {
((XmlStringBuilder) csq).appendXmlTo(res, enclosingXmlEnvironment); ((XmlStringBuilder) csq).appendXmlTo(appendable, enclosingXmlEnvironment);
} }
else if (csq instanceof XmlNsAttribute) { else if (csq instanceof XmlNsAttribute) {
XmlNsAttribute xmlNsAttribute = (XmlNsAttribute) csq; XmlNsAttribute xmlNsAttribute = (XmlNsAttribute) csq;
@ -632,7 +626,7 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
} }
} }
else { else {
res.append(csq); appendable.append(csq);
} }
} }
} }