Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. typedef XMLReaderElement ConfigurationElement;
  120. typedef XMLReaderAttribute ConfigurationAttribute;
  121. typedef XMLReaderData ConfigurationData;
  122. typedef XMLReaderTranslator ConfigurationTranslator;
  123. typedef XMLReaderMnemonic ConfigurationMnemonic;
  124. typedef XMLerator Configurator;
  125. class RawTranslator {
  126. private:
  127. int *IndexAssignment = 0;
  128. int *EndexAssignment = 0;
  129. std::string *RawDataAssignment = 0;
  130. public:
  131. void setRawDataAssignment(std::string &RawData);
  132. void setIndexAssignment(int &Index, int &Endex);
  133. void translate(int Index, int Endex, XMLReaderData &Data);
  134. };
  135. //// XMLReader Element /////////////////////////////////////////////////////
  136. //
  137. // Elements make up the core of a configuration. That is, a configuration is a
  138. // tree of elements. Elements translate directly to well formed xml elements in
  139. // a configuration file or string.
  140. class XMLReaderElement {
  141. private:
  142. std::string myName; // Elements have a name.
  143. // External important things I remember but don't touch...
  144. XMLReaderElement* myParent; // They may have a parrent.
  145. std::list<XMLerator*> myStartXMLerators; // Call these when we start Interpret()
  146. std::list<XMLerator*> myEndXMLerators; // Call these when we finish Interpret()
  147. // Internal / subordinate things I own and kill...
  148. std::list<XMLReaderAttribute*> myAttributes; // They may have a list of attributes.
  149. std::list<XMLReaderElement*> myElements; // They may have a list of sub-elements.
  150. std::list<XMLReaderMnemonic*> myMnemonics; // They may have a list of mnemonics.
  151. std::list<XMLReaderTranslator*> myTranslators; // They may have a list of translators.
  152. RawTranslator myRawTranslator; // Provides entire element.
  153. // During Interpret() operations we keep track of where we are seen...
  154. int myLine; // Last line number I was seen on.
  155. int myIndex; // Last char position I was seen on.
  156. int myEndex; // Last char of element.
  157. int myLength; // Last segment length.
  158. bool myCleanFlag; // Keep track of initialization.
  159. bool myInitOnInterpretFlag; // Initialize() at each Interpret()?
  160. void runStartXMLerators(XMLReaderData& D); // Does what it says ;-)
  161. void runEndXMLerators(XMLReaderData& D); // Does what it says ;-)
  162. public:
  163. XMLReaderElement(const char* Name); // Must be constructed with a name
  164. XMLReaderElement(const std::string Name); // either c string or c++ string.
  165. XMLReaderElement(const char* Name, XMLReaderElement& Parent); // Sub-elements are constructed with a
  166. XMLReaderElement(const std::string Name, XMLReaderElement& Parent); // parrent.
  167. // Upon desctruction an element will delete all subordinate objects:
  168. // * All sub element objects.
  169. // * All attribute objects.
  170. // * All mnemonic objects.
  171. // * All translator objects.
  172. // It is important to use new when passing one of these objects to an
  173. // element or attribute to prevent problems with the delete operation.
  174. // NORMALLY these things would be created using factory methods on the
  175. // element and attribute objects themselves - so be careful.
  176. // It will not delete XMLerators - they must
  177. // be deleted elsewhere because they may have been
  178. // re-used and this element wouldn't know about it ;-)
  179. ~XMLReaderElement(); // The descrutor clears and deletes all!
  180. // Elements can be probed for some simple, useful things.
  181. std::string Name(); // Get the name of this element.
  182. XMLReaderElement& Parent(); // Get the parent of this element.
  183. XMLReaderElement& Parent(XMLReaderElement& newParent); // Set the parent of this element.
  184. // Note - if there is no parent (an element is the root) then it will
  185. // return a reference to itself when Parent() is called.
  186. int Line(); // Get the last line number.
  187. int Index(); // Get the last data position.
  188. int Length(); // Get the last length.
  189. // Elements can contain either data or sub-elements.
  190. XMLReaderElement& Element(const char* Name); // Add a new sub element by c string name.
  191. XMLReaderElement& Element(const std::string Name); // Add a new sub element by c++ string name.
  192. //// Mapping element factory methods for convenience.
  193. //// Root-Node elements are _usually_ empty and without attributes in xml
  194. //// so we don't make any of that type of convenience constructor here.
  195. // char* versions
  196. XMLReaderElement& Element( // Mapping factory for convenience,
  197. const char* Name, // requires a name, of course,
  198. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  199. XMLReaderElement& Element( // Mapping factory for convenience,
  200. const char* Name, // requires a name, of course,
  201. std::string& x, std::string init = std::string("")); // Map to a string.
  202. XMLReaderElement& Element( // Mapping factory for convenience,
  203. const char* Name, // requires a name, of course,
  204. int& x, int init = 0, int radix = 0); // Map to an int.
  205. XMLReaderElement& Element( // Mapping factory for convenience,
  206. const char* Name, // requires a name, of course,
  207. double& x, double init = 0.0); // Map to a double.
  208. XMLReaderElement& Element( // Mapping factory for convenience,
  209. const char* Name, // requires a name, of course,
  210. bool& x, bool init = false); // Map to a boolean.
  211. // string versions
  212. XMLReaderElement& Element( // Mapping factory for convenience,
  213. const std::string Name, // requires a name, of course,
  214. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  215. XMLReaderElement& Element( // Mapping factory for convenience,
  216. const std::string Name, // requires a name, of course,
  217. std::string& x, std::string init = std::string("")); // Map to a string.
  218. XMLReaderElement& Element( // Mapping factory for convenience,
  219. const std::string Name, // requires a name, of course,
  220. int& x, int init = 0, int radix = 0); // Map to an int.
  221. XMLReaderElement& Element( // Mapping factory for convenience,
  222. const std::string Name, // requires a name, of course,
  223. double& x, double init = 0.0); // Map to a double.
  224. XMLReaderElement& Element( // Mapping factory for convenience,
  225. const std::string Name, // requires a name, of course,
  226. bool& x, bool init = false); // Map to a boolean.
  227. XMLReaderElement& RawData( // Copy entire element to
  228. std::string &Element); // a string.
  229. XMLReaderElement& RawIndex( // Copy indices of the entire
  230. int &Index, int &Endex); // element.
  231. // End methods for heading back up the tree at the end of an element.
  232. class EndNameDoesNotMatch {}; // Throw when End(name) doesn't match.
  233. XMLReaderElement& End(); // Return this element's parent.
  234. XMLReaderElement& End(const char* Name); // Check the name and return the parent
  235. XMLReaderElement& End(const std::string Name); // if the name is correct - or throw!
  236. // Elements can have attributes.
  237. XMLReaderAttribute& Attribute(const char* Name); // Add an attribute using a cstring.
  238. XMLReaderAttribute& Attribute(const std::string Name); // Add an attribute using a c++ string.
  239. //// Mapping Attribute factory methods for convenience.
  240. // char* versions
  241. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  242. const char* Name, // requires a name, of course,
  243. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  244. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  245. const char* Name, // requires a name, of course,
  246. std::string& x, std::string init = std::string("")); // Map to a string.
  247. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  248. const char* Name, // requires a name, of course,
  249. int& x, int init = 0, int radix = 0); // Map to an int.
  250. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  251. const char* Name, // requires a name, of course,
  252. double& x, double init = 0.0); // Map to a double.
  253. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  254. const char* Name, // requires a name, of course,
  255. bool& x, bool init = false); // Map to a boolean.
  256. // string versions
  257. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  258. const std::string Name, // requires a name, of course,
  259. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  260. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  261. const std::string Name, // requires a name, of course,
  262. std::string& x, std::string init = std::string("")); // Map to a string.
  263. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  264. const std::string Name, // requires a name, of course,
  265. int& x, int init = 0, int radix = 0); // Map to an int.
  266. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  267. const std::string Name, // requires a name, of course,
  268. double& x, double init = 0.0); // Map to a double.
  269. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  270. const std::string Name, // requires a name, of course,
  271. bool& x, bool init = false); // Map to a boolean.
  272. // Elements can Initialize() at each Interpret() call.
  273. XMLReaderElement& setInitOnInterpret(); // Set the init on interpret flag.
  274. // Elements can call external functions to aid in special operations
  275. // such as building lists.
  276. XMLReaderElement& atStartCall(XMLerator& Functor); // Add an atStart call-back to this element.
  277. XMLReaderElement& atEndCall(XMLerator& Functor); // Add an atEnd call-back to this element.
  278. // Extracting data from the element's contents is done with
  279. // translators. A good set of primatives are built in, but the user
  280. // can also make their own. If an Element is mapped to more than
  281. // one then they are all called once the element's contents are
  282. // collected. A translator takes the data provided by the element,
  283. // converts it into the expected type, and sets one or more variables
  284. // to the converted value. Usually - just one variable.
  285. XMLReaderElement& mapTo(XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  286. XMLReaderElement& mapTo(std::string& x, std::string init = std::string("")); // Map to a string.
  287. XMLReaderElement& mapTo(int& x, int init = 0, int radix = 0); // Map to an int.
  288. XMLReaderElement& mapTo(double& x, double init = 0.0); // Map to a double.
  289. XMLReaderElement& mapTo(bool& x, bool init = false); // Map to a boolean.
  290. // An Element's contents may use some special mnemonics to make a
  291. // XMLReader easier to understand and less error prone. When the
  292. // contents match a mnemnoic then the translation of the mnemonic is
  293. // passed to the Translators instead of the raw contents.
  294. XMLReaderElement& Mnemonic(const char* name, const char* value); // Add a mnemonic using c strings.
  295. XMLReaderElement& Mnemonic(const char* name, const std::string value); // Add a mnemonic using c & c++ strings.
  296. XMLReaderElement& Mnemonic(const std::string name, const char* value); // Add a mnemonic using c++ & c strings.
  297. XMLReaderElement& Mnemonic(const std::string name, const std::string value); // Add a mnemonic using c++ strings.
  298. // The way data gets into an element tree is that it is Interpret()ed
  299. // recursively. The data is loaded into a XMLReaderData object which
  300. // is passed to the top Element. That element interpretes the data, moves
  301. // the interpretation pointers, and passes the data on to it's subordinate
  302. // elements in turn. They do the same recursively. When the last sub -
  303. // element has had it's way with the data, the interpretation process is
  304. // complete. The XMLReaderData object will contain the original data
  305. // and a log of anything that happened during the interpretation process.
  306. //
  307. // Each time an element is asked to Interpret() data, it calls any atStart
  308. // configurators, translates any attributes, then either translates it's
  309. // contents or passes the data to it's children, then calls any atEnd
  310. // configurators.
  311. //
  312. // To ensure that the correct default values are used the Initialize() is
  313. // always called on all internal attributes and elements before any data is
  314. // interpreted. To prevent this from being inefficient, a boolean flag is
  315. // kept in each element to keep track of whether it is clean and if it is
  316. // then the call to Initialize will simply return (skipping subordinate
  317. // elements along the way).
  318. //
  319. // Interpret returns true if this object found itself at the current
  320. // Data.Index and false if not. This helps keep the recursive parsing
  321. // code simpler ;-)
  322. void initialize(); // Reset all translators to defaults.
  323. void notifyDirty(); // Set dirty (if translators change).
  324. bool interpret(XMLReaderData& Data); // (re) Interpret this data.
  325. };
  326. //// XMLReader Attribute ///////////////////////////////////////////////////
  327. //
  328. // Attributes translate directly to well formed xml attributes (within the
  329. // start tag of an element).
  330. class XMLReaderAttribute {
  331. private:
  332. std::string myName; // Elements have a name.
  333. XMLReaderElement& myParent; // They may have a parrent.
  334. std::list<XMLReaderMnemonic*> myMnemonics; // They may have a list of mnemonics.
  335. std::list<XMLReaderTranslator*> myTranslators; // They may have a list of translators.
  336. int myLine; // Last line number I was seen on.
  337. int myIndex; // Last char position I was seen on.
  338. int myLength; // Last segment length.
  339. public:
  340. XMLReaderAttribute(const char* Name, XMLReaderElement& Parent); // Sub-elements are constructed with a
  341. XMLReaderAttribute(const std::string Name, XMLReaderElement& Parent); // parrent.
  342. // Attributes delete their Mnemonics and Translators when they go.
  343. // See Elements for similar warnings about objects provided to
  344. // this object... you must use new to be safe, or better yet - stick to
  345. // the built in factory methods ;-)
  346. ~XMLReaderAttribute(); // Crush, Kill, Destroy!
  347. // Attributes can be probed for some simple, useful things.
  348. std::string Name(); // Get the name of this attribute.
  349. XMLReaderElement& Parent(); // Get the parent of this attribute.
  350. int Line(); // Get the last line number.
  351. int Index(); // Get the last data position.
  352. int Length(); // Get the last length.
  353. void notifyDirty(); // Attributes use this when they change.
  354. // For convenience in building configurations, an Attribute offers
  355. // some call-through methods to it's parrent Element. This allows for
  356. // clear, concise .method() coding that mimics an outline of the
  357. // configuration structure.
  358. //// For switching back to the parent element and adding new sub-elements.
  359. XMLReaderElement& Element(const char* Name); // Add a new sub element by c string name.
  360. XMLReaderElement& Element(const std::string Name); // Add a new sub element by c++ string name.
  361. //// Mapping element factory methods for convenience.
  362. //// Root-Node elements are _usually_ empty and without attributes in xml
  363. //// so we don't make any of that type of convenience constructor here.
  364. // char* versions
  365. XMLReaderElement& Element( // Mapping factory for convenience,
  366. const char* Name, // requires a name, of course,
  367. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  368. XMLReaderElement& Element( // Mapping factory for convenience,
  369. const char* Name, // requires a name, of course,
  370. std::string& x, std::string init = std::string("")); // Map to a string.
  371. XMLReaderElement& Element( // Mapping factory for convenience,
  372. const char* Name, // requires a name, of course,
  373. int& x, int init = 0, int radix = 0); // Map to an int.
  374. XMLReaderElement& Element( // Mapping factory for convenience,
  375. const char* Name, // requires a name, of course,
  376. double& x, double init = 0.0); // Map to a double.
  377. XMLReaderElement& Element( // Mapping factory for convenience,
  378. const char* Name, // requires a name, of course,
  379. bool& x, bool init = false); // Map to a boolean.
  380. // string versions
  381. XMLReaderElement& Element( // Mapping factory for convenience,
  382. const std::string Name, // requires a name, of course,
  383. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  384. XMLReaderElement& Element( // Mapping factory for convenience,
  385. const std::string Name, // requires a name, of course,
  386. std::string& x, std::string init = std::string("")); // Map to a string.
  387. XMLReaderElement& Element( // Mapping factory for convenience,
  388. const std::string Name, // requires a name, of course,
  389. int& x, int init = 0, int radix = 0); // Map to an int.
  390. XMLReaderElement& Element( // Mapping factory for convenience,
  391. const std::string Name, // requires a name, of course,
  392. double& x, double init = 0.0); // Map to a double.
  393. XMLReaderElement& Element( // Mapping factory for convenience,
  394. const std::string Name, // requires a name, of course,
  395. bool& x, bool init = false); // Map to a boolean.
  396. // End methods for heading back up the tree at the end of an element.
  397. XMLReaderElement& End(); // Return this element's parent.
  398. XMLReaderElement& End(const char* Name); // Check the name and return the parent
  399. XMLReaderElement& End(const std::string Name); // if the name is correct - or throw!
  400. //// For adding new attributes to the parent element.
  401. XMLReaderAttribute& Attribute(const char* Name); // Add an attribute using a cstring.
  402. XMLReaderAttribute& Attribute(const std::string Name); // Add an attribute using a c++ string.
  403. //// Mapping Attribute factory methods for convenience.
  404. // char* versions
  405. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  406. const char* Name, // requires a name, of course,
  407. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  408. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  409. const char* Name, // requires a name, of course,
  410. std::string& x, std::string init = std::string("")); // Map to a string.
  411. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  412. const char* Name, // requires a name, of course,
  413. int& x, int init = 0, int radix = 0); // Map to an int.
  414. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  415. const char* Name, // requires a name, of course,
  416. double& x, double init = 0.0); // Map to a double.
  417. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  418. const char* Name, // requires a name, of course,
  419. bool& x, bool init = false); // Map to a boolean.
  420. // string versions
  421. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  422. const std::string Name, // requires a name, of course,
  423. XMLReaderTranslator& newTranslator); // Add a Translator to this element.
  424. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  425. const std::string Name, // requires a name, of course,
  426. std::string& x, std::string init = std::string("")); // Map to a string.
  427. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  428. const std::string Name, // requires a name, of course,
  429. int& x, int init = 0, int radix = 0); // Map to an int.
  430. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  431. const std::string Name, // requires a name, of course,
  432. double& x, double init = 0.0); // Map to a double.
  433. XMLReaderAttribute& Attribute( // Mapping factory for convenience,
  434. const std::string Name, // requires a name, of course,
  435. bool& x, bool init = false); // Map to a boolean.
  436. //// Set Init On Interprete for the parent element.
  437. XMLReaderElement& setInitOnInterpret(); // Set the init on interpret flag.
  438. //// For adding configurators to the parent element.
  439. XMLReaderElement& atStartCall(XMLerator& Functor); // Add an atStart call-back to this element.
  440. XMLReaderElement& atEndCall(XMLerator& Functor); // Add an atEnd call-back to this element.
  441. // Of course, the most useful thing about attributes is that they can
  442. // be mapped to variables using translators. The same as those that
  443. // apply to the parent element's contents. Here they are for use on this
  444. // attribute.
  445. XMLReaderAttribute& mapTo(XMLReaderTranslator& newTranslator); // Add a Translator to this attribute.
  446. XMLReaderAttribute& mapTo(std::string& x, std::string init = std::string("")); // Map to a string.
  447. XMLReaderAttribute& mapTo(int& x, int init, int radix = 0); // Map to an int.
  448. XMLReaderAttribute& mapTo(double& x, double init = 0.0); // Map to a double.
  449. XMLReaderAttribute& mapTo(bool& x, bool init = false); // Map to a boolean.
  450. // Attributes can have mnemonics just like elements.
  451. XMLReaderAttribute& Mnemonic(const char* name, const char* value); // Add a mnemonic using a c string.
  452. XMLReaderAttribute& Mnemonic(const char* name, const std::string value); // Add a mnemonic using c & c++ strings.
  453. XMLReaderAttribute& Mnemonic(const std::string name, const char* value); // Add a mnemonic using c++ & c strings.
  454. XMLReaderAttribute& Mnemonic(const std::string name, const std::string value); // Add a mnemonic using a c++ string.
  455. // Attributes participate in the Interprete() task just like elements.
  456. void initialize(); // Reset all translators to defaults.
  457. bool interpret(XMLReaderData& Data); // (re) Interpret this data.
  458. };
  459. //// XMLReader Data ////////////////////////////////////////////////////////
  460. //
  461. // A XMLReaderData object holds on to the configuration source data and
  462. // provideds a place to log any information about how the configuration was
  463. // interpreted. It also creates and destroys a handy char[] to contain the
  464. // data. To make this beastie easier to handle, we use the Named Constructor
  465. // Idiom and hide the true constructor in the private section.
  466. class XMLReaderData { // XMLReader Data Source
  467. private:
  468. char* myDataBuffer; // The actual data buffer.
  469. int myBufferSize; // Size of the current buffer.
  470. int myIndex; // The current interpretation index.
  471. int myLine; // Current line number.
  472. public:
  473. XMLReaderData(const char* FileName); // Constructor from c string file name.
  474. XMLReaderData(const std::string FileName); // Constructor from c++ string file name.
  475. XMLReaderData(const char* Data, int Length); // Raw constructor from text buffer.
  476. ~XMLReaderData(); // Destroys the internal buffer etc.
  477. char Data(int Index); // Returns char from Data[Index]
  478. int Index(); // Reads the current Index.
  479. int Index(int i); // Changes the current Index.
  480. int Line(); // Reads the current Line number.
  481. int addNewLines(int Count); // Increments the Line number.
  482. std::string extract(int Index, int Endex) const ; // Return substring of buffer.
  483. std::stringstream Log; // Convenient Interpret log.
  484. };
  485. //// XMLReader Translator //////////////////////////////////////////////////
  486. //
  487. // A Translator converts the contents provided to it in string form into some
  488. // other data type. The object here is a prototype for that, followed by a
  489. // collection of the basic translators used for built-in mapTo()s.
  490. class XMLReaderTranslator { // Translators exist
  491. public:
  492. virtual ~XMLReaderTranslator(){}; // Stop No Virt Dtor warnings.
  493. virtual void translate(const char* Value) = 0; // Pure virtual translator.
  494. virtual void initialize() = 0; // Pure virtual initializer.
  495. };
  496. class StringTranslator : public XMLReaderTranslator {
  497. private:
  498. std::string& myVariable; // Variable to map.
  499. std::string myInitializer; // Initial/Default value.
  500. public:
  501. StringTranslator( // Construct this with
  502. std::string& Variable, // the variable to map,
  503. std::string Inititializer); // and the default value.
  504. void translate(const char* Value); // Provide a translation method.
  505. void initialize(); // Provide an initialization method.
  506. };
  507. class IntegerTranslator : public XMLReaderTranslator {
  508. private:
  509. int& myVariable; // Variable to map.
  510. int myInitializer; // Initial/Default value.
  511. int myRadix; // Radix for strtol()
  512. public:
  513. IntegerTranslator( // Construct this with
  514. int& Variable, // the variable to map,
  515. int Inititializer, // and the default value.
  516. int Radix); // For this one we also need a Radix.
  517. void translate(const char* Value); // Provide a translation method.
  518. void initialize(); // Provide an initialization method.
  519. };
  520. class DoubleTranslator : public XMLReaderTranslator {
  521. private:
  522. double& myVariable; // Variable to map.
  523. double myInitializer; // Initial/Default value.
  524. public:
  525. DoubleTranslator( // Construct this with
  526. double& Variable, // the variable to map,
  527. double Inititializer); // and the default value.
  528. void translate(const char* Value); // Provide a translation method.
  529. void initialize(); // Provide an initialization method.
  530. };
  531. class BoolTranslator : public XMLReaderTranslator {
  532. private:
  533. bool& myVariable; // Variable to map.
  534. bool myInitializer; // Initial/Default value.
  535. public:
  536. BoolTranslator( // Construct this with
  537. bool& Variable, // the variable to map,
  538. bool Inititializer); // and the default value.
  539. void translate(const char* Value); // Provide a translation method.
  540. void initialize(); // Provide an initialization method.
  541. };
  542. //// XMLReader Mnemonic ////////////////////////////////////////////////////
  543. //
  544. // A Mnemonic allows the actual contents of an element or attribute to be
  545. // exchanged for a different "named" value to help eliminate "magic numbers"
  546. // and "secret codes" from configurations. One way this might be used is to
  547. // map an enumeration to the appropriate integer values, or things like YES and
  548. // NO to boolean true and false (respectively) when turning on/off program
  549. // options.
  550. class XMLReaderMnemonic { // Mnemonics
  551. private:
  552. std::string myName; // What is the Mnemonic?
  553. std::string myValue; // What is the translation?
  554. public:
  555. XMLReaderMnemonic(std::string Name, std::string Value); // To make one, provide both parts.
  556. bool test(std::string Name); // Test to see if this Mnemonic matches.
  557. std::string Value(); // If it does then we will need it's value.
  558. };
  559. //// XMLerator //////////////////////////////////////////////////////////////
  560. //
  561. // An XMLerator is a "functor" or "closure" or "callback" that can be used to
  562. // support sophisticated interpretation options. The most basic and necessary
  563. // of these is support for list building. Consider an object created to contain
  564. // a list of records where each record might be represented as a collection of
  565. // attributes and elements. The object would have data elements mapped to the
  566. // attributes and elements in the configuration and then control elements which
  567. // are functors for initializing the list and storing new entries as they are
  568. // completed. The object here is a pure virtual prototype.
  569. class XMLerator { // XMLerators exist
  570. public:
  571. virtual void operator()(XMLReaderElement& E, XMLReaderData& D) = 0; // Pure virtual configurator.
  572. virtual ~XMLerator() {} // Virtual dtor keeps warnings away.
  573. };
  574. //// Include our inline methods ////////////////////////////////////////////////
  575. #include "XMLReader.inline.hpp"
  576. //// Utilities /////////////////////////////////////////////////////////////////
  577. // SetTrueOnComplete XMLerator //////////////////////////////////////////////
  578. class XMLeratorSetTrueOnComplete : public XMLerator { // XMLerator set's a boolean true.
  579. private:
  580. bool* myBoolean; // The boolean to set.
  581. public:
  582. XMLeratorSetTrueOnComplete(); // Must init to NULL for safety.
  583. void setup(bool& Target); // Link to the target boolean.
  584. void operator()(XMLReaderElement& E, XMLReaderData& D); // Handle the operation.
  585. };
  586. }
  587. #endif
  588. // End Of Include Only Once