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