/**
   * 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;
  }