/**
   * Read in the grounding file from the given InputStream
   *
   * @param inputStream The {@link InputStream} for reading
   * @param topology The {@link Topology} belonging to the Grounding
   * @return The read {@link Grounding}
   */
  public static Grounding readGrounding(InputStream inputStream, Topology topology) {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader parser;
    Grounding grounding = null;

    try {
      parser = factory.createXMLStreamReader(inputStream);

      if ((parser.getEventType() == XMLStreamConstants.START_DOCUMENT)) {
        parser.nextTag();
      }

      if (parser.getLocalName().equals("grounding")
          && !(parser.getEventType() == XMLStreamConstants.END_ELEMENT)) {

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

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

        parser.nextTag();
      }
      if (parser.getLocalName().equals("messageLinks")) {

        parser.nextTag();
        while (parser.hasNext() && parser.getLocalName().equals("messageLink")) {
          if (!(parser.getEventType() == XMLStreamConstants.END_ELEMENT)) {

            grounding
                .getMessageLinks()
                .add(
                    BPEL4ChorReader.readInMessageLinkGD(
                        parser, grounding.getNamespaceMap(), topology));
          }
          parser.nextTag();
        }
        parser.nextTag();
      }

      if (parser.getLocalName().equals("participantRefs")) {
        parser.nextTag();
        while (parser.hasNext() && parser.getLocalName().equals("participantRef")) {
          if (!(parser.getEventType() == XMLStreamConstants.END_ELEMENT)) {

            grounding
                .getParticipantRefs()
                .add(BPEL4ChorReader.readInParticipantRef(parser, grounding.getNamespaceMap()));
          }
          parser.nextTag();
        }
        parser.nextTag();
      }

      if (parser.getLocalName().equals("properties")) {
        parser.nextTag();
        while (parser.hasNext() && parser.getLocalName().equals("property")) {

          if (!(parser.getEventType() == XMLStreamConstants.END_ELEMENT)) {

            grounding
                .getProperties()
                .add(BPEL4ChorReader.readInProperty(parser, grounding.getNamespaceMap()));
          }
          parser.nextTag();
        }
        parser.nextTag();
      }

    } catch (XMLStreamException e) {
      e.printStackTrace();

    } catch (MalformedTLGLSyntaxException e) {
      e.printStackTrace();
    }

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