forked from ScottOaks/JavaPerformanceTuning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSAXParserTest.java
More file actions
66 lines (57 loc) · 1.98 KB
/
Copy pathSAXParserTest.java
File metadata and controls
66 lines (57 loc) · 1.98 KB
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
/*
* Copyright (c) 2013,2014 Scott Oaks. All rights reserved.
*/
package net.sdo;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public abstract class SAXParserTest extends AbstractParsingTest {
protected abstract XMLReader getReader() throws ParserConfigurationException, SAXException;
private static class SAXDoneException extends SAXParseException {
public SAXDoneException(String s) {
super(s, null);
}
}
protected class CustomizedInnerHandler extends DefaultHandler {
@Override
public void startElement(String space, String name, String raw, Attributes atts) {
if (name.length() == 0) {
name = raw;
}
if (name.equalsIgnoreCase(ITEM_ID)) {
isItemID = true;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXDoneException {
if (isItemID) {
String s = new String(ch, start, length);
isItemID = false;
if (addItemId(s)) {
throw new SAXDoneException("Done");
}
}
}
}
@Override
protected void engineRun(InputStream ins) throws IOException {
try {
InputSource source = new InputSource();
XMLReader reader = getReader();
CustomizedInnerHandler handler = new CustomizedInnerHandler();
reader.setContentHandler(handler);
source.setByteStream(ins);
reader.parse(source);
} catch (SAXDoneException sde) {
int i = 0;
} catch (Exception ex) {
throw new IOException("Can't complete parsing", ex);
}
}
}