// Implementation methods
// -------------------------------------------------------------------------
protected Document parseDocument() throws DocumentException, IOException,
XmlPullParserException {
DocumentFactory df = getDocumentFactory();
Document document = df.createDocument();
Element parent = null;
XmlPullParser pp = getXPPParser();
pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
while (true) {
int type = pp.nextToken();
switch (type) {
case XmlPullParser.PROCESSING_INSTRUCTION: {
String text = pp.getText();
int loc = text.indexOf(" ");
if (loc >= 0) {
String target = text.substring(0, loc);
String txt = text.substring(loc + 1);
document.addProcessingInstruction(target, txt);
} else {
document.addProcessingInstruction(text, "");
}
break;
}
case XmlPullParser.COMMENT: {
if (parent != null) {
parent.addComment(pp.getText());
} else {
document.addComment(pp.getText());
}
break;
}
case XmlPullParser.CDSECT: {
if (parent != null) {
parent.addCDATA(pp.getText());
} else {
String msg = "Cannot have text content outside of the "
+ "root document";
throw new DocumentException(msg);
}
break;
}
case XmlPullParser.ENTITY_REF:
break;
case XmlPullParser.END_DOCUMENT:
return document;
case XmlPullParser.START_TAG: {
QName qname = (pp.getPrefix() == null) ? df.createQName(pp
.getName(), pp.getNamespace()) : df.createQName(pp
.getName(), pp.getPrefix(), pp.getNamespace());
Element newElement = df.createElement(qname);
int nsStart = pp.getNamespaceCount(pp.getDepth() - 1);
int nsEnd = pp.getNamespaceCount(pp.getDepth());
for (int i = nsStart; i < nsEnd; i++) {
if (pp.getNamespacePrefix(i) != null) {
newElement.addNamespace(pp.getNamespacePrefix(i),
pp.getNamespaceUri(i));
}
}
for (int i = 0; i < pp.getAttributeCount(); i++) {
QName qa = (pp.getAttributePrefix(i) == null) ? df
.createQName(pp.getAttributeName(i)) : df
.createQName(pp.getAttributeName(i), pp
.getAttributePrefix(i), pp
.getAttributeNamespace(i));
newElement.addAttribute(qa, pp.getAttributeValue(i));
}