fipa_acl  1.4
Public Member Functions | List of all members
fipa::acl::XMLEnvelopeParser Class Reference

#include <xml_envelope_parser.h>

Inherits fipa::acl::EnvelopeParserImplementation.

Public Member Functions

bool parseData (const std::string &storage, ACLEnvelope &envelope)
 
- Public Member Functions inherited from fipa::acl::EnvelopeParserImplementation
virtual ~EnvelopeParserImplementation ()
 

Detailed Description

Definition at line 13 of file xml_envelope_parser.h.

Member Function Documentation

◆ parseData()

bool fipa::acl::XMLEnvelopeParser::parseData ( const std::string &  storage,
ACLEnvelope envelope 
)
virtual

Reimplemented from fipa::acl::EnvelopeParserImplementation.

Definition at line 11 of file xml_envelope_parser.cpp.

12 {
13  TiXmlDocument doc;
14  LOG_INFO_S << "XMLEnvelopeParser: starting to parse: " << storage;
15 
16  std::string envelopeXML;
17  std::string payloadData;
18 
19  // check whether we can split the document into envelope and content
20  std::string envelopeEndMarker = "</envelope>";
21  size_t pos = storage.find(envelopeEndMarker);
22  if(pos == std::string::npos)
23  {
24  LOG_WARN_S << "XMLEnvelopeParser: this is not an XML envelope. Could not find </envelope>";
25  return false;
26  } else {
27  envelopeXML = storage.substr(0, pos + envelopeEndMarker.size());
28 
29  size_t payloadPosition = pos + envelopeEndMarker.size();
30  if(payloadPosition < storage.length());
31  {
32  envelope.setPayload(storage.substr(payloadPosition));
33  }
34  }
35 
36  // Load the string into XML doc
37  const char* parseResult = doc.Parse(envelopeXML.c_str());
38  // A non-null parseResult usually indicates an error, but we seem to get that every time.
39  if(parseResult != NULL)
40  {
41  // non-null means some error
42  LOG_WARN_S << "Parsing envelope XML failed for (probably the payload): " << parseResult;
43  }
44  LOG_INFO_S << "XMLEnvelopeParser: basic XML parsing complete.";
45 
46  // The main node (envelope)
47  const TiXmlElement* envelopeElem = doc.FirstChildElement();
48 
49  if(envelopeElem == NULL)
50  {
51  LOG_WARN_S << "Parsing error: XML main node not found.";
52  return false;
53  }
54 
55  if(envelopeElem->ValueStr() != "envelope")
56  {
57  LOG_WARN_S << "Parsing error: XML main node not named 'fipa-message' but " << envelopeElem->ValueStr();
58  return false;
59  }
60 
61  // Parse all child elements
62  const TiXmlElement* pChild = envelopeElem->FirstChildElement();
63 
64  if(pChild == NULL)
65  {
66  LOG_WARN_S << "Parsing error: XML first params node not found.";
67  return false;
68  }
69 
70  // We assume the params have raising indices, starting with one.
71  int paramsIndex = 1;
72  // The first params is the Base envelope
73  try
74  {
75  envelope.setBaseEnvelope(parseParameters(pChild, paramsIndex++));
76  }
77  catch(std::exception& e)
78  {
79  LOG_WARN_S << "Parsing error base envelope: " << e.what();
80  return false;
81  }
82 
83  // All remaining params are extra envelopes
84  for ( pChild = pChild->NextSiblingElement(); pChild != 0; pChild = pChild->NextSiblingElement())
85  {
86  try
87  {
88  envelope.addExtraEnvelope(parseParameters(pChild, paramsIndex++));
89  }
90  catch(std::exception& e)
91  {
92  LOG_WARN_S << "Parsing error extra envelope: " << e.what();
93  return false;
94  }
95  }
96 
97  return true;
98 }

The documentation for this class was generated from the following files: