Commit fe5c6144 authored by Derek DeMoro's avatar Derek DeMoro Committed by derek

Updated to parse individual elements while handling non well-formed packets.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@595 b35dd754-fafc-0310-a699-88a17e54d16e
parent dc8736f3
...@@ -9,14 +9,25 @@ ...@@ -9,14 +9,25 @@
package org.dom4j.io; package org.dom4j.io;
import org.dom4j.*; import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.QName;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlPullParserFactory;
import java.io.*;
import java.net.URL;
/** /**
* <p><code>XPPPacketReader</code> is a Reader of DOM4J documents that * <p><code>XPPPacketReader</code> is a Reader of DOM4J documents that
* uses the fast * uses the fast
...@@ -103,7 +114,8 @@ public class XPPPacketReader { ...@@ -103,7 +114,8 @@ public class XPPPacketReader {
if (systemID.indexOf(':') >= 0) { if (systemID.indexOf(':') >= 0) {
// lets assume its a URL // lets assume its a URL
return read(new URL(systemID)); return read(new URL(systemID));
} else { }
else {
// lets assume that we are given a file name // lets assume that we are given a file name
return read(new File(systemID)); return read(new File(systemID));
} }
...@@ -252,18 +264,20 @@ public class XPPPacketReader { ...@@ -252,18 +264,20 @@ public class XPPPacketReader {
// Implementation methods // Implementation methods
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
protected Document parseDocument() throws DocumentException, IOException, XmlPullParserException { public Document parseDocument() throws DocumentException, IOException, XmlPullParserException {
DocumentFactory df = getDocumentFactory(); DocumentFactory df = getDocumentFactory();
Document document = df.createDocument(); Document document = df.createDocument();
Element parent = null; Element parent = null;
XmlPullParser pp = getXPPParser(); XmlPullParser pp = getXPPParser();
pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); int count = 0;
// pp.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, true);
// pp.setFeature(XmlPullParser.FEATURE_VALIDATION, true);
// pp.setFeature("http://xmlpull.org/v1/doc/features.html#xml-roundtrip", true);
while (true) { while (true) {
// int type = pp.next(); int type = -1;
int type = pp.nextToken(); try {
type = pp.nextToken();
}
catch(Exception ex){
return null;
}
switch (type) { switch (type) {
case XmlPullParser.PROCESSING_INSTRUCTION: case XmlPullParser.PROCESSING_INSTRUCTION:
{ {
...@@ -271,7 +285,8 @@ public class XPPPacketReader { ...@@ -271,7 +285,8 @@ public class XPPPacketReader {
int loc = text.indexOf(" "); int loc = text.indexOf(" ");
if (loc >= 0) { if (loc >= 0) {
document.addProcessingInstruction(text.substring(0, loc), text.substring(loc + 1)); document.addProcessingInstruction(text.substring(0, loc), text.substring(loc + 1));
} else }
else
document.addProcessingInstruction(text, ""); document.addProcessingInstruction(text, "");
break; break;
} }
...@@ -287,7 +302,8 @@ public class XPPPacketReader { ...@@ -287,7 +302,8 @@ public class XPPPacketReader {
{ {
if (parent != null) { if (parent != null) {
parent.addCDATA(pp.getText()); parent.addCDATA(pp.getText());
} else { }
else {
throw new DocumentException("Cannot have text content outside of the root document"); throw new DocumentException("Cannot have text content outside of the root document");
} }
break; break;
...@@ -318,10 +334,12 @@ public class XPPPacketReader { ...@@ -318,10 +334,12 @@ public class XPPPacketReader {
} }
if (parent != null) { if (parent != null) {
parent.add(newElement); parent.add(newElement);
} else { }
else {
document.add(newElement); document.add(newElement);
} }
parent = newElement; parent = newElement;
count++;
break; break;
} }
case XmlPullParser.END_TAG: case XmlPullParser.END_TAG:
...@@ -329,6 +347,10 @@ public class XPPPacketReader { ...@@ -329,6 +347,10 @@ public class XPPPacketReader {
if (parent != null) { if (parent != null) {
parent = parent.getParent(); parent = parent.getParent();
} }
count--;
if (count == 0) {
return document;
}
break; break;
} }
case XmlPullParser.TEXT: case XmlPullParser.TEXT:
...@@ -336,7 +358,8 @@ public class XPPPacketReader { ...@@ -336,7 +358,8 @@ public class XPPPacketReader {
String text = pp.getText(); String text = pp.getText();
if (parent != null) { if (parent != null) {
parent.addText(text); parent.addText(text);
} else { }
else {
throw new DocumentException("Cannot have text content outside of the root document"); throw new DocumentException("Cannot have text content outside of the root document");
} }
break; break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment