Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

XMLReader.hpp 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. // XMLReader.hpp
  2. //
  3. // (C) 2006 - 2009 MicroNeil Research Corporation.
  4. // See http://www.codedweller.com for details.
  5. //
  6. // This program is free software; you can redistribute it and/or modify it
  7. // under the terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 2 of the License, or (at your
  9. // option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful, but WITHOUT
  12. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. // more details.
  15. //
  16. // You should have received a copy of the GNU General Public License along with
  17. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  18. // Place, Suite 330, Boston, MA 02111-1307 USA
  19. //
  20. // What about this =============================================================
  21. // The XMLReader module provides a platform for reading configuration files
  22. // (or string data) containing well-formed xml and mapping that data to program
  23. // variables.
  24. // The idea is to provide the ability for an object or application to provide
  25. // a modular "XMLReader" object that models a hierarchical collection of
  26. // "settings" that can be represented easily in code and in xml.
  27. //
  28. // The following is an example model of a XMLReader in code and that same
  29. // XMLReader fully populated in xml.
  30. //
  31. // The code might look like this...
  32. //
  33. // int IntValue, DefaultInt = 3;
  34. // double DblValue, DefaultDbl = 3.14159;
  35. // bool BooleanValue, DefaultBool = false;
  36. // string StringValue, DefaultString = "NoStringHere";
  37. //
  38. // SpecialConfigurator : public XMLReaderHandler { // Create a special handler to build a list
  39. // ...
  40. // public:
  41. //
  42. // XMLReaderHandler& Startup(XMLReaderElement& E) { // This function returns a handy handler to
  43. // return MyStartupXMLReaderHandler; // (re) initialize this handler ;-)
  44. // }
  45. //
  46. // void Operator()() { // Each time the configurator is called
  47. // ...
  48. // }
  49. //
  50. // int Attribute1; // these items are interpreted and added
  51. // double Attribute2; // to the list. A XMLReaderHandler COULD
  52. // string Attribute3; // do something entirely different though ;-)
  53. // string Contents;
  54. // ...
  55. // } Special;
  56. //
  57. // XMLReaderElement SampleConfig("SampleConfiguration"); // Define a sample config (doc element)
  58. // SampleConfig // Populate the SampleConfig
  59. // .atStartCall(Special.Startup())
  60. // .Element("Integer", IntValue, DefaultInt).End() // Link an element to an int w/ default.
  61. // .Element("Double", DblValue, DefaultDbl).End("Double") // Link an element to a dbl w/ default.
  62. // .Element("String", StringValue, DefaultString).End("String") // Link an element to a string w/ default.
  63. // .Element("ComplexElements") // Create a sub element.
  64. // .Element("Complex1") // Sub element Complex1 has attributes.
  65. // .Attribute("IntAtt", IntValue, DefaultInt) // Complex1 has an integer attribute.
  66. // .Attribute("DblAtt", DblValue, DefaultDbl) // Complex1 has a dbl attribute.
  67. // .Element("IntAtt", IntValue).End() // IntAtt can also be set by a sub element.
  68. // .Element("DblAtt", DblValue).End() // DblAtt can also be set by a sub element.
  69. // .End() // That's it for Complex1.
  70. // .Element("Complex2") // Create the Complex2 sub element.
  71. // .Attribute("C2I", IntValue, DefaultInt) // C2I attribute.
  72. // .Attribute("C2D", DblValue) // C2D attribute - no default.
  73. // .Attribute("C2S", StringValue, DefultString) // C2S attribute - string w/ default.
  74. // .End("Complex2") // End of element throws if doesn't match.
  75. // .Element("Complex3", Special.Contents) // Element 3 using a special configurator.
  76. // .Attribute("A1", Special.Attribute1) // Set A1 and A2 and A3 and when the
  77. // .Attribute("A2", Special.Attribute2) // element has been completed, Special()
  78. // .Attribute("A3", Special.Attribute3) // will be called to record the entries.
  79. // .atEndCall(Special) // Here's where we register the handler.
  80. // .End() // Closing Complex3 to be ice.
  81. // .End() // Closing ComplexElements to be nice.
  82. // .End(); // Closing SampleConfiguration to be nice.
  83. //
  84. // The XML might look like this...
  85. //
  86. // <SampleConfiguration>
  87. // <Integer>10</Integer>
  88. // <Double>2.4</Double>
  89. // <String>This is a sample string</String>
  90. // <ComplexElements>
  91. // <Complex1 IntAtt="4" DblAtt="2.1324">
  92. // <IntAtt>24</IntAtt> <!-- changed IntAtt -->
  93. // </Complex1>
  94. // <Complex2 C2I='3' C2D='5.14' C2S='Some "string" we like' />
  95. // <Complex3> stuff in here </Complex3>
  96. // <Complex3> Another instance </Complex3>
  97. // <Complex3> Each one gets passed to Special() on activation </Complex3>
  98. // <Complex3> This way, Special() can build a list or some other </Complex3>
  99. // <Complex3> interesting thing with all of these. </Complex3>
  100. // <ComplexElements>
  101. // </SampleConfiguration>
  102. //
  103. // Include This Header Once Only ===============================================
  104. #ifndef XMLReader_included
  105. #define XMLReader_included
  106. #include <string>
  107. #include <sstream>
  108. #include <fstream>
  109. #include <cstring>
  110. #include <cstdlib>
  111. #include <list>
  112. namespace CodeDweller {
  113. class XMLReaderElement; // Elements exist
  114. class XMLReaderAttribute; // Attributes exist
  115. class XMLReaderData; // Data exists
  116. class XMLReaderTranslator; // Translators exist
  117. class XMLReaderMnemonic; // Mnemonics exist
  118. class XMLerator; // XMLerators exist
  119. class XMLeratorSetTrueOnComplete;
  120. typedef XMLReaderElement ConfigurationElement;
  121. typedef XMLReaderAttribute ConfigurationAttribute;
  122. typedef XMLReaderData ConfigurationData;
  123. typedef XMLReaderTranslator ConfigurationTranslator;
  124. typedef XMLReaderMnemonic ConfigurationMnemonic;
  125. typedef XMLerator Configurator;
  126. typedef XMLeratorSetTrueOnComplete ConfiguratorSetTrueOnComplete;
  127. class RawTranslator {
  128. private:
  129. int *IndexAssignment = 0;
  130. int *EndexAssignment = 0;
  131. std::string *RawDataAssignment = 0;
  132. public:
  133. void setRawDataAssignment(std::string &RawData);
  134. void setIndexAssignment(int &Index, int &Endex);
  135. void translate(int Index, int Endex, XMLReaderData &Data);
  136. };
  137. //// XMLReader Element /////////////////////////////////////////////////////
  138. //
  139. // Elements make up the core of a configuration. That is, a configuration is a
  140. // tree of elements. Elements translate directly to well formed xml elements in
  141. // a configuration file or string.
  142. class XMLReaderElement {
  143. private:
  144. std::string myName; // Elements have a name.
  145. // External important things I remember but don't touch...
  146. XMLReaderElement* myParent; // They may have a parrent.
  147. std::list<XMLerator*> myStartXMLerators; // Call these when we start Interpret()
  148. std::list<XMLerator*> myEndXMLerators; // Call these when we finish Interpret()
  149. // Internal / subordinate things I own and kill...
  150. std::list<XMLReaderAttribute*> myAttributes; // They may have a list of attributes.
  151. std::list<XMLReaderElement*> myElements; // They may have a list of sub-elements.
  152. std::list<XMLReaderMnemonic*> myMnemonics; // They may have a list of mnemonics.
  153. std::list<XMLReaderTranslator*> myTranslators; // They may have a list of translators.
  154. RawTranslator myRawTranslator; // Provides entire element.
  155. // During Interpret() operations we keep track of where we are seen...
  156. int myLine; // Last line number I was seen on.
  157. int myIndex; // Last char position I was seen on.
  158. int myEndex; // Last char of element.
  159. int myLength; // Last segment length.
  160. bool myCleanFlag; // Keep track of initialization.
  161. bool myInitOnInterpretFlag; // Initialize() at each Interpret()?
  162. void runStartXMLerators(XMLReaderData& D); // Does what it says ;-)
  163. void runEndXMLerators(XMLReaderData& D); // Does what it says ;-)
  164. public:
  165. XMLReaderElement(const char* Name); // Must be constructed with a name
  166. XMLReaderElement(const std::string Name); // either c string or c++ string.
  167. XMLReaderElement(const char* Name, XMLReaderElement& Parent); // Sub-elements are constructed with a
  168. XMLReaderElement(const std::string Name, XMLReaderElement& Parent); // parrent.
  169. // Upon desctruction an element will delete all subordinate objects:
  170. // * All sub element objects.
  171. // * All attribute objects.
  172. // * All mnemonic objects.
  173. // * All translator objects.
  174. // It is important to use new when passing one of these objects to an
  175. // element or attribute to prevent problems with the delete operation.
  176. // NORMALLY these things would be created using factory methods on the
  177. // element and attribute objects themselves - so be careful.
  178. // It will not delete XMLerators - they must
  179. // be deleted elsewhere because they may have been
  180. // re-used and this element wouldn't know about it ;-)
  181. ~XMLReaderElement(); // The descrutor clears and deletes all!
  182. // Elements can be probed for some simple, useful things.
  183. std::string Name(); // Get the name of this element.
  184. XMLReaderElement& Parent(); // Get the parent of this element.
  185. XMLReaderElement& Parent(XMLReaderElement& newParent); // Set the parent of this element.
  186. // Note - if there is no parent (an element is the root) then it will
  187. // return a reference to itself when Parent() is called.
  188. int Line(); // Get the last line number.
  189. int Index(); // Get the last data position.
  190. int Length(); // Get the last length.
  191. // Elements can contain either data or sub-elements.
  192. XMLReaderElement& Element(const char* Name); // Add a new sub element by c string name.
  193. XMLReaderElement& Element(const std::string Name); // Add a new sub element by c++ string name.
  194. //// Mapping element factory methods for convenience.
  195. //// Root-Node elements are _usually_ empty and without attributes in xml
  196. //// so we don't make any of that type of convenience constructor here.
  197. // char* versions
  198. XMLReaderElement& Element( // Mapping factory for convenience,
  199. const char* Name, // requires a name, of course,
  200. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  201. XMLReaderElement& Element( // Mapping factory for convenience,
  202. const char* Name, // requires a name, of course,
  203. std::string& x, std::string init = std::string("")); // Map to a string.
  204. XMLReaderElement& Element( // Mapping factory for convenience,
  205. const char* Name, // requires a name, of course,
  206. int& x, int init = 0, int radix = 0); // Map to an int.
  207. XMLReaderElement& Element( // Mapping factory for convenience,
  208. const char* Name, // requires a name, of course,
  209. double& x, double init = 0.0); // Map to a double.
  210. XMLReaderElement& Element( // Mapping factory for convenience,
  211. const char* Name, // requires a name, of course,
  212. bool& x, bool init = false); // Map to a boolean.
  213. // string versions
  214. XMLReaderElement& Element( // Mapping factory for convenience,
  215. const std::string Name, // requires a name, of course,
  216. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  217. XMLReaderElement& Element( // Mapping factory for convenience,
  218. const std::string Name, // requires a name, of course,
  219. std::string& x, std::string init = std::string("")); // Map to a string.
  220. XMLReaderElement& Element( // Mapping factory for convenience,
  221. const std::string Name, // requires a name, of course,
  222. int& x, int init = 0, int radix = 0); // Map to an int.
  223. XMLReaderElement& Element( // Mapping factory for convenience,
  224. const std::string Name, // requires a name, of course,
  225. double& x, double init = 0.0); // Map to a double.
  226. XMLReaderElement& Element( // Mapping factory for convenience,
  227. const std::string Name, // requires a name, of course,
  228. bool& x, bool init = false); // Map to a boolean.
  229. XMLReaderElement& RawData( // Copy entire element to
  230. std::string &Element); // a string.
  231. XMLReaderElement& RawIndex( // Copy indices of the entire
  232. int &Index, int &Endex); // element.
  233. // End methods for heading back up the tree at the end of an element.
  234. class EndNameDoesNotMatch {}; // Throw when End(name) doesn't match.
  235. XMLReaderElement& End(); // Return this element's parent.
  236. XMLReaderElement& End(const char* Name); // Check the name and return the parent
  237. XMLReaderElement& End(const std::string Name); // if the name is correct - or throw!
  238. // Elements can have attributes.
  239. XMLReaderAttribute& Attribute(const char* Name); // Add an attribute using a cstring.
  240. XMLReaderAttribute& Attribute(const std::string Name); // Add an attribute using a c++ string.
  241. //// Mapping Attribute factory methods for convenience.
  242. // char* versions
  243. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  244. const char* Name, // requires a name, of course,
  245. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  246. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  247. const char* Name, // requires a name, of course,
  248. std::string& x, std::string init = std::string("")); // Map to a string.
  249. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  250. const char* Name, // requires a name, of course,
  251. int& x, int init = 0, int radix = 0); // Map to an int.
  252. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  253. const char* Name, // requires a name, of course,
  254. double& x, double init = 0.0); // Map to a double.
  255. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  256. const char* Name, // requires a name, of course,
  257. bool& x, bool init = false); // Map to a boolean.
  258. // string versions
  259. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  260. const std::string Name, // requires a name, of course,
  261. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  262. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  263. const std::string Name, // requires a name, of course,
  264. std::string& x, std::string init = std::string("")); // Map to a string.
  265. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  266. const std::string Name, // requires a name, of course,
  267. int& x, int init = 0, int radix = 0); // Map to an int.
  268. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  269. const std::string Name, // requires a name, of course,
  270. double& x, double init = 0.0); // Map to a double.
  271. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  272. const std::string Name, // requires a name, of course,
  273. bool& x, bool init = false); // Map to a boolean.
  274. // Elements can Initialize() at each Interpret() call.
  275. XMLReaderElement& setInitOnInterpret(); // Set the init on interpret flag.
  276. // Elements can call external functions to aid in special operations
  277. // such as building lists.
  278. XMLReaderElement& atStartCall(XMLerator& Functor); // Add an atStart call-back to this element.
  279. XMLReaderElement& atEndCall(XMLerator& Functor); // Add an atEnd call-back to this element.
  280. // Extracting data from the element's contents is done with
  281. // translators. A good set of primatives are built in, but the user
  282. // can also make their own. If an Element is mapped to more than
  283. // one then they are all called once the element's contents are
  284. // collected. A translator takes the data provided by the element,
  285. // converts it into the expected type, and sets one or more variables
  286. // to the converted value. Usually - just one variable.
  287. XMLReaderElement& mapTo(XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  288. XMLReaderElement& mapTo(std::string& x, std::string init = std::string("")); // Map to a string.
  289. XMLReaderElement& mapTo(int& x, int init = 0, int radix = 0); // Map to an int.
  290. XMLReaderElement& mapTo(double& x, double init = 0.0); // Map to a double.
  291. XMLReaderElement& mapTo(bool& x, bool init = false); // Map to a boolean.
  292. // An Element's contents may use some special mnemonics to make a
  293. // XMLReader easier to understand and less error prone. When the
  294. // contents match a mnemnoic then the translation of the mnemonic is
  295. // passed to the Translators instead of the raw contents.
  296. XMLReaderElement& Mnemonic(const char* name, const char* value); // Add a mnemonic using c strings.
  297. XMLReaderElement& Mnemonic(const char* name, const std::string value); // Add a mnemonic using c & c++ strings.
  298. XMLReaderElement& Mnemonic(const std::string name, const char* value); // Add a mnemonic using c++ & c strings.
  299. XMLReaderElement& Mnemonic(const std::string name, const std::string value); // Add a mnemonic using c++ strings.
  300. // The way data gets into an element tree is that it is Interpret()ed
  301. // recursively. The data is loaded into a XMLReaderData object which
  302. // is passed to the top Element. That element interpretes the data, moves
  303. // the interpretation pointers, and passes the data on to it's subordinate
  304. // elements in turn. They do the same recursively. When the last sub -
  305. // element has had it's way with the data, the interpretation process is
  306. // complete. The XMLReaderData object will contain the original data
  307. // and a log of anything that happened during the interpretation process.
  308. //
  309. // Each time an element is asked to Interpret() data, it calls any atStart
  310. // configurators, translates any attributes, then either translates it's
  311. // contents or passes the data to it's children, then calls any atEnd
  312. // configurators.
  313. //
  314. // To ensure that the correct default values are used the Initialize() is
  315. // always called on all internal attributes and elements before any data is
  316. // interpreted. To prevent this from being inefficient, a boolean flag is
  317. // kept in each element to keep track of whether it is clean and if it is
  318. // then the call to Initialize will simply return (skipping subordinate
  319. // elements along the way).
  320. //
  321. // Interpret returns true if this object found itself at the current
  322. // Data.Index and false if not. This helps keep the recursive parsing
  323. // code simpler ;-)
  324. void initialize(); // Reset all translators to defaults.
  325. void notifyDirty(); // Set dirty (if translators change).
  326. bool interpret(XMLReaderData& Data); // (re) Interpret this data.
  327. };
  328. //// XMLReader Attribute ///////////////////////////////////////////////////
  329. //
  330. // Attributes translate directly to well formed xml attributes (within the
  331. // start tag of an element).
  332. class XMLReaderAttribute {
  333. private:
  334. std::string myName; // Elements have a name.
  335. XMLReaderElement& myParent; // They may have a parrent.
  336. std::list<XMLReaderMnemonic*> myMnemonics; // They may have a list of mnemonics.
  337. std::list<XMLReaderTranslator*> myTranslators; // They may have a list of translators.
  338. int myLine; // Last line number I was seen on.
  339. int myIndex; // Last char position I was seen on.
  340. int myLength; // Last segment length.
  341. public:
  342. XMLReaderAttribute(const char* Name, XMLReaderElement& Parent); // Sub-elements are constructed with a
  343. XMLReaderAttribute(const std::string Name, XMLReaderElement& Parent); // parrent.
  344. // Attributes delete their Mnemonics and Translators when they go.
  345. // See Elements for similar warnings about objects provided to
  346. // this object... you must use new to be safe, or better yet - stick to
  347. // the built in factory methods ;-)
  348. ~XMLReaderAttribute(); // Crush, Kill, Destroy!
  349. // Attributes can be probed for some simple, useful things.
  350. std::string Name(); // Get the name of this attribute.
  351. XMLReaderElement& Parent(); // Get the parent of this attribute.
  352. int Line(); // Get the last line number.
  353. int Index(); // Get the last data position.
  354. int Length(); // Get the last length.
  355. void notifyDirty(); // Attributes use this when they change.
  356. // For convenience in building configurations, an Attribute offers
  357. // some call-through methods to it's parrent Element. This allows for
  358. // clear, concise .method() coding that mimics an outline of the
  359. // configuration structure.
  360. //// For switching back to the parent element and adding new sub-elements.
  361. XMLReaderElement& Element(const char* Name); // Add a new sub element by c string name.
  362. XMLReaderElement& Element(const std::string Name); // Add a new sub element by c++ string name.
  363. //// Mapping element factory methods for convenience.
  364. //// Root-Node elements are _usually_ empty and without attributes in xml
  365. //// so we don't make any of that type of convenience constructor here.
  366. // char* versions
  367. XMLReaderElement& Element( // Mapping factory for convenience,
  368. const char* Name, // requires a name, of course,
  369. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  370. XMLReaderElement& Element( // Mapping factory for convenience,
  371. const char* Name, // requires a name, of course,
  372. std::string& x, std::string init = std::string("")); // Map to a string.
  373. XMLReaderElement& Element( // Mapping factory for convenience,
  374. const char* Name, // requires a name, of course,
  375. int& x, int init = 0, int radix = 0); // Map to an int.
  376. XMLReaderElement& Element( // Mapping factory for convenience,
  377. const char* Name, // requires a name, of course,
  378. double& x, double init = 0.0); // Map to a double.
  379. XMLReaderElement& Element( // Mapping factory for convenience,
  380. const char* Name, // requires a name, of course,
  381. bool& x, bool init = false); // Map to a boolean.
  382. // string versions
  383. XMLReaderElement& Element( // Mapping factory for convenience,
  384. const std::string Name, // requires a name, of course,
  385. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  386. XMLReaderElement& Element( // Mapping factory for convenience,
  387. const std::string Name, // requires a name, of course,
  388. std::string& x, std::string init = std::string("")); // Map to a string.
  389. XMLReaderElement& Element( // Mapping factory for convenience,
  390. const std::string Name, // requires a name, of course,
  391. int& x, int init = 0, int radix = 0); // Map to an int.
  392. XMLReaderElement& Element( // Mapping factory for convenience,
  393. const std::string Name, // requires a name, of course,
  394. double& x, double init = 0.0); // Map to a double.
  395. XMLReaderElement& Element( // Mapping factory for convenience,
  396. const std::string Name, // requires a name, of course,
  397. bool& x, bool init = false); // Map to a boolean.
  398. // End methods for heading back up the tree at the end of an element.
  399. XMLReaderElement& End(); // Return this element's parent.
  400. XMLReaderElement& End(const char* Name); // Check the name and return the parent
  401. XMLReaderElement& End(const std::string Name); // if the name is correct - or throw!
  402. //// For adding new attributes to the parent element.
  403. XMLReaderAttribute& Attribute(const char* Name); // Add an attribute using a cstring.
  404. XMLReaderAttribute& Attribute(const std::string Name); // Add an attribute using a c++ string.
  405. //// Mapping Attribute factory methods for convenience.
  406. // char* versions
  407. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  408. const char* Name, // requires a name, of course,
  409. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  410. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  411. const char* Name, // requires a name, of course,
  412. std::string& x, std::string init = std::string("")); // Map to a string.
  413. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  414. const char* Name, // requires a name, of course,
  415. int& x, int init = 0, int radix = 0); // Map to an int.
  416. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  417. const char* Name, // requires a name, of course,
  418. double& x, double init = 0.0); // Map to a double.
  419. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  420. const char* Name, // requires a name, of course,
  421. bool& x, bool init = false); // Map to a boolean.
  422. // string versions
  423. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  424. const std::string Name, // requires a name, of course,
  425. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  426. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  427. const std::string Name, // requires a name, of course,
  428. std::string& x, std::string init = std::string("")); // Map to a string.
  429. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  430. const std::string Name, // requires a name, of course,
  431. int& x, int init = 0, int radix = 0); // Map to an int.
  432. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  433. const std::string Name, // requires a name, of course,
  434. double& x, double init = 0.0); // Map to a double.
  435. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  436. const std::string Name, // requires a name, of course,
  437. bool& x, bool init = false); // Map to a boolean.
  438. //// Set Init On Interprete for the parent element.
  439. XMLReaderElement& setInitOnInterpret(); // Set the init on interpret flag.
  440. //// For adding configurators to the parent element.
  441. XMLReaderElement& atStartCall(XMLerator& Functor); // Add an atStart call-back to this element.
  442. XMLReaderElement& atEndCall(XMLerator& Functor); // Add an atEnd call-back to this element.
  443. // Of course, the most useful thing about attributes is that they can
  444. // be mapped to variables using translators. The same as those that
  445. // apply to the parent element's contents. Here they are for use on this
  446. // attribute.
  447. XMLReaderAttribute& mapTo(XMLReaderTranslator& newTranslator); // Add a Translator to this attribute.
  448. XMLReaderAttribute& mapTo(std::string& x, std::string init = std::string("")); // Map to a string.
  449. XMLReaderAttribute& mapTo(int& x, int init, int radix = 0); // Map to an int.
  450. XMLReaderAttribute& mapTo(double& x, double init = 0.0); // Map to a double.
  451. XMLReaderAttribute& mapTo(bool& x, bool init = false); // Map to a boolean.
  452. // Attributes can have mnemonics just like elements.
  453. XMLReaderAttribute& Mnemonic(const char* name, const char* value); // Add a mnemonic using a c string.
  454. XMLReaderAttribute& Mnemonic(const char* name, const std::string value); // Add a mnemonic using c & c++ strings.
  455. XMLReaderAttribute& Mnemonic(const std::string name, const char* value); // Add a mnemonic using c++ & c strings.
  456. XMLReaderAttribute& Mnemonic(const std::string name, const std::string value); // Add a mnemonic using a c++ string.
  457. // Attributes participate in the Interprete() task just like elements.
  458. void initialize(); // Reset all translators to defaults.
  459. bool interpret(XMLReaderData& Data); // (re) Interpret this data.
  460. };
  461. //// XMLReader Data ////////////////////////////////////////////////////////
  462. //
  463. // A XMLReaderData object holds on to the configuration source data and
  464. // provideds a place to log any information about how the configuration was
  465. // interpreted. It also creates and destroys a handy char[] to contain the
  466. // data. To make this beastie easier to handle, we use the Named Constructor
  467. // Idiom and hide the true constructor in the private section.
  468. class XMLReaderData { // XMLReader Data Source
  469. private:
  470. char* myDataBuffer; // The actual data buffer.
  471. int myBufferSize; // Size of the current buffer.
  472. int myIndex; // The current interpretation index.
  473. int myLine; // Current line number.
  474. public:
  475. XMLReaderData(const char* FileName); // Constructor from c string file name.
  476. XMLReaderData(const std::string FileName); // Constructor from c++ string file name.
  477. XMLReaderData(const char* Data, int Length); // Raw constructor from text buffer.
  478. ~XMLReaderData(); // Destroys the internal buffer etc.
  479. char Data(int Index); // Returns char from Data[Index]
  480. int Index(); // Reads the current Index.
  481. int Index(int i); // Changes the current Index.
  482. int Line(); // Reads the current Line number.
  483. int addNewLines(int Count); // Increments the Line number.
  484. std::string extract(int Index, int Endex) const ; // Return substring of buffer.
  485. std::stringstream Log; // Convenient Interpret log.
  486. };
  487. //// XMLReader Translator //////////////////////////////////////////////////
  488. //
  489. // A Translator converts the contents provided to it in string form into some
  490. // other data type. The object here is a prototype for that, followed by a
  491. // collection of the basic translators used for built-in mapTo()s.
  492. class XMLReaderTranslator { // Translators exist
  493. public:
  494. virtual ~XMLReaderTranslator(){}; // Stop No Virt Dtor warnings.
  495. virtual void translate(const char* Value) = 0; // Pure virtual translator.
  496. virtual void initialize() = 0; // Pure virtual initializer.
  497. };
  498. class StringTranslator : public XMLReaderTranslator {
  499. private:
  500. std::string& myVariable; // Variable to map.
  501. std::string myInitializer; // Initial/Default value.
  502. public:
  503. StringTranslator( // Construct this with
  504. std::string& Variable, // the variable to map,
  505. std::string Inititializer); // and the default value.
  506. void translate(const char* Value); // Provide a translation method.
  507. void initialize(); // Provide an initialization method.
  508. };
  509. class IntegerTranslator : public XMLReaderTranslator {
  510. private:
  511. int& myVariable; // Variable to map.
  512. int myInitializer; // Initial/Default value.
  513. int myRadix; // Radix for strtol()
  514. public:
  515. IntegerTranslator( // Construct this with
  516. int& Variable, // the variable to map,
  517. int Inititializer, // and the default value.
  518. int Radix); // For this one we also need a Radix.
  519. void translate(const char* Value); // Provide a translation method.
  520. void initialize(); // Provide an initialization method.
  521. };
  522. class DoubleTranslator : public XMLReaderTranslator {
  523. private:
  524. double& myVariable; // Variable to map.
  525. double myInitializer; // Initial/Default value.
  526. public:
  527. DoubleTranslator( // Construct this with
  528. double& Variable, // the variable to map,
  529. double Inititializer); // and the default value.
  530. void translate(const char* Value); // Provide a translation method.
  531. void initialize(); // Provide an initialization method.
  532. };
  533. class BoolTranslator : public XMLReaderTranslator {
  534. private:
  535. bool& myVariable; // Variable to map.
  536. bool myInitializer; // Initial/Default value.
  537. public:
  538. BoolTranslator( // Construct this with
  539. bool& Variable, // the variable to map,
  540. bool Inititializer); // and the default value.
  541. void translate(const char* Value); // Provide a translation method.
  542. void initialize(); // Provide an initialization method.
  543. };
  544. //// XMLReader Mnemonic ////////////////////////////////////////////////////
  545. //
  546. // A Mnemonic allows the actual contents of an element or attribute to be
  547. // exchanged for a different "named" value to help eliminate "magic numbers"
  548. // and "secret codes" from configurations. One way this might be used is to
  549. // map an enumeration to the appropriate integer values, or things like YES and
  550. // NO to boolean true and false (respectively) when turning on/off program
  551. // options.
  552. class XMLReaderMnemonic { // Mnemonics
  553. private:
  554. std::string myName; // What is the Mnemonic?
  555. std::string myValue; // What is the translation?
  556. public:
  557. XMLReaderMnemonic(std::string Name, std::string Value); // To make one, provide both parts.
  558. bool test(std::string Name); // Test to see if this Mnemonic matches.
  559. std::string Value(); // If it does then we will need it's value.
  560. };
  561. //// XMLerator //////////////////////////////////////////////////////////////
  562. //
  563. // An XMLerator is a "functor" or "closure" or "callback" that can be used to
  564. // support sophisticated interpretation options. The most basic and necessary
  565. // of these is support for list building. Consider an object created to contain
  566. // a list of records where each record might be represented as a collection of
  567. // attributes and elements. The object would have data elements mapped to the
  568. // attributes and elements in the configuration and then control elements which
  569. // are functors for initializing the list and storing new entries as they are
  570. // completed. The object here is a pure virtual prototype.
  571. class XMLerator { // XMLerators exist
  572. public:
  573. virtual void operator()(XMLReaderElement& E, XMLReaderData& D) = 0; // Pure virtual configurator.
  574. virtual ~XMLerator() {} // Virtual dtor keeps warnings away.
  575. };
  576. //// Include our inline methods ////////////////////////////////////////////////
  577. #include "XMLReader.inline.hpp"
  578. //// Utilities /////////////////////////////////////////////////////////////////
  579. // SetTrueOnComplete XMLerator //////////////////////////////////////////////
  580. class XMLeratorSetTrueOnComplete : public XMLerator { // XMLerator set's a boolean true.
  581. private:
  582. bool* myBoolean; // The boolean to set.
  583. public:
  584. XMLeratorSetTrueOnComplete(); // Must init to NULL for safety.
  585. void setup(bool& Target); // Link to the target boolean.
  586. void operator()(XMLReaderElement& E, XMLReaderData& D); // Handle the operation.
  587. };
  588. }
  589. #endif
  590. // End Of Include Only Once