1 | /*
|
---|
2 | * 08/16/2008
|
---|
3 | *
|
---|
4 | * XMLParser.java - Simple XML parser.
|
---|
5 | *
|
---|
6 | * This library is distributed under a modified BSD license. See the included
|
---|
7 | * RSyntaxTextArea.License.txt file for details.
|
---|
8 | */
|
---|
9 | package org.fife.ui.rsyntaxtextarea.parser;
|
---|
10 |
|
---|
11 | import javax.swing.text.Document;
|
---|
12 | import javax.swing.text.Element;
|
---|
13 | import javax.xml.parsers.FactoryConfigurationError;
|
---|
14 | import javax.xml.parsers.SAXParser;
|
---|
15 | import javax.xml.parsers.SAXParserFactory;
|
---|
16 | import org.xml.sax.*;
|
---|
17 | import org.xml.sax.helpers.*;
|
---|
18 |
|
---|
19 | import org.fife.io.DocumentReader;
|
---|
20 | import org.fife.ui.rsyntaxtextarea.RSyntaxDocument;
|
---|
21 |
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * A parser for XML documents. Adds squiggle underlines for any XML errors
|
---|
25 | * found (though most XML parsers don't really have error recovery and so only
|
---|
26 | * can find one error at a time).<p>
|
---|
27 | *
|
---|
28 | * This class isn't actually used by RSyntaxTextArea anywhere, but you can
|
---|
29 | * install and use it yourself. Doing so is as simple as:
|
---|
30 | *
|
---|
31 | * <pre>
|
---|
32 | * XmlParser xmlParser = new XmlParser();
|
---|
33 | * textArea.addParser(xmlParser);
|
---|
34 | * </pre>
|
---|
35 | *
|
---|
36 | * Also note that a single instance of this class can be installed on
|
---|
37 | * multiple instances of <code>RSyntaxTextArea</code>.
|
---|
38 | *
|
---|
39 | * For a more complete XML parsing solution, see the
|
---|
40 | * <a href="http://svn.fifesoft.com/viewvc-1.0.5/bin/cgi/viewvc.cgi/RSTALanguageSupport/trunk/?root=RSyntaxTextArea">RSTALanguageSupport
|
---|
41 | * project</a>'s <code>XmlLanguageSupport</code> class.
|
---|
42 | *
|
---|
43 | * @author Robert Futrell
|
---|
44 | * @version 1.1
|
---|
45 | */
|
---|
46 | public class XmlParser extends AbstractParser {
|
---|
47 |
|
---|
48 | private SAXParserFactory spf;
|
---|
49 | private DefaultParseResult result;
|
---|
50 |
|
---|
51 |
|
---|
52 | public XmlParser() {
|
---|
53 | result = new DefaultParseResult(this);
|
---|
54 | try {
|
---|
55 | spf = SAXParserFactory.newInstance();
|
---|
56 | } catch (FactoryConfigurationError fce) {
|
---|
57 | fce.printStackTrace();
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * {@inheritDoc}
|
---|
64 | */
|
---|
65 | public ParseResult parse(RSyntaxDocument doc, String style) {
|
---|
66 |
|
---|
67 | result.clearNotices();
|
---|
68 | Element root = doc.getDefaultRootElement();
|
---|
69 | result.setParsedLines(0, root.getElementCount()-1);
|
---|
70 |
|
---|
71 | if (spf==null) {
|
---|
72 | return result;
|
---|
73 | }
|
---|
74 |
|
---|
75 | try {
|
---|
76 | SAXParser sp = spf.newSAXParser();
|
---|
77 | Handler handler = new Handler(doc);
|
---|
78 | DocumentReader r = new DocumentReader(doc);
|
---|
79 | InputSource input = new InputSource(r);
|
---|
80 | sp.parse(input, handler);
|
---|
81 | r.close();
|
---|
82 | } catch (SAXParseException spe) {
|
---|
83 | // A fatal parse error - ignore; a ParserNotice was already created.
|
---|
84 | } catch (Exception e) {
|
---|
85 | e.printStackTrace();
|
---|
86 | result.addNotice(new DefaultParserNotice(this,
|
---|
87 | "Error parsing XML: " + e.getMessage(), 0, -1, -1));
|
---|
88 | }
|
---|
89 |
|
---|
90 | return result;
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Callback notified when errors are found in the XML document. Adds a
|
---|
97 | * notice to be squiggle-underlined.
|
---|
98 | */
|
---|
99 | private class Handler extends DefaultHandler {
|
---|
100 |
|
---|
101 | private Document doc;
|
---|
102 |
|
---|
103 | private Handler(Document doc) {
|
---|
104 | this.doc = doc;
|
---|
105 | }
|
---|
106 |
|
---|
107 | private void doError(SAXParseException e, int level) {
|
---|
108 | int line = e.getLineNumber() - 1;
|
---|
109 | Element root = doc.getDefaultRootElement();
|
---|
110 | Element elem = root.getElement(line);
|
---|
111 | int offs = elem.getStartOffset();
|
---|
112 | int len = elem.getEndOffset() - offs;
|
---|
113 | if (line==root.getElementCount()-1) {
|
---|
114 | len++;
|
---|
115 | }
|
---|
116 | DefaultParserNotice pn = new DefaultParserNotice(XmlParser.this,
|
---|
117 | e.getMessage(), line, offs, len);
|
---|
118 | pn.setLevel(level);
|
---|
119 | result.addNotice(pn);
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void error(SAXParseException e) {
|
---|
123 | doError(e, ParserNotice.ERROR);
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void fatalError(SAXParseException e) {
|
---|
127 | doError(e, ParserNotice.ERROR);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public void warning(SAXParseException e) {
|
---|
131 | doError(e, ParserNotice.WARNING);
|
---|
132 | }
|
---|
133 |
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | } |
---|