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.4 by pjm2, Tue Nov 14 09:48:06 2000 UTC vs.
Revision 1.9 by pjm2, Tue Nov 14 12:36:11 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 +    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");
# Line 21 | Line 33 | public class XMLPacketMaker extends HandlerBase {
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());
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 ();
48 >            System.out.println("The recieved packet did not contain valid XML, so I'm gonna reject it.");
49 >            //t.printStackTrace ();
50          }
51          System.exit (0);
52      }
# Line 37 | Line 57 | public class XMLPacketMaker extends HandlerBase {
57  
58      // For storing the tag heirarchy.
59      private ArrayList tagList = new ArrayList();
60 +    static private XMLPacket packet = null;
61  
62      //===========================================================
63      // SAX DocumentHandler methods
64      //===========================================================
65  
66      public void startDocument () throws SAXException {
67 <        nl();
47 <        nl();
48 <        emit ("START DOCUMENT");
49 <        nl();
50 <        emit ("<?xml version='1.0' encoding='UTF-8'?>");
67 >        // No purpose currently.
68      }
69  
70      public void endDocument () throws SAXException {
71 <        nl(); emit ("END DOCUMENT");
55 <        try {
56 <            nl();
57 <            out.flush ();
58 <        } catch (IOException e) {
59 <            throw new SAXException ("I/O error", e);
60 <        }
71 >        // No purpose currently.
72      }
73  
74 +    // 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      public void startElement (String name, AttributeList attrs) throws SAXException {
83          indentLevel++;
84 <        nl(); emit ("ELEMENT: ");
66 <        emit ("<"+name);
84 >        tagList.add(name);
85          if (attrs != null) {
86              for (int i = 0; i < attrs.getLength (); i++) {
87 <                nl();
70 <                emit("   ATTR: ");
71 <                emit (attrs.getName (i));
72 <                emit ("\t\"");
73 <                emit (attrs.getValue (i));
74 <                emit ("\"");
87 >                packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i));
88              }
89          }
77        if (attrs.getLength() > 0) nl();
78        emit (">");
90      }
91  
92 +    // When an XML element is finished with, we must remove
93 +    // the tag name from the tagList and decrement the indent
94 +    // level.
95      public void endElement (String name) throws SAXException {
96 <        nl();
83 <        emit ("END_ELM: ");
84 <        emit ("</"+name+">");
96 >        tagList.remove(tagList.size()-1);    
97          indentLevel--;
98      }
99  
100 +    // 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      public void characters (char[] buf, int offset, int len) throws SAXException {
89        nl(); emit ("CHARS:   ");
105          String s = new String(buf, offset, len);
106 <        if (!s.trim().equals("")) emit (s);
106 >        if (!s.trim().equals("")) {
107 >            packet.addParam(getPath(), s);
108 >        }
109      }
110  
111 +
112      //===========================================================
113      // Helpers ...
114      //===========================================================
115 <
116 <    // Wrap I/O exceptions in SAX exceptions, to
117 <    // suit handler signature requirements
118 <    private void emit (String s) throws SAXException {
119 <        try {
120 <            out.write (s);
121 <            out.flush ();
122 <        } catch (IOException e) {
123 <            throw new SAXException ("I/O error", e);
115 >    
116 >    
117 >    // Return the heirarchical string to be used as a key value
118 >    // in the XMLPacket.
119 >    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      }
128 <
109 <    // Start a new line
110 <    // and indent the next line appropriately
111 <    private void nl () throws SAXException {
112 <        String lineEnd =  System.getProperty("line.separator");
113 <        try {
114 <            out.write (lineEnd);
115 <            for (int i=0; i < indentLevel; i++) out.write(indentString);
116 <        } catch (IOException e) {
117 <            throw new SAXException ("I/O error", e);
118 <        }
119 <    }
128 >    
129   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines