/**
   * 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 Participant from given {@link XMLStreamReader}
   *
   * @param r The {@link XMLStreamReader}
   * @param nsMap the Namespace map with all needed data
   * @param contained Flag indicating if participant is contained in {@link ParticipantSet}
   * @return {@link Participant}
   * @throws MalformedTLGLSyntaxException
   * @throws XMLStreamException
   */
  private static Participant readInParticipant(
      XMLStreamReader r, INamespaceMap<String, String> nsMap, boolean contained)
      throws MalformedTLGLSyntaxException, XMLStreamException {
    Participant participant = new Participant();
    if (!(r.getEventType() == XMLStreamConstants.END_ELEMENT)) {
      participant.setName(BPEL4ChorReader.getStrAttribute(r, "name", true).toString());

      participant.setType(BPEL4ChorReader.getStrAttribute(r, "type", true).toString());
      if (!contained) {

        List<String> selects = BPEL4ChorReader.getStrsAttribute(r, "selects", false);
        if (selects != null) {
          participant.setSelects(selects);
        }
      } else {
        // Our Participant is contained in a Participant Set
        List<QName> forEachs = BPEL4ChorReader.getQNsAttribute(r, "forEach", nsMap, false);
        if (forEachs != null) {
          participant.setForEach(forEachs);
        }

        ContainmentValue value =
            (ContainmentValue) BPEL4ChorReader.getStrAttribute(r, "containment", false);
        if (value != null) {
          participant.setContainment(value);
        }
      }

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