ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/server/XMLReader/XMLPacketMaker.java
Revision: 1.10
Committed: Fri Nov 17 11:07:32 2000 UTC (24 years ago) by pjm2
Branch: MAIN
Changes since 1.9: +15 -92 lines
Log Message:
Added a new class - XMLPacketParser.java

This new class looks after the SAX parsing methods which are used to
create our XMLPacket contents.

The XMLPacketMaker class has also been altered to make use of the
XMLPacketParser class.  The XMLPacketMaker class now accepts a String
argument in its constructor.  It uses the XMLPacketParser to populate the
XMLPacket, for which the XMLPacketMaker provides an accessor.

File Contents

# Content
1 import java.io.*;
2 import java.util.ArrayList;
3
4 import org.xml.sax.*;
5
6 import javax.xml.parsers.SAXParserFactory;
7 import javax.xml.parsers.ParserConfigurationException;
8 import javax.xml.parsers.SAXParser;
9
10 // Paul Mutton, pjm2@ukc.ac.uk
11
12 // XMLPacketMaker - Creates an XMLPacket object.
13 public class XMLPacketMaker extends HandlerBase {
14
15 // No-args constructor. Generally not used.
16 public XMLPacketMaker () {
17 this.xml = "<packet></packet>";
18 }
19
20 // Constructor for accepting XML input.
21 public XMLPacketMaker (String xml) {
22 this.xml = xml;
23 }
24
25 public XMLPacket createXMLPacket() {
26
27 // Create the XMLPacket to store values in.
28 XMLPacket packet = new XMLPacket();
29
30 // Use the default (non-validating) parser
31 SAXParserFactory factory = SAXParserFactory.newInstance();
32 try {
33
34 // Parse the input
35 SAXParser saxParser = factory.newSAXParser();
36 saxParser.parse(xml, new XMLPacketParser(packet));
37
38 // Print out the entire contents of the packet's HashMap.
39 System.out.println("XMLPacket contents: -");
40 packet.printAll();
41
42 }
43 catch (Throwable t) {
44 System.out.println("The recieved packet did not contain valid XML, so I'm gonna reject it.");
45 //t.printStackTrace ();
46 }
47
48 return packet;
49 }
50
51 String xml;
52 }