use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.
the class ItemValidationTest method parseComplexItem.
@Test
public void parseComplexItem() throws Exception {
String itemContent = "<entry xmlns='http://www.w3.org/2005/Atom'>" + "<title>Soliloquy</title>" + "<summary>" + "To be, or not to be: that is the question:" + "Whether 'tis nobler in the mind to suffer" + "The slings and arrows of outrageous fortune," + "Or to take arms against a sea of troubles," + "And by opposing end them?" + "</summary>" + "<link rel='alternate' type='text/html' href='http://denmark.lit/2003/12/13/atom03'/>" + "<id>tag:denmark.lit,2003:entry-32397</id>" + "<published>2003-12-13T18:30:02Z</published>" + "<updated>2003-12-13T18:30:02Z</updated>" + "</entry>";
XmlPullParser parser = PacketParserUtils.getParserFor("<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" + "<event xmlns='http://jabber.org/protocol/pubsub#event'>" + "<items node='testNode'>" + "<item id='testid1' >" + itemContent + "</item>" + "</items>" + "</event>" + "</message>");
Stanza message = PacketParserUtils.parseMessage(parser);
XmlElement eventExt = message.getExtension(PubSubNamespace.event.getXmlns());
EventElement event = (EventElement) eventExt;
NamedElement itemExt = ((ItemsExtension) event.getExtensions().get(0)).items.get(0);
assertTrue(itemExt instanceof PayloadItem<?>);
PayloadItem<?> item = (PayloadItem<?>) itemExt;
assertEquals("testid1", item.getId());
assertTrue(item.getPayload() instanceof SimplePayload);
SimplePayload payload = (SimplePayload) item.getPayload();
assertEquals("entry", payload.getElementName());
assertEquals("http://www.w3.org/2005/Atom", payload.getNamespace());
assertXmlSimilar(itemContent, payload.toXML().toString());
}
use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.
the class ItemValidationTest method parseBasicItem.
@Test
public void parseBasicItem() throws Exception {
XmlPullParser parser = PacketParserUtils.getParserFor("<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" + "<event xmlns='http://jabber.org/protocol/pubsub#event'>" + "<items node='testNode'>" + "<item id='testid1' />" + "</items>" + "</event>" + "</message>");
Stanza message = PacketParserUtils.parseMessage(parser);
XmlElement eventExt = message.getExtension(PubSubNamespace.event.getXmlns());
assertTrue(eventExt instanceof EventElement);
EventElement event = (EventElement) eventExt;
assertEquals(EventElementType.items, event.getEventType());
assertEquals(1, event.getExtensions().size());
assertTrue(event.getExtensions().get(0) instanceof ItemsExtension);
assertEquals(1, ((ItemsExtension) event.getExtensions().get(0)).items.size());
NamedElement itemExt = ((ItemsExtension) event.getExtensions().get(0)).items.get(0);
assertTrue(itemExt instanceof Item);
assertEquals("testid1", ((Item) itemExt).getId());
}
use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.
the class OpenPgpContentElement method addCommonXml.
protected void addCommonXml(XmlStringBuilder xml) {
for (Jid toJid : to != null ? to : Collections.<Jid>emptySet()) {
xml.halfOpenElement(ELEM_TO).attribute(ATTR_JID, toJid).closeEmptyElement();
}
ensureTimestampStringSet();
xml.halfOpenElement(ELEM_TIME).attribute(ATTR_STAMP, timestampString).closeEmptyElement();
xml.openElement(ELEM_PAYLOAD);
for (XmlElement element : payload.values()) {
xml.append(element.toXML(getNamespace()));
}
xml.closeElement(ELEM_PAYLOAD);
}
use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.
the class ReferenceProvider method parse.
@Override
public ReferenceElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
Integer begin = ParserUtils.getIntegerAttribute(parser, ReferenceElement.ATTR_BEGIN);
Integer end = ParserUtils.getIntegerAttribute(parser, ReferenceElement.ATTR_END);
String typeString = parser.getAttributeValue(null, ReferenceElement.ATTR_TYPE);
ReferenceElement.Type type = ReferenceElement.Type.valueOf(typeString);
String anchor = parser.getAttributeValue(null, ReferenceElement.ATTR_ANCHOR);
String uriString = parser.getAttributeValue(null, ReferenceElement.ATTR_URI);
URI uri;
try {
uri = uriString != null ? new URI(uriString) : null;
} catch (URISyntaxException e) {
// TODO: Should be SmackParseException and probably be factored into ParserUtils.
throw new IOException(e);
}
XmlElement child = null;
outerloop: while (true) {
XmlPullParser.Event eventType = parser.next();
if (eventType == XmlPullParser.Event.START_ELEMENT) {
String elementName = parser.getName();
String namespace = parser.getNamespace();
ExtensionElementProvider<?> provider = ProviderManager.getExtensionProvider(elementName, namespace);
if (provider != null) {
child = provider.parse(parser);
}
} else if (eventType == XmlPullParser.Event.END_ELEMENT) {
break outerloop;
}
}
return new ReferenceElement(begin, end, type, anchor, uri, child);
}
use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.
the class FasteningElementProvider method parse.
@Override
public FasteningElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
FasteningElement.Builder builder = FasteningElement.builder();
builder.setOriginId(parser.getAttributeValue("", FasteningElement.ATTR_ID));
if (ParserUtils.getBooleanAttribute(parser, FasteningElement.ATTR_CLEAR, false)) {
builder.setClear();
}
if (ParserUtils.getBooleanAttribute(parser, FasteningElement.ATTR_SHELL, false)) {
builder.setShell();
}
outerloop: while (true) {
XmlPullParser.Event tag = parser.next();
switch(tag) {
case START_ELEMENT:
String name = parser.getName();
String namespace = parser.getNamespace();
// Parse external payload
if (MessageFasteningManager.NAMESPACE.equals(namespace) && ExternalElement.ELEMENT.equals(name)) {
ExternalElement external = new ExternalElement(parser.getAttributeValue("", ExternalElement.ATTR_NAME), parser.getAttributeValue("", ExternalElement.ATTR_ELEMENT_NAMESPACE));
builder.addExternalPayload(external);
continue;
}
// Parse wrapped payload
XmlElement wrappedPayload = PacketParserUtils.parseExtensionElement(name, namespace, parser, xmlEnvironment);
builder.addWrappedPayload(wrappedPayload);
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
default:
break;
}
}
return builder.build();
}
Aggregations