ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/server/XMLReader/XMLPacketMaker.java
Revision: 1.9
Committed: Tue Nov 14 12:36:11 2000 UTC (24 years ago) by pjm2
Branch: MAIN
Changes since 1.8: +2 -1 lines
Log Message:
Added test.bad - this is a file containing malformed XML markup to see if
the error handling in XMLPacketManager works properly.  XMLPacketMaker has
been altered accordingly, catching Throwable exceptions.

File Contents

# User Rev Content
1 pjm2 1.3 import java.io.*;
2     import java.util.ArrayList;
3    
4     import org.xml.sax.*;
5    
6 pjm2 1.7 import javax.xml.parsers.SAXParserFactory;
7     import javax.xml.parsers.ParserConfigurationException;
8     import javax.xml.parsers.SAXParser;
9 pjm2 1.3
10 pjm2 1.7 // Paul Mutton, pjm2@ukc.ac.uk
11 pjm2 1.3
12 pjm2 1.7 // XMLPacketMaker - Creates an XMLPacket object.
13 pjm2 1.3 public class XMLPacketMaker extends HandlerBase {
14 pjm2 1.5
15     public XMLPacketMaker () {
16     // default no-args constructor.
17     }
18    
19     // Constructor for accepting a reference to an XMLPacket
20     public XMLPacketMaker (XMLPacket packet) {
21     this.packet = packet;
22     }
23    
24 pjm2 1.3 public static void main(String[] args){
25     if (args.length != 1) {
26     System.err.println ("Usage: cmd filename");
27     System.exit (1);
28     }
29    
30     // Use the default (non-validating) parser
31     SAXParserFactory factory = SAXParserFactory.newInstance();
32     try {
33     // Set up output stream
34     out = new OutputStreamWriter (System.out, "UTF8");
35    
36 pjm2 1.5 // Create the XMLPacket to store values in.
37     packet = new XMLPacket();
38    
39 pjm2 1.3 // Parse the input
40     SAXParser saxParser = factory.newSAXParser();
41 pjm2 1.5 saxParser.parse(new File(args[0]), new XMLPacketMaker(packet));
42    
43 pjm2 1.8 // Print out the entire contents of the packet's HashMap.
44     System.out.println("XMLPacket contents: -");
45 pjm2 1.7 packet.printAll();
46 pjm2 1.3
47     } catch (Throwable t) {
48 pjm2 1.9 System.out.println("The recieved packet did not contain valid XML, so I'm gonna reject it.");
49     //t.printStackTrace ();
50 pjm2 1.3 }
51     System.exit (0);
52     }
53    
54     static private Writer out;
55     private String indentString = " "; // Amount to indent
56     private int indentLevel = 0;
57    
58 pjm2 1.4 // For storing the tag heirarchy.
59 pjm2 1.3 private ArrayList tagList = new ArrayList();
60 pjm2 1.5 static private XMLPacket packet = null;
61 pjm2 1.3
62     //===========================================================
63     // SAX DocumentHandler methods
64     //===========================================================
65    
66     public void startDocument () throws SAXException {
67 pjm2 1.8 // No purpose currently.
68 pjm2 1.3 }
69    
70     public void endDocument () throws SAXException {
71 pjm2 1.8 // No purpose currently.
72 pjm2 1.3 }
73    
74 pjm2 1.8 // Add each tag's attribute to the XMLPacket.
75     // Note that all attributes within an opening tag are
76     // stored as "someroot.sometag.attributes.attribute_name"
77     // E.g. If <packet> is the root node, then:
78     // <packet machine_name="raptor">
79     // is stored as:
80     // "packet.attributes.machine_name"
81     // within the XMLPacket.
82 pjm2 1.3 public void startElement (String name, AttributeList attrs) throws SAXException {
83     indentLevel++;
84 pjm2 1.5 tagList.add(name);
85 pjm2 1.3 if (attrs != null) {
86     for (int i = 0; i < attrs.getLength (); i++) {
87 pjm2 1.8 packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i));
88 pjm2 1.3 }
89     }
90     }
91    
92 pjm2 1.8 // When an XML element is finished with, we must remove
93     // the tag name from the tagList and decrement the indent
94     // level.
95 pjm2 1.3 public void endElement (String name) throws SAXException {
96 pjm2 1.5 tagList.remove(tagList.size()-1);
97 pjm2 1.3 indentLevel--;
98     }
99    
100 pjm2 1.8 // Any text falling within a pair of terminal tags must
101     // be added to the XMLPacket. Trim leading and trailing
102     // spaces and do not bother to add if there is no data
103     // specified within the tags.
104 pjm2 1.3 public void characters (char[] buf, int offset, int len) throws SAXException {
105     String s = new String(buf, offset, len);
106 pjm2 1.5 if (!s.trim().equals("")) {
107 pjm2 1.6 packet.addParam(getPath(), s);
108 pjm2 1.5 }
109 pjm2 1.3 }
110    
111 pjm2 1.8
112 pjm2 1.3 //===========================================================
113     // Helpers ...
114     //===========================================================
115 pjm2 1.8
116 pjm2 1.6
117 pjm2 1.7 // Return the heirarchical string to be used as a key value
118     // in the XMLPacket.
119 pjm2 1.6 private String getPath () {
120     String path = (String)tagList.get(0);
121     if (tagList.size() > 0) {
122     for (int i = 1 ; i < tagList.size() ; i++) {
123     path = path + "." + (String)tagList.get(i);
124     }
125     }
126     return path;
127 pjm2 1.3 }
128 pjm2 1.8
129 pjm2 1.3 }