fipa_acl  1.4
xml_message_format.cpp
Go to the documentation of this file.
1 #include "xml_message_format.h"
2 #include "xml_format.h"
3 
4 #include <boost/foreach.hpp>
5 
6 namespace fipa {
7 namespace acl {
8 
9 
10 std::string XMLMessageFormat::apply(const ACLMessage& aclMsg) const
11 {
12  TiXmlDocument doc;
13  TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
14  doc.LinkEndChild( decl );
15 
16  TiXmlElement * messageElem = new TiXmlElement( "fipa-message" );
17  // Apply communictaive act and conversation id
18  messageElem->SetAttribute("act", aclMsg.getPerformative());
19  messageElem->SetAttribute("conversation-id", aclMsg.getConversationID());
20  doc.LinkEndChild( messageElem );
21 
22  BOOST_FOREACH(TiXmlElement* elem, getParameters(aclMsg))
23  {
24  messageElem->LinkEndChild(elem);
25  }
26 
27  TiXmlPrinter printer;
28  printer.SetIndent("");
29  printer.SetLineBreak("");
30 
31  doc.Accept( &printer );
32 
33  return printer.Str();
34 }
35 
36 std::vector<TiXmlElement*> XMLMessageFormat::getParameters(const ACLMessage& aclMsg) const
37 {
38  using namespace MessageField;
39  std::vector<TiXmlElement*> vec;
40 
41  const AgentIDList& receivers = aclMsg.getAllReceivers();
42  if(!receivers.empty())
43  {
44  TiXmlElement* receiversElem = new TiXmlElement(MessageFieldTxt[RECEIVER]);
45  // Message uses name id and url href
46  BOOST_FOREACH(TiXmlElement* elem, XMLFormat::getAgentIDSequence(receivers, true, true))
47  {
48  receiversElem->LinkEndChild(elem);
49  }
50  vec.push_back(receiversElem);
51  }
52 
53  const AgentID& sender = aclMsg.getSender();
54  if(sender.isValid())
55  {
56  TiXmlElement* senderElem = new TiXmlElement(MessageFieldTxt[SENDER]);
57  // Message uses name id and url href
58  senderElem->LinkEndChild(XMLFormat::getAgentID(sender, true, true));
59  vec.push_back(senderElem);
60  }
61 
62  if(!aclMsg.getContentPtr()->empty())
63  {
64  TiXmlElement* contentElem = new TiXmlElement(MessageFieldTxt[CONTENT]);
65  TiXmlText* text = new TiXmlText(aclMsg.getContent());
66  text->SetCDATA(true);
67  contentElem->LinkEndChild(text);
68  vec.push_back(contentElem);
69  }
70 
71  const std::string& language = aclMsg.getLanguage();
72  if(!language.empty())
73  {
74  TiXmlElement* languageElem = new TiXmlElement(MessageFieldTxt[LANGUAGE]);
75  languageElem->LinkEndChild(new TiXmlText(language));
76  vec.push_back(languageElem);
77  }
78 
79  const std::string& encoding = aclMsg.getEncoding();
80  if(!encoding.empty())
81  {
82  TiXmlElement* encodingElem = new TiXmlElement(MessageFieldTxt[ENCODING]);
83  encodingElem->LinkEndChild(new TiXmlText(encoding));
84  vec.push_back(encodingElem);
85  }
86 
87  const std::string& ontology = aclMsg.getOntology();
88  if(!ontology.empty())
89  {
90  TiXmlElement* ontologyElem = new TiXmlElement(MessageFieldTxt[ONTOLOGY]);
91  ontologyElem->LinkEndChild(new TiXmlText(ontology));
92  vec.push_back(ontologyElem);
93  }
94 
95  const std::string& protocol = aclMsg.getProtocol();
96  if(!protocol.empty())
97  {
98  TiXmlElement* protocolElem = new TiXmlElement(MessageFieldTxt[PROTOCOL]);
99  protocolElem->LinkEndChild(new TiXmlText(protocol));
100  vec.push_back(protocolElem);
101  }
102 
103  const std::string& replyWith = aclMsg.getReplyWith();
104  if(!replyWith.empty())
105  {
106  TiXmlElement* replyWithElem = new TiXmlElement(MessageFieldTxt[REPLY_WITH]);
107  replyWithElem->LinkEndChild(new TiXmlText(replyWith));
108  vec.push_back(replyWithElem);
109  }
110 
111  const std::string& inReplyTo = aclMsg.getInReplyTo();
112  if(!inReplyTo.empty())
113  {
114  TiXmlElement* inReplyToElem = new TiXmlElement(MessageFieldTxt[IN_REPLY_TO]);
115  inReplyToElem->LinkEndChild(new TiXmlText(inReplyTo));
116  vec.push_back(inReplyToElem);
117  }
118 
119  base::Time replyBy = aclMsg.getReplyBy();
120  if(!replyBy.isNull())
121  {
122  TiXmlElement* replyByElem = new TiXmlElement(MessageFieldTxt[REPLY_BY]);
123  replyByElem->SetAttribute("time", XMLFormat::dateToStr(replyBy));
124  vec.push_back(replyByElem);
125  }
126 
127  const AgentIDList& replyTo = aclMsg.getAllReplyTo();
128  if(!replyTo.empty())
129  {
130  TiXmlElement* replyToElem = new TiXmlElement(MessageFieldTxt[REPLY_TO]);
131  // Message uses name id and url href
132  BOOST_FOREACH(TiXmlElement* elem, XMLFormat::getAgentIDSequence(replyTo, true, true))
133  {
134  replyToElem->LinkEndChild(elem);
135  }
136  vec.push_back(replyToElem);
137  }
138 
139  const std::string& conversationId = aclMsg.getConversationID();
140  if(!conversationId.empty())
141  {
142  TiXmlElement* conversationIdElem = new TiXmlElement(MessageFieldTxt[CONVERSATION_ID]);
143  conversationIdElem->LinkEndChild(new TiXmlText(conversationId));
144  vec.push_back(conversationIdElem);
145  }
146 
147  BOOST_FOREACH(TiXmlElement* elem, XMLFormat::getUserdefinedParameters(aclMsg.getUserdefParams()))
148  {
149  vec.push_back(elem);
150  }
151 
152  return vec;
153 }
154 
155 } // end namespace acl
156 } // end namespace fipa
std::string getEncoding() const
Definition: acl_message.h:258
std::string getContent() const
Definition: acl_message.h:294
std::vector< UserdefParam > getUserdefParams() const
Definition: acl_message.h:323
base::Time getReplyBy() const
Definition: acl_message.h:334
std::string getPerformative() const
Definition: acl_message.h:139
std::map< MessageField::Type, std::string > MessageFieldTxt
std::string getReplyWith() const
Definition: acl_message.h:217
std::vector< AgentID > AgentIDList
Definition: agent_id.h:20
std::string getOntology() const
Definition: acl_message.h:248
AgentIDList getAllReplyTo() const
Definition: acl_message.h:192
bool isValid() const
Definition: agent_id.cpp:31
static const std::string dateToStr(const base::Time &date)
Definition: xml_format.cpp:15
static TiXmlElement * getAgentID(const AgentID &aid, bool useNameId, bool useUrlHref)
Definition: xml_format.cpp:61
const std::string * getContentPtr() const
Definition: acl_message.h:299
std::string getLanguage() const
Definition: acl_message.h:268
static std::vector< TiXmlElement * > getAgentIDSequence(const AgentIDList &aidl, bool useNameId, bool useUrlHref)
Definition: xml_format.cpp:51
std::string getInReplyTo() const
Definition: acl_message.h:207
std::string apply(const ACLMessage &aclMsg) const
AgentID getSender() const
Definition: acl_message.h:311
std::string getProtocol() const
Definition: acl_message.h:238
This class provides a representation of a message conforming to the FIPA specification SC00061...
Definition: acl_message.h:55
static std::vector< TiXmlElement * > getUserdefinedParameters(const UserdefinedParameterList &params)
Definition: xml_format.cpp:145
Implements the general AgentID functionality, which is present throughout the fipa specifications(FIP...
Definition: agent_id.h:37
Foundation of Physical Intelligent Agents.
Definition: conversation.cpp:6
AgentIDList getAllReceivers() const
Definition: acl_message.h:166
std::string getConversationID() const
Definition: acl_message.h:227