1 /*-----------------------------------------------------------------------------
  2 Name:      Sax2Parser.h
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Default handling of Sax callbacks
  6 -----------------------------------------------------------------------------*/
  7 
  8 #ifndef _UTIL_PARSER_SAX2PARSER_H
  9 #define _UTIL_PARSER_SAX2PARSER_H
 10 
 11 #include <util/xmlBlasterDef.h>
 12 #include <util/parser/I_Parser.h>
 13 #include <util/plugin/I_Plugin.h>
 14 #include <string>
 15 #include <xercesc/util/TransService.hpp>
 16 #include <xercesc/sax2/DefaultHandler.hpp>
 17 #include <xercesc/util/XMLString.hpp>
 18 #include <util/StopParseException.h>
 19 
 20 #if defined(XERCES_HAS_CPP_NAMESPACE)
 21         // Since Xerces 2.2 namespace is introduced:
 22    XERCES_CPP_NAMESPACE_USE
 23 #endif
 24 
 25 namespace org { namespace xmlBlaster { namespace util { namespace parser {
 26     
 27 /**
 28  * Default xmlBlaster handling of Sax callbacks and errors.<p />
 29  * You may use this as a base class for your SAX handling.
 30  * <p>The encoding can be changed with <tt>xmlBlaster/encoding=<enc></tt> where
 31  * this is typically "iso-8859-1" or "UTF-8"
 32  * </p>
 33  * <p>
 34  * NOTE: Multibyte encoding "UTF-8" is currently not supported for xmlBlaster internal xml key and QoS markup!
 35  * </p>
 36  */
 37 class Dll_Export Sax2Parser : public I_Parser, public DefaultHandler,
 38                               public virtual org::xmlBlaster::util::plugin::I_Plugin
 39 {
 40    
 41 private:
 42    std::string ME;
 43    org::xmlBlaster::util::Global& global_;
 44    org::xmlBlaster::util::I_Log&    log_;
 45    XMLTranscoder* xmlBlasterTranscoder_;
 46    std::string encoding_;
 47 
 48    // Private copy ctor and assignement
 49    Sax2Parser(const Sax2Parser& data);
 50    Sax2Parser& operator=(const Sax2Parser& data);
 51 
 52 public:
 53    /**
 54     * Constructs an new object.
 55     * You need to call the init() method to parse the XML std::string.
 56     */
 57    Sax2Parser(org::xmlBlaster::util::Global& global, XmlHandlerBase *handler);
 58 
 59    ~Sax2Parser();
 60    
 61    /*
 62     * This method parses the XML std::string using the SAX parser.
 63     * @param xmlLiteral The XML std::string
 64     */
 65    void init(const std::string &xmlLiteral);
 66    
 67    /**
 68     * Does the actual parsing
 69     * @param xmlData Quality of service in XML notation
 70     */
 71    
 72    void parse(const std::string &xmlData);
 73 
 74 protected:
 75 
 76    std::string getLocationString(const SAXParseException &ex);
 77 
 78    AttributeMap& getAttributeMap(AttributeMap& attrMap, const Attributes &attrs);
 79 
 80    /**
 81     * Compares two std::strings (where name1 is a Unicode3.0 std::string!!) for 
 82     * unsensitive case compare. It returns true if the content of the
 83     * std::strings is equal (no matter what the case is). Using this method to
 84     * compare the std::strings should be portable to all platforms supported by
 85     * xerces.
 86     */
 87    bool caseCompare(const XMLCh *name1, const char *name2) ;
 88 
 89    /**
 90     * returns a trimmed value (usually from an attribute) as a standard C++ std::string
 91     */
 92    std::string getStringValue(const XMLCh* const value, bool doTrim=true) const;
 93 
 94    /**
 95     * gets the attribute specified by 'name' in the attribute list specified by 'list'. The result is put in 
 96     * the 'value' argument which is passed by reference. It returns 'true' if the attribute was found in the
 97     * specified attribute list or 'false' if it was not. In the later case, the value is untouched by this 
 98     * method. If the 'doTrim' argument is set to true, the std::string is trimmed before it is given back.
 99     */
100    bool getStringAttr(const Attributes& attrs, const XMLCh* const name, std::string& value, bool doTrim=true) const;
101 
102  public:
103 
104    /**
105     *  Helper method which encapsulates either the delete[] operator for Xerces-c versions older than 
106     * ver. 2.2.0 or which invokes Sax2Parser::releaseXMLCh(XMLCh**) for versions from 2.2.0 and higher. Per
107     * default it assumes you have 2.2.0 or higher. If you have an older version please set in your 
108     * build.properties or in your system environment the variable OLDXERCES.
109     */
110    static void releaseXMLCh(XMLCh** data);
111  
112    /**
113     *  Helper method which encapsulates either the delete[] operator for Xerces-c versions older than 
114     * ver. 2.2.0 or which invokes Sax2Parser::releaseXMLCh(XMLCh**) for versions from 2.2.0 and higher. Per
115     * default it assumes you have 2.2.0 or higher. If you have an older version please set in your 
116     * build.properties or in your system environment the variable OLDXERCES.
117     */
118    static void releaseXMLCh(char** data);
119 
120    /** Receive notification of character data inside an element. */
121    void characters(const XMLCh *const chars, const unsigned int length);
122    
123    /** Receive notification of the end of the document. */
124    void endDocument();
125 
126    /** Receive notification of the end of an element. */
127    void   endElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname);
128 
129    /** Receive notification of the beginning of the document. */
130    void   startDocument();
131 
132    /** Receive notification of the start of an element. */
133    void   startElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const Attributes &attrs);
134 
135 
136    // implementation of the ErrorHandler interface
137    /** Receive notification of a recoverable parser error. */
138    void   error(const SAXParseException &exc);
139 
140    /** Report a fatal XML parsing error. */
141    void   fatalError(const SAXParseException &exc);
142 
143    /** Receive notification of a parser warning. */
144    void   warning(const SAXParseException &exc);
145 
146    /** Receive notification of the end of a CDATA section. */
147    void   endCDATA();
148 
149    /** Receive notification of the end of the DTD declarations. */
150    // void   endDTD()
151 
152    /** Receive notification of the end of an entity. */
153    // void   endEntity(const XMLCh *const name)
154 
155    /** Receive notification of the start of a CDATA section. */
156    void   startCDATA();
157 
158    // these are probably not really needed here ...
159 
160    /** Receive notification of the start of the DTD declarations. */
161    // void startDTD (const XMLCh *const name, const XMLCh *const publicId, const XMLCh *const systemId) {};
162 
163    /** Receive notification of the start of an entity. */
164    // void startEntity(const XMLCh *const name) {};
165 
166    /** Report an element type declaration. */
167    // void elementDecl(const XMLCh *const name, const XMLCh *const model) {};
168 
169    /** Report an attribute type declaration. */
170    // void attributeDecl (const XMLCh *const eName, const XMLCh *const aName, const XMLCh *const type, const XMLCh *const mode, const XMLCh *const value) {};
171 
172    /** Report an internal entity declaration. */
173    // void internalEntityDecl (const XMLCh *const name, const XMLCh *const value) {};
174    // void externalEntityDecl (const XMLCh *const name, const XMLCh *const publicId, const XMLCh *const systemId) {};
175 
176    /** Receive notification of a processing instruction. */
177    // void processingInstruction(const XMLCh *const target, const XMLCh *const data) {};
178 
179    /** Reset the Docuemnt object on its reuse. */
180    // void resetDocument() {};
181 
182    /** Receive a Locator object for document events. */
183    // void setDocumentLocator(const Locator *const locator) {};
184 
185    /** Receive notification of the start of an namespace prefix mapping. */
186    // void startPrefixMapping(const XMLCh *const prefix, const XMLCh *const uri) {};
187 
188    /** Receive notification of the end of an namespace prefix mapping. */
189    // void endPrefixMapping(const XMLCh *const prefix) {};
190 
191    /** Receive notification of a skipped entity. */
192    // void skippedEntity(const XMLCh *const name) {};
193 
194    // implementation of the EntityResolver interface.
195    /** Resolve an external entity. */
196    // InputSource* resolveEntity(const XMLCh *const publicId, const XMLCh *const systemId) { return NULL; };
197 
198    /** Reset the Error handler object on its reuse. */
199    // void resetErrors() {};
200 
201    // implementation of DTDHandler interface.
202    /** Receive notification of a notation declaration. */
203    // void notationDecl(const XMLCh *const name, const XMLCh *const publicId, const XMLCh *const systemId) {};
204 
205    /** Reset the DTD object on its reuse. */
206    // void resetDocType() {};
207 
208    /** Receive notification of an unparsed entity declaration. */
209    // void unparsedEntityDecl(const XMLCh *const name, const XMLCh *const publicId, const XMLCh *const systemId, const XMLCh *const notationName) {};
210 
211    //implementation of LexicalHandler interface.
212    /** Receive notification of comments. */
213    // void comment(const XMLCh *const chars, const unsigned int length) {};
214 
215    /** Receive notification of ignorable whitespace in element content. */
216    // void ignorableWhitespace(const XMLCh *const chars, const unsigned int length);
217 
218    /**
219     * Get the name of the plugin. 
220     * @return "XERCES"
221     * @enforcedBy I_Plugin
222     */
223    std::string getType() { static std::string type = "XERCES"; return type; }
224 
225    /**
226     * Get the version of the plugin. 
227     * @return "1.0"
228     * @enforcedBy I_Plugin
229     */
230    std::string getVersion() { static std::string version = "1.0"; return version; }
231 
232    /**
233     * Command line usage.
234     * <p />
235     * These variables may be set in xmlBlaster.properties as well.
236     * Don't use the "-" prefix there.
237     */
238    static std::string usage();
239 };
240 }}}} // namespace
241 
242 #endif


syntax highlighted by Code2HTML, v. 0.9.1