#ifndef _utl_Reader_h_ #define _utl_Reader_h_ /*! \file Reader.h \brief Utility for reading data from XML files \author T. Paul \author P. Cattaneo \author D. Veberic \author J. Gonzalez \version $Id$ */ #include #include #include #include namespace utl { class ReaderStringInput; class ReaderErrorReporter; class Reader { public: enum EValidationType { eDTD, eSCHEMA, eNONE }; //! Constructor with validation options. Reader(const std::string& name, const EValidationType validationType = Reader::eNONE); //! Constructor from an input string (as opposed to file). EValidationType not (yet) used. Reader(const ReaderStringInput& inputString, const EValidationType validationType = Reader::eNONE); Reader(const Branch& branch) { if (branch.fDomNode && branch.fDomNode->getOwnerDocument()) { fTopBranch = Branch(branch.fDomNode->getOwnerDocument()->getDocumentElement(), branch.fOwner); fTopBranch.fOwner = branch.fOwner; } } std::string GetUri() const; //! Get the top Branch (represents same entity as document node) Branch GetTopBranch() const { return fTopBranch; } private: // Branch at the top of the document tree. Branch fTopBranch; // helper function for dumping out the XML tree (recursive), kept here just in case. // A function like this belongs in the BasicConstBranch class, not here. // void DumpMe(const Branch& b, const int indent = 2, const int indentIncrement = 2) const; // ------------------------------------------------------- // The following are all the static methods and variables // ------------------------------------------------------- enum EInputType { eFromFile = 0, // take XML input from file eFromMemBuf // take XML input from memory }; static ReaderErrorReporter* fgErrReporter; static bool fgInitialized; static void Initialize(); static Branch Parse(const std::string& input, const EValidationType validationType, const EInputType inputType); }; /** \class XStr \brief Class to help converting from std::string to XMLCh*. It handles allocation and deallocation of the XML array. */ class XStr { public: // Call the private transcoding method XStr(const std::string toTranscode) { fUnicodeForm = xercesc::XMLString::transcode(toTranscode.c_str()); } ~XStr() { if (fUnicodeForm) xercesc::XMLString::release(&fUnicodeForm); } const XMLCh* XmlString() const { return fUnicodeForm; } private: XStr(const XStr&); XStr& operator=(const XStr&); // This is the Unicode XMLCh format of the string. XMLCh* fUnicodeForm; }; /*! \class ReaderStringInput \brief This just defines a type which holds some character data to be parsed by the Reader. This class is for use in cases where the Reader should parse a string of XML information in memory instead of information in a file. It takes care of some of the pain of using the MemBufInputSource class of Xerces. */ class ReaderStringInput { public: ReaderStringInput() { } ReaderStringInput(const std::string& inputString) { fInputString = inputString; } ReaderStringInput(const std::ostringstream& inputStream) { fInputString = inputStream.str(); } ~ReaderStringInput() { } std::string GetInputString() const { return fInputString; } private: std::string fInputString; }; } #endif