From 6202cbe26b086f9febdc5a9aa3515ccf643b2301 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 29 Aug 2022 18:40:49 +0200 Subject: [PATCH] minor code clean up for tag and element --- .../eu/siacs/conversations/xml/Element.java | 343 +++++++++--------- .../java/eu/siacs/conversations/xml/Tag.java | 155 ++++---- .../conversations/xmpp/XmppConnection.java | 14 +- 3 files changed, 256 insertions(+), 256 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/xml/Element.java b/src/main/java/eu/siacs/conversations/xml/Element.java index 4d53a17b7..9c570df93 100644 --- a/src/main/java/eu/siacs/conversations/xml/Element.java +++ b/src/main/java/eu/siacs/conversations/xml/Element.java @@ -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 attributes = new Hashtable<>(); - private String content; - protected List children = new ArrayList<>(); + private final String name; + private Hashtable attributes = new Hashtable<>(); + private String content; + protected List 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 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 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 getChildren() { - return this.children; - } + public List getChildren() { + return this.children; + } - public Element setChildren(List children) { - this.children = children; - return this; - } + public Element setChildren(List 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 attributes) { - this.attributes = attributes; - return this; - } + public Element setAttributes(Hashtable 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 getAttributes() { - return this.attributes; - } + public Hashtable 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"); + } } diff --git a/src/main/java/eu/siacs/conversations/xml/Tag.java b/src/main/java/eu/siacs/conversations/xml/Tag.java index b9ef979ff..2e6098522 100644 --- a/src/main/java/eu/siacs/conversations/xml/Tag.java +++ b/src/main/java/eu/siacs/conversations/xml/Tag.java @@ -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 attributes = new Hashtable(); + protected int type; + protected String name; + protected Hashtable attributes = new Hashtable(); - 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 attributes) { - this.attributes = attributes; - return this; - } + public void setAttributes(final Hashtable 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> attributeSet = attributes.entrySet(); - Iterator> it = attributeSet.iterator(); - while (it.hasNext()) { - Entry 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> attributeSet = attributes.entrySet(); + for (final Entry 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 getAttributes() { - return this.attributes; - } + public Hashtable getAttributes() { + return this.attributes; + } } diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index bc77246e8..bd11fcbe2 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -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]; } }