• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ut_xml.h

Go to the documentation of this file.
00001 /* AbiSource Program Utilities
00002  * Copyright (C) 2001,2002 Francis James Franklin <fjf@alinameridon.com>
00003  * Copyright (C) 2001,2002 AbiSource, Inc.
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00018  * 02110-1301 USA.
00019  */
00020 
00021 /* Pseudoheader to include the right XML headers */
00022 
00023 #ifndef UTXML_H
00024 #define UTXML_H
00025 
00026 /* pre-emptive dismissal; ut_types.h is needed by just about everything,
00027  * so even if it's commented out in-file that's still a lot of work for
00028  * the preprocessor to do...
00029  */
00030 #ifndef UT_TYPES_H
00031 #include "ut_types.h"
00032 #endif
00033 #include "ut_bytebuf.h"
00034 
00035 /* gchar definition moved to ut_types.h */
00036 
00037 ABI_EXPORT char * UT_XML_Decode( const char * inKey );
00038 
00039 class ABI_EXPORT UT_XML
00040 {
00041  public:
00042   UT_XML ();
00043 
00044   virtual ~UT_XML ();
00045 
00046   /* Strip "svg:" from "svg:svg" etc. in element names, pass any other namespace indicators
00047    */
00048   void setNameSpace (const char * xml_namespace);
00049   UT_sint32           getNumMinorErrors(void) const
00050   { return m_iMinorErrors;}
00051 
00052   UT_sint32           getNumRecoveredErrors(void) const
00053   { return m_iRecoveredErrors;}
00054   void                incMinorErrors(void)
00055   { m_iMinorErrors++;}
00056   void                incRecoveredErrors(void)
00057   { m_iRecoveredErrors++;}
00058 
00059  private:
00060   bool m_is_chardata; // as opposed to SAX "default" data
00061 
00062   char * m_chardata_buffer;
00063 
00064   UT_uint32 m_chardata_length;
00065   UT_uint32 m_chardata_max;
00066   UT_sint32 m_iMinorErrors;
00067   UT_sint32 m_iRecoveredErrors;
00068 
00069   bool grow (char *& buffer, UT_uint32 & length, UT_uint32 & max, UT_uint32 require);
00070 
00071  protected:
00072   bool reset_all ();
00073  private:
00074   void flush_all ();
00075 
00076   const char * m_namespace;
00077   int m_nslength;
00078 
00079  public:
00080   /* Returns true iff the name of the first element is xml_type or opt_namespace:xml_type
00081    */
00082   bool sniff (const UT_ByteBuf * pBB, const char * xml_type);
00083   bool sniff (const char * buffer, UT_uint32 length, const char * xml_type);
00084 
00085  private:
00086   bool m_bSniffing;
00087   bool m_bValid;
00088 
00089   const char * m_xml_type;
00090 
00091  public:
00092   virtual UT_Error parse (const char * szFilename);
00093   virtual UT_Error parse (const char * buffer, UT_uint32 length);
00094 
00095   UT_Error parse (const UT_ByteBuf * pBB);
00096 
00097  public:
00098   void stop () { m_bStopped = true; } // call this to stop callbacks and to stop the feed to the parser
00099 
00100  protected:
00101   bool m_bStopped;
00102 
00103  public:
00104   class ABI_EXPORT Listener
00105     {
00106     public:
00107       virtual ~Listener () {}
00108 
00109       virtual void startElement (const gchar * name, const gchar ** atts) = 0;
00110       virtual void endElement (const gchar * name) = 0;
00111       virtual void charData (const gchar * buffer, int length) = 0;
00112 
00113     protected:
00114       Listener () {}
00115     };
00116 
00117   void setListener (Listener * pListener) { m_pListener = pListener; }
00118 
00119  protected:
00120   Listener * m_pListener;
00121 
00122  public:
00123 
00124   /* EXPERIMENTAL - Use With Caution!
00125    */
00126   class ABI_EXPORT ExpertListener
00127     {
00128     public:
00129       virtual ~ExpertListener () {}
00130 
00131       virtual void StartElement (const gchar * name, const gchar ** atts) = 0;
00132       virtual void EndElement (const gchar * name) = 0;
00133       virtual void CharData (const gchar * buffer, int length) = 0;
00134       virtual void ProcessingInstruction (const gchar * target, const gchar * data) = 0;
00135       virtual void Comment (const gchar * data) = 0;
00136       virtual void StartCdataSection () = 0;
00137       virtual void EndCdataSection () = 0;
00138       virtual void Default (const gchar * buffer, int length) = 0;
00139 
00140     protected:
00141       ExpertListener () {}
00142     };
00143 
00144   void setExpertListener (ExpertListener * pExpertListener) { m_pExpertListener = pExpertListener; }
00145 
00146  protected:
00147   ExpertListener * m_pExpertListener;
00148 
00149  public:
00150   class ABI_EXPORT Reader
00151     {
00152     public:
00153       virtual ~Reader () {}
00154 
00155       virtual bool  openFile (const char * szFilename) = 0;
00156       virtual UT_uint32 readBytes (char * buffer, UT_uint32 length) = 0;
00157       virtual void  closeFile (void) = 0;
00158 
00159     protected:
00160       Reader () {}
00161     };
00162 
00163   void setReader (Reader * pReader) { m_pReader = pReader; }
00164 
00165  protected:
00166   Reader * m_pReader;
00167 
00168  public:
00169   /* For UT_XML internal use only.
00170    *
00171    * However, it should be possible to set up redirections from one UT_XML into another, if multiple
00172    * namespaces require it - maybe something like:
00173    *
00174    * UT_XML xhtml;
00175    * xhtml.setListener (xhtml_listener);
00176    * xhtml.setNameSpace ("xhtml");
00177    *
00178    * UT_XML svg;
00179    * svg.setListener (svg_listener);
00180    * svg.setNameSpace ("svg");
00181    * svg.redirectNameSpace (&xhtml,"xhtml"); // Not yet implemented...
00182    * svg.parse (buffer);
00183    *
00184    * or vice versa... ?
00185    */
00186   void startElement (const gchar * name, const gchar ** atts);
00187   void endElement (const gchar * name);
00188   void charData (const gchar * buffer, int length);
00189   void processingInstruction (const gchar * target, const gchar * data);
00190   void comment (const gchar * data);
00191   void cdataSection (bool start);
00192   void defaultData (const gchar * buffer, int length);
00193 };
00194 
00195 class ABI_EXPORT DefaultReader : public UT_XML::Reader
00196 {
00197 public:
00198   DefaultReader ();
00199   virtual ~DefaultReader ();
00200 
00201   virtual bool      openFile (const char * szFilename);
00202   virtual UT_uint32 readBytes (char * buffer, UT_uint32 length);
00203   virtual void      closeFile (void);
00204 
00205 private:
00206   FILE * in;
00207 };
00208 
00209 /* This reads bytes from the buffer provided at construction.
00210  * open(<file>) resets the position within the buffer, but ignores <file>.
00211  *
00212  * NOTE: The buffer is not copied, or g_free()ed.
00213  */
00214 class ABI_EXPORT UT_XML_BufReader : public UT_XML::Reader
00215 {
00216 public:
00217   UT_XML_BufReader (const char * buffer, UT_uint32 length);
00218 
00219   virtual ~UT_XML_BufReader ();
00220 
00221   virtual bool      openFile (const char * szFilename);
00222   virtual UT_uint32 readBytes (char * buffer, UT_uint32 length);
00223   virtual void      closeFile (void);
00224 
00225 private:
00226   const char * const m_buffer;
00227   const char *       m_bufptr;
00228 
00229   UT_uint32          m_length;
00230 };
00231 
00232 #endif

Generated on Sun Feb 14 2021 for AbiWord by  doxygen 1.7.1