util/parser/Sax2XercesParser.h

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------
00002 Name:      Sax2XercesParser.h
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Default handling of Sax callbacks
00006 -----------------------------------------------------------------------------*/
00007 
00008 #ifndef _UTIL_PARSER_SAX2XERCESPARSER_H
00009 #define _UTIL_PARSER_SAX2XERCESPARSER_H
00010 
00011 #if defined(XMLBLASTER_MSXML_PLUGIN)
00012 #  error Implement Microsoft XML parser for /DXMLBLASTER_MSXML_PLUGIN
00013 #else  // XMLBLASTER_XERCES_PLUGIN
00014 
00015 #include <util/xmlBlasterDef.h>
00016 #include <util/parser/I_Parser.h>
00017 #include <util/plugin/I_Plugin.h>
00018 #include <string>
00019 #include <xercesc/util/TransService.hpp>
00020 #include <xercesc/sax2/Attributes.hpp>
00021 #include <xercesc/sax2/DefaultHandler.hpp>
00022 #include <xercesc/util/XMLString.hpp>
00023 #include <util/StopParseException.h>
00024 
00025 #if defined(XERCES_HAS_CPP_NAMESPACE)
00026         // Since Xerces 2.2 namespace is introduced:
00027    XERCES_CPP_NAMESPACE_USE
00028 #endif
00029 
00030 namespace org { namespace xmlBlaster { namespace util { namespace parser {
00031     
00042 class Dll_Export Sax2Parser : public I_Parser, public DefaultHandler,
00043                               public virtual org::xmlBlaster::util::plugin::I_Plugin
00044 {
00045    
00046 private:
00047    std::string ME;
00048    org::xmlBlaster::util::Global& global_;
00049    org::xmlBlaster::util::I_Log&    log_;
00050    XMLTranscoder* xmlBlasterTranscoder_;
00051    std::string encoding_;
00052 
00053    // Private copy ctor and assignement
00054    Sax2Parser(const Sax2Parser& data);
00055    Sax2Parser& operator=(const Sax2Parser& data);
00056 
00057 public:
00062    Sax2Parser(org::xmlBlaster::util::Global& global, XmlHandlerBase *handler);
00063 
00064    ~Sax2Parser();
00065    
00066    /*
00067     * This method parses the XML std::string using the SAX parser.
00068     * @param xmlLiteral The XML std::string
00069     */
00070    void init(const std::string &xmlLiteral);
00071    
00077    void parse(const std::string &xmlData);
00078 
00079 protected:
00080 
00081    std::string getLocationString(const SAXParseException &ex);
00082 
00083    AttributeMap& getAttributeMap(AttributeMap& attrMap, const Attributes &attrs);
00084 
00092    bool caseCompare(const XMLCh *name1, const char *name2) ;
00093 
00097    std::string getStringValue(const XMLCh* const value, bool doTrim=true) const;
00098 
00105    bool getStringAttr(const Attributes& attrs, const XMLCh* const name, std::string& value, bool doTrim=true) const;
00106 
00107  public:
00108 
00115    static void releaseXMLCh(XMLCh** data);
00116  
00123    static void releaseXMLCh(char** data);
00124 
00126    void characters(const XMLCh *const chars, const unsigned int length);
00127    
00129    void endDocument();
00130 
00132    void   endElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname);
00133 
00135    void   startDocument();
00136 
00138    void   startElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const Attributes &attrs);
00139 
00140 
00141    // implementation of the ErrorHandler interface
00143    void   error(const SAXParseException &exc);
00144 
00146    void   fatalError(const SAXParseException &exc);
00147 
00149    void   warning(const SAXParseException &exc);
00150 
00152    void   endCDATA();
00153 
00155    // void   endDTD()
00156 
00158    // void   endEntity(const XMLCh *const name)
00159 
00161    void   startCDATA();
00162 
00163    // these are probably not really needed here ...
00164 
00166    // void startDTD (const XMLCh *const name, const XMLCh *const publicId, const XMLCh *const systemId) {};
00167 
00169    // void startEntity(const XMLCh *const name) {};
00170 
00172    // void elementDecl(const XMLCh *const name, const XMLCh *const model) {};
00173 
00175    // void attributeDecl (const XMLCh *const eName, const XMLCh *const aName, const XMLCh *const type, const XMLCh *const mode, const XMLCh *const value) {};
00176 
00178    // void internalEntityDecl (const XMLCh *const name, const XMLCh *const value) {};
00179    // void externalEntityDecl (const XMLCh *const name, const XMLCh *const publicId, const XMLCh *const systemId) {};
00180 
00182    // void processingInstruction(const XMLCh *const target, const XMLCh *const data) {};
00183 
00185    // void resetDocument() {};
00186 
00188    // void setDocumentLocator(const Locator *const locator) {};
00189 
00191    // void startPrefixMapping(const XMLCh *const prefix, const XMLCh *const uri) {};
00192 
00194    // void endPrefixMapping(const XMLCh *const prefix) {};
00195 
00197    // void skippedEntity(const XMLCh *const name) {};
00198 
00199    // implementation of the EntityResolver interface.
00201    // InputSource* resolveEntity(const XMLCh *const publicId, const XMLCh *const systemId) { return NULL; };
00202 
00204    // void resetErrors() {};
00205 
00206    // implementation of DTDHandler interface.
00208    // void notationDecl(const XMLCh *const name, const XMLCh *const publicId, const XMLCh *const systemId) {};
00209 
00211    // void resetDocType() {};
00212 
00214    // void unparsedEntityDecl(const XMLCh *const name, const XMLCh *const publicId, const XMLCh *const systemId, const XMLCh *const notationName) {};
00215 
00216    //implementation of LexicalHandler interface.
00218    // void comment(const XMLCh *const chars, const unsigned int length) {};
00219 
00221    // void ignorableWhitespace(const XMLCh *const chars, const unsigned int length);
00222 
00228    std::string getType() { static std::string type = "XERCES"; return type; }
00229 
00235    std::string getVersion() { static std::string version = "1.0"; return version; }
00236 
00243    static std::string usage();
00244 };
00245 }}}} // namespace
00246 
00247 #endif // XMLBLASTER_XERCES_PLUGIN
00248 # endif // _UTIL_PARSER_SAX2XERCESPARSER_H