32 #if !defined(XML_PARSER_H)
33 #define XML_PARSER_H 1
42 #include <libxml/parser.h>
43 #include <libxml/tree.h>
44 #include <libxml/encoding.h>
45 #include <libxml/xmlwriter.h>
47 #include <boost/parameter.hpp>
48 #include <feel/feelcore/parameter.hpp>
50 #define MY_ENCODING "UTF-8"
70 template <
class ArgumentPack>
71 Parameter_impl( ArgumentPack
const& args )
73 name =
new std::string( (
const char* )args[_name] );
74 type = ( int )args[_type];
75 cmdName = ( args[_cmdName | 0] ?
76 new std::string( (
const char* )args[_cmdName | (
const char* )0] ) :
77 new std::string( *name ) );
78 latex = ( args[_latex | 0] ?
new std::string( (
const char* )args[_latex | (
const char* )0] ) : ( std::string* )0 );
79 values = ( args[_values | 0] ?
new std::string( (
const char* )args[_values | (
const char* )0] ) : ( std::string* )0 );
85 inline std::vector<std::string> getAttrNames()
87 std::vector<std::string> attrNames;
88 attrNames.push_back(
"name" );
91 attrNames.push_back(
"type" );
94 attrNames.push_back(
"cmd_name" );
97 attrNames.push_back(
"latex" );
107 inline std::vector<std::string> getAttrValues()
109 std::vector<std::string> attrValues;
110 attrValues.push_back( *name );
113 attrValues.push_back( ( type == 1 ?
"discrete" :
"continuous" ) );
116 attrValues.push_back( *cmdName );
119 attrValues.push_back( *latex );
126 inline std::string getValues()
133 inline std::string getName()
147 BOOST_PARAMETER_CONSTRUCTOR(
149 , ( required ( name,* ) ) ( required ( type,* ) ) ( optional ( cmdName,* ) ) ( optional ( latex,* ) ) ( optional ( values,* ) ) )
155 std::vector<Parameter> dependencies;
156 std::vector<std::string> funcs;
159 template <
class ArgumentPack>
160 Output_impl( ArgumentPack
const& args )
164 _latex=args[_latex ] )
166 dependencies=args[_dependencies | std::vector<Parameter>() ];
167 funcs=args[_funcs | std::vector<std::string>() ];
172 inline std::vector<Parameter> getDependencies()
179 inline std::vector<std::string> getFuncs()
192 BOOST_PARAMETER_CONSTRUCTOR(
196 ( required ( name,* ) )
197 ( optional ( latex,* ) )
198 ( optional ( dependencies,* ) )
199 ( optional ( funcs,* ) )
211 static void writeResponse( std::string filename,std::string name,std::vector<Parameter> params,std::vector<Output> Outputs )
213 xmlDocPtr doc = xmlNewDoc( ( xmlChar* )
"1.0" );
214 xmlNodePtr rootNode = xmlNewNode( 0, ( xmlChar* )
"response" );
215 xmlDocSetRootElement( doc, rootNode );
216 xmlNodePtr progNode = createNode(
"program_name",name );
217 xmlAddChild( rootNode,progNode );
219 for (
unsigned int i=0; i<params.size(); ++i )
221 xmlNodePtr paramNode = createNode(
"param",params[i].getAttrNames(),params[i].getAttrValues(),params[i].getValues() );
222 xmlAddChild( rootNode,paramNode );
225 for (
unsigned int i=0; i<Outputs.size(); ++i )
227 xmlNodePtr paramNode = createNode(
"output",Outputs[i].getAttrNames(),Outputs[i].getAttrValues() );
229 for (
unsigned int j=0; j<Outputs[i].getDependencies().size(); ++j )
231 xmlNode* aNewNode = xmlNewNode( 0,( xmlChar* )
"depend" );
232 xmlSetProp( aNewNode, ( xmlChar* )
"value", ( xmlChar* ) Outputs[i].getDependencies()[j].getName().c_str() );
233 xmlNodeSetContent( aNewNode,( xmlChar* ) Outputs[i].getFuncs()[j].c_str() );
234 xmlAddChild( paramNode,aNewNode );
237 xmlAddChild( rootNode,paramNode );
240 xmlSaveFileEnc( filename.c_str(), doc, MY_ENCODING );
251 static void writeResult( std::string filename,
253 std::vector<Parameter> params,
254 std::vector<Output> Outputs,
255 std::vector<std::string> paramValues,
256 std::vector<std::string> OutputValues )
259 xmlNode *root_element = 0;
261 doc = xmlReadFile( filename.c_str(), 0, 0 );
265 doc = xmlNewDoc( ( xmlChar* )
"1.0" );
266 root_element = xmlNewNode( 0, ( xmlChar* )
"result" );
267 xmlDocSetRootElement( doc, root_element );
268 printf(
"[xmlParser] warning : the file %s has been generated\n", filename.c_str() );
271 root_element = xmlDocGetRootElement( doc );
273 if ( root_element == 0 )
275 printf(
"[xmlParser] error : the file %s is empty\n", filename.c_str() );
279 xmlNode* aNode=findNode( root_element,
"program", name.c_str() );
282 for (
unsigned int i=0; i<params.size(); ++i )
284 aNewNode=findNode( aNode, params[i].getName(), paramValues[i] );
288 for (
unsigned int i=0; i<Outputs.size(); ++i )
292 for ( cur_node = aNode->children; cur_node; cur_node = cur_node->next )
294 if ( ( cur_node->type == XML_ELEMENT_NODE ) &&
295 ( std::strcmp( (
char* )cur_node->name,Outputs[i].getName().c_str() )==0 ) )
297 xmlUnlinkNode( cur_node );
298 xmlFreeNode( cur_node );
302 aNewNode=createNode( Outputs[i].getName(), OutputValues[i] );
303 xmlAddChild( aNode,aNewNode );
306 xmlSaveFileEnc( filename.c_str(), doc, MY_ENCODING );
309 static xmlNode* findNode( xmlNode* aNode, std::string nodeName, std::string attrVal )
311 xmlNode *cur_node = 0;
313 for ( cur_node = aNode->children; cur_node; cur_node = cur_node->next )
315 if ( ( cur_node->type == XML_ELEMENT_NODE ) &&
316 ( std::strcmp( (
char* )cur_node->name,nodeName.c_str() )==0 ) )
318 xmlChar*
val=xmlGetProp( cur_node,( xmlChar* )
"value" );
320 if ( ( val!=0 ) && ( std::strcmp( (
char* )val,attrVal.c_str() )==0 ) )
327 xmlNode* aNewNode = xmlNewNode( 0,( xmlChar* ) nodeName.c_str() );
328 xmlSetProp( aNewNode, ( xmlChar* )
"value", ( xmlChar* ) attrVal.c_str() );
329 xmlAddChild( aNode,aNewNode );
333 static xmlNode* createNode( std::string nodeName, std::vector<std::string> attrNames, std::vector<std::string> attrValues, std::string value=
"" )
335 if ( attrNames.size()!=attrValues.size() )
337 printf(
"[xmlParser] error : attributes names list size != attributes values list size\n" );
341 xmlNode* aNewNode = xmlNewNode( 0,( xmlChar* ) nodeName.c_str() );
343 for (
unsigned int i=0; i<attrNames.size(); ++i )
344 xmlSetProp( aNewNode, ( xmlChar* ) attrNames[i].c_str(), ( xmlChar* ) attrValues[i].c_str() );
346 if ( std::strcmp( value.c_str(),
"" )!=0 )
347 xmlNodeSetContent( aNewNode,( xmlChar* ) value.c_str() );
352 static xmlNode* createNode( std::string nodeName, std::string value=
"" )
354 xmlNode* aNewNode = xmlNewNode( 0,( xmlChar* ) nodeName.c_str() );
356 if ( std::strcmp( value.c_str(),
"" )!=0 )
357 xmlNodeSetContent( aNewNode,( xmlChar* ) value.c_str() );
Expr< Val< typename mpl::if_< boost::is_arithmetic< ExprT1 >, mpl::identity< Cst< ExprT1 > >, mpl::identity< ExprT1 > >::type::type > > val(ExprT1 const &__e1)
precompute expression tensor
Definition: val.hpp:304
output class to describe code outputs
Definition: xmlparser.hpp:189
parameter class to describe code inputs
Definition: xmlparser.hpp:143