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.7 by pjm2, Tue Nov 14 12:01:19 2000 UTC vs.
Revision 1.9 by pjm2, Tue Nov 14 12:36:11 2000 UTC

# Line 40 | Line 40 | public class XMLPacketMaker extends HandlerBase {
40              SAXParser saxParser = factory.newSAXParser();
41              saxParser.parse(new File(args[0]), new XMLPacketMaker(packet));
42  
43 <            // By means of example, print out some things from the packet: -
44 <            System.out.println("bung.wibble="+packet.getParam("bung.wibble"));
45 <            System.out.println("bung.ping.value1="+packet.getParam("bung.ping.value1"));            
46 <
47 <            // Print out the entire contents of the packet.
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 66 | Line 64 | public class XMLPacketMaker extends HandlerBase {
64      //===========================================================
65  
66      public void startDocument () throws SAXException {
67 <        nl();
70 <        nl();
71 <        emit ("START DOCUMENT");
72 <        nl();
73 <        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");
78 <        try {
79 <            nl();
80 <            out.flush ();
81 <        } catch (IOException e) {
82 <            throw new SAXException ("I/O error", e);
83 <        }
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          tagList.add(name);
89        nl(); emit ("ELEMENT: ");
90        emit ("<"+name);
85          if (attrs != null) {
86              for (int i = 0; i < attrs.getLength (); i++) {
87 <                nl();
94 <                emit("   ATTR: ");
95 <                emit (attrs.getName (i));
96 <                emit ("\t\"");
97 <                emit (attrs.getValue (i));
98 <                emit ("\"");
87 >                packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i));
88              }
89          }
101        if (attrs.getLength() > 0) nl();
102        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 {
106        nl();
107        emit ("END_ELM: ");
108        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 {
114        nl(); emit ("CHARS:   ");
105          String s = new String(buf, offset, len);
106          if (!s.trim().equals("")) {
117            emit (s);
107              packet.addParam(getPath(), s);
108          }
109      }
110  
111 +
112      //===========================================================
113      // Helpers ...
114      //===========================================================
125
126    // Wrap I/O exceptions in SAX exceptions, to
127    // suit handler signature requirements
128    private void emit (String s) throws SAXException {
129        try {
130            out.write (s);
131            out.flush ();
132        } catch (IOException e) {
133            throw new SAXException ("I/O error", e);
134        }
135    }
136
137    // Start a new line
138    // and indent the next line appropriately
139    private void nl () throws SAXException {
140        String lineEnd =  System.getProperty("line.separator");
141        try {
142            out.write (lineEnd);
143            for (int i=0; i < indentLevel; i++) out.write(indentString);
144        } catch (IOException e) {
145            throw new SAXException ("I/O error", e);
146        }
147    }
115      
116 +    
117      // Return the heirarchical string to be used as a key value
118      // in the XMLPacket.
119      private String getPath () {
# Line 157 | Line 125 | public class XMLPacketMaker extends HandlerBase {
125          }
126          return path;
127      }
128 +    
129   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines