fipa_acl  1.4
xml_parser.cpp
Go to the documentation of this file.
1 #include "xml_parser.h"
2 #include <base/Time.hpp>
3 #include <stdexcept>
4 
5 namespace fipa {
6 namespace acl {
7 
8 const AgentID XMLParser::parseAgentID(const TiXmlElement* aidElem)
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
38  }
39  else if(text.substr(0, 2) == "X-")
40  {
42  }
43  }
44  return aid;
45 }
46 
47 const AgentIDList XMLParser::parseAgentIDSequence(const TiXmlElement* aidlElem)
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 }
58 
59 const base::Time XMLParser::parseDate(const TiXmlElement* dateElem)
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 }
68 
69 const std::string XMLParser::parseName(const TiXmlElement* nameElem)
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 }
94 
95 const ReceivedObject XMLParser::parseReceivedObject(const TiXmlElement* receivedObjectElem)
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 }
171 
172 const std::string XMLParser::parseURL(const TiXmlElement* urlElem)
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 }
187 
188 const UserdefParam XMLParser::parseUserdefinedParameter(const TiXmlElement* paramElem)
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 }
201 
202 const base::Time XMLParser::strToDate(const std::string& dateStr)
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 }
218 
219 const char* XMLParser::extractContentOrAttribute(const TiXmlElement* elem, const char* identifier)
220 {
221  const char* text = elem->GetText();
222  if(text != NULL)
223  {
224  return text;
225  }
226  return elem->Attribute(identifier);
227 }
228 
229 } // end namespace acl
230 } // end namespace fipa
void setUserdefinedParameters(const UserdefinedParameterList &paramList)
static const ReceivedObject parseReceivedObject(const TiXmlElement *receivedObjectElem)
Definition: xml_parser.cpp:95
void setName(const std::string &name)
the method checks whether the passed name string is a word or not(according to the fipa spec) ...
Definition: agent_id.cpp:36
static const std::string parseURL(const TiXmlElement *urlElem)
Definition: xml_parser.cpp:172
void addAddress(const std::string &adr)
the method checks whether the passed address string is a word or not(according to the fipa spec) ...
Definition: agent_id.cpp:48
std::vector< UserdefParam > UserdefinedParameterList
Definition: userdef_param.h:94
std::vector< AgentID > AgentIDList
Definition: agent_id.h:20
void addUserdefParam(const UserdefParam &p)
Add a userdefined parameter.
Definition: agent_id.cpp:80
static const AgentIDList parseAgentIDSequence(const TiXmlElement *aidlElem)
Definition: xml_parser.cpp:47
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
void setResolvers(const Resolvers &resolvers)
Set list of resolvers.
Definition: agent_id.h:132
static const AgentID parseAgentID(const TiXmlElement *aidElem)
Definition: xml_parser.cpp:8
void setFrom(const URL &from)
void setId(const ID &id)
static const char * extractContentOrAttribute(const TiXmlElement *elem, const char *identifier="href")
Definition: xml_parser.cpp:219
static const std::string parseName(const TiXmlElement *nameElem)
Definition: xml_parser.cpp:69
overloaded equality operator for UserdefParam class objects
Definition: userdef_param.h:25
static const base::Time parseDate(const TiXmlElement *dateElem)
Definition: xml_parser.cpp:59
void setBy(const URL &by)
Implements the general AgentID functionality, which is present throughout the fipa specifications(FIP...
Definition: agent_id.h:37
void setDate(const base::Time &date)