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.1 by pjm2, Mon Nov 13 12:31:27 2000 UTC vs.
Revision 1.8 by pjm2, Tue Nov 14 12:23:17 2000 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines