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/I_Log.h>
14 #include <util/Timestamp.h>
15 #include <string>
16 #include <map>
17 #include <util/StringTrim.h>
18 #include <util/thread/ThreadImpl.h>
19
20 namespace org { namespace xmlBlaster { namespace util { namespace parser {
21
22 typedef std::map<std::string, std::string> AttributeMap;
23
24 /**
25 * Abstraction for the xml handling<p />
26 * You may use this as the interface to extend in your specific XML handling (example SAX2).
27 */
28 class Dll_Export XmlHandlerBase {
29
30 private:
31 std::string ME; // SaxHandlerBase
32
33 /**
34 * Does the actual parsing
35 * @param xmlData Quality of service in XML notation
36 */
37 void parse(const std::string &xmlData);
38
39 protected:
40 std::string character_;
41 std::string attributeCharacter_;
42 bool inAttribute_; // Nested <attribute>WITH OWN VALUES (attributeCharacter_)</attribute>
43 bool doTrimStrings_;
44 org::xmlBlaster::util::StringTrim trimmer_;
45 std::string locale_;
46
47 std::string getStartElementAsString(const std::string &name, const AttributeMap &attrMap);
48
49 /**
50 * The original XML std::string in ASCII representation, for example:
51 * <code> <qos></qos>"</code>
52 */
53 std::string xmlLiteral_;
54 org::xmlBlaster::util::Global& global_;
55 org::xmlBlaster::util::I_Log& log_;
56
57 org::xmlBlaster::util::thread::Mutex invocationMutex_;
58
59 public:
60 /**
61 * Constructs an new object.
62 * You need to call the init() method to parse the XML std::string.
63 */
64 XmlHandlerBase(org::xmlBlaster::util::Global& global);
65
66 virtual ~XmlHandlerBase() { };
67
68 /*
69 * This method parses the XML std::string using the SAX parser.
70 * @param xmlLiteral The XML std::string
71 */
72 void init(const std::string &xmlLiteral);
73
74 /**
75 * Get the locale (the xerces default is "en_US").
76 * Others are "de_DE.iso-8859-1" or "en_US.UTF-8"
77 */
78 std::string getLocale();
79
80 /**
81 * @return returns the literal xml std::string
82 */
83 std::string toString()
84 {
85 return xmlLiteral_;
86 }
87
88 /**
89 * @return returns the literal xml std::string
90 */
91 std::string toXml()
92 {
93 return xmlLiteral_;
94 }
95
96 /** Start document */
97 virtual void startDocument();
98
99 /** Start element. */
100 virtual void startElement(const std::string &name, const AttributeMap& attrs);
101
102 /**
103 * Characters.
104 * The text between two tags, in the following example 'Hello':
105 * <key>Hello</key>. This method is different from the java version
106 * since the c++ parser always starts at the first character, so you
107 * don't specify start.
108 */
109 virtual void characters(const std::string &ch);
110
111 /** End element. */
112 virtual void endElement(const std::string &name);
113
114 /** End document. */
115 virtual void endDocument();
116
117 /** Warning. */
118 virtual void warning(const std::string &exTxt);
119
120 /** Error. */
121 virtual void error(const std::string &exTxt);
122
123 /** Fatal error. */
124 virtual void fatalError(const std::string &exTxt);
125
126 virtual void endCDATA();
127
128 virtual void startCDATA();
129
130 /**
131 * returns a value (usually from an attribute) as an integer
132 */
133 int getIntValue(const std::string &value) const;
134
135 /**
136 * returns a value (usually from an attribute) as a long
137 */
138 long getLongValue(const std::string &value) const;
139
140 /**
141 * returns a value (usually from an attribute) as a Timestamp
142 */
143 Timestamp getTimestampValue(const std::string &value) const;
144
145 /**
146 * returns a value (usually from an attribute) as a bool
147 */
148 bool getBoolValue(const std::string &value) const;
149
150 /**
151 * gets the attribute specified by 'name' in the attribute list specified by 'list'. The result is put in
152 * the 'value' argument which is passed by reference. It returns 'true' if the attribute was found in the
153 * specified attribute list or 'false' if it was not. In the later case, the value is untouched by this
154 * method. If the 'doTrim' argument is set to true, the std::string is trimmed before it is given back.
155 */
156 bool getStringAttr(const AttributeMap &attrs, const std::string &name, std::string& value, bool doTrim=true) const;
157
158 /**
159 * gets the attribute specified by 'name' in the attribute list specified by 'list'. The result is put in
160 * the 'value' argument which is passed by reference. It returns 'true' if the attribute was found in the
161 * specified attribute list or 'false' if it was not. In the later case, the value is untouched by this
162 * method.
163 */
164 bool getIntAttr(const AttributeMap &attrs, const std::string &name, int& value) const;
165
166 /**
167 * gets the attribute specified by 'name' in the attribute list specified by 'list'. The result is put in
168 * the 'value' argument which is passed by reference. It returns 'true' if the attribute was found in the
169 * specified attribute list or 'false' if it was not. In the later case, the value is untouched by this
170 * method.
171 */
172 bool getLongAttr(const AttributeMap &attrs, const std::string &name, long& value) const;
173
174 /**
175 * gets the attribute specified by 'name' in the attribute list specified by 'list'. The result is put in
176 * the 'value' argument which is passed by reference. It returns 'true' if the attribute was found in the
177 * specified attribute list or 'false' if it was not. In the later case, the value is untouched by this
178 * method.
179 */
180 bool getTimestampAttr(const AttributeMap &attrs, const std::string &name, Timestamp& value) const;
181
182 /**
183 * gets the attribute specified by 'name' in the attribute list specified by 'list'. The result is put in
184 * the 'value' argument which is passed by reference. It returns 'true' if the attribute was found in the
185 * specified attribute list or 'false' if it was not. In the later case, the value is untouched by this
186 * method.
187 */
188 bool getBoolAttr(const AttributeMap &attrs, const std::string &name, bool& value) const;
189
190 };
191 }}}} // namespace
192
193 #endif
syntax highlighted by Code2HTML, v. 0.9.1