/** * Parses a ConferenceMedium extension sub-packet and creates a {@link * ConferenceMediumPacketExtension} instance. At the beginning of the method call, the xml parser * will be positioned on the opening element of the packet extension. As required by the smack * API, at the end of the method call, the parser will be positioned on the closing element of the * packet extension. * * @param parser an XML parser positioned at the opening <tt>ConferenceMedium</tt> element. * @return a new {@link ConferenceMediumPacketExtension} instance. * @throws java.lang.Exception if an error occurs parsing the XML. */ public PacketExtension parseExtension(XmlPullParser parser) throws Exception { boolean done = false; int eventType; String elementName = null; String label = parser.getAttributeValue("", ConferenceMediumPacketExtension.LABEL_ATTR_NAME); if (label == null) { throw new Exception("Coin medium element must contain entity attribute"); } ConferenceMediumPacketExtension ext = new ConferenceMediumPacketExtension("entry", label); while (!done) { eventType = parser.next(); elementName = parser.getName(); if (eventType == XmlPullParser.START_TAG) { if (elementName.equals(MediaPacketExtension.ELEMENT_DISPLAY_TEXT)) { ext.setDisplayText(CoinIQProvider.parseText(parser)); } else if (elementName.equals(MediaPacketExtension.ELEMENT_STATUS)) { ext.setStatus(CoinIQProvider.parseText(parser)); } else if (elementName.equals(MediaPacketExtension.ELEMENT_TYPE)) { ext.setType(CoinIQProvider.parseText(parser)); } } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals(ConferenceMediumPacketExtension.ELEMENT_NAME)) { done = true; } } } return ext; }
/** * Creates a <tt>CarbonPacketExtension</tt> by parsing an XML document. * * @param parser the parser to use. * @return the created <tt>CarbonPacketExtension</tt>. * @throws Exception */ @Override public PacketExtension parseExtension(XmlPullParser parser) throws Exception { CarbonPacketExtension packetExtension = new CarbonPacketExtension(elementName); // now parse the sub elements boolean done = false; String elementName; ForwardedPacketExtension extension = null; while (!done) { switch (parser.next()) { case XmlPullParser.START_TAG: { elementName = parser.getName(); if (ForwardedPacketExtension.ELEMENT_NAME.equals(elementName)) { extension = (ForwardedPacketExtension) super.parseExtension(parser); if (extension != null) { packetExtension.addChildExtension(extension); } } break; } case XmlPullParser.END_TAG: { elementName = parser.getName(); if (this.elementName.equals(elementName)) { done = true; } break; } } } return packetExtension; }
/** * Parses a User extension sub-packet and creates a {@link UserPacketExtension} instance. At the * beginning of the method call, the xml parser will be positioned on the opening element of the * packet extension. As required by the smack API, at the end of the method call, the parser will * be positioned on the closing element of the packet extension. * * @param parser an XML parser positioned at the opening <tt>User</tt> element. * @return a new {@link UserPacketExtension} instance. * @throws java.lang.Exception if an error occurs parsing the XML. */ public UserPacketExtension parseExtension(XmlPullParser parser) throws Exception { boolean done = false; int eventType; String elementName = null; String entity = parser.getAttributeValue("", UserPacketExtension.ENTITY_ATTR_NAME); StateType state = StateType.full; String stateStr = parser.getAttributeValue("", UserPacketExtension.STATE_ATTR_NAME); if (stateStr != null) { state = StateType.parseString(stateStr); } if (entity == null) { throw new Exception("Coin user element must contain entity attribute"); } UserPacketExtension ext = new UserPacketExtension(entity); ext.setAttribute(UserPacketExtension.STATE_ATTR_NAME, state); while (!done) { eventType = parser.next(); elementName = parser.getName(); if (eventType == XmlPullParser.START_TAG) { if (elementName.equals(UserPacketExtension.ELEMENT_DISPLAY_TEXT)) { ext.setDisplayText(CoinIQProvider.parseText(parser)); } else if (elementName.equals(EndpointPacketExtension.ELEMENT_NAME)) { PacketExtensionProvider provider = new EndpointProvider(); PacketExtension childExtension = provider.parseExtension(parser); ext.addChildExtension(childExtension); } } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals(UserPacketExtension.ELEMENT_NAME)) { done = true; } } } return ext; }
/** * Parses the given <tt>parser</tt> in order to create a <tt>FileElement</tt> from it. * * @param parser the parser to parse * @see IQProvider#parseIQ(XmlPullParser) */ public IQ parseIQ(final XmlPullParser parser) throws Exception { boolean done = false; // si String id = parser.getAttributeValue("", "id"); String mimeType = parser.getAttributeValue("", "mime-type"); StreamInitiation initiation = new StreamInitiation(); // file String name = null; String size = null; String hash = null; String date = null; String desc = null; ThumbnailElement thumbnail = null; boolean isRanged = false; // feature DataForm form = null; DataFormProvider dataFormProvider = new DataFormProvider(); int eventType; String elementName; String namespace; while (!done) { eventType = parser.next(); elementName = parser.getName(); namespace = parser.getNamespace(); if (eventType == XmlPullParser.START_TAG) { if (elementName.equals("file")) { name = parser.getAttributeValue("", "name"); size = parser.getAttributeValue("", "size"); hash = parser.getAttributeValue("", "hash"); date = parser.getAttributeValue("", "date"); } else if (elementName.equals("desc")) { desc = parser.nextText(); } else if (elementName.equals("range")) { isRanged = true; } else if (elementName.equals("x") && namespace.equals("jabber:x:data")) { form = (DataForm) dataFormProvider.parseExtension(parser); } else if (elementName.equals("thumbnail")) { thumbnail = new ThumbnailElement(parser.getText()); } } else if (eventType == XmlPullParser.END_TAG) { if (elementName.equals("si")) { done = true; } // The name-attribute is required per XEP-0096, so ignore the // IQ if the name is not set to avoid exceptions. Particularly, // the SI response of Empathy contains an invalid, empty // file-tag. else if (elementName.equals("file") && name != null) { long fileSize = 0; if (size != null && size.trim().length() != 0) { try { fileSize = Long.parseLong(size); } catch (NumberFormatException e) { logger.warn( "Received an invalid file size," + " continuing with fileSize set to 0", e); } } FileElement file = new FileElement(name, fileSize); file.setHash(hash); if (date != null) { // try all known date formats boolean found = false; if (date.matches(".*?T\\d+:\\d+:\\d+(\\.\\d+)?(\\+|-)\\d+:\\d+")) { int timeZoneColon = date.lastIndexOf(":"); date = date.substring(0, timeZoneColon) + date.substring(timeZoneColon + 1, date.length()); } for (DateFormat fmt : DATE_FORMATS) { try { file.setDate(fmt.parse(date)); found = true; break; } catch (ParseException ex) { } } if (!found) { logger.warn("Unknown dateformat on incoming file transfer: " + date); } } if (thumbnail != null) file.setThumbnailElement(thumbnail); file.setDesc(desc); file.setRanged(isRanged); initiation.setFile(file); } } } initiation.setSesssionID(id); initiation.setMimeType(mimeType); initiation.setFeatureNegotiationForm(form); return initiation; }
/** * Retrieve DiscoverInfo for a specific node. * * @param caps the <tt>Caps</tt> i.e. the node, the hash and the ver * @return The corresponding DiscoverInfo or null if none is known. */ public static DiscoverInfo getDiscoverInfoByCaps(Caps caps) { synchronized (caps2discoverInfo) { DiscoverInfo discoverInfo = caps2discoverInfo.get(caps); /* * If we don't have the discoverInfo in the runtime cache yet, we * may have it remembered in a previous application instance. */ if (discoverInfo == null) { ConfigurationService configurationService = getConfigService(); String capsPropertyName = getCapsPropertyName(caps); String xml = configurationService.getString(capsPropertyName); if ((xml != null) && (xml.length() != 0)) { IQProvider discoverInfoProvider = (IQProvider) ProviderManager.getInstance() .getIQProvider("query", "http://jabber.org/protocol/disco#info"); if (discoverInfoProvider != null) { XmlPullParser parser = new MXParser(); try { parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new StringReader(xml)); // Start the parser. parser.next(); } catch (XmlPullParserException xppex) { parser = null; } catch (IOException ioex) { parser = null; } if (parser != null) { try { discoverInfo = (DiscoverInfo) discoverInfoProvider.parseIQ(parser); } catch (Exception ex) { } if (discoverInfo != null) { if (caps.isValid(discoverInfo)) caps2discoverInfo.put(caps, discoverInfo); else { logger.error( "Invalid DiscoverInfo for " + caps.getNodeVer() + ": " + discoverInfo); /* * The discoverInfo doesn't seem valid * according to the caps which means that we * must have stored invalid information. * Delete the invalid information in order * to not try to validate it again. */ configurationService.removeProperty(capsPropertyName); } } } } } } return discoverInfo; } }