1 /* "CodeWorker": a scripting language for parsing and generating text. 2 3 Copyright (C) 1996-1997, 1999-2002 C�dric Lemaire 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 19 To contact the author: codeworker@free.fr 20 */ 21 22 #ifndef _UtlXMLStream_h_ 23 #define _UtlXMLStream_h_ 24 25 #include <string> 26 #include <vector> 27 #include <fstream> 28 29 30 namespace CodeWorker { 31 class UtlDate; 32 33 class UtlXMLStream { 34 35 // common part 36 public: 37 38 // mode write default bec 39 UtlXMLStream(const std::string& sFileName, const bool bModeRead ); 40 UtlXMLStream(std::iostream& myStream); 41 42 virtual ~UtlXMLStream(); 43 getFileName()44 inline const std::string& getFileName() const { return _sFileName; } 45 virtual std::string normalizeAttributeName(const std::string& sName) const; 46 47 // write part 48 private: 49 std::ostream* _pOutputStream; 50 std::string _sIndentation; 51 bool _bOwnerOfFileStream; 52 53 public: UtlXMLStream(std::ostream & myStream)54 inline UtlXMLStream(std::ostream& myStream) : _pOutputStream(&myStream), _bOwnerOfFileStream(false) {} 55 getOutputStream()56 inline std::ostream& getOutputStream() const { return *_pOutputStream; } 57 58 virtual void writeStartTag(const std::string& sTag); 59 virtual void writeEndTag(const std::string& sTag); 60 virtual void writeTag(const std::string& sTag); 61 virtual void writeBeginningOfObject(const std::string& sTypeName); 62 virtual void writeEndOfObject(const std::string& sTypeName); 63 virtual void writeEndOfObject(); 64 virtual void writeAttribute(const std::string& sName, int iValue); 65 virtual void writeAttribute(const std::string& sName, long lValue); 66 virtual void writeAttribute(const std::string& sName, double dValue); 67 virtual void writeAttribute(const std::string& sName, const std::string& sValue); 68 virtual void writeAttribute(const std::string& sName, bool bValue); 69 virtual void writeAttribute(const std::string& sName, const UtlDate& myValue); 70 virtual void writeEndOfAttributes(); 71 virtual void writeArrayElement(int iValue); 72 virtual void writeArrayElement(double dValue); 73 virtual void writeArrayElement(const std::string& sValue); 74 virtual void writeArrayElement(bool bValue); 75 virtual void writeArrayElement(const UtlDate& myValue); 76 virtual void writeHashtableEntry(const std::string& sKey, const std::string& sValue); 77 virtual void writeHashtableEntry(const std::string& sKey, double dValue); 78 virtual void writeHashtableEntry(const std::string& sKey, const std::vector<double>& listOfValues); 79 virtual void writeHashtableEntry(const std::string& sKey, const std::vector<std::string>& listOfValues); 80 virtual void writeHashtableEntry(int iKey, int iValue); 81 virtual void writeBeginningOfAggregation(const std::string& sOwnerClass, const std::string& sName); 82 virtual void writeEndOfAggregation(const std::string& sOwnerClass, const std::string& sName); 83 virtual void writeBeginningOfAssociation(const std::string& sOwnerClass, const std::string& sName); 84 virtual void writeEndOfAssociation(const std::string& sOwnerClass, const std::string& sName); 85 virtual void writeObjectReference(const std::string& sTypeName, const std::string& /*sIDAttrName*/, const std::string& sIdentifier); 86 87 std::string convertToXMLText(const std::string& sText); 88 89 90 // Read part 91 private: 92 std::istream* _pInputStream; 93 std::string _sFileName; 94 95 public: 96 UtlXMLStream(std::istream& myStream); getInputStream()97 inline std::istream& getInputStream() const { return *_pInputStream; } 98 99 virtual bool readStartTag( std::string& sTag); 100 virtual bool readEndTag( std::string& sTag); 101 virtual bool readTag( std::string& sTag); 102 virtual bool readBeginningOfObject( std::string& sTypeName); 103 virtual bool readEndOfObject( std::string& sTypeName); 104 virtual bool readEndOfObject(); 105 virtual bool readAttribute( std::string& sName, int& iValue); 106 virtual bool readAttribute( std::string& sName, long& lValue); 107 virtual bool readAttribute( std::string& sName, double& dValue); 108 virtual bool readAttribute( std::string& sName, std::string& sValue); 109 virtual bool readAttribute( std::string& sName, bool& bValue); 110 virtual bool readAttribute( std::string& sName, UtlDate& myValue); 111 virtual bool readEndOfAttributes(); 112 virtual bool readArrayElement(int& iValue); 113 virtual bool readArrayElement(double& dValue); 114 virtual bool readArrayElement( std::string& sValue); 115 virtual bool readArrayElement(bool& bValue); 116 virtual bool readArrayElement( UtlDate& myValue); 117 virtual bool readHashtableEntry( std::string& sKey, std::string& sValue); 118 virtual bool readHashtableEntry( std::string& sKey, double& dValue); 119 virtual bool readHashtableEntry( std::string& sKey, std::vector<double>& listOfValues); 120 virtual bool readHashtableEntry( std::string& sKey, std::vector<std::string>& listOfValues); 121 virtual bool readBeginningOfAggregation( std::string& sOwnerClass, std::string& sName); 122 virtual bool readEndOfAggregation( std::string& sOwnerClass, std::string& sName); 123 virtual bool readBeginningOfAssociation( std::string& sOwnerClass, std::string& sName); 124 virtual bool readEndOfAssociation( std::string& sOwnerClass, std::string& sName); 125 virtual bool readObjectReference( std::string& sTypeName, std::string& sIDAttrName, std::string& sIdentifier); 126 127 128 129 private: 130 std::string convertXMLTextToClassicText(const std::string& sText); 131 132 bool readKeyOfHashTable(std::string& sKey); 133 bool readBeginningOfAttribute() ; 134 bool readEndOfAttribute() ; 135 bool readEndOfHashTable() ; 136 bool readName(std::string& sName) ; 137 bool readBeginOfArrayElement(std::string sType) ; 138 bool readEndOfArrayElement() ; 139 }; 140 } 141 142 #endif 143