00001 #ifndef _CTBxmlIStream_HXX
00002 #define _CTBxmlIStream_HXX 1
00003
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <strstream.h>
00017
00018 #include "CTBgrab.hxx"
00019 #include "CTBpair.hxx"
00020 #include "CTBstring.hxx"
00021 #include "CTBtime.hxx"
00022 #include "CTBvector.hxx"
00023 #include "CTBxmlLexer.hxx"
00024
00025 class CTBxmlToken {
00026
00027 friend class CTBxmlIStream;
00028
00029 public:
00030 enum type { invalid = -1,
00031 fail,
00032 eof,
00033 data,
00034 starttag,
00035 endtag,
00036 emptytag,
00037 comment,
00038 xml,
00039 pi
00040 };
00041
00042 explicit CTBxmlToken(int i_type = CTBxmlToken::invalid);
00043 CTBxmlToken(const CTBxmlToken& rhs);
00044 CTBxmlToken(CTBxmlToken& rhs, bool b_grab);
00045 ~CTBxmlToken();
00046
00047 int Type() const;
00048 const CTBstring& Text() const;
00049 const CTBstring& Name() const;
00050 const CTBstring& Value() const;
00051
00052 CTBint NAttribute() const;
00053 const CTBstring& AttributeName(CTBint i_ind) const;
00054 const CTBstring& AttributeValue(CTBint i_ind) const;
00055
00056 void Clear();
00057 void Grab(CTBxmlToken& rhs);
00058
00059 void ToStream(ostream& os) const;
00060
00061 void Dump(int i_indent=0, ostream& os=cout,
00062 const char* p_text=0) const;
00063
00064 bool operator!() const;
00065 operator void* () const;
00066
00067 CTBxmlToken& operator=(const CTBxmlToken& rhs);
00068
00069 private:
00070
00071 typedef CTBpair<CTBstring,CTBstring> pair_ss_t;
00072
00073 int mi_type;
00074 CTBstring m_text;
00075 CTBstring m_name;
00076 CTBstring m_value;
00077 CTBvector<pair_ss_t> m_attribute;
00078 };
00079
00080 ostream& operator<<(ostream& os, const CTBxmlToken& obj);
00081
00082 class CTBgrab<CTBxmlToken> : public CTBgrabable<CTBxmlToken> {};
00083
00084
00085
00086 class CTBxmlOpen;
00087 class CTBxmlClose;
00088
00089 class CTBxmlIStream {
00090 public:
00091 CTBxmlIStream();
00092 explicit CTBxmlIStream(istream* p_is);
00093 explicit CTBxmlIStream(const char* c_filename, int i_mode=ios::in,
00094 int i_prot=0664);
00095
00096 virtual ~CTBxmlIStream();
00097
00098 void Attach(istream* p_is);
00099 void Open(const char* c_filename, int i_mode=ios::in,
00100 int i_prot=0664);
00101 void Close();
00102
00103 bool Good() const;
00104 bool Bad() const;
00105 bool Fail() const;
00106 bool Eof() const;
00107 bool Eod() const;
00108
00109 void TrimWhite(bool b_trim);
00110 void DropWhite(bool b_drop);
00111
00112 bool TrimWhite() const;
00113 bool DropWhite() const;
00114
00115 void Get(CTBxmlToken& token);
00116 void GetTagOrData(CTBxmlToken& token);
00117 void GetTagOrNonWhiteData(CTBxmlToken& token);
00118
00119 void Unget(CTBxmlToken& token);
00120
00121 const CTBxmlToken& Peek();
00122 const CTBxmlToken& PeekTagOrData();
00123 const CTBxmlToken& PeekTagOrNonWhiteData();
00124
00125 bool NextNotEnd();
00126
00127 bool CheckElement(const char* c_name);
00128
00129 void GetTaggedValue(const char* c_name, int& i_value);
00130 void GetTaggedValue(const char* c_name, float& f_value);
00131 void GetTaggedValue(const char* c_name, double& d_value);
00132 void GetTaggedValue(const char* c_name, CTBstring& value);
00133 void GetTaggedValue(const char* c_name, CTBtime& value);
00134
00135 void OpenElement(const char* c_name = 0);
00136 void CloseElement();
00137 void DropElement();
00138
00139 istream* GetDataStream();
00140
00141 void ToStream(ostream& os) const;
00142
00143 void Dump(int i_indent=0, ostream& os=cout,
00144 const char* p_text=0) const;
00145
00146 bool operator!() const;
00147
00148 operator void* () const;
00149
00150 CTBxmlIStream& operator>>(CTBxmlToken& token);
00151
00152 CTBxmlIStream& operator>>(const CTBxmlOpen& manip);
00153 CTBxmlIStream& operator>>(const CTBxmlClose& manip);
00154
00155 private:
00156 void GetDataFromLexer(CTBxmlToken& token);
00157 void GetTagFromLexer(CTBxmlToken& token);
00158
00159 void SetFail(const char* c_message);
00160
00161 const CTBxmlToken& FailToken() const;
00162
00163 CTBstring ConvertCharRef(const CTBstring& cref);
00164 CTBstring ConvertEntityRef(const CTBstring& eref);
00165
00166 int GetLexerToken(int i_lexmode, CTBxmlToken& token);
00167
00168 private:
00169 istream* mp_is;
00170 bool mb_attached;
00171 bool mb_trimwhite;
00172 bool mb_dropwhite;
00173 bool mb_fail;
00174 bool mb_bad;
00175 bool mb_eof;
00176 istrstream* mp_iss;
00177 CTBstring m_data;
00178 CTBvector<CTBstring> m_tagstack;
00179 CTBint mi_taglevel;
00180 CTBxmlToken m_untoken;
00181 CTBxmlToken m_token;
00182 CTBxmlLexer m_lex;
00183 CTBxmlLexerToken m_lextoken;
00184 };
00185
00186 template <class T>
00187 CTBxmlIStream& operator>>(CTBxmlIStream& xis, T& obj);
00188
00189 ostream& operator<<(ostream& os, const CTBxmlIStream& obj);
00190
00191
00192
00193 class CTBxmlOpen {
00194 public:
00195 explicit CTBxmlOpen(const char* c_name = 0);
00196 const char* Name() const;
00197 private:
00198 const char* mc_name;
00199 };
00200
00201 class CTBxmlClose {
00202 public:
00203 explicit CTBxmlClose();
00204 private:
00205 };
00206
00207 #include "CTBxmlIStream.icc"
00208
00209 #endif