private CcsClient() { // Add FCMPacketExtension ProviderManager.addExtensionProvider(Util.FCM_ELEMENT_NAME, Util.FCM_NAMESPACE, new ExtensionElementProvider<FcmPacketExtension>() { @Override public FcmPacketExtension parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException { final String json = parser.nextText(); return new FcmPacketExtension(json); } }); }
/** * Parses an extension element. * * @param elementName the XML element name of the extension element. * @param namespace the XML namespace of the stanza(/packet) extension. * @param parser the XML parser, positioned at the starting element of the extension. * @return an extension element. */ public static ExtensionElement parseExtensionElement(String elementName, String namespace, XmlPullParser parser) throws XmlPullParserException, IOException, SmackException { ParserUtils.assertAtStartTag(parser); // See if a provider is registered to handle the extension. ExtensionElementProvider<ExtensionElement> provider = ProviderManager.getExtensionProvider(elementName, namespace); if (provider != null) { return provider.parse(parser); } final int initialDepth = parser.getDepth(); // No providers registered, so use a default extension. DefaultExtensionElement extension = new DefaultExtensionElement(elementName, namespace); outerloop: while (true) { int eventType = parser.next(); switch (eventType) { case XmlPullParser.START_TAG: String name = parser.getName(); // If an empty element, set the value with the empty string. if (parser.isEmptyElementTag()) { extension.setValue(name,""); } // Otherwise, get the the element text. else { eventType = parser.next(); if (eventType == XmlPullParser.TEXT) { String value = parser.getText(); extension.setValue(name, value); } } break; case XmlPullParser.END_TAG: if (parser.getDepth() == initialDepth) { break outerloop; } } } return extension; }
@Override public Item parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException { String id = parser.getAttributeValue(null, "id"); String node = parser.getAttributeValue(null, "node"); int tag = parser.next(); if (tag == XmlPullParser.END_TAG) { return new Item(id, node); } else { String payloadElemName = parser.getName(); String payloadNS = parser.getNamespace(); final ExtensionElementProvider<ExtensionElement> extensionProvider = ProviderManager.getExtensionProvider(payloadElemName, payloadNS); if (extensionProvider == null) { CharSequence payloadText = PacketParserUtils.parseElement(parser, true); return new PayloadItem<SimplePayload>(id, node, new SimplePayload(payloadElemName, payloadNS, payloadText)); } else { return new PayloadItem<ExtensionElement>(id, node, extensionProvider.parse(parser)); } } }
/** * Parses a PEPEvent stanza(/packet) and extracts a PEPItem from it. * (There is only one per <event>.) * * @param parser the XML parser, positioned at the starting element of the extension. * @return a PacketExtension. * @throws IOException * @throws XmlPullParserException * @throws SmackException */ @Override public ExtensionElement parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException { ExtensionElement pepItem = null; boolean done = false; while (!done) { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG) { if (parser.getName().equals("event")) { } else if (parser.getName().equals("items")) { // Figure out the node for this event. String node = parser.getAttributeValue("", "node"); // Get the parser for this kind of node, and if found then parse the node. ExtensionElementProvider<?> nodeParser = nodeParsers.get(node); if (nodeParser != null) { pepItem = nodeParser.parse(parser); } } } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals("event")) { done = true; } } } return pepItem; }
protected final void parseFeatures(XmlPullParser parser) throws XmlPullParserException, IOException, SmackException { streamFeatures.clear(); final int initialDepth = parser.getDepth(); while (true) { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG && parser.getDepth() == initialDepth + 1) { ExtensionElement streamFeature = null; String name = parser.getName(); String namespace = parser.getNamespace(); switch (name) { case StartTls.ELEMENT: streamFeature = PacketParserUtils.parseStartTlsFeature(parser); break; case Mechanisms.ELEMENT: streamFeature = new Mechanisms(PacketParserUtils.parseMechanisms(parser)); break; case Bind.ELEMENT: streamFeature = Bind.Feature.INSTANCE; break; case Session.ELEMENT: streamFeature = PacketParserUtils.parseSessionFeature(parser); break; case Compress.Feature.ELEMENT: streamFeature = PacketParserUtils.parseCompressionFeature(parser); break; default: ExtensionElementProvider<ExtensionElement> provider = ProviderManager.getStreamFeatureProvider(name, namespace); if (provider != null) { streamFeature = provider.parse(parser); } break; } if (streamFeature != null) { // 将特性放入列表中 addStreamFeature(streamFeature); } } else if (eventType == XmlPullParser.END_TAG && parser.getDepth() == initialDepth) { break; } } if (hasFeature(Mechanisms.ELEMENT, Mechanisms.NAMESPACE)) { // Only proceed with SASL auth if TLS is disabled or if the server doesn't announce it if (!hasFeature(StartTls.ELEMENT, StartTls.NAMESPACE) || config.getSecurityMode() == SecurityMode.disabled) { // connecting算事完成了 saslFeatureReceived.reportSuccess(); } } // If the server reported the bind feature then we are that that we did SASL and maybe // STARTTLS. We can then report that the last 'stream:features' have been parsed if (hasFeature(Bind.ELEMENT, Bind.NAMESPACE)) { if (!hasFeature(Compress.Feature.ELEMENT, Compress.NAMESPACE) || !config.isCompressionEnabled()) { // This was was last features from the server is either it did not contain // compression or if we disabled it lastFeaturesReceived.reportSuccess(); } } afterFeaturesReceived(); }
public static void registerPEPParserExtension(String node, ExtensionElementProvider<?> pepItemParser) { nodeParsers.put(node, pepItemParser); }
@Override public NewJingleIQ parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { NewJingleIQ jingleIQ = new NewJingleIQ(); NewJingleAction action = NewJingleAction.parseString(parser.getAttributeValue("", "action")); String initiator = parser.getAttributeValue("", "initiator"); String responder = parser.getAttributeValue("", "responder"); String sid = parser.getAttributeValue("", "sid"); jingleIQ.setAction(action); jingleIQ.setInitiator(initiator); jingleIQ.setResponder(responder); jingleIQ.setSID(sid); boolean done = false; try { while (!done) { int eventType = parser.next(); String elementName = parser.getName(); String namespace = parser.getNamespace(); if (eventType == XmlPullParser.START_TAG) { ExtensionElementProvider provider = ProviderManager.getExtensionProvider(elementName, namespace); if (provider != null) { Element child = provider.parse(parser); if (child instanceof NewContentPacketExtension) { jingleIQ.addContent((NewContentPacketExtension)child); } else { throw new IOException("JingleProvider doesn't handle child element " + elementName + " in namespace " + namespace); } } else { throw new IOException("JingleProvider: no provider found for element " + elementName + " in namespace " + namespace); } } if (eventType == XmlPullParser.END_TAG && parser.getName().equals("jingle")) { done = true; } } } catch (Exception e) { } return jingleIQ; }