Orocos Real-Time Toolkit  2.8.3
XMLRPCDemarshaller.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jan 19 14:11:20 CET 2004 XMLRPCDemarshaller.hpp
3 
4  XMLRPCDemarshaller.hpp - description
5  -------------------
6  begin : Mon January 19 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 #ifndef PI_PROPERTIES_XMLRPCDESERIALIZER
39 #define PI_PROPERTIES_XMLRPCDESERIALIZER
40 
41 #include <xercesc/util/PlatformUtils.hpp>
42 #include <xercesc/util/TransService.hpp>
43 #include <xercesc/sax2/SAX2XMLReader.hpp>
44 #include <xercesc/sax2/XMLReaderFactory.hpp>
45 #include <xercesc/sax2/DefaultHandler.hpp>
46 #include <xercesc/sax2/Attributes.hpp>
47 #include <xercesc/util/XMLUniDefs.hpp>
48 #include <xercesc/framework/LocalFileInputSource.hpp>
49 #include <iostream>
50 #include <fstream>
51 //#include "../os/rtstreams.hpp"
52 #include <sstream>
53 #include <vector>
54 #include <stack>
55 #include <map>
56 #include <string>
57 #include "../Property.hpp"
58 #include "StreamProcessor.hpp"
59 #include "MarshallInterface.hpp"
60 #include <istream>
61 
62 namespace RTT
63 { namespace marsh {
64 
65  //using namespace os; // Should be removed soon
66  using std::cerr;
67  using std::endl;
68 
69 #ifdef XERCES_CPP_NAMESPACE
70  using namespace XERCES_CPP_NAMESPACE;
71 #endif
72 
73  class RTT_API SAX2XMLRPCHandler : public DefaultHandler
74  {
75 
79  PropertyBag &bag;
80  enum State { STATE_NAME, STATE_BOOLEAN, STATE_CHAR, STATE_INT, STATE_DOUBLE, STATE_STRING, STATE_PROPERTIES};
81  std::stack<State> state_stack;
82 
83  public:
84 
85  SAX2XMLRPCHandler( PropertyBag &b ) : bag( b )
86  {}
87 
88  void endElement( const XMLCh* const uri,
89  const XMLCh* const localname,
90  const XMLCh* const qname )
91  {
92  char *ln = XMLString::transcode( localname );
93  if ( !strcmp( ln, "xmlrpc" ) )
94  {
95  state_stack.pop();
96  }
97  else
98  if ( !strcmp( ln, "boolean" ) )
99  {
100  //bool v;
101  std::stringstream buffer;
102  buffer << value.sv;
103 
104  bag.add( new Property<bool>( name, "", true ) );
105 
106  state_stack.pop();
107  }
108  else
109  if ( !strcmp( ln, "char" ) )
110  {
111  bag.add( new Property<char>( name, "", 'Z' ) );
112  state_stack.pop();
113  }
114  else
115  if ( !strcmp( ln, "int" ) )
116  {
117  bag.add( new Property<int>( name, "", 1234 ) );
118  state_stack.pop();
119  }
120  else
121  if ( !strcmp( ln, "double" ) )
122  {
123  bag.add( new Property<double>( name, "", 6.789 ) );
124  state_stack.pop();
125  }
126  else
127  if ( !strcmp( ln, "string" ) )
128  {
129  bag.add( new Property<std::string>( name, "", value.sv ) );
130  state_stack.pop();
131  }
132  else
133  if ( !strcmp( ln, "name" ) )
134  {
135  state_stack.pop();
136  }
137  }
138 
139  void startElement( const XMLCh* const uri,
140  const XMLCh* const localname,
141  const XMLCh* const qname,
142  const Attributes& attributes )
143  {
144  char *ln = XMLString::transcode( localname );
145  if ( !strcmp( ln, "boolean" ) )
146  state_stack.push( STATE_BOOLEAN );
147  else
148  if ( !strcmp( ln, "char" ) )
149  state_stack.push( STATE_CHAR );
150  else
151  if ( !strcmp( ln, "int" ) )
152  state_stack.push( STATE_INT );
153  else
154  if ( !strcmp( ln, "double" ) )
155  state_stack.push( STATE_DOUBLE );
156  else
157  if ( !strcmp( ln, "string" ) )
158  state_stack.push( STATE_STRING );
159  else
160  if ( !strcmp( ln, "xmlrpc" ) )
161  state_stack.push( STATE_PROPERTIES );
162  else
163  if ( !strcmp( ln, "name" ) )
164  state_stack.push( STATE_NAME );
165  }
166 #if 0
167  void warning( const SAXParseException& exception )
168  {
169  cerr << "\nWarning\n";
170  }
171  void error( const SAXParseException& exception )
172  {
173  cerr << "\nError\n";
174  }
175  void fatalError( const SAXParseException& exception )
176  {
177  cerr << "\nFatal error\n";
178  }
179 #endif
180  void characters( const XMLCh* const chars, const unsigned int length )
181  {
182  char* string_value;
183  switch ( state_stack.top() )
184  {
185  case STATE_NAME:
186  name = XMLString::transcode( chars );
187  break;
188 
189  case STATE_STRING:
190  value.sv = XMLString::transcode( chars );
191  break;
192 
193  case STATE_BOOLEAN:
194  string_value = XMLString::transcode( chars );
195 
196  break;
197  // TODO convert content to these types
198  case STATE_INT:
199  case STATE_CHAR:
200  case STATE_DOUBLE:
201  case STATE_PROPERTIES:
202  break;
203  }
204  }
205 
206 
210  char* name;
211 
215  union {
216  int iv;
217  char *sv;
218  } value;
219 
220  };
221 
222 
228  : public DemarshallInterface
229  {
230  typedef unsigned short XMLCh;
231 
232  XMLCh* name;
233  InputSource* fis;
234 
235  public:
236 
240  XMLRPCDemarshaller( const std::string filename ) : name(0), fis(0)
241  {
242  XMLPlatformUtils::Initialize();
243  name = XMLString::transcode( filename.c_str() );
244  fis = new LocalFileInputSource( name );
245  delete[] name;
246  }
247 
249  {
250  delete fis;
251  XMLPlatformUtils::Terminate();
252  }
253 
254  virtual bool deserialize( PropertyBag &v )
255  {
256  try
257  {
258  XMLPlatformUtils::Initialize();
259  }
260  catch ( const XMLException & toCatch )
261  {
262  cerr << "Error during initialization! :\n" /*<< StrX(toCatch.getMessage())*/ << endl;
263  }
264  catch ( ... )
265  {
266  cerr << "other exception" << endl;
267  }
268 
269  SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
270  //char *xmlFile = "test.xml";
271  int errorCount = 0;
272  try
273  {
274  SAX2XMLRPCHandler handler( v );
275  parser->setContentHandler( &handler );
276  parser->setErrorHandler( &handler );
277  // parser->setFeature( XMLUni::fgXercesSchemaFullChecking, false );
278  // parser->setFeature( XMLUni::fgSAX2CoreValidation, false );
279  // parser->setFeature( XMLUni::fgXercesDynamic, false );
280  // parser->setFeature( XMLUni::fgSAX2CoreNameSpaces, false );
281  // parser->setFeature( XMLUni::fgSAX2CoreNameSpacePrefixes, false );
282  // parser->setFeature( XMLUni::fgXercesSchema, false );
283 
284  //parser->parse( xmlFile );
285  parser->parse( *fis );
286  errorCount = parser->getErrorCount();
287  }
288  catch ( const XMLException & toCatch )
289  {
290  cerr << "\nAn XML parsing error occurred\n Error: " << endl;
291  XMLPlatformUtils::Terminate();
292  return false;
293  }
294  catch ( ... )
295  {
296  cerr << "General error" << endl;
297  XMLPlatformUtils::Terminate();
298  return false;
299  }
300  return true;
301  }
302 
303  };
304 }}
305 #endif
void characters(const XMLCh *const chars, const unsigned int length)
void startElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const Attributes &attributes)
#define RTT_API
Definition: rtt-config.h:97
A container for holding references to properties.
Definition: PropertyBag.hpp:96
XMLRPCDemarshaller(const std::string filename)
Read Properties from a local file.
basic_ostreams & endl(basic_ostreams &s)
Flush and newline.
Definition: rtstreams.cpp:110
A DemarshallInterface for the XMLRPC Protocol.
void endElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname)
An interface for extracting properties from a format.
virtual bool deserialize(PropertyBag &v)
Deserialize data to a property bag.
char * name
The name of the property.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51