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.3 by pjm2, Tue Nov 14 09:32:31 2000 UTC vs.
Revision 1.7 by pjm2, Tue Nov 14 12:01:19 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 +            // 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.
48 +            packet.printAll();
49 +
50          } catch (Throwable t) {
51              t.printStackTrace ();
52          }
# Line 35 | Line 57 | public class XMLPacketMaker extends HandlerBase {
57      private String indentString = "    "; // Amount to indent
58      private int indentLevel = 0;
59  
60 +    // For storing the tag heirarchy.
61      private ArrayList tagList = new ArrayList();
62 +    static private XMLPacket packet = null;
63  
64      //===========================================================
65      // SAX DocumentHandler methods
# Line 61 | Line 85 | public class XMLPacketMaker extends HandlerBase {
85  
86      public void startElement (String name, AttributeList attrs) throws SAXException {
87          indentLevel++;
88 +        tagList.add(name);
89          nl(); emit ("ELEMENT: ");
90          emit ("<"+name);
91          if (attrs != null) {
# Line 81 | Line 106 | public class XMLPacketMaker extends HandlerBase {
106          nl();
107          emit ("END_ELM: ");
108          emit ("</"+name+">");
109 +        tagList.remove(tagList.size()-1);    
110          indentLevel--;
111      }
112  
113      public void characters (char[] buf, int offset, int len) throws SAXException {
114          nl(); emit ("CHARS:   ");
115          String s = new String(buf, offset, len);
116 <        if (!s.trim().equals("")) emit (s);
116 >        if (!s.trim().equals("")) {
117 >            emit (s);
118 >            packet.addParam(getPath(), s);
119 >        }
120      }
121  
122      //===========================================================
# Line 115 | Line 144 | public class XMLPacketMaker extends HandlerBase {
144          } catch (IOException e) {
145              throw new SAXException ("I/O error", e);
146          }
147 +    }
148 +    
149 +    // Return the heirarchical string to be used as a key value
150 +    // in the XMLPacket.
151 +    private String getPath () {
152 +        String path = (String)tagList.get(0);
153 +        if (tagList.size() > 0) {
154 +            for (int i = 1 ; i < tagList.size() ; i++) {
155 +                path = path + "." + (String)tagList.get(i);
156 +            }
157 +        }
158 +        return path;
159      }
160   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines