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

#include <xml_parser.h>

Static Public Member Functions

static const ReceivedObject parseReceivedObject (const TiXmlElement *receivedObjectElem)
 
static const base::Time parseDate (const TiXmlElement *dateElem)
 
static const base::Time strToDate (const std::string &dateStr)
 
static const std::string parseURL (const TiXmlElement *urlElem)
 
static const std::string parseName (const TiXmlElement *nameElem)
 
static const AgentID parseAgentID (const TiXmlElement *aidElem)
 
static const AgentIDList parseAgentIDSequence (const TiXmlElement *aidlElem)
 
static const UserdefParam parseUserdefinedParameter (const TiXmlElement *paramElem)
 
static const char * extractContentOrAttribute (const TiXmlElement *elem, const char *identifier="href")
 

Detailed Description

A set of static methods to parse certain XML elemenets containing FIPA compliant data. All methods throw an exception of the content is not FIPA compliant.

Definition at line 18 of file xml_parser.h.

Member Function Documentation

◆ extractContentOrAttribute()

const char * fipa::acl::XMLParser::extractContentOrAttribute ( const TiXmlElement *  elem,
const char *  identifier = "href" 
)
static

For all message parameters FIPA allows to set the content directly or to point to an URL with a href attribute. The content of either will be returned or NULL if none is set.

Definition at line 219 of file xml_parser.cpp.

220 {
221  const char* text = elem->GetText();
222  if(text != NULL)
223  {
224  return text;
225  }
226  return elem->Attribute(identifier);
227 }

◆ parseAgentID()

const AgentID fipa::acl::XMLParser::parseAgentID ( const TiXmlElement *  aidElem)
static

Definition at line 8 of file xml_parser.cpp.

9 {
10  if(aidElem->ValueStr() != "agent-identifier")
11  {
12  throw std::runtime_error("Parsing error: node not named 'agent-identifier' but " + aidElem->ValueStr());
13  }
14 
15  AgentID aid;
16 
17  const TiXmlElement * pChild = aidElem->FirstChildElement();
18  // The first child is the name
19  aid.setName(parseName(pChild));
20 
21  for ( pChild = pChild->NextSiblingElement(); pChild != 0; pChild = pChild->NextSiblingElement())
22  {
23  // The remaining childs can be resolvers, addresses, or user-defined parameters
24  const std::string& text = pChild->ValueStr();
25  if(text == "addresses")
26  {
27  // Each child element is an URL
28  const TiXmlElement * pInnerChild;
29  for ( pInnerChild = pChild->FirstChildElement(); pInnerChild != 0; pInnerChild = pInnerChild->NextSiblingElement())
30  {
31  aid.addAddress(parseURL(pInnerChild));
32  }
33  }
34  else if(text == "resolvers")
35  {
36  // It's a list of agent ids
37  aid.setResolvers(parseAgentIDSequence(pChild));
38  }
39  else if(text.substr(0, 2) == "X-")
40  {
41  aid.addUserdefParam(parseUserdefinedParameter(pChild));
42  }
43  }
44  return aid;
45 }
static const std::string parseURL(const TiXmlElement *urlElem)
Definition: xml_parser.cpp:172
static const AgentIDList parseAgentIDSequence(const TiXmlElement *aidlElem)
Definition: xml_parser.cpp:47
static const UserdefParam parseUserdefinedParameter(const TiXmlElement *paramElem)
Definition: xml_parser.cpp:188
static const std::string parseName(const TiXmlElement *nameElem)
Definition: xml_parser.cpp:69

◆ parseAgentIDSequence()

const AgentIDList fipa::acl::XMLParser::parseAgentIDSequence ( const TiXmlElement *  aidlElem)
static

Definition at line 47 of file xml_parser.cpp.

48 {
49  AgentIDList list;
50  // All children are AgentIDs
51  const TiXmlElement * pChild;
52  for ( pChild = aidlElem->FirstChildElement(); pChild != 0; pChild = pChild->NextSiblingElement())
53  {
54  list.push_back(parseAgentID(pChild));
55  }
56  return list;
57 }
std::vector< AgentID > AgentIDList
Definition: agent_id.h:20
static const AgentID parseAgentID(const TiXmlElement *aidElem)
Definition: xml_parser.cpp:8

◆ parseDate()

const base::Time fipa::acl::XMLParser::parseDate ( const TiXmlElement *  dateElem)
static

Definition at line 59 of file xml_parser.cpp.

60 {
61  const char* date = dateElem->GetText();
62  if(date == NULL)
63  {
64  throw std::runtime_error("ill-formed date element");
65  }
66  return strToDate(std::string(date));
67 }
static const base::Time strToDate(const std::string &dateStr)
Definition: xml_parser.cpp:202

◆ parseName()

const std::string fipa::acl::XMLParser::parseName ( const TiXmlElement *  nameElem)
static

Definition at line 69 of file xml_parser.cpp.

70 {
71  if(nameElem->ValueStr() != "name")
72  {
73  throw std::runtime_error("Parsing error: node not named 'name' but " + nameElem->ValueStr());
74  }
75 
76  // Name can be the value or in an "id" or "refid" attribute
77  const char* p = nameElem->GetText();
78  if(p != NULL)
79  {
80  return std::string(p);
81  }
82  p = nameElem->Attribute("id");
83  if(p != NULL)
84  {
85  return std::string(p);
86  }
87  p = nameElem->Attribute("refid");
88  if(p != NULL)
89  {
90  return std::string(p);
91  }
92  throw std::runtime_error("ill-formed name element");
93 }

