1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-29 17:52:06 +01:00

Streamline String to Enum transformation

for the core stanza Enums.
This commit is contained in:
Florian Schmaus 2014-06-10 18:35:24 +02:00
parent 0fe263106a
commit 009057e2e2
4 changed files with 51 additions and 26 deletions

View file

@ -18,8 +18,6 @@
package org.jivesoftware.smack.packet; package org.jivesoftware.smack.packet;
import java.util.Locale; import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
@ -41,7 +39,6 @@ import org.jivesoftware.smack.util.XmlStringBuilder;
* @author Matt Tucker * @author Matt Tucker
*/ */
public abstract class IQ extends Packet { public abstract class IQ extends Packet {
private static final Logger LOGGER = Logger.getLogger(IQ.class.getName());
private Type type = Type.get; private Type type = Type.get;
@ -203,18 +200,10 @@ public abstract class IQ extends Packet {
* @param string the String value to covert. * @param string the String value to covert.
* @return the corresponding Type. * @return the corresponding Type.
* @throws IllegalArgumentException when not able to parse the string parameter * @throws IllegalArgumentException when not able to parse the string parameter
* @throws NullPointerException if the string is null
*/ */
public static Type fromString(String string) { public static Type fromString(String string) {
Type type = null; return Type.valueOf(string.toLowerCase(Locale.US));
try {
type = Type.valueOf(string.toLowerCase(Locale.US));
}
catch (Exception e) {
final String msg = "Could not transform string '" + string + "' to IQ.Type";
LOGGER.log(Level.WARNING, msg, e);
throw new IllegalArgumentException(msg, e);
}
return type;
} }
} }
} }

View file

@ -647,13 +647,17 @@ public class Message extends Packet {
*/ */
error; error;
public static Type fromString(String name) { /**
try { * Converts a String into the corresponding types. Valid String values that can be converted
return Type.valueOf(name); * to types are: "normal", "chat", "groupchat", "headline" and "error".
} *
catch (Exception e) { * @param string the String value to covert.
return normal; * @return the corresponding Type.
} * @throws IllegalArgumentException when not able to parse the string parameter
* @throws NullPointerException if the string is null
*/
public static Type fromString(String string) {
return Type.valueOf(string.toLowerCase(Locale.US));
} }
} }

View file

@ -17,6 +17,8 @@
package org.jivesoftware.smack.packet; package org.jivesoftware.smack.packet;
import java.util.Locale;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
/** /**
@ -305,7 +307,21 @@ public class Presence extends Packet {
/** /**
* The presence packet contains an error message. * The presence packet contains an error message.
*/ */
error error;
/**
* Converts a String into the corresponding types. Valid String values that can be converted
* to types are: "available", "unavailable", "subscribe", "subscribed", "unsubscribe",
* "unsubscribed" and "error".
*
* @param string the String value to covert.
* @return the corresponding Type.
* @throws IllegalArgumentException when not able to parse the string parameter
* @throws NullPointerException if the string is null
*/
public static Type fromString(String string) {
return Type.valueOf(string.toLowerCase(Locale.US));
}
} }
/** /**
@ -336,6 +352,19 @@ public class Presence extends Packet {
/** /**
* Do not disturb. * Do not disturb.
*/ */
dnd dnd;
/**
* Converts a String into the corresponding types. Valid String values that can be converted
* to types are: "chat", "available", "away", "xa", and "dnd".
*
* @param string the String value to covert.
* @return the corresponding Type.
* @throws IllegalArgumentException when not able to parse the string parameter
* @throws NullPointerException if the string is null
*/
public static Mode fromString(String string) {
return Mode.valueOf(string.toLowerCase(Locale.US));
}
} }
} }

View file

@ -147,7 +147,10 @@ public class PacketParserUtils {
message.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id); message.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);
message.setTo(parser.getAttributeValue("", "to")); message.setTo(parser.getAttributeValue("", "to"));
message.setFrom(parser.getAttributeValue("", "from")); message.setFrom(parser.getAttributeValue("", "from"));
message.setType(Message.Type.fromString(parser.getAttributeValue("", "type"))); String typeString = parser.getAttributeValue("", "type");
if (typeString != null) {
message.setType(Message.Type.fromString(typeString));
}
String language = getLanguageAttribute(parser); String language = getLanguageAttribute(parser);
// determine message's default language // determine message's default language
@ -257,7 +260,7 @@ public class PacketParserUtils {
String typeString = parser.getAttributeValue("", "type"); String typeString = parser.getAttributeValue("", "type");
if (typeString != null && !typeString.equals("")) { if (typeString != null && !typeString.equals("")) {
try { try {
type = Presence.Type.valueOf(typeString); type = Presence.Type.fromString(typeString);
} }
catch (IllegalArgumentException iae) { catch (IllegalArgumentException iae) {
LOGGER.warning("Found invalid presence type " + typeString); LOGGER.warning("Found invalid presence type " + typeString);
@ -301,7 +304,7 @@ public class PacketParserUtils {
else if (elementName.equals("show")) { else if (elementName.equals("show")) {
String modeText = parser.nextText(); String modeText = parser.nextText();
try { try {
presence.setMode(Presence.Mode.valueOf(modeText)); presence.setMode(Presence.Mode.fromString(modeText));
} }
catch (IllegalArgumentException iae) { catch (IllegalArgumentException iae) {
LOGGER.warning("Found invalid presence mode " + modeText); LOGGER.warning("Found invalid presence mode " + modeText);