Repository navigation
Expand file tree
/
Copy pathMessageFromRobot.java
More file actions
166 lines (142 loc) · 5.2 KB
/
Copy pathMessageFromRobot.java
File metadata and controls
166 lines (142 loc) · 5.2 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package java_xml_library;
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
//import org.xml.sax.InputSource;
//import org.xml.sax.InputSource;
//import org.xml.sax.InputSource;
//import javax.xml.transform.*;
//import javax.xml.transform.dom.*;
//import javax.xml.transform.stream.*;
//import robotMessages.*;
//import MicroTime.*;
class MessageFromRobot extends RobotMessages
// For messages that are sent from a driver or controller to the robot
//
// Usage:
//
// $mfr = new messageFromRobot($rawXMLString); // build from the raw XML
// $mfr = new messageToRobot($driverAddr, $robotAddr, $responseValue [, $comment]);
{
public MessageFromRobot(String xmlStr)
// single-argument constructor: Build this class from its XML string
{
timeStamp = new MicroTime().smicrotime();
// parse XML string to create Document
// SAXParserFactory factory = SAXParserFactory.newInstance();
// try {
//
// out = new OutputStreamWriter (System.out, "UTF8");
// SAXParser saxParser = factory.newSAXParser();
// saxParser.parse( new File(XML_FILE_NAME), new BooksLibrary() );
//
// } catch (Throwable err) {
// err.printStackTrace ();
// }
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setFeature("http://xml.org/sax/features/namespaces", false);
dbf.setFeature("http://xml.org/sax/features/validation", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder db = dbf.newDocumentBuilder();
ByteArrayInputStream stream = new ByteArrayInputStream(xmlStr.getBytes());
XML = db.parse(stream);
} catch (Exception e) {
e.printStackTrace();
}
Element rootElement = XML.getDocumentElement();
// <t>timeStamp</t>
Element t = XML.createElement("t");
t.appendChild (XML.createTextNode(timeStamp));
rootElement.appendChild(t);
// <d>driverAddr</d>
NodeList nl = XML.getElementsByTagName("d");
Node n = nl.item(0);
Element e = (Element)n;
driverAddr = e.getTextContent();
// <r>robotAddr</r>
nl = XML.getElementsByTagName("r");
n = nl.item(0);
e = (Element)n;
robotAddr = e.getTextContent();
// <re>responseValue</re>
nl = XML.getElementsByTagName("re");
n = nl.item(0);
e = (Element)n;
responseValue = e.getTextContent();
// <co>comment</co>
nl = XML.getElementsByTagName("co");
n = nl.item(0);
e = (Element)n;
comment = e.getTextContent();
}
public MessageFromRobot(String da, String ra, String rv)
// 3-argument constructor: build this class from its component data with a response but without a comment
{
// set the class' properties from the passed-in values; if they're unspecified, set as empty
timeStamp = new MicroTime().smicrotime();
driverAddr = da;
robotAddr = ra;
responseValue = rv;
comment = "";
Element m = XML.createElement("m");
XML.appendChild(m);
Element rootElement = XML.getDocumentElement();
// <t>timeStamp</t>
Element t = XML.createElement("t");
t.appendChild (XML.createTextNode(timeStamp));
rootElement.appendChild(t);
// $this->XML->addChild('d', $this->driverAddr);
Element d = XML.createElement("d");
d.appendChild (XML.createTextNode(driverAddr));
rootElement.appendChild(d);
// $this->XML->addChild('r', $this->robotAddr);
Element r = XML.createElement("r");
r.appendChild (XML.createTextNode(robotAddr));
rootElement.appendChild(r);
// $this->XML->addChild('re', $this->responseValue);
Element re = XML.createElement("re");
re.appendChild (XML.createTextNode(responseValue));
rootElement.appendChild(re);
// $this->XML->addChild('co', $this->comment);
Element co = XML.createElement("co");
co.appendChild (XML.createTextNode(comment));
rootElement.appendChild(re);
}
public MessageFromRobot(String da, String ra, String rv, String co)
// 4-argument constructor: build this class from its component data with a response and a comment
{
// set the class' properties from the passed-in values; if they're unspecified, set as empty
timeStamp = new MicroTime().smicrotime();
driverAddr = da;
robotAddr = ra;
responseValue = rv;
comment = co;
Element m = XML.createElement("m");
XML.appendChild(m);
Element rootElement = XML.getDocumentElement();
// <t>timeStamp</t>
Element t = XML.createElement("t");
t.appendChild (XML.createTextNode(timeStamp));
rootElement.appendChild(t);
// $this->XML->addChild('d', $this->driverAddr);
Element d = XML.createElement("d");
d.appendChild (XML.createTextNode(driverAddr));
rootElement.appendChild(d);
// $this->XML->addChild('r', $this->robotAddr);
Element r = XML.createElement("r");
r.appendChild (XML.createTextNode(robotAddr));
rootElement.appendChild(r);
// $this->XML->addChild('re', $this->responseValue);
Element re = XML.createElement("re");
re.appendChild (XML.createTextNode(responseValue));
rootElement.appendChild(re);
// $this->XML->addChild('co', $this->comment);
Element c = XML.createElement("co");
c.appendChild (XML.createTextNode(comment));
rootElement.appendChild(c);
}
}