ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/util/uk/org/iscream/cms/util/XMLStringParser.java
Revision: 1.4
Committed: Thu Nov 30 02:38:17 2000 UTC (23 years, 5 months ago) by ajm
Branch: MAIN
Branch point for: SERVER_PACKAGEBUILD
Changes since 1.3: +4 -4 lines
Log Message:
Changed package structure
uk.ac.ukc.iscream.refman and xml -> uk.ac.ukc.iscream.util

File Contents

# User Rev Content
1 tdb 1.3 //---PACKAGE DECLARATION---
2 ajm 1.4 package uk.ac.ukc.iscream.util;
3 tdb 1.3
4     //---IMPORTS---
5 pjm2 1.1 import java.io.*;
6     import java.util.ArrayList;
7    
8     import org.xml.sax.*;
9     import javax.xml.parsers.SAXParserFactory;
10     import javax.xml.parsers.ParserConfigurationException;
11     import javax.xml.parsers.SAXParser;
12    
13 tdb 1.3 /**
14     * XMLStringParser - Used to assist in creating XMLPacket objects.
15     *
16 ajm 1.4 * @author $Author: tdb1 $
17     * @version $Id: XMLStringParser.java,v 1.3 2000/11/29 19:11:53 tdb1 Exp $
18 tdb 1.3 */
19     public class XMLStringParser extends HandlerBase {
20    
21     //---FINAL ATTRIBUTES---
22    
23     /**
24     * The current CVS revision of this class
25     */
26 ajm 1.4 public final String REVISION = "$Revision: 1.3 $";
27 tdb 1.3
28     //---STATIC METHODS---
29 pjm2 1.1
30 tdb 1.3 //---CONSTRUCTORS---
31 pjm2 1.1
32     // No-args constructor. Generally not used.
33     public XMLStringParser () {
34     this.packet = new XMLPacket();
35     }
36    
37     // Constructor for accepting a reference to an XMLPacket
38     public XMLStringParser (XMLPacket packet) {
39     this.packet = packet;
40     }
41    
42 tdb 1.3 //---PUBLIC METHODS---
43    
44 pjm2 1.1 // Accessor to the XMLPacket.
45     public XMLPacket getXMLPacket() {
46     return packet;
47     }
48    
49     //===========================================================
50     // SAX DocumentHandler methods
51     //===========================================================
52    
53     public void startDocument () throws SAXException {
54 pjm2 1.2 //System.out.println("XMLPacketParser - Starting parse process...");
55 pjm2 1.1 }
56    
57     public void endDocument () throws SAXException {
58 pjm2 1.2 //System.out.println("XMLPacketParser - I just finished parsing an XML String.");
59 pjm2 1.1 }
60    
61     // Add each tag's attribute to the XMLPacket.
62     // Note that all attributes within an opening tag are
63     // stored as "someroot.sometag.attributes.attribute_name"
64     // E.g. If <packet> is the root node, then:
65     // <packet machine_name="raptor">
66     // is stored as:
67     // "packet.attributes.machine_name"
68     // within the XMLPacket.
69     public void startElement (String name, AttributeList attrs) throws SAXException {
70     indentLevel++;
71     tagList.add(name);
72     if (attrs != null) {
73     for (int i = 0; i < attrs.getLength (); i++) {
74     packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i));
75     }
76     }
77     }
78    
79     // When an XML element is finished with, we must remove
80     // the tag name from the tagList and decrement the indent
81     // level.
82     public void endElement (String name) throws SAXException {
83     tagList.remove(tagList.size()-1);
84     indentLevel--;
85     }
86    
87     // Any text falling within a pair of terminal tags must
88     // be added to the XMLPacket. Trim leading and trailing
89     // spaces and do not bother to add if there is no data
90     // specified within the tags.
91     public void characters (char[] buf, int offset, int len) throws SAXException {
92     String s = new String(buf, offset, len);
93     if (!s.trim().equals("")) {
94     packet.addParam(getPath(), s);
95     }
96     }
97    
98 tdb 1.3 /**
99     * Overrides the {@link java.lang.Object#toString() Object.toString()}
100     * method to provide clean logging (every class should have this).
101     *
102     * @return the name of this class and its CVS revision
103     */
104     public String toString() {
105     return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")";
106     }
107    
108     //---PRIVATE METHODS---
109 pjm2 1.1
110     //===========================================================
111     // Helpers ...
112     //===========================================================
113    
114    
115     // Return the heirarchical string to be used as a key value
116     // in the XMLPacket.
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 tdb 1.3
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     }