Position parser at START_ELEMENT before parsing

This PR aims to provide parseXrdLinkReferencesFor() method the ability
to parse forward to the first START_ELEMENT tag.The HttpLookupMethodTest
tests the HttpLookupMethod class by parsing String. This makes use of
PacketParserUtils.getParserFor(String), which already does forward
winding to reach START_ELEMENT. However when fetching endpoints from a
remote host meta data, PacketParserUtils.getParserFor(InputStream) is
used which doesn't do winding in any form. And thus, even though
HttpLookupMethodTest tests pass, this implementation would crash while
parsing remote host-meta.
This commit is contained in:
Aditya Borikar 2020-07-24 06:30:53 +05:30
parent e35175a3d5
commit 7796b367cc
3 changed files with 14 additions and 8 deletions

View File

@ -27,6 +27,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smack.xml.XmlPullParser; import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException; import org.jivesoftware.smack.xml.XmlPullParserException;
@ -132,6 +133,7 @@ public final class HttpLookupMethod {
* @throws URISyntaxException exception to indicate that a string could not be parsed as a URI reference * @throws URISyntaxException exception to indicate that a string could not be parsed as a URI reference
*/ */
public static List<URI> parseXrdLinkReferencesFor(XmlPullParser parser, String relation) throws IOException, XmlPullParserException, URISyntaxException { public static List<URI> parseXrdLinkReferencesFor(XmlPullParser parser, String relation) throws IOException, XmlPullParserException, URISyntaxException {
ParserUtils.forwardToStartElement(parser);
List<URI> uriList = new ArrayList<>(); List<URI> uriList = new ArrayList<>();
int initialDepth = parser.getDepth(); int initialDepth = parser.getDepth();

View File

@ -82,14 +82,7 @@ public class PacketParserUtils {
public static XmlPullParser getParserFor(Reader reader) throws XmlPullParserException, IOException { public static XmlPullParser getParserFor(Reader reader) throws XmlPullParserException, IOException {
XmlPullParser parser = SmackXmlParser.newXmlParser(reader); XmlPullParser parser = SmackXmlParser.newXmlParser(reader);
// Wind the parser forward to the first start tag ParserUtils.forwardToStartElement(parser);
XmlPullParser.Event event = parser.getEventType();
while (event != XmlPullParser.Event.START_ELEMENT) {
if (event == XmlPullParser.Event.END_DOCUMENT) {
throw new IllegalArgumentException("Document contains no start tag");
}
event = parser.next();
}
return parser; return parser;
} }

View File

@ -63,6 +63,17 @@ public class ParserUtils {
assert parser.getEventType() == XmlPullParser.Event.END_ELEMENT; assert parser.getEventType() == XmlPullParser.Event.END_ELEMENT;
} }
public static void forwardToStartElement(XmlPullParser parser) throws XmlPullParserException, IOException {
// Wind the parser forward to the first start tag
XmlPullParser.Event event = parser.getEventType();
while (event != XmlPullParser.Event.START_ELEMENT) {
if (event == XmlPullParser.Event.END_DOCUMENT) {
throw new IllegalArgumentException("Document contains no start tag");
}
event = parser.next();
}
}
public static void forwardToEndTagOfDepth(XmlPullParser parser, int depth) public static void forwardToEndTagOfDepth(XmlPullParser parser, int depth)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
XmlPullParser.Event event = parser.getEventType(); XmlPullParser.Event event = parser.getEventType();