fintp_base
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TemplateParser.h
Go to the documentation of this file.
1 /*
2 * FinTP - Financial Transactions Processing Application
3 * Copyright (C) 2013 Business Information Systems (Allevo) S.R.L.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>
17 * or contact Allevo at : 031281 Bucuresti, 23C Calea Vitan, Romania,
18 * phone +40212554577, office@allevo.ro <mailto:office@allevo.ro>, www.allevo.ro.
19 */
20 
21 #ifndef TEMPLATEPARSER_H
22 #define TEMPLATEPARSER_H
23 
24 #include <vector>
25 #include <map>
26 
27 #ifdef WIN32
28  #define __MSXML_LIBRARY_DEFINED__
29 #endif
30 #include <xercesc/dom/DOM.hpp>
31 //#include <xercesc/dom/DOMBuilder.hpp>
32 #include <xercesc/sax/ErrorHandler.hpp>
33 //#include <xercesc/parsers/DOMBuilderImpl.hpp>
34 
35 //#include "../Collections.h"
36 #include "WorkItemPool.h"
37 #include "DllMain.h"
38 
39 XERCES_CPP_NAMESPACE_USE
40 
41 using namespace std;
42 
43 namespace FinTP
44 {
45  class TemplateNode;
46 
47  //EXPIMP_TEMPLATE template class ExportedObject std::vector< string >;
48 
50  {
51  public :
52 
57  {
61  Add,
69  Fail
70  };
71 
72  private :
73 
74  string m_Value;
79  bool m_Regex;
80 
82 
84  vector< string > m_GroupNames;
85 
86  // matches at most once a regex node
87  NodeMatchReturn MatchSingle( ManagedBuffer buffer, DOMElement* root );
88 
89  public :
90 
91  TemplateData();
92  explicit TemplateData( const DOMNode* node );
93  ~TemplateData();
94 
95  // returns true is the node is a regex, false if it is a reference to an inner template
96  bool IsRegex() const { return m_Regex; }
97 
98  //
99  bool MustBeSuccessive() const { return m_Successive; }
100 
101  int getMinOccurs() const { return m_MinOccurs; }
102  int getMaxOccurs() const { return m_MaxOccurs; }
103 
104  string getValue() const { return m_Value; }
105  string getFriendlyName() const { return m_FriendlyName; }
106 
107  const TemplateNode* getParent() const { return m_Parent; }
108  void setParent( const TemplateNode* parent ) { m_Parent = parent; }
109 
110  int getMatchEnd() const { return m_MatchEnd; }
111 
112  // returns true if the buffer matches the given regex / innertemplate
113  NodeMatchReturn Matches( ManagedBuffer buffer, DOMNode *root );
114  };
115 
116  //EXPIMP_TEMPLATE template class ExportedObject std::vector< TemplateData >;
117 
118  //class TemplateNode;
119  //EXPIMP_TEMPLATE template class ExportedObject std::vector< TemplateNode >;
120 
125  {
126  protected :
127  string m_Id;
128  bool m_Every;
131 
132  // regex nodes
133  vector< TemplateData > m_Data;
134 
135  // sub templates
136  vector< TemplateNode > m_Children;
137 
138  public :
139 
140  TemplateNode();
141  TemplateNode( const TemplateNode& source );
142  virtual ~TemplateNode();
143 
144  TemplateNode& operator=( const TemplateNode& source );
145 
146  // if all inner elements must match
147  bool MustMatchAllNodes() const { return m_Every; }
148 
149  // parses a node and constructs its child templates/data
150  virtual void Parse( DOMNode* );
151  virtual bool MatchTemplate( ManagedBuffer buffer, DOMNode *root );
152  //virtual bool AddLineInfoInDom( const string value, DOMNode** node, bool& first );
153 
154  string getId() const { return m_Id; }
155  unsigned int getChildrenCount() const { return m_Children.size(); }
156 
157  const TemplateNode* getParent() const { return m_Parent; }
158  void setParent( const TemplateNode* parent ) { m_Parent = parent; }
159 
160  const vector< TemplateData >& getData() const { return m_Data; }
161  const vector< TemplateNode >& getChildren() const { return m_Children; }
162 
163  int getMatchEnd() const { return m_MatchEnd; }
164 
165  const TemplateNode& FindTemplate( const string& templateName ) const;
166  };
167 
172  {
173  private :
174  // CR/LF/CRLF mode for handling new line in output files
175  string m_CrLfMode;
176 
177  public :
178 
179  // .ctor & destructor
181  ~TemplateNodeRoot();
182 
183  //string getCrLfMode( ) { return m_CrLfMode; };
184 
185  bool MatchTemplate( ManagedBuffer buffer, DOMNode *root );
186 
187  void Parse( DOMNode* );
188  };
189 
190  class ExportedObject TemplateFilter //: public DOMBuilderFilter
191  {
192  private :
193  //unsigned long m_WhatToShow;
194 
195  public:
196 
197  TemplateFilter(){}//: DOMBuilderFilter(){};
199 
200  // overrides
201  //unsigned long getWhatToShow() const { return m_WhatToShow; }
202  //unsigned void setWhatToShow(unsigned long toShow) { m_WhatToShow = toShow; }
203  short acceptNode( const DOMNode* node ) const;
204  };
205 
210  {
211  private :
212 
214  std::string m_TemplateFile;
215 
216  void ReleaseParseDocument();
217 
219  XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *m_ParseDocument;
220 
221  public:
222 
223  // .ctor and destructor.
224  // reads templateFilename and compiles a template tree
225  TemplateParser();
226  explicit TemplateParser( const string& templateFilename );
227  ~TemplateParser();
228  //TemplateParser( const TemplateParser& source );
229 
230  // root accessor
231  const TemplateNodeRoot& getRoot() const { return m_Root; }
232  // gets the name of the file used as template
233  string getTemplateFile() const { return m_TemplateFile; }
234 
235  // returns true if the buffer matches the given root template node
236  bool Matches( ManagedBuffer buffer, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* root );
237 
238  //use only for ReconS file, this file is structured by lines
239  //bool MatchByLine( ManagedBuffer buffer, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* root );
240 
241  int getMatchEnd() const { return m_MatchEnd; }
242 
244  };
245 
246  typedef pair< std::string, TemplateParser > ParserEntry;
247  //EXPIMP_TEMPLATE template class ExportedObject std::map< std::string, TemplateParser >;
248 
253  {
254  private :
255  static map< std::string, TemplateParser > Cache;
256 
257  // private .ctor since the class has only static methods we don't need to instantiate it
259 
260  public:
261 
262  // destructor
264 
265  // create a new instance of TemplateParser or return an existing parser
266  static TemplateParser getParser( const string& filename );
267  };
268 }
269 
270 #endif // TEMPLATEPARSER_H