mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-09 17:19:39 +02:00
Properly escape Bookmarks and FormField XML
by using XmlStringBuilder. Fixes SMACK-577 Also extend LazyStringBuilder with a cache. And extend XmlStringBuilder with some more convenience methods. Move the ELEMENT and NAMESPACE definition from Form to DataForm, where it belongs.
This commit is contained in:
parent
2ce7656180
commit
26b5bc0212
9 changed files with 140 additions and 95 deletions
|
@ -22,13 +22,20 @@ import java.util.List;
|
|||
public class LazyStringBuilder implements Appendable, CharSequence {
|
||||
|
||||
private final List<CharSequence> list;
|
||||
|
||||
|
||||
private String cache;
|
||||
|
||||
private void invalidateCache() {
|
||||
cache = null;
|
||||
}
|
||||
|
||||
public LazyStringBuilder() {
|
||||
list = new ArrayList<CharSequence>(20);
|
||||
}
|
||||
|
||||
public LazyStringBuilder append(LazyStringBuilder lsb) {
|
||||
list.addAll(lsb.list);
|
||||
invalidateCache();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -36,6 +43,7 @@ public class LazyStringBuilder implements Appendable, CharSequence {
|
|||
public LazyStringBuilder append(CharSequence csq) {
|
||||
assert csq != null;
|
||||
list.add(csq);
|
||||
invalidateCache();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -43,17 +51,22 @@ public class LazyStringBuilder implements Appendable, CharSequence {
|
|||
public LazyStringBuilder append(CharSequence csq, int start, int end) {
|
||||
CharSequence subsequence = csq.subSequence(start, end);
|
||||
list.add(subsequence);
|
||||
invalidateCache();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LazyStringBuilder append(char c) {
|
||||
list.add(Character.toString(c));
|
||||
invalidateCache();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length() {
|
||||
if (cache != null) {
|
||||
return cache.length();
|
||||
}
|
||||
int length = 0;
|
||||
for (CharSequence csq : list) {
|
||||
length += csq.length();
|
||||
|
@ -63,6 +76,9 @@ public class LazyStringBuilder implements Appendable, CharSequence {
|
|||
|
||||
@Override
|
||||
public char charAt(int index) {
|
||||
if (cache != null) {
|
||||
return cache.charAt(index);
|
||||
}
|
||||
for (CharSequence csq : list) {
|
||||
if (index < csq.length()) {
|
||||
return csq.charAt(index);
|
||||
|
@ -80,10 +96,13 @@ public class LazyStringBuilder implements Appendable, CharSequence {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder(length());
|
||||
for (CharSequence csq : list) {
|
||||
sb.append(csq);
|
||||
if (cache == null) {
|
||||
StringBuilder sb = new StringBuilder(length());
|
||||
for (CharSequence csq : list) {
|
||||
sb.append(csq);
|
||||
}
|
||||
cache = sb.toString();
|
||||
}
|
||||
return sb.toString();
|
||||
return cache;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -168,6 +168,25 @@ public class XmlStringBuilder implements Appendable, CharSequence {
|
|||
return this;
|
||||
}
|
||||
|
||||
public XmlStringBuilder emptyElement(String element) {
|
||||
halfOpenElement(element);
|
||||
return closeEmptyElement();
|
||||
}
|
||||
|
||||
public XmlStringBuilder condEmptyElement(boolean condition, String element) {
|
||||
if (condition) {
|
||||
emptyElement(element);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public XmlStringBuilder condAttribute(boolean condition, String name, String value) {
|
||||
if (condition) {
|
||||
attribute(name, value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public XmlStringBuilder append(CharSequence csq) {
|
||||
assert csq != null;
|
||||
|
@ -207,4 +226,18 @@ public class XmlStringBuilder implements Appendable, CharSequence {
|
|||
public String toString() {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (!(other instanceof XmlStringBuilder)) {
|
||||
return false;
|
||||
}
|
||||
XmlStringBuilder otherXmlStringBuilder = (XmlStringBuilder) other;
|
||||
return toString().equals(otherXmlStringBuilder.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return toString().hashCode();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue