ExpatParserTestpublic class ExpatParserTest extends TestCase
Fields Summary |
---|
private static final String | SNIPPET | static final String | XML |
Methods Summary |
---|
private void | checkNamespacesInOne(org.xmlpull.v1.XmlPullParser parser)
assertEquals(2, parser.getNamespaceCount(1));
// Prefix for default namespace is null.
assertNull(parser.getNamespacePrefix(0));
assertEquals("ns:default", parser.getNamespaceUri(0));
assertEquals("n1", parser.getNamespacePrefix(1));
assertEquals("ns:1", parser.getNamespaceUri(1));
assertEquals("ns:default", parser.getNamespace(null));
// KXML returns null.
// assertEquals("ns:default", parser.getNamespace(""));
| private void | checkNamespacesInTwo(org.xmlpull.v1.XmlPullParser parser)
// These should still be valid.
checkNamespacesInOne(parser);
assertEquals(3, parser.getNamespaceCount(2));
// Default ns should still be in the stack
assertNull(parser.getNamespacePrefix(0));
assertEquals("ns:default", parser.getNamespaceUri(0));
| private static org.xmlpull.v1.XmlPullParser | newPullParser()Creates a new pull parser with namespace support.
ExpatPullParser parser = new ExpatPullParser();
parser.setNamespaceProcessingEnabled(true);
return parser;
| private static void | parse(java.lang.String xml, org.xml.sax.ContentHandler contentHandler)Parses the given xml string and fires events on the given SAX handler.
try {
XMLReader reader = new ExpatReader();
reader.setContentHandler(contentHandler);
reader.parse(new InputSource(new StringReader(xml)));
}
catch (IOException e) {
throw new AssertionError(e);
}
| private static void | parse(java.io.Reader in, org.xml.sax.ContentHandler contentHandler)Parses xml from the given reader and fires events on the given SAX
handler.
XMLReader reader = new ExpatReader();
reader.setContentHandler(contentHandler);
reader.parse(new InputSource(in));
| private static void | parse(java.io.InputStream in, org.apache.harmony.xml.ExpatParserTest$Encoding encoding, org.xml.sax.ContentHandler contentHandler)Parses xml from the given input stream and fires events on the given SAX
handler.
try {
XMLReader reader = new ExpatReader();
reader.setContentHandler(contentHandler);
InputSource source = new InputSource(in);
source.setEncoding(encoding.expatName);
reader.parse(source);
} catch (IOException e) {
throw new AssertionError(e);
}
| public void | testCdata()
Reader in = new StringReader(
"<a><![CDATA[<b></b>]]> <![CDATA[<c></c>]]></a>");
ExpatReader reader = new ExpatReader();
TestCdataHandler handler = new TestCdataHandler();
reader.setContentHandler(handler);
reader.setLexicalHandler(handler);
reader.parse(new InputSource(in));
assertEquals(2, handler.startCdata);
assertEquals(2, handler.endCdata);
assertEquals("<b></b> <c></c>", handler.buffer.toString());
| public void | testDtd()
Reader in = new StringReader(
"<?xml version=\"1.0\"?><!DOCTYPE foo PUBLIC 'bar' 'tee'><a></a>");
ExpatReader reader = new ExpatReader();
TestDtdHandler handler = new TestDtdHandler();
reader.setContentHandler(handler);
reader.setLexicalHandler(handler);
reader.parse(new InputSource(in));
assertEquals("foo", handler.name);
assertEquals("bar", handler.publicId);
assertEquals("tee", handler.systemId);
assertTrue(handler.ended);
| public void | testExceptions()
// From startElement().
ContentHandler contentHandler = new DefaultHandler() {
@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
throw new SAXException();
}
};
try {
parse(SNIPPET, contentHandler);
fail();
} catch (SAXException checked) { /* expected */ }
// From endElement().
contentHandler = new DefaultHandler() {
@Override
public void endElement(String uri, String localName,
String qName)
throws SAXException {
throw new SAXException();
}
};
try {
parse(SNIPPET, contentHandler);
fail();
} catch (SAXException checked) { /* expected */ }
// From characters().
contentHandler = new DefaultHandler() {
@Override
public void characters(char ch[], int start, int length)
throws SAXException {
throw new SAXException();
}
};
try {
parse(SNIPPET, contentHandler);
fail();
} catch (SAXException checked) { /* expected */ }
| public void | testExpatPullParserNamespaces()
XmlPullParser pullParser = newPullParser();
pullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
pullParser.setInput(new StringReader(XML));
testPullParserNamespaces(pullParser);
| public void | testExternalEntity()
class Handler extends DefaultHandler {
List<String> elementNames = new ArrayList<String>();
StringBuilder text = new StringBuilder();
public InputSource resolveEntity(String publicId, String systemId)
throws IOException, SAXException {
if (publicId.equals("publicA") && systemId.equals("systemA")) {
return new InputSource(new StringReader("<a/>"));
} else if (publicId.equals("publicB")
&& systemId.equals("systemB")) {
/*
* Explicitly set the encoding here or else the parser will
* try to use the parent parser's encoding which is utf-16.
*/
InputSource inputSource = new InputSource(
new ByteArrayInputStream("bob".getBytes("utf-8")));
inputSource.setEncoding("utf-8");
return inputSource;
}
throw new AssertionError();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
elementNames.add(localName);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
elementNames.add("/" + localName);
}
@Override
public void characters(char ch[], int start, int length)
throws SAXException {
text.append(ch, start, length);
}
}
Reader in = new StringReader("<?xml version=\"1.0\"?>\n"
+ "<!DOCTYPE foo [\n"
+ " <!ENTITY a PUBLIC 'publicA' 'systemA'>\n"
+ " <!ENTITY b PUBLIC 'publicB' 'systemB'>\n"
+ "]>\n"
+ "<foo>\n"
+ " &a;<b>&b;</b></foo>");
ExpatReader reader = new ExpatReader();
Handler handler = new Handler();
reader.setContentHandler(handler);
reader.setEntityResolver(handler);
reader.parse(new InputSource(in));
assertEquals(Arrays.asList("foo", "a", "/a", "b", "/b", "/foo"),
handler.elementNames);
assertEquals("bob", handler.text.toString().trim());
| public void | testExternalEntityDownload()
class Server implements Runnable {
private final ServerSocket serverSocket;
Server() throws IOException {
serverSocket = new ServerSocket(8080);
}
public void run() {
try {
Socket socket = serverSocket.accept();
final InputStream in = socket.getInputStream();
Thread inputThread = new Thread() {
public void run() {
try {
byte[] buffer = new byte[1024];
while (in.read(buffer) > -1) { /* ignore */ }
} catch (IOException e) {
e.printStackTrace();
}
}
};
inputThread.setDaemon(true);
inputThread.start();
OutputStream out = socket.getOutputStream();
String body = "<bar></bar>";
String response = "HTTP/1.0 200 OK\n"
+ "Content-Length: " + body.length() + "\n"
+ "\n"
+ body;
out.write(response.getBytes("UTF-8"));
out.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Handler extends DefaultHandler {
List<String> elementNames = new ArrayList<String>();
public InputSource resolveEntity(String publicId, String systemId)
throws IOException, SAXException {
// The parser should have resolved the systemId.
assertEquals("http://localhost:8080/systemBar", systemId);
return new InputSource(systemId);
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
elementNames.add(localName);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
elementNames.add("/" + localName);
}
}
// Start server to serve up the XML for 'systemBar'.
Thread serverThread = new Thread(new Server());
serverThread.setDaemon(true);
serverThread.start();
// 'systemBar', the external entity, is relative to 'systemFoo':
Reader in = new StringReader("<?xml version=\"1.0\"?>\n"
+ "<!DOCTYPE foo [\n"
+ " <!ENTITY bar SYSTEM 'systemBar'>\n"
+ "]>\n"
+ "<foo>&bar;</foo>");
ExpatReader reader = new ExpatReader();
Handler handler = new Handler();
reader.setContentHandler(handler);
reader.setEntityResolver(handler);
InputSource source = new InputSource(in);
source.setSystemId("http://localhost:8080/systemFoo");
reader.parse(source);
assertEquals(Arrays.asList("foo", "bar", "/bar", "/foo"),
handler.elementNames);
| public void | testKxmlPullParserNamespaces()
XmlPullParser pullParser = new KXmlParser();
pullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
pullParser.setInput(new StringReader(XML));
testPullParserNamespaces(pullParser);
| public void | testNamespaces()
try {
NamespaceHandler handler = new NamespaceHandler();
parse(XML, handler);
handler.validate();
} catch (SAXException e) {
throw new RuntimeException(e);
}
| public void | testProcessingInstructions()
Reader in = new StringReader(
"<?bob lee?><a></a>");
ExpatReader reader = new ExpatReader();
TestProcessingInstrutionHandler handler
= new TestProcessingInstrutionHandler();
reader.setContentHandler(handler);
reader.parse(new InputSource(in));
assertEquals("bob", handler.target);
assertEquals("lee", handler.data);
| public void | testPullParser()
try {
XmlPullParser parser = newPullParser();
// Test reader.
parser.setInput(new StringReader(SNIPPET));
validate(parser);
// Test input stream.
parser.setInput(new ByteArrayInputStream(SNIPPET.getBytes()),
"UTF-8");
validate(parser);
} catch (XmlPullParserException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
| private void | testPullParserNamespaces(org.xmlpull.v1.XmlPullParser parser)
assertEquals(0, parser.getDepth());
assertEquals(0, parser.getNamespaceCount(0));
try {
parser.getNamespaceCount(1);
fail();
} catch (IndexOutOfBoundsException e) { /* expected */ }
// one
assertEquals(XmlPullParser.START_TAG, parser.next());
assertEquals(1, parser.getDepth());
checkNamespacesInOne(parser);
// n1:two
assertEquals(XmlPullParser.START_TAG, parser.nextTag());
assertEquals(2, parser.getDepth());
checkNamespacesInTwo(parser);
// Body of two.
assertEquals(XmlPullParser.TEXT, parser.next());
// End of two.
assertEquals(XmlPullParser.END_TAG, parser.nextTag());
// Depth should still be 2.
assertEquals(2, parser.getDepth());
// We should still be able to see the namespaces from two.
checkNamespacesInTwo(parser);
// End of one.
assertEquals(XmlPullParser.END_TAG, parser.nextTag());
// Depth should be back to 1.
assertEquals(1, parser.getDepth());
// We can still see the namespaces in one.
checkNamespacesInOne(parser);
// We shouldn't be able to see the namespaces in two anymore.
try {
parser.getNamespaceCount(2);
fail();
} catch (IndexOutOfBoundsException e) { /* expected */ }
assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
// We shouldn't be able to see the namespaces in one anymore.
try {
parser.getNamespaceCount(1);
fail();
} catch (IndexOutOfBoundsException e) { /* expected */ }
assertEquals(0, parser.getNamespaceCount(0));
| public void | testSax()
try {
// Parse String.
TestHandler handler = new TestHandler();
parse(SNIPPET, handler);
validate(handler);
// Parse Reader.
handler = new TestHandler();
parse(new StringReader(SNIPPET), handler);
validate(handler);
// Parse InputStream.
handler = new TestHandler();
parse(new ByteArrayInputStream(SNIPPET.getBytes()),
Encoding.UTF_8, handler);
validate(handler);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
| static void | validate(org.apache.harmony.xml.ExpatParserTest$TestHandler handler)
assertEquals("dagny", handler.startElementName);
assertEquals("dagny", handler.endElementName);
assertEquals("hello", handler.text.toString());
| static void | validate(org.xmlpull.v1.XmlPullParser parser)
assertEquals(XmlPullParser.START_DOCUMENT, parser.getEventType());
assertEquals(0, parser.getDepth());
assertEquals(XmlPullParser.START_TAG, parser.next());
assertEquals(1, parser.getDepth());
assertEquals("dagny", parser.getName());
assertEquals(1, parser.getAttributeCount());
assertEquals("dad", parser.getAttributeName(0));
assertEquals("bob", parser.getAttributeValue(0));
assertEquals("bob", parser.getAttributeValue(null, "dad"));
assertEquals(XmlPullParser.TEXT, parser.next());
assertEquals(1, parser.getDepth());
assertEquals("hello", parser.getText());
assertEquals(XmlPullParser.END_TAG, parser.next());
assertEquals(1, parser.getDepth());
assertEquals("dagny", parser.getName());
assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
assertEquals(0, parser.getDepth());
|
|