ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/server/XMLReader/XMLPacketParser.java
Revision: 1.2
Committed: Fri Nov 17 12:16:39 2000 UTC (24 years ago) by pjm2
Branch: MAIN
Changes since 1.1: +2 -2 lines
Log Message:
Changed a lot of parts of UDPReader, UDPReaderThread, XMLPacketMaker,
XMLPacketParser and XMLSender so that they all work together.  XMLSender
is just a summy program to send UDP data to the UDPReader.

File Contents

# User Rev Content
1 pjm2 1.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     // XMLPacketParser - Creates an XMLPacket object.
13     public class XMLPacketParser extends HandlerBase {
14    
15     // No-args constructor. Generally not used.
16     public XMLPacketParser () {
17     this.packet = new XMLPacket();
18     }
19    
20     // Constructor for accepting a reference to an XMLPacket
21     public XMLPacketParser (XMLPacket packet) {
22     this.packet = packet;
23     }
24    
25     // Accessor to the XMLPacket.
26     public XMLPacket getXMLPacket() {
27     return packet;
28     }
29    
30     private int indentLevel = 0;
31    
32     // For storing the tag heirarchy.
33     private ArrayList tagList = new ArrayList();
34     private XMLPacket packet;
35    
36     //===========================================================
37     // SAX DocumentHandler methods
38     //===========================================================
39    
40     public void startDocument () throws SAXException {
41 pjm2 1.2 System.out.println("XMLPacketParser - Starting parse process...");
42 pjm2 1.1 }
43    
44     public void endDocument () throws SAXException {
45 pjm2 1.2 System.out.println("XMLPacketParser - I just finished parsing an XML String.");
46 pjm2 1.1 }
47    
48     // Add each tag's attribute to the XMLPacket.
49     // Note that all attributes within an opening tag are
50     // stored as "someroot.sometag.attributes.attribute_name"
51     // E.g. If <packet> is the root node, then:
52     // <packet machine_name="raptor">
53     // is stored as:
54     // "packet.attributes.machine_name"
55     // within the XMLPacket.
56     public void startElement (String name, AttributeList attrs) throws SAXException {
57     indentLevel++;
58     tagList.add(name);
59     if (attrs != null) {
60     for (int i = 0; i < attrs.getLength (); i++) {
61     packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i));
62     }
63     }
64     }
65    
66     // When an XML element is finished with, we must remove
67     // the tag name from the tagList and decrement the indent
68     // level.
69     public void endElement (String name) throws SAXException {
70     tagList.remove(tagList.size()-1);
71     indentLevel--;
72     }
73    
74     // Any text falling within a pair of terminal tags must
75     // be added to the XMLPacket. Trim leading and trailing
76     // spaces and do not bother to add if there is no data
77     // specified within the tags.
78     public void characters (char[] buf, int offset, int len) throws SAXException {
79     String s = new String(buf, offset, len);
80     if (!s.trim().equals("")) {
81     packet.addParam(getPath(), s);
82     }
83     }
84    
85    
86     //===========================================================
87     // Helpers ...
88     //===========================================================
89    
90    
91     // Return the heirarchical string to be used as a key value
92     // in the XMLPacket.
93     private String getPath () {
94     String path = (String)tagList.get(0);
95     if (tagList.size() > 0) {
96     for (int i = 1 ; i < tagList.size() ; i++) {
97     path = path + "." + (String)tagList.get(i);
98     }
99     }
100     return path;
101     }
102    
103     }