source: XIOS/trunk/src/node/variable.cpp @ 1075

Last change on this file since 1075 was 1075, checked in by mhnguyen, 7 years ago

Improving error message of variable parsing

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 6.3 KB
Line 
1#include "variable.hpp"
2
3#include "attribute_template.hpp"
4#include "object_template.hpp"
5#include "group_template.hpp"
6#include "object_factory.hpp"
7#include "xios_spl.hpp"
8#include "type.hpp"
9#include "context.hpp"
10#include "context_client.hpp"
11#include <boost/algorithm/string.hpp>
12
13namespace xios {
14
15   /// ////////////////////// Définitions ////////////////////// ///
16
17   CVariable::CVariable(void)
18      : CObjectTemplate<CVariable>()
19      , CVariableAttributes()
20      , content()
21   { /* Ne rien faire de plus */ }
22
23   CVariable::CVariable(const StdString & id)
24      : CObjectTemplate<CVariable>(id)
25      , CVariableAttributes()
26      , content()
27   { /* Ne rien faire de plus */ }
28
29   CVariable::~CVariable(void)
30   { /* Ne rien faire de plus */ }
31
32   StdString CVariable::GetName(void)   { return (StdString("variable")); }
33   StdString CVariable::GetDefName(void){ return (CVariable::GetName()); }
34   ENodeType CVariable::GetType(void)   { return (eVariable); }
35
36   void CVariable::parse(xml::CXMLNode & node)
37   {
38      SuperClass::parse(node);
39      StdString id = (this->hasId()) ? this->getId() : StdString("undefined");
40      if (!node.getContent(this->content))
41      {
42        xml::THashAttributes attributes = node.getAttributes();
43        StdString variableName = attributes["name"];
44
45        node.goToParentElement();
46        StdString parentName = node.getElementName();
47        attributes = node.getAttributes();
48        error << "The variable id = " << id << " and name = " << variableName << " does not have any content. Please define it!" << std::endl
49              << "This variable is inside another element whose attributes are :" << std::endl;
50
51        for (xml::THashAttributes::iterator it = attributes.begin(); it != attributes.end(); ++it)
52        {
53           error << it->first << "=\"" << it->second.c_str() << "\" ";
54        }
55        error << std::endl; 
56
57         ERROR("CVariable::parse(xml::CXMLNode & node)",
58               << "[ variable id = " << id
59               << " ] variable is not defined !. It does not have any content. See error log for more details.");
60      }
61      content = boost::trim_copy(content) ;
62   }
63
64   const StdString& CVariable::getVariableOutputName(void) const
65   {
66     return name.isEmpty() ? getId() : name;
67   }
68
69   const StdString & CVariable::getContent (void) const
70   {
71      return (this->content);
72   }
73
74   void CVariable::setContent(const StdString& contentStr)
75   {
76     this->content = contentStr;
77   }
78
79   StdString CVariable::toString(void) const
80   {
81      StdOStringStream oss;
82
83      oss << "<" << CVariable::GetName() << " ";
84      if (this->hasId())
85         oss << " id=\"" << this->getId() << "\" ";
86      oss << SuperClassAttribute::toString() << ">" << std::endl
87          << this->content /*<< std::endl*/;
88      oss << "</" << CVariable::GetName() << " >";
89      return (oss.str());
90   }
91
92   /*
93   *\brief Sending value of a variable with its id from client to server
94   *
95   */
96   void CVariable::sendValue()
97   {
98     CContext* context=CContext::getCurrent() ;
99     if (!context->hasServer)
100     {
101       CContextClient* client=context->client ;
102
103       CEventClient event(this->getType(),EVENT_ID_VARIABLE_VALUE) ;
104       if (client->isServerLeader())
105       {
106         CMessage msg ;
107         msg<<this->getId() ;
108         msg<<content ;
109         const std::list<int>& ranks = client->getRanksServerLeader();
110         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
111           event.push(*itRank,1,msg);
112         client->sendEvent(event) ;
113       }
114       else client->sendEvent(event) ;
115    }
116   }
117
118   /*
119   *\brief Receive value of a variable with its id from client to server
120   *
121   */
122   void CVariable::recvValue(CEventServer& event)
123   {
124      CBufferIn* buffer=event.subEvents.begin()->buffer;
125      string id;
126      *buffer>>id ;
127      get(id)->recvValue(*buffer);
128   }
129
130   /*
131   *\brief Receive value of a variable with its id from client to server
132   *
133   */
134   void CVariable::recvValue(CBufferIn& buffer)
135   {
136      string str ;
137      buffer>>str;
138      setContent(str);
139   }
140
141   bool CVariable::dispatchEvent(CEventServer& event)
142   {
143    if (SuperClass::dispatchEvent(event)) return true ;
144    else
145    {
146      switch(event.type)
147      {
148        case EVENT_ID_VARIABLE_VALUE :
149          recvValue(event) ;
150          return true ;
151          break ;
152
153        default :
154          ERROR("bool CVariable::dispatchEvent(CEventServer& event)",<<"Unknown Event") ;
155          return false ;
156      }
157    }
158   }
159
160/*
161   void CVariable::toBinary(StdOStream & os) const
162   {
163     const StdString & content = this->content;
164     const StdSize size        =  content.size();
165     SuperClass::toBinary(os);
166
167     os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize));
168     os.write (content.data(), size * sizeof(char));
169   }
170
171   void CVariable::fromBinary(StdIStream & is)
172   {
173      SuperClass::fromBinary(is);
174      StdSize size  = 0;
175      is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
176      this->content.assign(size, ' ');
177      is.read (const_cast<char *>(this->content.data()), size * sizeof(char));
178   }
179*/
180   void CVariableGroup::parse(xml::CXMLNode & node, bool withAttr)
181   {
182      CVariableGroup* group_ptr = (this->hasId())
183         ? CVariableGroup::get(this->getId()) : CVariableGroup::get(this);
184
185      StdString content;
186      if (this->getId().compare(CVariableGroup::GetDefName()) != 0 && node.getContent(content))
187      {
188        StdSize beginid = 0, endid = 0, begindata = 0, enddata = 0;
189        StdString subdata, subid;
190
191        while ((beginid = content.find_first_not_of ( " \r\n\t;", enddata)) != StdString::npos)
192        {
193           endid   = content.find_first_of ( " \r\n\t=", beginid );
194           subid   = content.substr ( beginid, endid-beginid);
195           subid   = boost::to_lower_copy(boost::trim_copy(subid)) ;
196
197           begindata = content.find_first_of ( "=", endid ) + 1;
198           enddata   = content.find_first_of ( ";", begindata );
199           subdata   = content.substr ( begindata, enddata-begindata);
200           subdata   = boost::trim_copy(subdata) ;
201           group_ptr->createChild(subid)->content = subdata ;
202        }
203      }
204      else
205      {
206         SuperClass::parse(node, withAttr);
207      }
208      //SuperClass::parse(node, withAttr);
209
210   }
211
212} // namespace xios
Note: See TracBrowser for help on using the repository browser.