Writer.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _XML2_B_WRITER_H
00014 #define _XML2_B_WRITER_H
00015 
00016 #include <support/Vector.h>
00017 #include <support/IByteStream.h>
00018 #include <xml/XMLOStr.h>
00019 
00020 #if _SUPPORTS_NAMESPACE
00021 namespace palmos {
00022 namespace xml {
00023 using namespace support;
00024 #endif
00025 
00026 // Used for writing.  Should be private XXX
00027 // =====================================================================
00028 enum {
00029     stfLeaf     = 0x00000001,
00030     stfCanAddWS = 0x00000002
00031 };
00032 
00033 // BOutputStream
00034 // =====================================================================
00035 class BOutputStream : public CXMLOStr {
00036 public:
00037                         BOutputStream(const sptr<IByteOutput>& stream, bool writeHeader=false);
00038     virtual             ~BOutputStream();
00039     
00040     virtual status_t    StartTag(SString &name, SValue &attributes);
00041     virtual status_t    EndTag(SString &name);
00042     virtual status_t    TextData(const char * data, int32_t size);
00043     virtual status_t    Comment(const char *data, int32_t size);
00044 
00045 private:
00046     void        Indent();
00047 
00048     sptr<IByteOutput>   m_stream;
00049     int32_t             m_depth;
00050     int32_t             m_lastPrettyDepth;
00051     bool                m_isLeaf:1;
00052     bool                m_openStartTag:1;
00053     uint8_t             m_reserved:6;
00054 };
00055 
00056 
00057 
00058 // Consider this an inverse parser
00059 class BWriter
00060 {
00061 public:
00062                         BWriter(const sptr<IByteOutput>& data, uint32_t formattingStyle);
00063     virtual             ~BWriter();
00064     
00065     // Stuff that goes only in DTDs
00066     //virtual status_t  BeginDoctype(const SString & elementName, const SString & systemID, const SString & publicID);
00067     //virtual status_t  EndDoctype();
00068     //virtual status_t  OnElementDecl(...);
00069     //virtual status_t  OnEntityDecl(...);
00070     
00071     // Stuff that goes only in the document part
00072     virtual status_t    StartTag(const SString &name, const SValue &attributes, uint32_t formattingHints=0);
00073     virtual status_t    EndTag(); // Will always do propper start-tag matching
00074     virtual status_t    TextData(const char * data, int32_t size);
00075     virtual status_t    CData(const char    * data, int32_t size);
00076     virtual status_t    WriteEscaped(const char * data, int32_t size);
00077     
00078     // Stuff that can go anywhere
00079     virtual status_t    Comment(const char *data, int32_t size);
00080     virtual status_t    ProcessingInstruction(const SString & target, const SString & data);
00081     
00082     // Formatting Styles
00083     enum
00084     {
00085         BALANCE_WHITESPACE      = 0x00000001,
00086         SKIP_DOCTYPE_OPENER     = 0x00000002
00087     };
00088     
00089     // Formatting Hints
00090     enum
00091     {
00092         NO_EXTRA_WHITESPACE     = 0x80000002
00093     };
00094     
00095 private:
00096     status_t    open_doctype();
00097     status_t    indent();
00098     
00099     sptr<IByteOutput>   m_stream;
00100     SVector<SString>    m_elementStack;
00101     uint32_t                m_formattingStyle;
00102     bool                m_openStartTag;
00103     bool                m_doneDOCTYPE;
00104     bool                m_startedInternalDTD;
00105     int32_t             m_depth;
00106     int32_t             m_lastPrettyDepth;
00107 };
00108 
00109 #if _SUPPORTS_NAMESPACE
00110 }; // namespace xml
00111 }; // namespace palmos
00112 #endif
00113 
00114 #endif // _XML2_B_WRITER_H