From 38e84b835c2101206d846f7ab6fc444914661753 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Mon, 20 Dec 2010 11:51:41 -0800 Subject: Capture the DTD body while it is being parsed. Change-Id: Ibef02ca759eb56a00f0f72f4063d129ef5350d21 http://b/3241492 --- xml/src/main/java/org/kxml2/io/KXmlParser.java | 48 +++++++++++++++++++------- 1 file changed, 36 insertions(+), 12 deletions(-) (limited to 'xml/src/main/java/org/kxml2') diff --git a/xml/src/main/java/org/kxml2/io/KXmlParser.java b/xml/src/main/java/org/kxml2/io/KXmlParser.java index 9fb858b..f9d6461 100644 --- a/xml/src/main/java/org/kxml2/io/KXmlParser.java +++ b/xml/src/main/java/org/kxml2/io/KXmlParser.java @@ -110,6 +110,13 @@ public class KXmlParser implements XmlPullParser, Closeable { private boolean keepNamespaceAttributes; /** + * If non-null, the contents of the read buffer must be copied into this + * string builder before the read buffer is overwritten. This is used to + * capture the raw DTD text while parsing the DTD. + */ + private StringBuilder bufferCapture; + + /** * Entities defined in or for this document. This map is created lazily. */ private Map documentEntities; @@ -408,10 +415,7 @@ public class KXmlParser implements XmlPullParser, Closeable { } break; case DOCDECL: - readDoctype(); - if (justOneToken) { - text = ""; // TODO: support capturing the doctype text - } + readDoctype(justOneToken); break; default: @@ -563,16 +567,32 @@ public class KXmlParser implements XmlPullParser, Closeable { * Read the document's DTD. Although this parser is non-validating, the DTD * must be parsed to capture entity values and default attribute values. */ - private void readDoctype() throws IOException, XmlPullParserException { + private void readDoctype(boolean saveDtdText) throws IOException, XmlPullParserException { read(START_DOCTYPE); - skip(); - rootElementName = readName(); - readExternalId(true, true); - skip(); - if (peekCharacter() == '[') { - readInternalSubset(); + + int startPosition = -1; + if (saveDtdText) { + bufferCapture = new StringBuilder(); + startPosition = position; } - skip(); + try { + skip(); + rootElementName = readName(); + readExternalId(true, true); + skip(); + if (peekCharacter() == '[') { + readInternalSubset(); + } + skip(); + } finally { + if (saveDtdText) { + bufferCapture.append(buffer, 0, position); + bufferCapture.delete(0, startPosition); + text = bufferCapture.toString(); + bufferCapture = null; + } + } + read('>'); } @@ -1456,6 +1476,10 @@ public class KXmlParser implements XmlPullParser, Closeable { } } + if (bufferCapture != null) { + bufferCapture.append(buffer, 0, position); + } + if (limit != position) { limit -= position; System.arraycopy(buffer, position, buffer, 0, limit); -- cgit v1.1