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.6 by pjm2, Tue Nov 14 10:15:54 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 {
14  
15 +    // No-args constructor.  Generally not used.
16      public XMLPacketMaker () {
17 <        // default no-args constructor.
17 >        this.xml = "<packet></packet>";
18      }
19  
20 <    // Constructor for accepting a reference to an XMLPacket
21 <    public XMLPacketMaker (XMLPacket packet) {
22 <        this.packet = packet;
20 >    // Constructor for accepting XML input.
21 >    public XMLPacketMaker (String xml) {
22 >        this.xml = xml;
23      }
24  
25 <    public static void main(String[] args){
23 <        if (args.length != 1) {
24 <            System.err.println ("Usage: cmd filename");
25 <            System.exit (1);
26 <        }
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 {
31            // Set up output stream
32            out = new OutputStreamWriter (System.out, "UTF8");
33  
34            // Create the XMLPacket to store values in.
35            packet = new XMLPacket();
36
34              // Parse the input
35              SAXParser saxParser = factory.newSAXParser();
36 <            saxParser.parse(new File(args[0]), new XMLPacketMaker(packet));
36 >            saxParser.parse(xml, new XMLPacketParser(packet));
37  
41            // Print out some things from the packet: -
42            System.out.println("bung.wibble="+packet.getParam("bung.wibble"));
43            System.out.println("bung.ping.value1="+packet.getParam("bung.ping.value1"));
44            
45
46        } catch (Throwable t) {
47            t.printStackTrace ();
38          }
39 <        System.exit (0);
40 <    }
51 <
52 <    static private Writer out;
53 <    private String indentString = "    "; // Amount to indent
54 <    private int indentLevel = 0;
55 <
56 <    // For storing the tag heirarchy.
57 <    private ArrayList tagList = new ArrayList();
58 <    static private XMLPacket packet = null;
59 <
60 <    //===========================================================
61 <    // SAX DocumentHandler methods
62 <    //===========================================================
63 <
64 <    public void startDocument () throws SAXException {
65 <        nl();
66 <        nl();
67 <        emit ("START DOCUMENT");
68 <        nl();
69 <        emit ("<?xml version='1.0' encoding='UTF-8'?>");
70 <    }
71 <
72 <    public void endDocument () throws SAXException {
73 <        nl(); emit ("END DOCUMENT");
74 <        try {
75 <            nl();
76 <            out.flush ();
77 <        } catch (IOException e) {
78 <            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 <        tagList.add(name);
85 <        nl(); emit ("ELEMENT: ");
86 <        emit ("<"+name);
87 <        if (attrs != null) {
88 <            for (int i = 0; i < attrs.getLength (); i++) {
89 <                nl();
90 <                emit("   ATTR: ");
91 <                emit (attrs.getName (i));
92 <                emit ("\t\"");
93 <                emit (attrs.getValue (i));
94 <                emit ("\"");
95 <            }
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      }
100
101    public void endElement (String name) throws SAXException {
102        nl();
103        emit ("END_ELM: ");
104        emit ("</"+name+">");
105        tagList.remove(tagList.size()-1);    
106        indentLevel--;
107    }
108
109    public void characters (char[] buf, int offset, int len) throws SAXException {
110        nl(); emit ("CHARS:   ");
111        String s = new String(buf, offset, len);
112        if (!s.trim().equals("")) {
113            emit (s);
114            packet.addParam(getPath(), s);
115        }
116    }
117
118    //===========================================================
119    // Helpers ...
120    //===========================================================
121
122    // Wrap I/O exceptions in SAX exceptions, to
123    // suit handler signature requirements
124    private void emit (String s) throws SAXException {
125        try {
126            out.write (s);
127            out.flush ();
128        } catch (IOException e) {
129            throw new SAXException ("I/O error", e);
130        }
131    }
132
133    // Start a new line
134    // and indent the next line appropriately
135    private void nl () throws SAXException {
136        String lineEnd =  System.getProperty("line.separator");
137        try {
138            out.write (lineEnd);
139            for (int i=0; i < indentLevel; i++) out.write(indentString);
140        } catch (IOException e) {
141            throw new SAXException ("I/O error", e);
142        }
143    }
51      
52 <    private String getPath () {
146 <        String path = (String)tagList.get(0);
147 <        if (tagList.size() > 0) {
148 <            for (int i = 1 ; i < tagList.size() ; i++) {
149 <                path = path + "." + (String)tagList.get(i);
150 <            }
151 <        }
152 <        return path;
153 <    }
52 >    String xml;
53   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines