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.5 by pjm2, Tue Nov 14 10:04:01 2000 UTC vs.
Revision 1.8 by pjm2, Tue Nov 14 12:23:17 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 () {
# Line 38 | Line 40 | public class XMLPacketMaker extends HandlerBase {
40              SAXParser saxParser = factory.newSAXParser();
41              saxParser.parse(new File(args[0]), new XMLPacketMaker(packet));
42  
43 <            // Print out some things from the packet: -
44 <            
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 ();
# Line 60 | Line 63 | public class XMLPacketMaker extends HandlerBase {
63      //===========================================================
64  
65      public void startDocument () throws SAXException {
66 <        nl();
64 <        nl();
65 <        emit ("START DOCUMENT");
66 <        nl();
67 <        emit ("<?xml version='1.0' encoding='UTF-8'?>");
66 >        // No purpose currently.
67      }
68  
69      public void endDocument () throws SAXException {
70 <        nl(); emit ("END DOCUMENT");
72 <        try {
73 <            nl();
74 <            out.flush ();
75 <        } catch (IOException e) {
76 <            throw new SAXException ("I/O error", e);
77 <        }
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);
83        nl(); emit ("ELEMENT: ");
84        emit ("<"+name);
84          if (attrs != null) {
85              for (int i = 0; i < attrs.getLength (); i++) {
86 <                nl();
88 <                emit("   ATTR: ");
89 <                emit (attrs.getName (i));
90 <                emit ("\t\"");
91 <                emit (attrs.getValue (i));
92 <                emit ("\"");
86 >                packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i));
87              }
88          }
95        if (attrs.getLength() > 0) nl();
96        emit (">");
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 {
100        nl();
101        emit ("END_ELM: ");
102        emit ("</"+name+">");
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 {
108        nl(); emit ("CHARS:   ");
104          String s = new String(buf, offset, len);
105          if (!s.trim().equals("")) {
106 <            emit (s);
112 <            packet.addParam((String)tagList.get(tagList.size()-1), s);
106 >            packet.addParam(getPath(), s);
107          }
108      }
109  
110 +
111      //===========================================================
112      // Helpers ...
113      //===========================================================
114 <
115 <    // Wrap I/O exceptions in SAX exceptions, to
116 <    // suit handler signature requirements
117 <    private void emit (String s) throws SAXException {
118 <        try {
119 <            out.write (s);
120 <            out.flush ();
121 <        } catch (IOException e) {
122 <            throw new SAXException ("I/O error", e);
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 <
131 <    // Start a new line
132 <    // and indent the next line appropriately
133 <    private void nl () throws SAXException {
134 <        String lineEnd =  System.getProperty("line.separator");
135 <        try {
136 <            out.write (lineEnd);
137 <            for (int i=0; i < indentLevel; i++) out.write(indentString);
138 <        } catch (IOException e) {
139 <            throw new SAXException ("I/O error", e);
140 <        }
141 <    }
127 >    
128   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines