Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

BXMLOutput.hpp

Go to the documentation of this file.
00001 // This class will handle binary format of xml file.
00002 // When you decompress the file with xdemill -b, you call this BXMLOutput class.
00003 // This will call sax2tsax obj. and this will call tsax2bin obj. to create binary format file.
00004 //
00005 // Author : Jason Kim (jasonkim@cs.washington.edu)
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         // Initialize
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         // Manage the buffer to decompress file
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             // try to alloc a large enough block
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         // Private method
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         // Constructor for this class
00079         BXMLOutput()
00080         {
00081                 IFileStream* pfs;
00082                 CreateFileStream(&IID_IFileStream, (void**)&pfs);
00083 
00084                 if (pfs)
00085                 {
00086                         // Want to save this output as stdout
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         // Destructor for this class
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                 // HandleStartLabel
00112                 _CopyStringToBuffer(str, len);
00113                 XTOKEN xt = g_ptt->XTOKENFromStr(m_psz, XST_ELEMENT); // "bib"
00114                 xmllabel = xt;
00115                 m_pch->startElement(xt);
00116         }
00117 
00118         virtual void endElement(char *str,int len)
00119         {
00120                 // Handles an end tag
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                 // Handles an end tag
00130                 // Need more study for this case
00131                 // IMPLEMENT LATER
00132                 m_pch->endElement(xmllabel);
00133         }
00134 
00135         virtual void startAttribute(char *str,int len)
00136         {
00137                 // Handles a given attribute name
00138                 _CopyStringToBuffer(str, len);
00139                 m_xtAttribute = g_ptt->XTOKENFromStr(m_psz, XST_ATTRIBUTE);
00140 
00141                 // Handles an attribute value
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                 // Nothing...
00149         }
00150 
00151         virtual void characters(char *str,int len)
00152         {
00153                 // Handles a piece of text.
00154                 // 'leftwslen' and 'rightwslen' specify how many white spaces (' ', '\n', ...)
00155                 // are on the left and right end of the text.
00156                 // If 'iscont=1', then 'rightwslen' must be zero and for the text piece
00157                 // coming afterwards, 'leftwslen' must be zero.
00158                 // It is also possible that 'len=leftwslen=rightwslen'
00159 
00160                 //
00161                 // hide the leading and trailing whitespace from the TSAX client
00162                 //
00163 
00164                 // Have to check int leftwslen,int rightwslen
00165                 // str += leftwslen;
00166                 // len = len - (leftwslen + rightwslen);
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                 // Nothing...
00181         }
00182 
00183         virtual void attribWhitespaces(char *str,int len)
00184         {
00185                 // Nothing...
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 }; // End of class
00201 
00202 #endif

Generated on Sat Dec 22 16:01:48 2001 for XMILLforBinaryFormat by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001