@Override public XHTMLExtension parse(XmlPullParser parser, int initialDepth) throws IOException, XmlPullParserException { XHTMLExtension xhtmlExtension = new XHTMLExtension(); while (true) { int eventType = parser.getEventType(); String name = parser.getName(); if (eventType == XmlPullParser.START_TAG) { if (name.equals(Message.BODY)) { xhtmlExtension.addBody(PacketParserUtils.parseElement(parser)); } } else if (eventType == XmlPullParser.END_TAG) { if (parser.getDepth() == initialDepth) { return xhtmlExtension; } } parser.next(); } }
/** * Returns an Iterator for the XHTML bodies in the message. Returns null if * the message does not contain an XHTML extension. * * @param message an XHTML message * @return an Iterator for the bodies in the message or null if none. */ public static List<CharSequence> getBodies(Message message) { XHTMLExtension xhtmlExtension = XHTMLExtension.from(message); if (xhtmlExtension != null) return xhtmlExtension.getBodies(); else return null; }
/** * Enables or disables the XHTML support on a given connection.<p> * * Before starting to send XHTML messages to a user, check that the user can handle XHTML * messages. Enable the XHTML support to indicate that this client handles XHTML messages. * * @param connection the connection where the service will be enabled or disabled * @param enabled indicates if the service will be enabled or disabled */ public synchronized static void setServiceEnabled(XMPPConnection connection, boolean enabled) { if (isServiceEnabled(connection) == enabled) return; if (enabled) { ServiceDiscoveryManager.getInstanceFor(connection).addFeature(XHTMLExtension.NAMESPACE); } else { ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(XHTMLExtension.NAMESPACE); } }
@Test public void parsesWell() throws IOException, XmlPullParserException { XmlPullParser parser = PacketParserUtils.newXmppParser(); parser.setInput(getClass().getResourceAsStream(XHTML_EXTENSION_SAMPLE_RESOURCE_NAME), "UTF-8"); parser.next(); XHTMLExtensionProvider provider = new XHTMLExtensionProvider(); ExtensionElement extension = provider.parse(parser, parser.getDepth()); assertThat(extension, instanceOf(XHTMLExtension.class)); XHTMLExtension attachmentsInfo = (XHTMLExtension) extension; assertThat(sampleXhtml(), equalsCharSequence(attachmentsInfo.getBodies().get(0))); }
/** * Returns true if the message contains an XHTML extension. * * @param message the message to check if contains an XHTML extentsion or not * @return a boolean indicating whether the message is an XHTML message */ public static boolean isXHTMLMessage(Message message) { return message.getExtension(XHTMLExtension.ELEMENT, XHTMLExtension.NAMESPACE) != null; }
/** * Returns true if the XHTML support is enabled for the given connection. * * @param connection the connection to look for XHTML support * @return a boolean indicating if the XHTML support is enabled for the given connection */ public static boolean isServiceEnabled(XMPPConnection connection) { return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(XHTMLExtension.NAMESPACE); }