Parser.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 _B_XML2_PARSER_H
00014 #define _B_XML2_PARSER_H
00015 
00016 #include <support/Atom.h>
00017 #include <support/IByteStream.h>
00018 #include <support/Binder.h>
00019 #include <support/Vector.h>
00020 
00021 #include <xml/IXMLOStr.h>
00022 #include <xml/XMLDefs.h>
00023 #include <xml/DataSource.h>
00024 
00025 #if _SUPPORTS_NAMESPACE
00026 namespace palmos {
00027 namespace xml {
00028 using namespace support;
00029 #endif
00030 
00031 // Forward References in namespace palmos::xml
00032 // =====================================================================
00033 class BXMLParseContext;
00034 class BParser;
00035 class BCreator;
00036 
00037 
00038 // BXMLParseContext -- Hook Functions for what the parser encounters
00039 // =====================================================================
00040 class BXMLParseContext
00041 {
00042 public:
00043                 BXMLParseContext();
00044     virtual     ~BXMLParseContext();
00045     
00046     // These fields are always set to the current line and column (or as
00047     // close of an approximation as possible).  They are probably most
00048     // useful for error messages.
00049     int32_t line;
00050     int32_t column;
00051     
00052                         // Called at the beginning of the document parsing process.
00053     virtual status_t    OnDocumentBegin(uint32_t flags);
00054     
00055                         // Called at the end of the document parsing process.
00056                         // If you get this call, then you know that parsing was successful.
00057     virtual status_t    OnDocumentEnd();
00058                         // Called at the end of the document parsing process.
00059                         // You get this call if you don't get OnDocumentEnd().
00060     virtual status_t    OnDocumentFail();
00061     
00062     // The following functions are fairly self explanitory.
00063     // Whenever there's a SString that isn't const, you are allowed to use the
00064     // SString::Adopt function to take the string buffer, and leave the original
00065     // string empty.  This is just a performance optimization.
00066     
00067     virtual status_t    OnStartTag(             SString     & name,
00068                                                 SValue      & attributes        );
00069                                     
00070     virtual status_t    OnEndTag(               SString     & name              );
00071     
00072     virtual status_t    OnTextData(             const char  * data,
00073                                                 int32_t     size                );
00074     
00075     virtual status_t    OnCData(                const char  * data,
00076                                                 int32_t     size                );
00077     
00078     virtual status_t    OnComment(              const char  * data,
00079                                                 int32_t     size                );
00080     
00081     virtual status_t    OnDocumentTypeBegin(    SString     & name              );
00082     
00083     virtual status_t    OnExternalSubset(       SString     & publicID,
00084                                                 SString     & systemID,
00085                                                 uint32_t        parseFlags          );
00086     
00087     virtual status_t    OnInternalSubsetBegin();
00088     
00089     virtual status_t    OnInternalSubsetEnd();
00090     
00091     virtual status_t    OnDocumentTypeEnd();
00092     
00093     virtual status_t    OnProcessingInstruction(SString     & target,
00094                                                 SString     & data              );
00095     
00096     virtual status_t    OnElementDecl(          SString     & name,
00097                                                 SString     & data              );
00098     
00099     virtual status_t    OnAttributeDecl(        SString     & element,
00100                                                 SString     & name,
00101                                                 SString     & type,
00102                                                 SString     & behavior,
00103                                                 SString     & defaultVal        );
00104     
00105     virtual status_t    OnInternalParsedEntityDecl( SString & name,
00106                                                     SString & internalData,
00107                                                     bool    parameter,
00108                                                     uint32_t    parseFlags          );
00109     
00110     virtual status_t    OnExternalParsedEntityDecl( SString & name,
00111                                                     SString & publicID,
00112                                                     SString & systemID,
00113                                                     bool     parameter,
00114                                                     uint32_t    parseFlags          );
00115     
00116     virtual status_t    OnUnparsedEntityDecl(       SString & name,
00117                                                     SString & publicID,
00118                                                     SString & systemID,
00119                                                     SString & notation          );
00120     
00121     virtual status_t    OnNotationDecl(             SString & name,
00122                                                     SString & value             );
00123     
00124                         // This is a hook function to notify the subclass that we
00125                         // encountered a PE in a text section.  Subclasses might
00126                         // either look up replacement text and insert it, or look
00127                         // parsed objects and insert them.
00128     virtual status_t    OnGeneralParsedEntityRef(   SString & name              );
00129     
00130                         // This is a hook function to find out the replacement text
00131                         // for a general entity when it occurs in an attribute.  The
00132                         // value is then substituted into the attribute as if it
00133                         // had never been there.  If you want this behavior, you must
00134                         // set the B_XML_HANDLE_ATTRIBUTE_ENTITIES flag.
00135     virtual status_t    OnGeneralParsedEntityRef(   SString & name,
00136                                                     SString & replacement       );
00137     
00138                         // This is a hook function to notify the subclass when an 
00139                         // entity occurred in the DTD, but in a place where it would
00140                         // be better for the subclass to just insert its objects into
00141                         // the stream than to send back the replacement text, and 
00142                         // have this parser have to reparse it each time it occurs.
00143     virtual status_t    OnParameterEntityRef(       SString & name              );
00144     
00145                         // This is a hook function to find the replacement text for
00146                         // a parameter entity.  It will then be parsed, and the normal
00147                         // other functions will be called.
00148     virtual status_t    OnParameterEntityRef(       SString & name,
00149                                                     SString & replacement       );
00150 
00151                         // An error occurred.  If you would like to ignore the error,
00152                         // and continue processing, then return B_OK from this
00153                         // function, and processing will continue.
00154                         // If fatal is true, a fatal error occurred, and we're not
00155                         // going to be able to recover, no matter what you return.
00156                         // debugLineNo is the line number from which OnError was called
00157                         // XXX Should debugLineNo be made public?
00158                         // The code and data parameters are currently unused.  I have
00159                         // visions of using them to help recover from errors 
00160                         // (for example, pass in potentially corrupted structures, and
00161                         // allow the OnError function to have a go at correcting them)
00162     virtual status_t    OnError(status_t error, bool fatal, int32_t debugLineNo,
00163                                     uint32_t code = 0, void * data = NULL);
00164 
00165 };
00166 
00167 
00168 class BCreator : public virtual SAtom
00169 {
00170 public:
00171                         BCreator(); 
00172     
00173     virtual             ~BCreator();
00174     
00175     virtual status_t    OnStartTag(             SString         & name,
00176                                                 SValue          & attributes,
00177                                                 sptr<BCreator>  & newCreator    );
00178                                     
00179     virtual status_t    OnEndTag(               SString         & name          );
00180     
00181     virtual status_t    OnText(                 SString         & data          );
00182     
00183     virtual status_t    OnComment(              SString         & name          );
00184     
00185     virtual status_t    OnProcessingInstruction(SString         & target,
00186                                                 SString         & data          );
00187                                                 
00188     virtual status_t    Done();
00189 };
00190 
00191 
00192 // ParseXML -- Do some parsing.
00193 // =====================================================================
00194 status_t    _IMPEXP_SUPPORT ParseXML(BXMLParseContext *context, BXMLDataSource *data, uint32_t flags = 0);
00195 status_t    _IMPEXP_SUPPORT ParseXML(const sptr<BCreator>& creator, BXMLDataSource *data, uint32_t flags = 0);
00196 
00197 
00198 #if _SUPPORTS_NAMESPACE
00199 }; // namespace xml
00200 }; // namespace palmos
00201 #endif
00202 
00203 #endif // _B_XML2_PARSER_H