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