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

@ -133,9 +133,8 @@ public class Element {
return this; return this;
} }
public Element removeAttribute(String name) { public void removeAttribute(final String name) {
this.attributes.remove(name); this.attributes.remove(name);
return this;
} }
public Element setAttributes(Hashtable<String, String> attributes) { public Element setAttributes(Hashtable<String, String> attributes) {
@ -171,21 +170,21 @@ public class Element {
public String toString() { public String toString() {
final StringBuilder elementOutput = new StringBuilder(); final StringBuilder elementOutput = new StringBuilder();
if ((content == null) && (children.size() == 0)) { if ((content == null) && (children.size() == 0)) {
Tag emptyTag = Tag.empty(name); final Tag emptyTag = Tag.empty(name);
emptyTag.setAtttributes(this.attributes); emptyTag.setAttributes(this.attributes);
elementOutput.append(emptyTag.toString()); elementOutput.append(emptyTag);
} else { } else {
Tag startTag = Tag.start(name); final Tag startTag = Tag.start(name);
startTag.setAtttributes(this.attributes); startTag.setAttributes(this.attributes);
elementOutput.append(startTag); elementOutput.append(startTag);
if (content != null) { if (content != null) {
elementOutput.append(XmlHelper.encodeEntities(content)); elementOutput.append(XmlHelper.encodeEntities(content));
} else { } else {
for (Element child : children) { for (final Element child : children) {
elementOutput.append(child.toString()); elementOutput.append(child.toString());
} }
} }
Tag endTag = Tag.end(name); final Tag endTag = Tag.end(name);
elementOutput.append(endTag); elementOutput.append(endTag);
} }
return elementOutput.toString(); return elementOutput.toString();

View file

@ -1,7 +1,8 @@
package eu.siacs.conversations.xml; package eu.siacs.conversations.xml;
import org.jetbrains.annotations.NotNull;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
@ -42,29 +43,26 @@ public class Tag {
return name; return name;
} }
public String getAttribute(String attrName) { public String getAttribute(final String attrName) {
return this.attributes.get(attrName); return this.attributes.get(attrName);
} }
public Tag setAttribute(String attrName, String attrValue) { public Tag setAttribute(final String attrName, final String attrValue) {
this.attributes.put(attrName, attrValue); this.attributes.put(attrName, attrValue);
return this; return this;
} }
public Tag setAtttributes(Hashtable<String, String> attributes) { public void setAttributes(final Hashtable<String, String> attributes) {
this.attributes = attributes; this.attributes = attributes;
return this;
} }
public boolean isStart(String needle) { public boolean isStart(String needle) {
if (needle == null) if (needle == null) return false;
return false;
return (this.type == START) && (needle.equals(this.name)); return (this.type == START) && (needle.equals(this.name));
} }
public boolean isEnd(String needle) { public boolean isEnd(String needle) {
if (needle == null) if (needle == null) return false;
return false;
return (this.type == END) && (needle.equals(this.name)); return (this.type == END) && (needle.equals(this.name));
} }
@ -72,18 +70,17 @@ public class Tag {
return (this.type == NO); return (this.type == NO);
} }
@NotNull
public String toString() { public String toString() {
StringBuilder tagOutput = new StringBuilder(); final StringBuilder tagOutput = new StringBuilder();
tagOutput.append('<'); tagOutput.append('<');
if (type == END) { if (type == END) {
tagOutput.append('/'); tagOutput.append('/');
} }
tagOutput.append(name); tagOutput.append(name);
if (type != END) { if (type != END) {
Set<Entry<String, String>> attributeSet = attributes.entrySet(); final Set<Entry<String, String>> attributeSet = attributes.entrySet();
Iterator<Entry<String, String>> it = attributeSet.iterator(); for (final Entry<String, String> entry : attributeSet) {
while (it.hasNext()) {
Entry<String, String> entry = it.next();
tagOutput.append(' '); tagOutput.append(' ');
tagOutput.append(entry.getKey()); tagOutput.append(entry.getKey());
tagOutput.append("=\""); tagOutput.append("=\"");

View file

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