minor code clean up for tag and element

This commit is contained in:
Daniel Gultsch 2022-08-29 18:40:49 +02:00
parent 5fc8ff899a
commit 6202cbe26b
3 changed files with 256 additions and 256 deletions

View file

@ -12,207 +12,206 @@ import eu.siacs.conversations.xmpp.Jid;
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
public class Element {
private final String name;
private Hashtable<String, String> attributes = new Hashtable<>();
private String content;
protected List<Element> children = new ArrayList<>();
private final String name;
private Hashtable<String, String> attributes = new Hashtable<>();
private String content;
protected List<Element> children = new ArrayList<>();
public Element(String name) {
this.name = name;
}
public Element(String name) {
this.name = name;
}
public Element(String name, String xmlns) {
this.name = name;
this.setAttribute("xmlns", xmlns);
}
public Element(String name, String xmlns) {
this.name = name;
this.setAttribute("xmlns", xmlns);
}
public Element addChild(Element child) {
this.content = null;
children.add(child);
return child;
}
public Element addChild(Element child) {
this.content = null;
children.add(child);
return child;
}
public Element addChild(String name) {
this.content = null;
Element child = new Element(name);
children.add(child);
return child;
}
public Element addChild(String name) {
this.content = null;
Element child = new Element(name);
children.add(child);
return child;
}
public Element addChild(String name, String xmlns) {
this.content = null;
Element child = new Element(name);
child.setAttribute("xmlns", xmlns);
children.add(child);
return child;
}
public Element addChild(String name, String xmlns) {
this.content = null;
Element child = new Element(name);
child.setAttribute("xmlns", xmlns);
children.add(child);
return child;
}
public Element setContent(String content) {
this.content = content;
this.children.clear();
return this;
}
public Element setContent(String content) {
this.content = content;
this.children.clear();
return this;
}
public Element findChild(String name) {
for (Element child : this.children) {
if (child.getName().equals(name)) {
return child;
}
}
return null;
}
public Element findChild(String name) {
for (Element child : this.children) {
if (child.getName().equals(name)) {
return child;
}
}
return null;
}
public String findChildContent(String name) {
Element element = findChild(name);
return element == null ? null : element.getContent();
}
public String findChildContent(String name) {
Element element = findChild(name);
return element == null ? null : element.getContent();
}
public LocalizedContent findInternationalizedChildContentInDefaultNamespace(String name) {
return LocalizedContent.get(this, name);
}
public LocalizedContent findInternationalizedChildContentInDefaultNamespace(String name) {
return LocalizedContent.get(this, name);
}
public Element findChild(String name, String xmlns) {
for (Element child : this.children) {
if (name.equals(child.getName()) && xmlns.equals(child.getAttribute("xmlns"))) {
return child;
}
}
return null;
}
public Element findChild(String name, String xmlns) {
for (Element child : this.children) {
if (name.equals(child.getName()) && xmlns.equals(child.getAttribute("xmlns"))) {
return child;
}
}
return null;
}
public Element findChildEnsureSingle(String name, String xmlns) {
final List<Element> results = new ArrayList<>();
for (Element child : this.children) {
if (name.equals(child.getName()) && xmlns.equals(child.getAttribute("xmlns"))) {
results.add(child);
}
}
if (results.size() == 1) {
return results.get(0);
}
return null;
}
public Element findChildEnsureSingle(String name, String xmlns) {
final List<Element> results = new ArrayList<>();
for (Element child : this.children) {
if (name.equals(child.getName()) && xmlns.equals(child.getAttribute("xmlns"))) {
results.add(child);
}
}
if (results.size() == 1) {
return results.get(0);
}
return null;
}
public String findChildContent(String name, String xmlns) {
Element element = findChild(name,xmlns);
return element == null ? null : element.getContent();
}
public String findChildContent(String name, String xmlns) {
Element element = findChild(name, xmlns);
return element == null ? null : element.getContent();
}
public boolean hasChild(final String name) {
return findChild(name) != null;
}
public boolean hasChild(final String name) {
return findChild(name) != null;
}
public boolean hasChild(final String name, final String xmlns) {
return findChild(name, xmlns) != null;
}
public boolean hasChild(final String name, final String xmlns) {
return findChild(name, xmlns) != null;
}
public List<Element> getChildren() {
return this.children;
}
public List<Element> getChildren() {
return this.children;
}
public Element setChildren(List<Element> children) {
this.children = children;
return this;
}
public Element setChildren(List<Element> children) {
this.children = children;
return this;
}
public final String getContent() {
return content;
}
public final String getContent() {
return content;
}
public Element setAttribute(String name, String value) {
if (name != null && value != null) {
this.attributes.put(name, value);
}
return this;
}
public Element setAttribute(String name, String value) {
if (name != null && value != null) {
this.attributes.put(name, value);
}
return this;
}
public Element setAttribute(String name, Jid value) {
if (name != null && value != null) {
this.attributes.put(name, value.toEscapedString());
}
return this;
}
public Element setAttribute(String name, Jid value) {
if (name != null && value != null) {
this.attributes.put(name, value.toEscapedString());
}
return this;
}
public Element removeAttribute(String name) {
this.attributes.remove(name);
return this;
}
public void removeAttribute(final String name) {
this.attributes.remove(name);
}
public Element setAttributes(Hashtable<String, String> attributes) {
this.attributes = attributes;
return this;
}
public Element setAttributes(Hashtable<String, String> attributes) {
this.attributes = attributes;
return this;
}
public String getAttribute(String name) {
if (this.attributes.containsKey(name)) {
return this.attributes.get(name);
} else {
return null;
}
}
public String getAttribute(String name) {
if (this.attributes.containsKey(name)) {
return this.attributes.get(name);
} else {
return null;
}
}
public Jid getAttributeAsJid(String name) {
final String jid = this.getAttribute(name);
if (jid != null && !jid.isEmpty()) {
try {
return Jid.ofEscaped(jid);
} catch (final IllegalArgumentException e) {
return InvalidJid.of(jid, this instanceof MessagePacket);
}
}
return null;
}
public Jid getAttributeAsJid(String name) {
final String jid = this.getAttribute(name);
if (jid != null && !jid.isEmpty()) {
try {
return Jid.ofEscaped(jid);
} catch (final IllegalArgumentException e) {
return InvalidJid.of(jid, this instanceof MessagePacket);
}
}
return null;
}
public Hashtable<String, String> getAttributes() {
return this.attributes;
}
public Hashtable<String, String> getAttributes() {
return this.attributes;
}
@NotNull
public String toString() {
final StringBuilder elementOutput = new StringBuilder();
if ((content == null) && (children.size() == 0)) {
Tag emptyTag = Tag.empty(name);
emptyTag.setAtttributes(this.attributes);
elementOutput.append(emptyTag.toString());
} else {
Tag startTag = Tag.start(name);
startTag.setAtttributes(this.attributes);
elementOutput.append(startTag);
if (content != null) {
elementOutput.append(XmlHelper.encodeEntities(content));
} else {
for (Element child : children) {
elementOutput.append(child.toString());
}
}
Tag endTag = Tag.end(name);
elementOutput.append(endTag);
}
return elementOutput.toString();
}
@NotNull
public String toString() {
final StringBuilder elementOutput = new StringBuilder();
if ((content == null) && (children.size() == 0)) {
final Tag emptyTag = Tag.empty(name);
emptyTag.setAttributes(this.attributes);
elementOutput.append(emptyTag);
} else {
final Tag startTag = Tag.start(name);
startTag.setAttributes(this.attributes);
elementOutput.append(startTag);
if (content != null) {
elementOutput.append(XmlHelper.encodeEntities(content));
} else {
for (final Element child : children) {
elementOutput.append(child.toString());
}
}
final Tag endTag = Tag.end(name);
elementOutput.append(endTag);
}
return elementOutput.toString();
}
public final String getName() {
return name;
}
public final String getName() {
return name;
}
public void clearChildren() {
this.children.clear();
}
public void clearChildren() {
this.children.clear();
}
public void setAttribute(String name, long value) {
this.setAttribute(name, Long.toString(value));
}
public void setAttribute(String name, long value) {
this.setAttribute(name, Long.toString(value));
}
public void setAttribute(String name, int value) {
this.setAttribute(name, Integer.toString(value));
}
public void setAttribute(String name, int value) {
this.setAttribute(name, Integer.toString(value));
}
public boolean getAttributeAsBoolean(String name) {
String attr = getAttribute(name);
return (attr != null && (attr.equalsIgnoreCase("true") || attr.equalsIgnoreCase("1")));
}
public boolean getAttributeAsBoolean(String name) {
String attr = getAttribute(name);
return (attr != null && (attr.equalsIgnoreCase("true") || attr.equalsIgnoreCase("1")));
}
public String getNamespace() {
return getAttribute("xmlns");
}
public String getNamespace() {
return getAttribute("xmlns");
}
}

View file

@ -1,104 +1,101 @@
package eu.siacs.conversations.xml;
import org.jetbrains.annotations.NotNull;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import eu.siacs.conversations.utils.XmlHelper;
public class Tag {
public static final int NO = -1;
public static final int START = 0;
public static final int END = 1;
public static final int EMPTY = 2;
public static final int NO = -1;
public static final int START = 0;
public static final int END = 1;
public static final int EMPTY = 2;
protected int type;
protected String name;
protected Hashtable<String, String> attributes = new Hashtable<String, String>();
protected int type;
protected String name;
protected Hashtable<String, String> attributes = new Hashtable<String, String>();
protected Tag(int type, String name) {
this.type = type;
this.name = name;
}
protected Tag(int type, String name) {
this.type = type;
this.name = name;
}
public static Tag no(String text) {
return new Tag(NO, text);
}
public static Tag no(String text) {
return new Tag(NO, text);
}
public static Tag start(String name) {
return new Tag(START, name);
}
public static Tag start(String name) {
return new Tag(START, name);
}
public static Tag end(String name) {
return new Tag(END, name);
}
public static Tag end(String name) {
return new Tag(END, name);
}
public static Tag empty(String name) {
return new Tag(EMPTY, name);
}
public static Tag empty(String name) {
return new Tag(EMPTY, name);
}
public String getName() {
return name;
}
public String getName() {
return name;
}
public String getAttribute(String attrName) {
return this.attributes.get(attrName);
}
public String getAttribute(final String attrName) {
return this.attributes.get(attrName);
}
public Tag setAttribute(String attrName, String attrValue) {
this.attributes.put(attrName, attrValue);
return this;
}
public Tag setAttribute(final String attrName, final String attrValue) {
this.attributes.put(attrName, attrValue);
return this;
}
public Tag setAtttributes(Hashtable<String, String> attributes) {
this.attributes = attributes;
return this;
}
public void setAttributes(final Hashtable<String, String> attributes) {
this.attributes = attributes;
}
public boolean isStart(String needle) {
if (needle == null)
return false;
return (this.type == START) && (needle.equals(this.name));
}
public boolean isStart(String needle) {
if (needle == null) return false;
return (this.type == START) && (needle.equals(this.name));
}
public boolean isEnd(String needle) {
if (needle == null)
return false;
return (this.type == END) && (needle.equals(this.name));
}
public boolean isEnd(String needle) {
if (needle == null) return false;
return (this.type == END) && (needle.equals(this.name));
}
public boolean isNo() {
return (this.type == NO);
}
public boolean isNo() {
return (this.type == NO);
}
public String toString() {
StringBuilder tagOutput = new StringBuilder();
tagOutput.append('<');
if (type == END) {
tagOutput.append('/');
}
tagOutput.append(name);
if (type != END) {
Set<Entry<String, String>> attributeSet = attributes.entrySet();
Iterator<Entry<String, String>> it = attributeSet.iterator();
while (it.hasNext()) {
Entry<String, String> entry = it.next();
tagOutput.append(' ');
tagOutput.append(entry.getKey());
tagOutput.append("=\"");
tagOutput.append(XmlHelper.encodeEntities(entry.getValue()));
tagOutput.append('"');
}
}
if (type == EMPTY) {
tagOutput.append('/');
}
tagOutput.append('>');
return tagOutput.toString();
}
@NotNull
public String toString() {
final StringBuilder tagOutput = new StringBuilder();
tagOutput.append('<');
if (type == END) {
tagOutput.append('/');
}
tagOutput.append(name);
if (type != END) {
final Set<Entry<String, String>> attributeSet = attributes.entrySet();
for (final Entry<String, String> entry : attributeSet) {
tagOutput.append(' ');
tagOutput.append(entry.getKey());
tagOutput.append("=\"");
tagOutput.append(XmlHelper.encodeEntities(entry.getValue()));
tagOutput.append('"');
}
}
if (type == EMPTY) {
tagOutput.append('/');
}
tagOutput.append('>');
return tagOutput.toString();
}
public Hashtable<String, String> getAttributes() {
return this.attributes;
}
public Hashtable<String, String> getAttributes() {
return this.attributes;
}
}

View file

@ -498,6 +498,10 @@ public class XmppConnection implements Runnable {
+ ")");
account.setKey(
Account.PINNED_MECHANISM_KEY, String.valueOf(saslMechanism.getPriority()));
if (version == SaslMechanism.Version.SASL_2) {
final String authorizationIdentifier = success.findChildContent("authorization-identifier");
Log.d(Config.LOGTAG,account.getJid().asBareJid()+": SASL 2.0 authorization identifier was "+authorizationIdentifier);
}
if (version == SaslMechanism.Version.SASL) {
tagReader.reset();
sendStartStream();
@ -1179,7 +1183,7 @@ public class XmppConnection implements Runnable {
Log.d(Config.LOGTAG, account.getJid() + ": disconnecting because of bind failure. (no jid)");
}
} else {
Log.d(Config.LOGTAG, account.getJid() + ": disconnecting because of bind failure (" + packet.toString());
Log.d(Config.LOGTAG, account.getJid() + ": disconnecting because of bind failure (" + packet);
}
final Element error = packet.findChild("error");
if (packet.getType() == IqPacket.TYPE.ERROR && error != null && error.hasChild("conflict")) {
@ -1449,7 +1453,7 @@ public class XmppConnection implements Runnable {
features.carbonsEnabled = true;
} else {
Log.d(Config.LOGTAG, account.getJid().asBareJid()
+ ": error enableing carbons " + packet.toString());
+ ": could not enable carbons " + packet);
}
});
}
@ -1472,7 +1476,7 @@ public class XmppConnection implements Runnable {
failPendingMessages(text);
throw new StateChangingException(Account.State.POLICY_VIOLATION);
} else {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": stream error " + streamError.toString());
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": stream error " + streamError);
throw new StateChangingException(Account.State.STREAM_ERROR);
}
}
@ -1839,8 +1843,8 @@ public class XmppConnection implements Runnable {
Log.d(Config.LOGTAG, "getting certificate chain");
try {
return KeyChain.getCertificateChain(mXmppConnectionService, alias);
} catch (Exception e) {
Log.d(Config.LOGTAG, e.getMessage());
} catch (final Exception e) {
Log.d(Config.LOGTAG, "could not get certificate chain", e);
return new X509Certificate[0];
}
}