◆ parseReceivedObject()

const ReceivedObject fipa::acl::XMLParser::parseReceivedObject ( const TiXmlElement *  receivedObjectElem)
static

Definition at line 95 of file xml_parser.cpp.

96 {
97  ReceivedObject ro;
99 
100  // Each element here can also have an "value" attribute
101 
102  const TiXmlElement * pChild = receivedObjectElem->FirstChildElement();
103  // The first child is received-by
104  const std::string* byValue = pChild->Attribute(std::string("value"));
105  if(byValue != NULL)
106  {
107  ro.setBy(*byValue);
108  }
109  else
110  {
111  // Then it must be url child
112  const std::string byUrl = parseURL(pChild->FirstChildElement());
113  ro.setBy(byUrl);
114  }
115 
116  for ( pChild = pChild->NextSiblingElement(); pChild != 0; pChild = pChild->NextSiblingElement())
117  {
118  // The remaining childs can be received-from, received-date, received-id, received-via, and user-defined
119  const std::string& text = pChild->ValueStr();
120  if(text == "received-from")
121  {
122  const std::string* fromValue = pChild->Attribute(std::string("value"));
123  if(fromValue != NULL)
124  {
125  ro.setFrom(*fromValue);
126  }
127  else
128  {
129  // Then it must be url child
130  const std::string fromUrl = parseURL(pChild->FirstChildElement());
131  ro.setFrom(fromUrl);
132  }
133  }
134  else if(text == "received-date")
135  {
136  const std::string* date = pChild->Attribute(std::string("value"));
137  if(date == NULL)
138  {
139  throw std::runtime_error("ill-formed received-date element");
140  }
141  ro.setDate(strToDate(*date));
142  }
143  else if(text == "received-id")
144  {
145  const std::string* id = pChild->Attribute(std::string("value"));
146  if(id == NULL)
147  {
148  throw std::runtime_error("ill-formed received-id element");
149  }
150  ro.setId(*id);
151  }
152  else if(text == "received-via")
153  {
154  const std::string* via = pChild->Attribute(std::string("value"));
155  if(via == NULL)
156  {
157  throw std::runtime_error("ill-formed received-via element");
158  }
159  ro.setId(*via);
160  }
161  else if(text == "user-defined")
162  {
163  params.push_back(parseUserdefinedParameter(pChild));
164  }
165  }
166  // Since there is no "addUserdefParam" in ReceivedObject
167  ro.setUserdefinedParameters(params);
168 
169  return ro;
170 }
static const std::string parseURL(const TiXmlElement *urlElem)
Definition: xml_parser.cpp:172
std::vector< UserdefParam > UserdefinedParameterList
Definition: userdef_param.h:94
static const base::Time strToDate(const std::string &dateStr)
Definition: xml_parser.cpp:202
static const UserdefParam parseUserdefinedParameter(const TiXmlElement *paramElem)
Definition: xml_parser.cpp:188

◆ parseURL()

const std::string fipa::acl::XMLParser::parseURL ( const TiXmlElement *  urlElem)
static

Definition at line 172 of file xml_parser.cpp.

173 {
174  if(urlElem->ValueStr() != "url")
175  {
176  throw std::runtime_error("Parsing error: node not named 'url' but " + urlElem->ValueStr());
177  }
178 
179  // URL can be the value or in an href attribute
180  const char* url = extractContentOrAttribute(urlElem);
181  if(url == NULL)
182  {
183  throw std::runtime_error("ill-formed URL element");
184  }
185  return std::string(url);
186 }
static const char * extractContentOrAttribute(const TiXmlElement *elem, const char *identifier="href")
Definition: xml_parser.cpp:219

◆ parseUserdefinedParameter()

const UserdefParam fipa::acl::XMLParser::parseUserdefinedParameter ( const TiXmlElement *  paramElem)
static

Definition at line 188 of file xml_parser.cpp.

189 {
190  const char* href = paramElem->Attribute("href");
191  const char* value = paramElem->GetText();
192  // Throw an exception if either is NULL
193  if(href == NULL || value == NULL)
194  {
195  throw std::runtime_error("ill-formed user defined parameter");
196  }
197  // cut "X-"
198  return UserdefParam(std::string(href).substr(2), std::string(value));
199 
200 }

◆ strToDate()

const base::Time fipa::acl::XMLParser::strToDate ( const std::string &  dateStr)
static

Definition at line 202 of file xml_parser.cpp.

203 {
204  // XXX this does not parse relative times (+/- as first char), or UTC Z at the end.
205 
206  // Put a colon back after seconds
207  const std::string modDateStr = dateStr.substr(0, 15) + ":" + dateStr.substr(15);
208  try
209  {
210  return base::Time::fromString(modDateStr, base::Time::Milliseconds, "%Y%m%dT%H%M%S");
211  }
212  catch(std::exception& e)
213  {
214  // Maybe JADE's "Z"?
215  return base::Time::fromString(modDateStr, base::Time::Milliseconds, "%Y%m%dZ%H%M%S");
216  }
217 }

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