1 |
tdb |
1.2 |
/* |
2 |
tdb |
1.3 |
* i-scream central monitoring system |
3 |
|
|
* http://www.i-scream.org.uk |
4 |
tdb |
1.2 |
* Copyright (C) 2000-2002 i-scream |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU General Public License |
8 |
|
|
* as published by the Free Software Foundation; either version 2 |
9 |
|
|
* of the License, or (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
pjm2 |
1.1 |
//---PACKAGE DECLARATION--- |
22 |
|
|
|
23 |
|
|
//---IMPORTS--- |
24 |
|
|
import java.io.*; |
25 |
|
|
import java.util.ArrayList; |
26 |
|
|
|
27 |
|
|
import org.xml.sax.*; |
28 |
|
|
import javax.xml.parsers.SAXParserFactory; |
29 |
|
|
import javax.xml.parsers.ParserConfigurationException; |
30 |
|
|
import javax.xml.parsers.SAXParser; |
31 |
|
|
|
32 |
|
|
/** |
33 |
|
|
* XMLStreamParser - Used to build XMLPacket objects. |
34 |
|
|
* |
35 |
tdb |
1.3 |
* @author $Author: tdb $ |
36 |
|
|
* @version $Id: XMLStreamParser.java,v 1.2 2002/05/18 18:15:56 tdb Exp $ |
37 |
pjm2 |
1.1 |
*/ |
38 |
|
|
public class XMLStreamParser extends HandlerBase { |
39 |
|
|
|
40 |
|
|
//---FINAL ATTRIBUTES--- |
41 |
|
|
|
42 |
|
|
/** |
43 |
|
|
* The current CVS revision of this class |
44 |
|
|
*/ |
45 |
tdb |
1.3 |
public final String REVISION = "$Revision: 1.2 $"; |
46 |
pjm2 |
1.1 |
|
47 |
|
|
//---STATIC METHODS--- |
48 |
|
|
|
49 |
|
|
//---CONSTRUCTORS--- |
50 |
|
|
|
51 |
|
|
/** |
52 |
|
|
* Constructor for the XMLStreamParser. |
53 |
|
|
*/ |
54 |
|
|
public XMLStreamParser (XMLPacket packet) { |
55 |
|
|
_packet = packet; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
//---PUBLIC METHODS--- |
59 |
|
|
|
60 |
|
|
|
61 |
|
|
public void startDocument () throws SAXException { |
62 |
|
|
//System.out.println("Packet found"); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
public void endDocument () throws SAXException { |
66 |
|
|
//System.out.println("Packet Ended"); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
/** |
70 |
|
|
* Add each tag's attribute to the XMLPacket. |
71 |
|
|
* Note that all attributes within an opening tag are |
72 |
|
|
* stored as "someroot.sometag.attributes.attribute_name" |
73 |
|
|
* E.g. If <packet> is the root node, then: |
74 |
|
|
* <packet machine_name="raptor"> |
75 |
|
|
* is stored as: |
76 |
|
|
* "packet.attributes.machine_name" |
77 |
|
|
* within the XMLPacket. |
78 |
|
|
*/ |
79 |
|
|
public void startElement (String name, AttributeList attrs) throws SAXException { |
80 |
|
|
_indentLevel++; |
81 |
|
|
_tagList.add(name); |
82 |
|
|
if (attrs != null) { |
83 |
|
|
for (int i = 0; i < attrs.getLength (); i++) { |
84 |
|
|
_packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i)); |
85 |
|
|
} |
86 |
|
|
} |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
/** |
90 |
|
|
* When an XML element is finished with, we must remove |
91 |
|
|
* the tag name from the tagList and decrement the indent |
92 |
|
|
* level. |
93 |
|
|
*/ |
94 |
|
|
public void endElement (String name) throws SAXException { |
95 |
|
|
_tagList.remove(_tagList.size()-1); |
96 |
|
|
_indentLevel--; |
97 |
|
|
} |
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 |
|
|
*/ |
105 |
|
|
public void characters (char[] buf, int offset, int len) throws SAXException { |
106 |
|
|
String s = new String(buf, offset, len); |
107 |
|
|
if (!s.trim().equals("")) { |
108 |
|
|
_packet.addParam(getPath(), s); |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
//---PRIVATE METHODS--- |
113 |
|
|
|
114 |
|
|
/** |
115 |
|
|
* Return the heirarchical string to be used as a key value |
116 |
|
|
* in the XMLPacket. |
117 |
|
|
*/ |
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 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
129 |
|
|
|
130 |
|
|
//---ATTRIBUTES--- |
131 |
|
|
|
132 |
|
|
private int _indentLevel = 0; |
133 |
|
|
private ArrayList _tagList = new ArrayList(); |
134 |
|
|
private XMLPacket _packet; |
135 |
|
|
|
136 |
|
|
//---STATIC ATTRIBUTES--- |
137 |
|
|
|
138 |
|
|
} |