/**
   * Read ParticipantSet from given parser (also recursive)
   *
   * @param r The given {@link XMLStreamReader}
   * @param nsMap The used namespacemap
   * @return ParticipantSet
   * @throws MalformedTLGLSyntaxException
   * @throws XMLStreamException
   */
  private static ParticipantSet readInParticipantSet(
      XMLStreamReader r, INamespaceMap<String, String> nsMap)
      throws MalformedTLGLSyntaxException, XMLStreamException {
    ParticipantSet participantSet = null;

    String psName = null;
    String psType = null;
    if (!(r.getEventType() == XMLStreamConstants.END_ELEMENT)) {

      psName = BPEL4ChorReader.getStrAttribute(r, "name", true).toString();
      psType = BPEL4ChorReader.getStrAttribute(r, "type", true).toString();

      participantSet = new ParticipantSet(psName, psType);

      QName scope = BPEL4ChorReader.getQNAttribute(r, "scope", nsMap, false);
      if (scope != null) {
        participantSet.setScope(scope);
      }

      List<QName> forEachs = BPEL4ChorReader.getQNsAttribute(r, "forEach", nsMap, false);
      if ((forEachs != null) && (forEachs.size() > 0)) {
        participantSet.setForEach(forEachs);
      }
    }

    r.nextTag();
    // Read (sub)participantSet(s)
    if (r.hasNext() && r.getLocalName().equals("participantSet")) {
      while (r.hasNext() && r.getLocalName().equals("participantSet")) {
        ParticipantSet participantSetSub = BPEL4ChorReader.readInParticipantSet(r, nsMap);
        participantSet.getParticipantSetList().add(participantSetSub);
      }
      r.nextTag();
    }

    // Read (sub)participant(s)
    if (r.hasNext() && r.getLocalName().equals("participant")) {
      while (r.hasNext() && r.getLocalName().equals("participant")) {

        Participant participant = BPEL4ChorReader.readInParticipant(r, nsMap, true);
        participantSet.getParticipantList().add(participant);
      }
      r.nextTag();
    }
    return participantSet;
  }
  /**
   * Read in the topology file from the given InputStream
   *
   * @param inputStream The {@link InputStream} for reading
   * @return read {@link Topology}
   */
  public static Topology readTopology(InputStream inputStream) {

    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader parser;
    Topology topology = null;
    try {
      parser = factory.createXMLStreamReader(inputStream);
      if ((parser.getEventType() == XMLStreamConstants.START_DOCUMENT)) {
        parser.nextTag();
      }

      // Read Topology Head
      if (parser.getLocalName().equals("topology")
          && !(parser.getEventType() == XMLStreamConstants.END_ELEMENT)) {

        topology = new Topology();
        for (int i = 0; i < parser.getNamespaceCount(); i++) {
          if (parser.getNamespacePrefix(i) != null) {
            topology
                .getNamespaceMap()
                .addNamespace(parser.getNamespaceURI(i), parser.getNamespacePrefix(i));
          }
        }

        topology.setName(BPEL4ChorReader.getStrAttribute(parser, "name", true).toString());

        topology.setTargetNamespace(
            BPEL4ChorReader.getStrAttribute(parser, "targetNamespace", true).toString());

        parser.nextTag();
      }

      // Read participantTypes
      if (parser.getLocalName().equals("participantTypes")) {

        parser.nextTag();

        // Read participantType(s)
        while (parser.hasNext() && parser.getLocalName().equals("participantType")) {
          if (!(parser.getEventType() == XMLStreamConstants.END_ELEMENT)) {

            ParticipantType participantType =
                BPEL4ChorReader.readInParticipantType(parser, topology.getNamespaceMap());
            topology.getParticipantTypes().add(participantType);
          }
          parser.nextTag();
        }
        parser.nextTag();
      }

      // Read participants
      if (parser.getLocalName().equals("participants")) {

        parser.nextTag();

        // Read participant(s)
        if (parser.hasNext() && parser.getLocalName().equals("participant")) {
          while (parser.hasNext() && parser.getLocalName().equals("participant")) {
            Participant participant =
                BPEL4ChorReader.readInParticipant(parser, topology.getNamespaceMap(), false);
            topology.getParticipants().add(participant);
          }
          if (parser.getLocalName() != "participantSet") {
            parser.nextTag();
          }
        }
        // Read participantSet(s)
        if (parser.hasNext() && parser.getLocalName().equals("participantSet")) {
          while (parser.hasNext() && parser.getLocalName().equals("participantSet")) {
            ParticipantSet participantSet =
                BPEL4ChorReader.readInParticipantSet(parser, topology.getNamespaceMap());
            topology.getParticipantSets().add(participantSet);
          }
          parser.nextTag();
        }
      }

      // Read messageLinks
      if (parser.getLocalName().equals("messageLinks")) {

        parser.nextTag();

        // Read messageLink(s)
        while (parser.hasNext() && parser.getLocalName().equals("messageLink")) {
          if (!(parser.getEventType() == XMLStreamConstants.END_ELEMENT)) {

            MessageLink messageLink =
                BPEL4ChorReader.readInMessageLinkTL(parser, topology.getNamespaceMap());

            topology.getMessageLinks().add(messageLink);
          }
          parser.nextTag();
        }
        parser.nextTag();
      }

      int event = parser.next();
      if (event == XMLStreamConstants.END_DOCUMENT) {
        parser.close();
      }
    } catch (XMLStreamException e) {
      e.printStackTrace();
    } catch (MalformedTLGLSyntaxException e) {
      e.printStackTrace();
    }

    return topology;
  }