00001
00002
00003
00004
00005
00006
00007
00008 #ifndef BXMLOutput_HPP
00009 #define BXMLOutput_HPP
00010
00011 #include "xmltk.h"
00012 #include "ISAXClient.hpp"
00013
00014 extern XTOKEN xmllabel;
00015
00016 class BXMLOutput : public ISAXClient
00017 {
00018
00019 bool IUnknownCL_Init(IUnknownCL *punk, IStream* pws, bool bForStdout)
00020 {
00021 ITSAX2Bin *p2bin;
00022 bool bRet = punk->QueryInterface(&IID_ITSAX2Bin, (void**)&p2bin);
00023
00024 if (bRet)
00025 {
00026 bRet = p2bin->Init(pws, bForStdout);
00027 p2bin->Release();
00028 }
00029
00030 return bRet;
00031 }
00032
00033
00034 void _CopyStringToBuffer(char *psz, int cch, bool fTrimWhitespace = false)
00035 {
00036 unsigned int cchBuf;
00037
00038 m_psz = m_szBuf;
00039 cchBuf = ARRAYSIZE(m_szBuf);
00040
00041 if ((unsigned int)cch >= ARRAYSIZE(m_szBuf)-1)
00042 {
00043
00044
00045
00046 if (m_pszBig)
00047 free(m_pszBig);
00048
00049 m_pszBig = (char*)calloc(cch+1, sizeof(char));
00050 if (m_pszBig)
00051 {
00052 m_psz = m_pszBig;
00053 cchBuf = cch;
00054 }
00055 else
00056 {
00057 myassert(0);
00058 fprintf(stderr, "warning: low memory, data lost\n");
00059 }
00060 }
00061
00062 m_cch = min((unsigned int)cch, cchBuf);
00063 strncpy(m_psz, psz, m_cch);
00064 m_psz[m_cch] = 0;
00065 }
00066
00067
00068 char m_szBuf[4096];
00069 char *m_pszBig;
00070 char *m_psz;
00071 int m_cch;
00072
00073 ITSAXContentHandler *m_pch;
00074 XTOKEN m_xtAttribute;
00075
00076 public:
00077
00078
00079 BXMLOutput()
00080 {
00081 IFileStream* pfs;
00082 CreateFileStream(&IID_IFileStream, (void**)&pfs);
00083
00084 if (pfs)
00085 {
00086
00087 pfs->SetFile(stdout);
00088 CreateTSAX2Bin(&IID_ITSAXContentHandler, (void**)&m_pch);
00089
00090 if (m_pch)
00091 {
00092 IUnknownCL_Init(m_pch, pfs, true);
00093 }
00094 pfs->Release();
00095 }
00096 m_pch->AddRef();
00097 m_pch->startDocument();
00098 }
00099
00100
00101 virtual ~BXMLOutput()
00102 {
00103 m_pch->endDocument();
00104 m_pch->Release();
00105 }
00106
00107 #ifndef XDEMILL_NOOUTPUT
00108
00109 virtual void startElement(char *str,int len)
00110 {
00111
00112 _CopyStringToBuffer(str, len);
00113 XTOKEN xt = g_ptt->XTOKENFromStr(m_psz, XST_ELEMENT);
00114 xmllabel = xt;
00115 m_pch->startElement(xt);
00116 }
00117
00118 virtual void endElement(char *str,int len)
00119 {
00120
00121
00122 _CopyStringToBuffer(str, len);
00123 XTOKEN xt = g_ptt->XTOKENFromStr(m_psz, XST_ELEMENT);
00124 m_pch->endElement(xt);
00125 }
00126
00127 virtual void endEmptyElement()
00128 {
00129
00130
00131
00132 m_pch->endElement(xmllabel);
00133 }
00134
00135 virtual void startAttribute(char *str,int len)
00136 {
00137
00138 _CopyStringToBuffer(str, len);
00139 m_xtAttribute = g_ptt->XTOKENFromStr(m_psz, XST_ATTRIBUTE);
00140
00141
00142 _CopyStringToBuffer(str, len);
00143 m_pch->attribute(m_xtAttribute, m_psz, m_cch);
00144 }
00145
00146 virtual void endAttribute(char *str=NULL,int len=0)
00147 {
00148
00149 }
00150
00151 virtual void characters(char *str,int len)
00152 {
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 len = max(0,len);
00169
00170 _CopyStringToBuffer(str, len);
00171
00172 if (m_cch > 0)
00173 {
00174 m_pch->characters(m_psz, m_cch);
00175 }
00176 }
00177
00178 virtual void whitespaces(char *str,int len)
00179 {
00180
00181 }
00182
00183 virtual void attribWhitespaces(char *str,int len)
00184 {
00185
00186 }
00187
00188 #else
00189 virtual void startElement(char *str,int len);
00190 virtual void endElement(char *str,int len);
00191 virtual void endEmptyElement();
00192 virtual void startAttribute(char *str,int len);
00193 virtual void endAttribute(char *str=NULL,int len=0);
00194 virtual void characters(char *str,int len);
00195 virtual void whitespaces(char *str,int len);
00196 virtual void attribWhitespaces(char *str,int len);
00197 #endif
00198
00199
00200 };
00201
00202 #endif