mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-12-26 20:47:58 +01:00
Streamline String to Enum transformation
for the core stanza Enums.
This commit is contained in:
parent
0fe263106a
commit
009057e2e2
4 changed files with 51 additions and 26 deletions
|
@ -18,8 +18,6 @@
|
|||
package org.jivesoftware.smack.packet;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
|
@ -41,7 +39,6 @@ import org.jivesoftware.smack.util.XmlStringBuilder;
|
|||
* @author Matt Tucker
|
||||
*/
|
||||
public abstract class IQ extends Packet {
|
||||
private static final Logger LOGGER = Logger.getLogger(IQ.class.getName());
|
||||
|
||||
private Type type = Type.get;
|
||||
|
||||
|
@ -202,19 +199,11 @@ public abstract class IQ extends Packet {
|
|||
*
|
||||
* @param string the String value to covert.
|
||||
* @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) {
|
||||
Type type = null;
|
||||
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;
|
||||
return Type.valueOf(string.toLowerCase(Locale.US));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -647,13 +647,17 @@ public class Message extends Packet {
|
|||
*/
|
||||
error;
|
||||
|
||||
public static Type fromString(String name) {
|
||||
try {
|
||||
return Type.valueOf(name);
|
||||
}
|
||||
catch (Exception e) {
|
||||
return normal;
|
||||
}
|
||||
/**
|
||||
* Converts a String into the corresponding types. Valid String values that can be converted
|
||||
* to types are: "normal", "chat", "groupchat", "headline" 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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.jivesoftware.smack.packet;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
/**
|
||||
|
@ -305,7 +307,21 @@ public class Presence extends Packet {
|
|||
/**
|
||||
* 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.
|
||||
*/
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -147,7 +147,10 @@ public class PacketParserUtils {
|
|||
message.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);
|
||||
message.setTo(parser.getAttributeValue("", "to"));
|
||||
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);
|
||||
|
||||
// determine message's default language
|
||||
|
@ -257,7 +260,7 @@ public class PacketParserUtils {
|
|||
String typeString = parser.getAttributeValue("", "type");
|
||||
if (typeString != null && !typeString.equals("")) {
|
||||
try {
|
||||
type = Presence.Type.valueOf(typeString);
|
||||
type = Presence.Type.fromString(typeString);
|
||||
}
|
||||
catch (IllegalArgumentException iae) {
|
||||
LOGGER.warning("Found invalid presence type " + typeString);
|
||||
|
@ -301,7 +304,7 @@ public class PacketParserUtils {
|
|||
else if (elementName.equals("show")) {
|
||||
String modeText = parser.nextText();
|
||||
try {
|
||||
presence.setMode(Presence.Mode.valueOf(modeText));
|
||||
presence.setMode(Presence.Mode.fromString(modeText));
|
||||
}
|
||||
catch (IllegalArgumentException iae) {
|
||||
LOGGER.warning("Found invalid presence mode " + modeText);
|
||||
|
|
Loading…
Reference in a new issue