Marlin  1.17.1
 All Classes Namespaces Functions Variables Enumerations Friends Pages
XMLParser.h
1 #ifndef XMLParser_h
2 #define XMLParser_h 1
3 
4 #include "IParser.h"
5 #include "StringParameters.h"
6 #include "marlin/tinyxml.h"
7 
8 #include <fstream>
9 #include <string>
10 #include <iostream>
11 #include <memory>
12 
13 class TiXmlNode ;
14 class TiXmlDocument ;
15 
16 namespace marlin{
17 
18  typedef std::map< std::string , std::shared_ptr<StringParameters> > StringParametersMap;
19 
106  class XMLParser : public IParser {
107 
108 
109  public:
110 
111 
114  class LCTokenizer{
115 
116  std::vector< std::string >& _tokens ;
117  char _del ;
118  char _last ;
119  size_t _max ;
120  public:
121 
122  LCTokenizer( std::vector< std::string >& tokens, char del, size_t max=-1 ) : _tokens(tokens) , _del(del), _last(del), _max(max) {
123  }
124 
125 
126  void operator()(const char& c) {
127 
128  if( c != _del or _tokens.size() >= _max ) {
129 
130  if( _last == _del and _tokens.size() < _max ) {
131  _tokens.push_back("") ;
132  }
133  _tokens.back() += c ;
134  result() ;
135  }
136  _last = c ;
137 
138  }
139 
140  ~LCTokenizer(){
141  }
142 
143  std::vector<std::string> & result() {
144 
145  return _tokens ;
146 
147  }
148  };
149 
150 
151  XMLParser( const std::string& fileName, bool forCCheck=false ) ;
152  virtual ~XMLParser() ;
153 
155  void setCmdLineParameters( const CommandLineParametersMap & cmdlineparams ){
156  _cmdlineparams = cmdlineparams ;
157  }
158 
160  void parse() ;
161 
163  std::shared_ptr<StringParameters> getParameters( const std::string& sectionName ) const ;
164 
166  void write(const std::string &filen) const ;
167  protected:
168 
171  void parametersFromNode(TiXmlNode* section, std::map<std::string, std::string>& constants, std::pair<unsigned,unsigned>* typeCount=0) ;
172 
174  const char* getAttribute( TiXmlNode* node , const std::string& name ) ;
175 
178  void replacegroups(TiXmlNode* section) ;
179 
181  TiXmlNode* findElement( TiXmlNode* node , const std::string& type,
182  const std::string& attribute, const std::string& value ) ;
183 
184 
188  void processconditions( TiXmlNode* current , const std::string& conditions ) ;
189 
193  void processIncludeElements( TiXmlElement* element , const std::map<std::string, std::string>& constants );
194 
195  void processIncludeElement( TiXmlElement* element , const std::map<std::string, std::string>& constants , TiXmlDocument &document);
196 
197  void processConstants( TiXmlNode* node , std::map<std::string, std::string>& constants ) ;
198  void processConstant( TiXmlElement* element , std::map<std::string, std::string>& constants ) ;
199 
200  std::string &performConstantReplacement( std::string& value, const std::map<std::string, std::string>& constants ) ;
201 
202  void checkForNestedIncludes( const TiXmlNode *node );
203 
204  mutable StringParametersMap _map{};
205  StringParameters* _current ;
206  std::unique_ptr<TiXmlDocument> _doc{};
207 
208  std::string _fileName ;
209 
210  private:
211  XMLParser() = delete;
212  XMLParser(const marlin::XMLParser&) = delete;
213  XMLParser& operator=(const marlin::XMLParser&) = delete;
214  bool _forCCheck; //boolean variable set to true if parser is used for consistency checking
215 
216  CommandLineParametersMap _cmdlineparams{};
217 
218  };
219 
220 } // end namespace marlin
221 #endif
222 
XML parser for Marlin steering files.
Definition: XMLParser.h:106
void setCmdLineParameters(const CommandLineParametersMap &cmdlineparams)
set command line parameters
Definition: XMLParser.h:155
void write(const std::string &filen) const
Write the parsed XML tree in an other file.
Definition: XMLParser.cc:306
void processIncludeElements(TiXmlElement *element, const std::map< std::string, std::string > &constants)
Helper method - recursively replace all with the corresponding file content.
Definition: XMLParser.cc:583
const char * getAttribute(TiXmlNode *node, const std::string &name)
Return named attribute - throws ParseException if attribute doesn&#39;t exist.
Definition: XMLParser.cc:315
void processconditions(TiXmlNode *current, const std::string &conditions)
Helper method - recursively moves processors from &lt;if&gt; tags to top level (&lt;execute&gt;) and adds corresp...
Definition: XMLParser.cc:524
Interface for a parser of a steering file to be used with marlin.
Definition: IParser.h:19
Helper class for XMLParser.
Definition: XMLParser.h:114
std::shared_ptr< StringParameters > getParameters(const std::string &sectionName) const
Return the StringParameters for the section as read from the xml file.
Definition: XMLParser.cc:511
Always the top level node.
Definition: tinyxml.h:1366
TiXmlNode * findElement(TiXmlNode *node, const std::string &type, const std::string &attribute, const std::string &value)
Helper method - finds child element of node with given type and attribute value.
Definition: XMLParser.cc:946
void parse()
Parse the input file.
Definition: XMLParser.cc:24
The parent class for everything in the Document Object Model.
Definition: tinyxml.h:425
void replacegroups(TiXmlNode *section)
Helper method - replaces all &lt;group&gt; tag with corresponding &lt;processor&gt; tags.
Definition: XMLParser.cc:899
void parametersFromNode(TiXmlNode *section, std::map< std::string, std::string > &constants, std::pair< unsigned, unsigned > *typeCount=0)
Extracts all parameters from the given node and adss them to the current StringParameters object...
Definition: XMLParser.cc:336
The element is a container class.
Definition: tinyxml.h:943