Exemple #1
0
  /**
   * Parses a PEPEvent 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 Exception if a parsing error occurs.
   */
  public PacketExtension parseExtension(XmlPullParser parser) throws Exception {

    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.
          PacketExtensionProvider nodeParser = nodeParsers.get(node);
          if (nodeParser != null) {
            pepItem = nodeParser.parseExtension(parser);
          }
        }
      } else if (eventType == XmlPullParser.END_TAG) {
        if (parser.getName().equals("event")) {
          done = true;
        }
      }
    }

    return pepItem;
  }
Exemple #2
0
  /**
   * 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;
  }