-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.java
More file actions
74 lines (64 loc) · 1.98 KB
/
Message.java
File metadata and controls
74 lines (64 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.fellowrobots;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class Message {
public enum Command {
UNKNOWN,
LOGIN,
LOGOUT,
TURN_RIGHT,
}
private long message_id;
private String robot_name;
private Command command;
public Message(String message_xml) {
message_id = 0;
robot_name = "";
command = Command.UNKNOWN;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(message_xml));
Document message = builder.parse(is);
message_id = Long.parseLong(getTagValue(message, "id"));
robot_name = getTagValue(message, "robotname");
command = parseCommand(getTagValue(message, "command"));
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public long getMessageId() {
return message_id;
}
public String getRobotName() {
return robot_name;
}
public Command getCommand() {
return command;
}
private String getTagValue(Document message, String tag_name) {
NodeList nodes = message.getElementsByTagName(tag_name);
if (nodes.getLength() == 0) return "";
return nodes.item(0).getFirstChild().getNodeValue().trim();
}
private Command parseCommand(String command_text) {
if (command_text.equals("login")) return Command.LOGIN;
else if (command_text.equals("logout")) return Command.LOGOUT;
else if (command_text.equals("right")) return Command.TURN_RIGHT;
else return Command.UNKNOWN;
}
}