summaryrefslogtreecommitdiffstats
path: root/parser/html/java/htmlparser/src/nu/validator/saxtree/TreeParser.java
blob: a9d92deb0c9e82789879e5b5b9bf539254d097de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * Copyright (c) 2007 Henri Sivonen
 * Copyright (c) 2008 Mozilla Foundation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

package nu.validator.saxtree;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;

/**
 * A tree visitor that replays a tree as SAX events.
 * @version $Id$
 * @author hsivonen
 */
public final class TreeParser implements Locator {
    
    /**
     * The content handler.
     */
    private final ContentHandler contentHandler;

    /**
     * The lexical handler.
     */
    private final LexicalHandler lexicalHandler;

    /**
     * The current locator.
     */
    private Locator locatorDelegate;

    /**
     * The constructor.
     * 
     * @param contentHandler
     *            must not be <code>null</code>
     * @param lexicalHandler
     *            may be <code>null</code>
     */
    public TreeParser(final ContentHandler contentHandler,
            final LexicalHandler lexicalHandler) {
        if (contentHandler == null) {
            throw new IllegalArgumentException("contentHandler was null.");
        }
        this.contentHandler = contentHandler;
        if (lexicalHandler == null) {
            this.lexicalHandler = new NullLexicalHandler();
        } else {
            this.lexicalHandler = lexicalHandler;
        }
    }

    /**
     * Causes SAX events for the tree rooted at the argument to be emitted.
     * <code>startDocument()</code> and <code>endDocument()</code> are only
     * emitted for a <code>Document</code> node.
     * 
     * @param node
     *            the root
     * @throws SAXException
     */
    public void parse(Node node) throws SAXException {
        contentHandler.setDocumentLocator(this);
        Node current = node;
        Node next;
        for (;;) {
            current.visit(this);
            if ((next = current.getFirstChild()) != null) {
                current = next;
                continue;
            }
            for (;;) {
                current.revisit(this);
                if (current == node) {
                    return;
                }
                if ((next = current.getNextSibling()) != null) {
                    current = next;
                    break;
                }
                current = current.getParentNode();
            }
        }
    }

    /**
     * @see org.xml.sax.ContentHandler#characters(char[], int, int)
     */
    void characters(char[] ch, int start, int length, Locator locator)
            throws SAXException {
        this.locatorDelegate = locator;
        contentHandler.characters(ch, start, length);
    }

    /**
     * @see org.xml.sax.ContentHandler#endDocument()
     */
    void endDocument(Locator locator) throws SAXException {
        this.locatorDelegate = locator;
        contentHandler.endDocument();
    }

    /**
     * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
     *      java.lang.String, java.lang.String)
     */
    void endElement(String uri, String localName, String qName, Locator locator)
            throws SAXException {
        this.locatorDelegate = locator;
        contentHandler.endElement(uri, localName, qName);
    }

    /**
     * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
     */
    void endPrefixMapping(String prefix, Locator locator) throws SAXException {
        this.locatorDelegate = locator;
        contentHandler.endPrefixMapping(prefix);
    }

    /**
     * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
     */
    void ignorableWhitespace(char[] ch, int start, int length, Locator locator)
            throws SAXException {
        this.locatorDelegate = locator;
        contentHandler.ignorableWhitespace(ch, start, length);
    }

    /**
     * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
     *      java.lang.String)
     */
    void processingInstruction(String target, String data, Locator locator)
            throws SAXException {
        this.locatorDelegate = locator;
        contentHandler.processingInstruction(target, data);
    }

    /**
     * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
     */
    void skippedEntity(String name, Locator locator) throws SAXException {
        this.locatorDelegate = locator;
        contentHandler.skippedEntity(name);
    }

    /**
     * @see org.xml.sax.ContentHandler#startDocument()
     */
    void startDocument(Locator locator) throws SAXException {
        this.locatorDelegate = locator;
        contentHandler.startDocument();
    }

    /**
     * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
     *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    void startElement(String uri, String localName, String qName,
            Attributes atts, Locator locator) throws SAXException {
        this.locatorDelegate = locator;
        contentHandler.startElement(uri, localName, qName, atts);
    }

    /**
     * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
     *      java.lang.String)
     */
    void startPrefixMapping(String prefix, String uri, Locator locator)
            throws SAXException {
        this.locatorDelegate = locator;
        contentHandler.startPrefixMapping(prefix, uri);
    }

    /**
     * @see org.xml.sax.ext.LexicalHandler#comment(char[], int, int)
     */
    void comment(char[] ch, int start, int length, Locator locator)
            throws SAXException {
        this.locatorDelegate = locator;
        lexicalHandler.comment(ch, start, length);
    }

    /**
     * @see org.xml.sax.ext.LexicalHandler#endCDATA()
     */
    void endCDATA(Locator locator) throws SAXException {
        this.locatorDelegate = locator;
        lexicalHandler.endCDATA();
    }

    /**
     * @see org.xml.sax.ext.LexicalHandler#endDTD()
     */
    void endDTD(Locator locator) throws SAXException {
        this.locatorDelegate = locator;
        lexicalHandler.endDTD();
    }

    /**
     * @see org.xml.sax.ext.LexicalHandler#endEntity(java.lang.String)
     */
    void endEntity(String name, Locator locator) throws SAXException {
        this.locatorDelegate = locator;
        lexicalHandler.endEntity(name);
    }

    /**
     * @see org.xml.sax.ext.LexicalHandler#startCDATA()
     */
    void startCDATA(Locator locator) throws SAXException {
        this.locatorDelegate = locator;
        lexicalHandler.startCDATA();
    }

    /**
     * @see org.xml.sax.ext.LexicalHandler#startDTD(java.lang.String,
     *      java.lang.String, java.lang.String)
     */
    void startDTD(String name, String publicId, String systemId, Locator locator)
            throws SAXException {
        this.locatorDelegate = locator;
        lexicalHandler.startDTD(name, publicId, systemId);
    }

    /**
     * @see org.xml.sax.ext.LexicalHandler#startEntity(java.lang.String)
     */
    void startEntity(String name, Locator locator) throws SAXException {
        this.locatorDelegate = locator;
        lexicalHandler.startEntity(name);
    }

    /**
     * @see org.xml.sax.Locator#getColumnNumber()
     */
    public int getColumnNumber() {
        if (locatorDelegate == null) {
            return -1;
        } else {
            return locatorDelegate.getColumnNumber();
        }
    }

    /**
     * @see org.xml.sax.Locator#getLineNumber()
     */
    public int getLineNumber() {
        if (locatorDelegate == null) {
            return -1;
        } else {
            return locatorDelegate.getLineNumber();
        }
    }

    /**
     * @see org.xml.sax.Locator#getPublicId()
     */
    public String getPublicId() {
        if (locatorDelegate == null) {
            return null;
        } else {

            return locatorDelegate.getPublicId();
        }
    }

    /**
     * @see org.xml.sax.Locator#getSystemId()
     */
    public String getSystemId() {
        if (locatorDelegate == null) {
            return null;
        } else {
            return locatorDelegate.getSystemId();
        }
    }
}