GME  13
GenParser.h
Go to the documentation of this file.
00001 
00002 #ifndef MGA_GENPARSER_H
00003 #define MGA_GENPARSER_H
00004 
00005 #include <xercesc/sax/HandlerBase.hpp>
00006 #include <comutil.h>
00007 
00008 XERCES_CPP_NAMESPACE_USE
00009 
00010 template<class DERIVED>
00011 struct CGenParserFunc ;
00012 
00013 class CMgaMetaParser;
00014 
00015 
00016 // --------------------------- XmlStr
00017 
00018 class XmlStr : public std::tstring
00019 {
00020 public:
00021         XmlStr() { };
00022         XmlStr(const XMLCh* const input);
00023         XmlStr(const XMLCh* const input, unsigned int len);
00024         XmlStr(XmlStr&& input) : std::tstring(std::move(input)) { }
00025 };
00026 
00027 // --------------------------- CGenParser
00028 
00029 //struct CGenParserFunc_Base;
00030 
00031 class CGenParser : public HandlerBase
00032 {
00033 public:
00034         typedef std::vector< std::pair<std::tstring, std::tstring> > attributes_type;
00035         typedef attributes_type::const_iterator attributes_iterator;
00036 
00037 
00038         CGenParser() : locator(NULL),/* elementfuncs(NULL),*/ err_line(0), err_column(0) { }
00039 
00040 // ------- Erros
00041 
00042 public:
00043         void SetErrorInfo2(HRESULT hr);
00044         void ThrowXmlError(const TCHAR *format, ...);
00045 
00046 // ------- Handler Base
00047 
00048 public:
00049         virtual InputSource *resolveEntity (const XMLCh* const publicId, const XMLCh* const systemId);
00050 
00051     virtual void startDocument();
00052     virtual void endDocument();
00053 
00054     virtual void startElement(const XMLCh* const name, AttributeList& attributes);
00055     virtual void endElement(const XMLCh* const name);
00056 
00057         virtual void characters(const XMLCh* const chars,const XMLSize_t);
00058 
00059     virtual void error(const SAXParseException& exception);
00060     virtual void fatalError(const SAXParseException& exception);
00061 
00062         virtual void setDocumentLocator(const Locator *const locator);
00063         virtual void fireStartFunction(const std::tstring& name, const attributes_type& attributes) = 0;
00064         virtual void fireEndFunction(const std::tstring& name) = 0;
00065         virtual void resetDocument() {
00066                 // GME-441
00067                 locator = NULL;
00068         }
00069 
00070 public:
00071         const Locator *locator;
00072 
00073 // ------- Attributes
00074 
00075 
00076         
00077 
00078 
00079         static const std::tstring *GetByNameX(const attributes_type &attributes, const TCHAR *name);
00080 
00081         static const std::tstring &GetByName(const attributes_type &attributes, const TCHAR *name)
00082         {
00083                 const std::tstring *p = GetByNameX(attributes, name);
00084                 const static std::tstring nullstr;
00085 
00086                 if( p == NULL ) p = &nullstr;
00087 //                      HR_THROW(E_INVALID_DTD);
00088 
00089                 return *p;
00090         }
00091 
00092         template<class INTERFACE, class FUNC_INTERFACE>
00093         void Attr(attributes_iterator i, const TCHAR *name, INTERFACE p, 
00094                 HRESULT (__stdcall FUNC_INTERFACE::*func)(BSTR))
00095         {
00096                 if( i->first == name )
00097                 {
00098                         FUNC_INTERFACE *q = p;
00099                         ASSERT( q != NULL );
00100 
00101                         COMTHROW( (q->*func)(PutInBstr(i->second)) );
00102                 }
00103         }
00104 
00105         static long toLong(std::tstring s);
00106         static unsigned long toULong(std::tstring s);
00107 
00108         template<class INTERFACE, class FUNC_INTERFACE>
00109         void Attr(attributes_iterator i, const TCHAR *name, INTERFACE p,
00110         HRESULT (__stdcall FUNC_INTERFACE::*func)(long))
00111         {
00112                 if( i->first == name )
00113                 {
00114                         FUNC_INTERFACE *q = p;
00115                         ASSERT( q != NULL );
00116 
00117                         COMTHROW( (q->*func)(toLong(i->second)) );
00118                 }
00119         }
00120 
00121         template<class INTERFACE, class FUNC_INTERFACE>
00122         void Attr(attributes_iterator i, const TCHAR *name, INTERFACE p,
00123         HRESULT (__stdcall FUNC_INTERFACE::*func)(unsigned long))
00124         {
00125                 if( i->first == name )
00126                 {
00127                         FUNC_INTERFACE *q = p;
00128                         ASSERT( q != NULL );
00129 
00130                         COMTHROW( (q->*func)(toULong(i->second)) );
00131                 }
00132         }
00133 
00134 // ------- ElementFuncs
00135 
00136 public:
00137 
00138         // Used to be: CGenParserFunc_Base *elementfuncs;
00139         //CGenParserFunc <CMgaMetaParser> *elementfuncs;
00140 
00141         void StartNone(const attributes_type &attributes) { }
00142         void EndNone() { }
00143 
00144 // ------- Properties
00145 
00146 public:
00147         std::tstring xmlfile;
00148         _bstr_t errorinfo;
00149         long err_line;
00150         long err_column;
00151 
00152         typedef unsigned long counter_type;
00153         counter_type counter;           // we do not advance the counter in GenParser
00154 
00155         struct element_type
00156         {
00157                 std::tstring name;
00158                 std::tstring chardata;
00159                 CComObjPtr<IUnknown> object;
00160                 std::tstring exstrinfo;
00161                 long   exnuminfo;
00162 
00163                 counter_type begin;
00164                 counter_type end;
00165         };
00166 
00167         std::vector<element_type> elements;
00168 
00169 public:
00170         element_type &GetCurrent() { ASSERT( !elements.empty() ); return elements.back(); }
00171         const element_type &GetPrevious() const
00172         {
00173                 ASSERT( elements.size() >= 2 );
00174 
00175                 std::vector<element_type>::const_iterator i = elements.end();
00176                 --i; --i;
00177                 return *i;
00178         }
00179 
00180         std::tstring &GetCurrData() { return GetCurrent().chardata; }
00181         const std::tstring &GetPrevName() const { return GetPrevious().name; }
00182         
00183         template<class T>
00184         void GetPrevObj(CComObjPtr<T> &obj) const
00185         {
00186                 ASSERT( GetPrevious().object != NULL );
00187                 COMTHROW( GetPrevious().object.QueryInterface(obj) );
00188                 ASSERT( obj != NULL );
00189         }
00190 
00191         _bstr_t PutInBstrAttr(const attributes_type &attributes, const TCHAR *name)
00192         {
00193                 const std::tstring *s = GetByNameX(attributes, name);
00194                 if( s == NULL )
00195                         return _bstr_t("");
00196 
00197                 return _bstr_t(s->c_str());
00198         }
00199 };
00200 
00201 // --------------------------- CGenParserFunc
00202 /*
00203 struct CGenParserFunc_Base
00204 {
00205         CGenParserFunc_Base(const char *n) : name(n) { }
00206 
00207         std::tstring name;
00208 
00209         virtual void Start(CGenParser *parser, const CGenParser::attributes_type &attributes) = 0;
00210         virtual void End(CGenParser *parser) = 0;
00211 };
00212 */
00213 template<class DERIVED>
00214 struct CGenParserFunc //: public CGenParserFunc_Base
00215 {
00216         typedef void (DERIVED::*StartFunc)(const CGenParser::attributes_type &);
00217         typedef void (DERIVED::*EndFunc)();
00218 
00219         CGenParserFunc(const TCHAR *n) : name(n) { }
00220 
00221         std::tstring name;
00222 
00223         virtual void Start(CGenParser *parser, const CGenParser::attributes_type &attributes)
00224         { 
00225                 (static_cast<DERIVED*>(parser)->*start)(attributes);  
00226         }
00227 
00228         virtual void End(CGenParser *parser)
00229         {
00230                 (static_cast<DERIVED*>(parser)->*end)();
00231         }
00232 
00233         CGenParserFunc(const TCHAR *n, StartFunc s, EndFunc e) : 
00234                 name(n), start(s), end(e) { }
00235 
00236         StartFunc start;
00237         EndFunc end;
00238 };
00239 
00240 #endif//MGA_GENPARSER_H