最后一步是解析和处理实际的回调事件。要帮助完成此任务,需要修改在“如何生成 XML”中创建的 Event 类,以使该类能够从 XML 文档构建 Event 并创建 XML Element。此更改需要使用其他构造函数(获取 XML 文档)、retrieveValues 方法、添加两个成员变量(vendor 和 publisher)、所有字段的访问程序方法,最后,还需要打印方法。
创建实现上述逻辑的 Java 代码。
请注意,此代码与“如何解析注册回复”中说明的 RegReply 类的代码类似。
public Event(Document doc)
{
nvpairs = new Vector();
retrieveValues(doc);
}
public void print(PrintStream out)
{
out.println("\tCLASS=" + regClass);
out.println("\tSUBCLASS=" + regSubclass);
out.println("\tVENDOR=" + vendor);
out.println("\tPUBLISHER=" + publisher);
for (int i = 0; i < nvpairs.size(); i++) {
NVPair tempNv = (NVPair)
(nvpairs.elementAt(i));
out.print("\t\t");
tempNv.print(out);
}
}
private void retrieveValues(Document doc)
{
Node n;
NodeList nl;
String nodeName;
// Find the SC_EVENT element.
nl = doc.getElementsByTagName("SC_EVENT");
if (nl.getLength() != 1) {
System.out.println("Error in parsing: can't find "
+ "SC_EVENT node.");
return;
}
n = nl.item(0);
//
// Retrieve the values of the CLASS, SUBCLASS,
// VENDOR and PUBLISHER attributes.
//
regClass = ((Element)n).getAttribute("CLASS");
regSubclass = ((Element)n).getAttribute("SUBCLASS");
publisher = ((Element)n).getAttribute("PUBLISHER");
vendor = ((Element)n).getAttribute("VENDOR");
// Retrieve all the nv pairs
for (Node child = n.getFirstChild(); child != null;
child = child.getNextSibling())
{
nvpairs.add(new NVPair((Element)child));
}
}
public String getRegClass()
{
return (regClass);
}
public String getSubclass()
{
return (regSubclass);
}
public String getVendor()
{
return (vendor);
}
public String getPublisher()
{
return (publisher);
}
public Vector getNvpairs()
{
return (nvpairs);
}
private String vendor, publisher;
实现用于支持 XML 解析的 NVPair 类的其他构造函数和方法。
对 Event 类的更改(如步骤 1 所述)需要对 NVPair 类进行类似的更改。
public NVPair(Element elem)
{
retrieveValues(elem);
}
public void print(PrintStream out)
{
out.println("NAME=" + name + " VALUE=" + value);
}
private void retrieveValues(Element elem)
{
Node n;
NodeList nl;
String nodeName;
// Find the NAME element
nl = elem.getElementsByTagName("NAME");
if (nl.getLength() != 1) {
System.out.println("Error in parsing: can't find "
+ "NAME node.");
return;
}
// Get the TEXT section
n = nl.item(0).getFirstChild();
if (n == null || n.getNodeType() != Node.TEXT_NODE) {
System.out.println("Error in parsing: can't find "
+ "TEXT section.");
return;
}
// Retrieve the value
name = n.getNodeValue();
// Now get the value element
nl = elem.getElementsByTagName("VALUE");
if (nl.getLength() != 1) {
System.out.println("Error in parsing: can't find "
+ "VALUE node.");
return;
}
// Get the TEXT section
n = nl.item(0).getFirstChild();
if (n == null || n.getNodeType() != Node.TEXT_NODE) {
System.out.println("Error in parsing: can't find "
+ "TEXT section.");
return;
}
// Retrieve the value
value = n.getNodeValue();
}
public String getName()
{
return (name);
}
public String getValue()
{
return (value);
}
}
实现 EventReceptionThread 中的 while 循环,它将等待事件回调。
“如何定义事件接收线程”中介绍了 EventReceptionThread。
while(true) {
Socket sock = listeningSock.accept();
Document doc = db.parse(sock.getInputStream());
Event event = new Event(doc);
client.processEvent(event);
sock.close();
}