ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/server/XMLReader/XMLPacketMaker.java
Revision: 1.13
Committed: Fri Nov 17 14:45:56 2000 UTC (24 years ago) by pjm2
Branch: MAIN
Changes since 1.12: +15 -4 lines
Log Message:
Fixed a major parsing problem by wrapping the XML in an InputSource object
and removing null characters from the input.  Also modified the XMLSender
class so that it no longer sent "null" when the last line of an input file
is reached.

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 pjm2 1.10 // No-args constructor. Generally not used.
16 pjm2 1.5 public XMLPacketMaker () {
17 pjm2 1.10 this.xml = "<packet></packet>";
18 pjm2 1.5 }
19    
20 pjm2 1.10 // Constructor for accepting XML input.
21     public XMLPacketMaker (String xml) {
22     this.xml = xml;
23 pjm2 1.5 }
24    
25 pjm2 1.10 public XMLPacket createXMLPacket() {
26    
27     // Create the XMLPacket to store values in.
28     XMLPacket packet = new XMLPacket();
29 pjm2 1.3
30     // Use the default (non-validating) parser
31     SAXParserFactory factory = SAXParserFactory.newInstance();
32     try {
33 pjm2 1.5
34 pjm2 1.3 // Parse the input
35 pjm2 1.13
36     xml.trim();
37    
38     //debug stuff
39     byte[] temp = xml.getBytes();
40     System.out.println("String length: "+temp.length+" ");
41     for (int i = 0 ; i < temp.length ; i++){
42     System.out.print((char)temp[i]);
43     }
44    
45     InputSource inputSource = new InputSource(new StringReader(xml));
46 pjm2 1.3 SAXParser saxParser = factory.newSAXParser();
47 pjm2 1.13 saxParser.parse(inputSource, new XMLPacketParser(packet));
48 pjm2 1.5
49 pjm2 1.10 }
50 pjm2 1.13 //catch (IOException e){
51     // System.out.println("IOException: "+e);
52     //}
53 pjm2 1.12 catch (Throwable t) {
54 pjm2 1.11 System.out.println("XMLPacketMaker - I just received an XML packet that did not contain valid XML.");
55 pjm2 1.12 //System.out.println(e);
56     t.printStackTrace();
57 pjm2 1.11 return null;
58 pjm2 1.3 }
59 pjm2 1.10
60     return packet;
61 pjm2 1.3 }
62 pjm2 1.8
63 pjm2 1.10 String xml;
64 pjm2 1.3 }