fintp_utils
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
XmlUtil.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 XMLUTIL_H
22 #define XMLUTIL_H
23 
24 #include "DllMainUtils.h"
25 #include <map>
26 #include <string>
27 #include <pthread.h>
28 
29 #ifdef WIN32
30  #define __MSXML_LIBRARY_DEFINED__
31 #endif
32 
33 #include <xercesc/util/XMLString.hpp>
34 #include <xercesc/dom/DOM.hpp>
35 //#include <xercesc/dom/DOMBuilder.hpp>
36 //#include <xercesc/parsers/DOMBuilderImpl.hpp>
37 #include <xercesc/parsers/XercesDOMParser.hpp>
38 #include <xercesc/framework/MemBufInputSource.hpp>
39 #include <xercesc/framework/MemBufFormatTarget.hpp>
40 #include <xercesc/framework/Wrapper4InputSource.hpp>
41 #include <xercesc/framework/StdOutFormatTarget.hpp>
42 #include <xercesc/dom/DOMErrorHandler.hpp>
43 #include <xercesc/util/XercesDefs.hpp>
44 #include <xercesc/sax/ErrorHandler.hpp>
45 #include <xercesc/util/TransService.hpp>
46 
47 using namespace std;
48 XERCES_CPP_NAMESPACE_USE
49 
50 namespace FinTP
51 {
52  //Contains:
53  // StrX, XStr - transcoding
54  // XmlUtil - serialize / deserialize to/from string
55  // DOMWriterTreeErrorHandler - error handler for DOMWriter
56  // XercesDOMTreeErrorHandler - error handler for XERCESDOMParser
57 
58  // ---------------------------------------------------------------------------
59  // This is a simple class that lets us do easy (though not terribly efficient)
60  // trancoding of char* data to XMLCh data.
61  // ---------------------------------------------------------------------------
62  class XStr
63  {
64  public :
65  // Constructors and Destructor
66  explicit XStr( const char* toTranscode )
67  {
68  // Call the private transcoding method
69  fUnicodeForm = XMLString::transcode( toTranscode );
70  }
71 
72  explicit XStr( const std::string& toTranscode )
73  {
74  // Call the private transcoding method
75  fUnicodeForm = XMLString::transcode( toTranscode.c_str() );
76  }
77 
78  ~XStr()
79  {
80  try
81  {
82  XMLString::release( &fUnicodeForm );
83  }catch(...){};
84  }
85 
86  // Getter methods
87  const XMLCh* uniForm() const
88  {
89  return fUnicodeForm;
90  }
91 
92  private :
93  // Private data members
94  // fUnicodeForm - This is the Unicode XMLCh format of the string.
95  XMLCh* fUnicodeForm;
96  XStr() : fUnicodeForm( NULL ) {};
97  };
98 
99  class StrX
100  {
101  public :
102  // -----------------------------------------------------------------------
103  // Constructors and Destructor
104  // -----------------------------------------------------------------------
105  explicit StrX( const XMLCh* toTranscode )
106  {
107  // Call the private transcoding method
108  fLocalForm = XMLString::transcode( toTranscode );
109  }
110 
111  ~StrX()
112  {
113  try
114  {
115  XMLString::release( &fLocalForm );
116  }catch( ... ){};
117  }
118 
119 
120  // -----------------------------------------------------------------------
121  // Getter methods
122  // -----------------------------------------------------------------------
123  const char* locForm() const
124  {
125  return fLocalForm;
126  }
127 
128  private :
129  // -----------------------------------------------------------------------
130  // Private data members
131  //
132  // fLocalForm
133  // This is the local code page form of the string.
134  // -----------------------------------------------------------------------
135  char* fLocalForm;
136  StrX() : fLocalForm( NULL ) {};
137  };
138 
139  #define unicodeForm( str ) ( XStr( str ).uniForm() )
140  #define localForm( str ) ( StrX( str ).locForm() )
141 
142  class ExportedUtilsObject XercesDOMTreeErrorHandler : public ErrorHandler
143  {
144  public:
145  // -----------------------------------------------------------------------
146  // Constructors and Destructor
147  // -----------------------------------------------------------------------
148  XercesDOMTreeErrorHandler() : m_SawErrors( false )
149  {
150  }
151 
153  {
154  }
155 
156 
157  // -----------------------------------------------------------------------
158  // Implementation of the error handler interface
159  // -----------------------------------------------------------------------
160  void warning( const SAXParseException& toCatch );
161  void error( const SAXParseException& toCatch );
162  void fatalError( const SAXParseException& toCatch );
163  void resetErrors();
164 
165  // -----------------------------------------------------------------------
166  // Getter methods
167  // -----------------------------------------------------------------------
168  bool getSawErrors() const { return m_SawErrors; }
169 
170  string getLastError() const { return m_LastError; }
171 
172  private :
173  // -----------------------------------------------------------------------
174  // Private data members
175  //
176  // fSawErrors
177  // This is set if we get any errors, and is queryable via a getter
178  // method. Its used by the main code to suppress output if there are
179  // errors.
180  // -----------------------------------------------------------------------
182  string m_LastError;
183  };
184 
185  class DOMWriterTreeErrorHandler : public DOMErrorHandler
186  {
187  public:
188 
191 
193  bool handleError( const DOMError& domError );
194  void resetErrors() const{};
195 
196  private :
197  /* Unimplemented constructors and operators */
198  explicit DOMWriterTreeErrorHandler( const DOMErrorHandler& );
200  };
201 
203  {
204  public:
205 
206  static string SerializeToString( const XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* doc, XERCES_CPP_NAMESPACE_QUALIFIER DOMImplementationLS* impl, int prettyPrint = 0 );
207  static string SerializeToString( const XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* document );
208  static string SerializeToString( const XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* node );
209  static string SerializeNodeToString( const XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* node, int prettyPrint = 0 );
210 
211  static XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* DeserializeFromFile( const string& filename );
212  static XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* DeserializeFromString( const string& buffer );
213  static XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* DeserializeFromString( const unsigned char* buffer, const unsigned long bufferSize );
214 
215  static void printDOM( const XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* doc );
216 
217  static string getNamespace( const XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* doc );
218 
219  static string XMLChtoString( const XMLCh* inputXml );
220  ~XmlUtil();
221 
222  static void Terminate();
223 
224  private :
225 
226  XmlUtil();
227 
228  XercesDOMParser* m_Parser;
229  XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *m_ParseDocument;
231  XMLTranscoder* m_UTF8Transcoder;
232 
233  //static pthread_mutex_t InstanceSyncMutex;
234  //static map< pthread_t, XmlUtil* > m_Instance;
235  static pthread_once_t KeysCreate;
236  static pthread_key_t InstanceKey;
237 
238  static void CreateKeys();
239  static void DeleteInstances( void* data );
240 
241  // the one and only instance
243  static bool m_Initialized;
244 
245  static XmlUtil* getInstance();
246 
247  XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* internalDeserialize( const unsigned char* buffer, const unsigned long bufferSize );
248  XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* internalDeserialize( const string& filename );
249 
250  void ReleaseParser();
251  void ReleaseUTF8Transcoder();
252  void CreateParser();
253  void CreateUTF8Transcoder();
254  };
255 }
256 
257 #endif // XMLUTIL_H