1 /*-----------------------------------------------------------------------------
2 Name: XmlHandlerBase.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_XMLHANDLERBASE_H
9 #define _UTIL_XMLHANDLERBASE_H
10
11 #include <util/xmlBlasterDef.h>
12 #include <util/XmlBlasterException.h>
13 #include <util/Timestamp.h>
14 #include <string>
15 #include <map>
16
17 namespace org { namespace xmlBlaster {
18 namespace util {
19
20 typedef std::map<std::string, std::string> Attributes;
21
22 /**
23 * Abstraction for the xml handling<p />
24 * You may use this as the interface to extend in your specific XML handling (example SAX2).
25 */
26 class Dll_Export XmlHandlerBase {
27
28 public:
29
30 /*
31 * This method parses the XML std::string using the SAX parser.
32 * @param xmlLiteral The XML std::string
33 */
34 virtual void init(const std::string &xmlLiteral) = 0;
35
36 /**
37 * @return returns the literal xml std::string
38 */
39 virtual std::string toString() = 0;
40
41 /**
42 * @return returns the literal xml std::string
43 */
44 virtual std::string toXml() = 0;
45
46 /** Start document */
47 virtual void startDocument() = 0;
48
49 /** Start element. */
50 virtual void startElement(const std::string &name, Attributes& attrs) = 0;
51
52 /**
53 * Characters.
54 * The text between two tags, in the following example 'Hello':
55 * <key>Hello</key>. This method is different from the java version
56 * since the c++ parser always starts at the first character, so you
57 * don't specify start.
58 */
59 virtual void characters(const std::string &ch) = 0;
60
61 /** Ignorable whitespace. */
62 virtual void ignorableWhitespace(const std::string &ch) = 0;
63
64 /** End element. */
65 void endElement(const std::string &name) = 0;
66
67 /** End document. */
68 virtual void endDocument() = 0;
69
70 /** Warning. */
71 virtual void warning(const XmlBlasterException &ex) = 0;
72
73 /** Error. */
74 virtual void error(const XmlBlasterException &ex) = 0;
75
76 /** Fatal error. */
77 virtual void fatalError(const XmlBlasterException &ex) = 0;
78
79 void endCDATA() = 0;
80
81 void startCDATA() = 0;
82
83 /**
84 * returns a value (usually from an attribute) as an integer
85 */
86 int getIntValue(const std::string &value) const;
87
88 /**
89 * returns a value (usually from an attribute) as a long
90 */
91 long getLongValue(const std::string &value) const;
92
93 /**
94 * returns a value (usually from an attribute) as a Timestamp
95 */
96 Timestamp getTimestampValue(const std::string &value) const;
97
98 /**
99 * returns a value (usually from an attribute) as a bool
100 */
101 bool getBoolValue(const std::string &value) const;
102
103 /**
104 * gets the attribute specified by 'name' in the attribute list specified by 'list'. The result is put in
105 * the 'value' argument which is passed by reference. It returns 'true' if the attribute was found in the
106 * specified attribute list or 'false' if it was not. In the later case, the value is untouched by this
107 * method. If the 'doTrim' argument is set to true, the std::string is trimmed before it is given back.
108 */
109 bool getStringAttr(const Attributes &attrs, const std::string &name, std::string& value, bool doTrim=true) const;
110
111 /**
112 * gets the attribute specified by 'name' in the attribute list specified by 'list'. The result is put in
113 * the 'value' argument which is passed by reference. It returns 'true' if the attribute was found in the
114 * specified attribute list or 'false' if it was not. In the later case, the value is untouched by this
115 * method.
116 */
117 bool getIntAttr(const Attributes &attrs, const std::string &name, int& value) const;
118
119 /**
120 * gets the attribute specified by 'name' in the attribute list specified by 'list'. The result is put in
121 * the 'value' argument which is passed by reference. It returns 'true' if the attribute was found in the
122 * specified attribute list or 'false' if it was not. In the later case, the value is untouched by this
123 * method.
124 */
125 bool getLongAttr(const Attributes &attrs, const std::string &name, long& value) const;
126
127 /**
128 * gets the attribute specified by 'name' in the attribute list specified by 'list'. The result is put in
129 * the 'value' argument which is passed by reference. It returns 'true' if the attribute was found in the
130 * specified attribute list or 'false' if it was not. In the later case, the value is untouched by this
131 * method.
132 */
133 bool getTimestampAttr(const Attributes &attrs, const std::string &name, Timestamp& value) const;
134
135 /**
136 * gets the attribute specified by 'name' in the attribute list specified by 'list'. The result is put in
137 * the 'value' argument which is passed by reference. It returns 'true' if the attribute was found in the
138 * specified attribute list or 'false' if it was not. In the later case, the value is untouched by this
139 * method.
140 */
141 bool getBoolAttr(const Attributes &attrs, const std::string &name, bool& value) const;
142
143 };
144 }}} // namespace
145
146 #endif
syntax highlighted by Code2HTML, v. 0.9.1