Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
15 arune 1
/*
2
 * XMLParser.java
3
 *
4
 * Created on den 27 augusti 2003, 17:18
5
 */
6
package Macbeth.XML;
7
 
8
import java.io.*;
9
import org.xml.sax.*;
10
import org.xml.sax.helpers.DefaultHandler;
11
import javax.xml.parsers.SAXParserFactory;
12
import javax.xml.parsers.SAXParser;
13
import java.util.*;
14
 
15
/**
16
 * A helper class for parsing XML files.
17
 * @author Jimmy
18
 */
19
public class XMLParser extends DefaultHandler {
20
 
21
    private XMLDataHandler dataHandler;
22
    private SAXParserFactory factory;
23
    private SAXParser saxParser;
24
 
25
    /**
26
     * Creates a new instance of XMLParser.
27
     * @param dataHandler The XMLDataHandler that should be
28
     * invoked when XML data is being parsed.
29
     */
30
    public XMLParser(XMLDataHandler dataHandler) {
31
        this.dataHandler = dataHandler;
32
        try {
33
            //use the default (non-validating) parser
34
            factory = SAXParserFactory.newInstance();
35
 
36
            //create a parser
37
            saxParser = factory.newSAXParser();
38
 
39
            XMLReader parser = saxParser.getXMLReader();
40
            try {
41
                parser.setFeature("http://xml.org/sax/features/validation", false);
42
                //parser.setFeature("http://xml.org/sax/features/external-general-entities", false);
43
                //parser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
44
                } catch (SAXNotRecognizedException e) {
45
                  System.err.println("Unknown feature specified: " + e.getMessage());
46
                } catch (SAXNotSupportedException e) {
47
                  System.err.println("Unsupported feature specified: " + e.getMessage());
48
                } catch (SAXException e) {
49
                  System.err.println("Error in setting feature: " + e.getMessage());
50
                }
51
 
52
        } catch (Exception e) {
53
            e.printStackTrace();
54
        }
55
    }
56
 
57
    /**
58
     * Parses an XML-file and invokes the XML data handler for each tag.
59
     * @param xmlfile The XML-file to parse.
60
     * @throws FileNotFoundException If the file isn't found.
61
     * @throws IOException If an I/O-error occurs while reading file.
62
     * @throws SAXException If a parse error occurs. Probably
63
     * because of bad XML-syntax in file.
64
     */
65
    public void parseFile(String xmlfile) throws FileNotFoundException,IOException,SAXException {
66
        saxParser.parse(new File(xmlfile), this);
67
    }
68
 
69
    /**
70
     * Parses an XML-string and invokes the XML data handler for each tag.
71
     * @param xmlstring The XML-string to parse.
72
     * @throws IOException If an IO error occured while reading the string.
73
     * @throws SAXException If a parse error occured. Probably because of bad syntax in file.
74
     */
75
    public void parseString(String xmlstring) throws IOException, SAXException {
76
        ByteArrayInputStream xmlStream = new ByteArrayInputStream(xmlstring.getBytes());
77
        InputSource xmlSource = new InputSource(xmlStream);
78
        saxParser.parse(xmlSource, this);
79
    }
80
 
81
    /**
82
     * Called by SAX parser when start of XML-document has been reached.
83
     */
84
    public void startDocument() {
85
        dataHandler.XMLdocumentStart();
86
    }
87
 
88
    /**
89
     * Called by SAX parser when end of XML-document has been reached.
90
     */
91
    public void endDocument() {
92
        dataHandler.XMLdocumentEnd();
93
    }
94
 
95
    /**
96
     * Called by SAX parser when start of an XML-element was found.
97
     * TODO: describe these parameters
98
     * @param namespaceURI
99
     * @param lName
100
     * @param qName
101
     * @param attrs
102
     */
103
    public void startElement(String namespaceURI, String lName, String qName, Attributes attrs) {
104
        //get element name
105
        String elementName = lName;
106
        if (elementName.equals("")) {
107
            elementName = qName; // namespaceAware = false
108
        }
109
        //get attributes
110
        HashMap attributes = new HashMap(256);
111
        if (attrs != null) {
112
            for (int i=0; i<attrs.getLength(); i++) {
113
                String attributeName = attrs.getLocalName(i); // Attr name
114
                if (attributeName.equals("")) attributeName = attrs.getQName(i);
115
                String attributeValue = attrs.getValue(i);
116
                attributes.put(attributeName, attributeValue);
117
            }
118
        }
119
        dataHandler.XMLstartElement(elementName, attributes);
120
    }
121
 
122
    /**
123
     * Called by SAX parser when end of an XML-element was found.
124
     * TODO: describe these parameters
125
     * @param namespaceURI
126
     * @param sName
127
     * @param qName
128
     * @throws SAXException
129
     */
130
    public void endElement(String namespaceURI, String sName, String qName) throws SAXException {
131
        String name;
132
        if (sName.equals("")) {
133
            name = qName;
134
        } else {
135
            name = sName;
136
        }
137
        dataHandler.XMLendElement(name);
138
    }
139
 
140
    /**
141
     * Called by SAX parser when characters were found inside an element.
142
     * TODO: describe these parameters
143
     * @param buf
144
     * @param offset
145
     * @param len
146
     * @throws SAXException
147
     */
148
    public void characters(char buf[], int offset, int len) throws SAXException {
149
        String s = new String(buf, offset, len);
150
        dataHandler.XMLelementData(s);
151
    }
152
 
153
}