ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/server/XMLReader/XMLPacketMaker.java
(Generate patch)

Comparing experimental/server/XMLReader/XMLPacketMaker.java (file contents):
Revision 1.3 by pjm2, Tue Nov 14 09:32:31 2000 UTC vs.
Revision 1.12 by pjm2, Fri Nov 17 12:44:47 2000 UTC

# Line 3 | Line 3 | 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;  
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 {
12    public static void main(String[] args){
13        if (args.length != 1) {
14            System.err.println ("Usage: cmd filename");
15            System.exit (1);
16        }
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 {
21            // Set up output stream
22            out = new OutputStreamWriter (System.out, "UTF8");
33  
34              // Parse the input
35              SAXParser saxParser = factory.newSAXParser();
36 <            saxParser.parse(new File(args[0]), new XMLPacketMaker());
36 >            saxParser.parse(xml, new XMLPacketParser(packet));
37  
28        } catch (Throwable t) {
29            t.printStackTrace ();
38          }
39 <        System.exit (0);
40 <    }
33 <
34 <    static private Writer out;
35 <    private String indentString = "    "; // Amount to indent
36 <    private int indentLevel = 0;
37 <
38 <    private ArrayList tagList = new ArrayList();
39 <
40 <    //===========================================================
41 <    // SAX DocumentHandler methods
42 <    //===========================================================
43 <
44 <    public void startDocument () throws SAXException {
45 <        nl();
46 <        nl();
47 <        emit ("START DOCUMENT");
48 <        nl();
49 <        emit ("<?xml version='1.0' encoding='UTF-8'?>");
50 <    }
51 <
52 <    public void endDocument () throws SAXException {
53 <        nl(); emit ("END DOCUMENT");
54 <        try {
55 <            nl();
56 <            out.flush ();
57 <        } catch (IOException e) {
58 <            throw new SAXException ("I/O error", e);
39 >        catch (IOException e){
40 >            System.out.println("IOException: "+e);
41          }
42 <    }
43 <
44 <    public void startElement (String name, AttributeList attrs) throws SAXException {
45 <        indentLevel++;
46 <        nl(); emit ("ELEMENT: ");
65 <        emit ("<"+name);
66 <        if (attrs != null) {
67 <            for (int i = 0; i < attrs.getLength (); i++) {
68 <                nl();
69 <                emit("   ATTR: ");
70 <                emit (attrs.getName (i));
71 <                emit ("\t\"");
72 <                emit (attrs.getValue (i));
73 <                emit ("\"");
74 <            }
42 >        catch (Throwable t) {
43 >            System.out.println("XMLPacketMaker - I just received an XML packet that did not contain valid XML.");
44 >            //System.out.println(e);
45 >            t.printStackTrace();
46 >            return null;
47          }
48 <        if (attrs.getLength() > 0) nl();
49 <        emit (">");
48 >        
49 >        return packet;
50      }
51 <
52 <    public void endElement (String name) throws SAXException {
81 <        nl();
82 <        emit ("END_ELM: ");
83 <        emit ("</"+name+">");
84 <        indentLevel--;
85 <    }
86 <
87 <    public void characters (char[] buf, int offset, int len) throws SAXException {
88 <        nl(); emit ("CHARS:   ");
89 <        String s = new String(buf, offset, len);
90 <        if (!s.trim().equals("")) emit (s);
91 <    }
92 <
93 <    //===========================================================
94 <    // Helpers ...
95 <    //===========================================================
96 <
97 <    // Wrap I/O exceptions in SAX exceptions, to
98 <    // suit handler signature requirements
99 <    private void emit (String s) throws SAXException {
100 <        try {
101 <            out.write (s);
102 <            out.flush ();
103 <        } catch (IOException e) {
104 <            throw new SAXException ("I/O error", e);
105 <        }
106 <    }
107 <
108 <    // Start a new line
109 <    // and indent the next line appropriately
110 <    private void nl () throws SAXException {
111 <        String lineEnd =  System.getProperty("line.separator");
112 <        try {
113 <            out.write (lineEnd);
114 <            for (int i=0; i < indentLevel; i++) out.write(indentString);
115 <        } catch (IOException e) {
116 <            throw new SAXException ("I/O error", e);
117 <        }
118 <    }
51 >    
52 >    String xml;
53   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines