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