mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-12-22 12:37:58 +01:00
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:
parent
e35175a3d5
commit
7796b367cc
3 changed files with 14 additions and 8 deletions
|
@ -27,6 +27,7 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
|
||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||
import org.jivesoftware.smack.util.ParserUtils;
|
||||
import org.jivesoftware.smack.xml.XmlPullParser;
|
||||
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
|
||||
*/
|
||||
public static List<URI> parseXrdLinkReferencesFor(XmlPullParser parser, String relation) throws IOException, XmlPullParserException, URISyntaxException {
|
||||
ParserUtils.forwardToStartElement(parser);
|
||||
List<URI> uriList = new ArrayList<>();
|
||||
int initialDepth = parser.getDepth();
|
||||
|
||||
|
|
|
@ -82,14 +82,7 @@ public class PacketParserUtils {
|
|||
|
||||
public static XmlPullParser getParserFor(Reader reader) throws XmlPullParserException, IOException {
|
||||
XmlPullParser parser = SmackXmlParser.newXmlParser(reader);
|
||||
// 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();
|
||||
}
|
||||
ParserUtils.forwardToStartElement(parser);
|
||||
return parser;
|
||||
}
|
||||
|
||||
|
|
|
@ -63,6 +63,17 @@ public class ParserUtils {
|
|||
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)
|
||||
throws XmlPullParserException, IOException {
|
||||
XmlPullParser.Event event = parser.getEventType();
|
||||
|
|
Loading…
Reference in a new issue